Пример #1
0
        public void ParserShouldHandleSimpleExpressions()
        {
            ECalcLexer  lexer  = new ECalcLexer(new ANTLRStringStream("3*(5+2)"));
            ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer));

            parser.expression();
        }
Пример #2
0
        public CommonTree Parse(string expression)
        {
            ECalcLexer  lexer  = new ECalcLexer(new ANTLRStringStream(expression));
            ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer));

            return((CommonTree)parser.expression().Tree);
        }
Пример #3
0
        protected CommonTree Parse(string expression)
        {
            ECalcLexer  lexer  = new ECalcLexer(new ANTLRStringStream(expression));
            ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer));

            try
            {
                RuleReturnScope rule = parser.expression();
                if (parser.HasError)
                {
                    //modify by wsl to translate
                    //throw new EvaluationException(parser.ErrorMessage + " " + parser.ErrorPosition);
                    throw new EvaluationException("语法严重错误,系统无法解释计算公式表达式");
                }
                return(rule.Tree as CommonTree);
            }
            catch (EvaluationException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new EvaluationException(e.Message, e);
            }
        }
Пример #4
0
        public void ParserShouldHandleTree()
        {
            ECalcLexer  lexer  = new ECalcLexer(new ANTLRStringStream(" 2 * (3 + 5)"));
            ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer));

            CommonTree tree = (CommonTree)parser.expression().Tree;

            Console.WriteLine(tree.ToStringTree());
        }
Пример #5
0
        public void ParserShouldHandleTree()
        {
            ECalcLexer  lexer  = new ECalcLexer(new ANTLRStringStream(" 2 * (3 + 5)"));
            ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer));

            CommonTree tree = (CommonTree)parser.expression().Tree;

            //Console.WriteLine(tree.ToStringTree());
            //modify by wsl
            Assert.AreEqual("(* 2 (+ 3 5))", tree.ToStringTree());
        }
Пример #6
0
        public void ParserShouldHandleBasExpressions()
        {
            ECalcLexer  lexer  = new ECalcLexer(new ANTLRStringStream("3*(5+2"));
            ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer));

            try
            {
                parser.expression();
                Assert.Fail();
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Parser exception: " + e.Message);
            }
        }