예제 #1
0
        private Parser <CstNode> CreateParser(string startWith, params string[] ruleDefinitions)
        {
            var grammar = new AdHocGrammar();

            foreach (var ruleDefinition in ruleDefinitions)
            {
                grammar.DefineRule(ruleDefinition);
            }

            grammar.StartWith(startWith);
            return(new Parser <CstNode>(grammar));
        }
예제 #2
0
        public void Grammar_WithStartRule_ReturnsStartRefOnStart()
        {
            var grammar = new AdHocGrammar();

            grammar.TerminalType("A", "start");
            grammar.StartWith("start");

            var ruleRef = grammar.Start();

            ruleRef.Should().NotBeNull();
            ruleRef.RefName.Should().Be("start");
            ruleRef.Name.Should().Be("start");
        }
예제 #3
0
        public void EvaluatingGrammar_WithMockRule_RunsMockRule()
        {
            var mockRule = new MockRule("mock", MockRuleBehavior.Success);
            var grammar  = new AdHocGrammar();

            grammar.DefineRule("start", mockRule);
            grammar.StartWith("start");
            var lexer   = StaticLexer.FromWords();
            var opsMock = new RuleOperationsMock(new TokenStream(lexer).Start());

            var ruleRef = grammar.Start();
            var result  = opsMock.Evaluate(ruleRef).Result;

            result.IsSuccess.Should().BeTrue();
            mockRule.DidGrab.Should().BeTrue();
            result.Index.Index.Should().Be(0);
        }
예제 #4
0
        private static IGrammar <CstNode> SimpleGrammar()
        {
            var grammar = new AdHocGrammar();

            // main   := A choose R{2,5} .
            // choose := U | V
            grammar.Sequence("main",
                             grammar.TerminalType("A"),
                             grammar.Choice("choose",
                                            grammar.TerminalValue("U"),
                                            grammar.TerminalValue("V")
                                            ),
                             grammar.Repeat(null, grammar.TerminalType("R"), 2, 5),
                             grammar.Any(null)
                             );

            grammar.StartWith("main");

            return(grammar);
        }