예제 #1
0
        public RuleSet compile(String inputString)
        {
            AntlrInputStream     input  = new AntlrInputStream(inputString);
            RuleSetGrammarLexer  lexer  = new RuleSetGrammarLexer(input);
            ITokenStream         tokens = new CommonTokenStream(lexer);
            RuleSetGrammarParser parser = new RuleSetGrammarParser(tokens);

            TreeBuilder treeBuilder = new TreeBuilder();

            parser.AddParseListener(treeBuilder);
            parser.AddErrorListener(new ThrowExceptionErrorListener());

            parser.rule_set();

            return(treeBuilder.ruleSet);
        }
예제 #2
0
        public void TestRule()
        {
            List <Tuple <bool, string> > tests = new List <Tuple <bool, string> >()
            {
                Tuple.Create <bool, string>(true, "if true then conclusion;"),
                Tuple.Create <bool, string>(true, "if false then conclusion;"),
                Tuple.Create <bool, string>(true, "if (true) then conclusion;"),
                Tuple.Create <bool, string>(true, "if (false) then conclusion;"),


                Tuple.Create <bool, string>(true, "if a then conclusion;"),
                Tuple.Create <bool, string>(true, "if (a) then conclusion;"),
                Tuple.Create <bool, string>(true, "if a or b then conclusion;"),
                Tuple.Create <bool, string>(true, "if a and b then conclusion;"),
                Tuple.Create <bool, string>(true, "if (a or b) and c then conclusion;"),
                Tuple.Create <bool, string>(true, "if a or b and c then conclusion;"),

                Tuple.Create <bool, string>(true, "if a_1 or b_2 and c_3_aaa then conclusion;"),

                Tuple.Create <bool, string>(true, "if a >= b then conclusion;"),
                Tuple.Create <bool, string>(true, "if a > b then conclusion;"),
                Tuple.Create <bool, string>(true, "if a = b then conclusion;"),
                Tuple.Create <bool, string>(true, "if a < b then conclusion;"),
                Tuple.Create <bool, string>(true, "if a <= b then conclusion;"),
                Tuple.Create <bool, string>(true, "if a > 0.1 then conclusion;"),
                Tuple.Create <bool, string>(true, "if 1.12 <= b then conclusion;"),
                Tuple.Create <bool, string>(true, "if 0.1 = 4 then conclusion;"),
                Tuple.Create <bool, string>(true, "if 5 = 5.0 then conclusion;"),
                Tuple.Create <bool, string>(true, "if a + 5 * b <= c / 12.0 - 1 then conclusion;"),
                Tuple.Create <bool, string>(true, "if (a) > (1.23) * (1 + 4) then conclusion;"),
                Tuple.Create <bool, string>(true, "if 1 < 4 and true then conclusion;"),
                Tuple.Create <bool, string>(true, "if -(a * b) < 12 and true then conclusion;"),

                Tuple.Create <bool, string>(true, ""), // empty rule file
                Tuple.Create <bool, string>(true, "//  comment in a rule file"),
                Tuple.Create <bool, string>(true, "//  comment with new line\n"),


                /* Invalid rules. */
                Tuple.Create <bool, string>(false, "true"),
                Tuple.Create <bool, string>(false, "false"),

                Tuple.Create <bool, string>(false, "if true then conclusion"), // No semicolon

                Tuple.Create <bool, string>(false, "if then conclusion;"),
                Tuple.Create <bool, string>(false, "if true conclusion;"),
                Tuple.Create <bool, string>(false, "if then;"),
                Tuple.Create <bool, string>(false, "if;"),
                Tuple.Create <bool, string>(false, "if"),
                Tuple.Create <bool, string>(false, "then;"),
                Tuple.Create <bool, string>(false, "then"),

                Tuple.Create <bool, string>(false, "a + b"),

                Tuple.Create <bool, string>(false, "if a ++ b then conclusion;"),
                Tuple.Create <bool, string>(false, "if a + (+b) then conclusion;"),

                Tuple.Create <bool, string>(false, "true and false;"),

                Tuple.Create <bool, string>(false, "if abc $ 123 then conclusion;"),
                Tuple.Create <bool, string>(false, "if abc @ 123 then conclusion;"),

                Tuple.Create <bool, string>(false, "if aa( == 123 then conclusion;"),
                Tuple.Create <bool, string>(false, "if bb) == 123 then conclusion;"),

                Tuple.Create <bool, string>(false, "if true then conclusion1 conclusion2;"),

                // Considered invalid for now
                Tuple.Create <bool, string>(false, "if function(a, b, c) then conclusion;"),

                Tuple.Create <bool, string>(false, "if logical_function() then conclusion;"),
                Tuple.Create <bool, string>(false, "if logical_function() and (true) then conclusion;"),

                Tuple.Create <bool, string>(false, "if numeric_function() == 123 then conclusion;"),
                Tuple.Create <bool, string>(false, "if numeric_function() >= 0.12 then conclusion;"),
            };


            foreach (var test in tests)
            {
                var desiredResult = test.Item1;
                var testString    = test.Item2;

                AntlrInputStream    input  = new AntlrInputStream(testString);
                RuleSetGrammarLexer lexer  = new RuleSetGrammarLexer(input);
                ITokenStream        tokens = new CommonTokenStream(lexer);

                RuleSetGrammarParser parser = new RuleSetGrammarParser(tokens);

                parser.RemoveErrorListeners();
                parser.AddErrorListener(new ThrowExceptionErrorListener());

                if (desiredResult)
                {
                    ParserRuleContext ruleContext = parser.rule_set();
                    string            t           = ruleContext.GetText();
                    Assert.IsNull(ruleContext.exception);
                }
                else
                {
                    try
                    {
                        ParserRuleContext ruleContext = parser.rule_set();
                        // fail("Failed on \"" + testString + "\"");
                        Assert.IsNotNull(ruleContext);
                    }
                    catch (ArgumentException e)
                    {
                        Console.Write(e.ToString());
                        // deliberately do nothing
                    }
                }
            }
        }