public void Nested() { const string TestString = "1 2 3 4 5 6 7 8 9 0"; var tokenizer = new Tokenizer(); var tokens = tokenizer.Tokenize(TestString).ToList(); TokenConsumer consumer = new TokenConsumer(tokens); using (var scope = consumer.BeginScope()) { int val; Assert.IsTrue(scope.EatInt(out val).Ok()); Assert.AreEqual(1, val); scope.Commit(); Assert.IsTrue(scope.EatInt(out val).Ok()); Assert.AreEqual(2, val); } using (var scope = consumer.BeginScope()) { int val; Assert.IsTrue(scope.EatInt(out val).Ok()); Assert.AreEqual(2, val); Assert.IsTrue(scope.EatInt(out val).Ok()); Assert.AreEqual(3, val); } }
public static IEnumerable<PersonHappinessRow> ParseHappiness(this string text) { Tokenizer tokenizer = new Tokenizer(); var sequence = tokenizer.Tokenize(text); var consumer = new TokenConsumer(sequence); using (var scope = consumer.BeginScope()) { return scope.ParseHappiness(); } }
public void CreateAssignAnd() { Tokenizer tokenizer = new Tokenizer(); var tokenStream = tokenizer.Tokenize("x AND y -> d"); ExpressionParser parser = new ExpressionParser(); TokenConsumer consumer = new TokenConsumer(tokenStream); using (var scope = consumer.BeginScope()) { var expression = ExpressionParser.CreateAssignExpression(scope); Assert.AreEqual(new AssignStatement(new AndExpression(16, new VariableExpression("x"), new VariableExpression("y")), new VariableExpression("d")), expression); } }
public IEnumerable<RouteInfo> GetRoutes(IEnumerable<Token> tokenStream) { TokenConsumer consumer = new TokenConsumer(tokenStream); RouteInfo nfo; using (var scope = consumer.BeginScope()) { while ((nfo = EatRoute(scope)) != null) { scope.Commit(); yield return nfo; } } if (!consumer.IsEmpty) { throw new InvalidOperationException("Failed to consume all tokens"); } }
public IEnumerable<AssignStatement> GenerateStatements(IEnumerable<Token> tokens) { TokenConsumer consumer = new TokenConsumer(tokens); using (ITokenConsumeScope scope = consumer.BeginScope()) { var cmd = CreateAssignExpression(scope); while (cmd != null) { scope.Commit(); yield return cmd; cmd = CreateAssignExpression(scope); } } if (!consumer.IsEmpty) { throw new InvalidOperationException("Failed to consume all tokens.."); } }
public IEnumerable<LightCommand> GenerateCommands(IEnumerable<Token> tokens) { TokenConsumer consumer = new TokenConsumer(tokens); using (ITokenConsumeScope scope = consumer.BeginScope()) { LightCommand cmd = GenerateCommand(scope); while (cmd != null) { scope.Commit(); yield return cmd; cmd = GenerateCommand(scope); } } if (!consumer.IsEmpty) { throw new InvalidOperationException("Failed to consume all tokens.."); } }