Permalink
Browse files

TokenStream no longer implements IEnumerable<Token>.

  • Loading branch information...
1 parent 5bbd8f1 commit bf54d88f3d54fe108297ab290b4160e820aa54d5 @plioi committed Apr 1, 2012
Showing with 12 additions and 32 deletions.
  1. +0 −11 src/Parsley.Test/TokenStreamTests.cs
  2. +11 −3 src/Parsley/ParsingAssertions.cs
  3. +1 −18 src/Parsley/TokenStream.cs
@@ -131,16 +131,5 @@ public void ProvidesPositionOfCurrentToken()
tokens.Advance().Advance().Advance().Position.Line.ShouldEqual(1);
tokens.Advance().Advance().Advance().Position.Column.ShouldEqual(10);
}
-
- [Fact]
- public void CanBeEnumerated()
- {
- var tokens = new TokenStream(Tokens()).ToArray();
- tokens.Length.ShouldEqual(4);
- tokens[0].ShouldEqual(upper, "ABC", 1, 1);
- tokens[1].ShouldEqual(lower, "def", 1, 4);
- tokens[2].ShouldEqual(upper, "GHI", 1, 7);
- tokens[3].ShouldEqual(TokenKind.EndOfInput, "", 1, 10);
- }
}
}
@@ -72,7 +72,15 @@ private static Reply<T> Succeeds<T>(this Reply<T> reply)
public static Reply<T> LeavingUnparsedTokens<T>(this Reply<T> reply, params string[] expectedLiterals)
{
- var actualLiterals = reply.UnparsedTokens.Where(x => x.Kind != TokenKind.EndOfInput).Select(x => x.Literal).ToArray();
+ var stream = reply.UnparsedTokens;
+
+ var actualLiterals = new List<string>();
+
+ while (stream.Current.Kind != TokenKind.EndOfInput)
+ {
+ actualLiterals.Add(stream.Current.Literal);
+ stream = stream.Advance();
+ }
Action raiseError = () =>
{
@@ -81,10 +89,10 @@ public static Reply<T> LeavingUnparsedTokens<T>(this Reply<T> reply, params stri
String.Join(", ", actualLiterals));
};
- if (actualLiterals.Length != expectedLiterals.Length)
+ if (actualLiterals.Count != expectedLiterals.Length)
raiseError();
- for (int i = 0; i < actualLiterals.Length; i++)
+ for (int i = 0; i < actualLiterals.Count; i++)
if (actualLiterals[i] != expectedLiterals[i])
raiseError();
@@ -1,10 +1,9 @@
using System;
-using System.Collections;
using System.Collections.Generic;
namespace Parsley
{
- public class TokenStream : IEnumerable<Token>
+ public class TokenStream
{
private readonly Token current;
private readonly Lazy<TokenStream> rest;
@@ -59,21 +58,5 @@ private TokenStream LazyAdvance(IEnumerator<Token> enumerator)
return new TokenStream(new Token(TokenKind.EndOfInput, endPosition, ""));
}
-
- public IEnumerator<Token> GetEnumerator()
- {
- var head = Current;
-
- yield return head;
-
- if (head.Kind != TokenKind.EndOfInput)
- foreach (var token in Advance())
- yield return token;
- }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
}
}

0 comments on commit bf54d88

Please sign in to comment.