public TokenStream(IEnumerable<Token> tokens) { var enumerator = tokens.GetEnumerator(); current = enumerator.MoveNext() ? enumerator.Current : new Token(TokenKind.EndOfInput, new Position(1, 1), ""); rest = new Lazy<TokenStream>(() => LazyAdvance(enumerator)); }
private static double ParseAmount(Token number) { var literal = number.Literal; var match = TimePartRegex.Match(literal); if (match.Success) { var whole = Int32.Parse(match.Groups["whole"].Value); var fraction = Int32.Parse(match.Groups["fraction"].Value); return whole + (fraction / 60d); } return Double.Parse(literal, NumberStyles.Any); }
public bool TryMatch(Text text, out Token token) { var match = Match(text); if (match.Success) { token = new Token(this, text.Position, match.Value); return true; } token = null; return false; }
public void ImprovingDefaultMessagesWithAKnownExpectation() { var labeled = Label(AB, "'A' followed by 'B'"); //When p succeeds after consuming input, Label(p) is the same as p. AB.Parses(Tokenize("AB")).WithNoMessage().WithValue(Literal("AB")); labeled.Parses(Tokenize("AB")).WithNoMessage().WithValue(Literal("AB")); //When p fails after consuming input, Label(p) is the same as p. AB.FailsToParse(Tokenize("A!")).LeavingUnparsedTokens("!").WithMessage("(1, 2): B expected"); labeled.FailsToParse(Tokenize("A!")).LeavingUnparsedTokens("!").WithMessage("(1, 2): B expected"); //When p succeeds but does not consume input, Label(p) still succeeds but the potential error is included. var succeedWithoutConsuming = new Token(null, null, "$").SucceedWithThisValue(); succeedWithoutConsuming .PartiallyParses(Tokenize("!")) .LeavingUnparsedTokens("!") .WithNoMessage() .WithValue(Literal("$")); Label(succeedWithoutConsuming, "nothing") .PartiallyParses(Tokenize("!")) .LeavingUnparsedTokens("!") .WithMessage("(1, 1): nothing expected") .WithValue(Literal("$")); //When p fails but does not consume input, Label(p) fails with the given expectation. AB.FailsToParse(Tokenize("!")).LeavingUnparsedTokens("!").WithMessage("(1, 1): A expected"); labeled.FailsToParse(Tokenize("!")).LeavingUnparsedTokens("!").WithMessage("(1, 1): 'A' followed by 'B' expected"); }
private TokenStream(Token current) { this.current = current; rest = new Lazy<TokenStream>(() => this); }
private TokenStream(Token current, IEnumerator<Token> enumerator) { this.current = current; rest = new Lazy<TokenStream>(() => LazyAdvance(enumerator)); }