public void It_Can_Handle_Recursive_Rules() { IRuleParser ListParser() { var list = new List <string>(); return(Starts.With ( A.Word(list.Add), One.Of ( The.Symbol("}"), A.Rule <List <string> >(ListParser, rest => list.AddRange(rest)) ) ).ReturnsNode(list)); } var parser = ListParser(); string src = "a b c d e f g}"; var tokens = new Lexer().ToTokens(src); var resultList = parser .FeedAll(tokens) .AssertComplete <List <string> >(); var expectedList = new string[] { "a", "b", "c", "d", "e", "f", "g" }; Assert.Equal(expectedList, resultList); }
public void You_Can_Compose_Multiple_Rules() { var funcDef = new FunctionDefinition(); var classDef = new ClassDefinition(); classDef.Functions = new List <FunctionDefinition>(); var funcParser = Starts .With ( The.Keyword("function"), A.Word(n => funcDef.Name = n), The.Symbol("("), The.Symbol(")"), The.Symbol("{"), The.Symbol("}") ) .ReturnsNode(funcDef); var classParser = Starts .With ( The.Keyword("class"), A.Word(n => classDef.Name = n), The.Symbol("{"), A.Rule <FunctionDefinition>(() => funcParser, classDef.Functions.Add), The.Symbol("}") ) .ReturnsNode(classDef); // Generate some tokens to parse with the parser we just made string src = @" class FooBar { function DoThing() {} } "; var tokens = new Lexer().ToTokens(src).ToArray(); var result = classParser.FeedAll(tokens); // Assert that it correctly did stuff. result.AssertComplete(); Assert.Equal(classDef, result.node); Assert.Equal("FooBar", classDef.Name); Assert.Contains(funcDef, classDef.Functions); Assert.Equal("DoThing", funcDef.Name); }
public void You_Can_Express_An_Empty_Class() { var classDef = new ClassDefinition(); var classParser = Starts .With ( The.Keyword("class"), A.Word(name => classDef.Name = name), The.Symbol("{"), The.Symbol("}") ) .ReturnsNode(classDef); // Generate some tokens to parse with the parser we just made string src = "class FooBar {}"; var tokens = new Lexer().ToTokens(src); var result = classParser.FeedAll(tokens); // Assert that it correctly did stuff. result.AssertComplete(); Assert.Equal(classDef, result.node); Assert.Equal("FooBar", classDef.Name); }