public void RuleDefinition_WithRecursion_IdentifiesMatchingAAndB() { var parser = CreateParser("main", "main := 'A' main? 'B'"); var result = parser.Parse(new TokenStream(new CharacterLexer("AAABBB")).Start()); CstNode.Reduce(result).Should().Be("main[A main[A main[A B] B] B]"); parser.Invoking(p => p.Parse(new TokenStream(new CharacterLexer("AAABB")).Start())).Should().Throw <ParserException>().Where(e => e.Id == ParserExceptionId.ParsingFailed); parser.Invoking(p => p.Parse(new TokenStream(new CharacterLexer("AABBB")).Start())).Should().Throw <ParserException>().Where(e => e.Id == ParserExceptionId.PartialMatch); }
public void SimpleGrammar_ParseText_ShouldSucceedAndReturnCorrectCst(string text, string expectedReduction) { var stream = new TokenStream(new CharacterLexer(text)); var parser = new Parser <CstNode>(SimpleGrammar()); var root = parser.Parse(stream.Start()); root.Should().NotBeNull(); root.Name.Should().Be("main"); CstNode.Reduce(root).Should().Be(expectedReduction); }
public void RuleDefinition_WithPositiveLookahead_ResetsForNextMatch() { var parser = CreateParser("main", "main := &'A' secondary | .*", "secondary := 'A' 'B' 'C'" ); var result = parser.Parse(new TokenStream(new CharacterLexer("ABC")).Start()); CstNode.Reduce(result).Should().Be("main[A secondary[A B C]]"); result = parser.Parse(new TokenStream(new CharacterLexer("BCA")).Start()); CstNode.Reduce(result).Should().Be("main[B C A]"); result = parser.Parse(new TokenStream(new CharacterLexer("AABC")).Start()); CstNode.Reduce(result).Should().Be("main[A A B C]"); }