Ejemplo n.º 1
0
    public static string Creator(string mathInput)
    {
        var        input  = new AntlrInputStream(mathInput);
        var        lexer  = new mathLexer(input);
        var        tokens = new CommonTokenStream(lexer);
        var        parser = new mathParser(tokens);
        IParseTree tree   = parser.prog();

        var visitor = new visitorMath();

        return(visitor.Visit(tree));
    }
Ejemplo n.º 2
0
        /// <summary>
        /// 编译公式,默认
        /// </summary>
        /// <param name="exp">公式</param>
        /// <returns></returns>
        public bool Parse(string exp)
        {
            if (string.IsNullOrWhiteSpace(exp))
            {
                LastError = "Parameter exp invalid !";
                return(false);
            }
            //try {

            var stream             = new CaseChangingCharStream(new AntlrInputStream(exp));
            var lexer              = new mathLexer(stream);
            var tokens             = new CommonTokenStream(lexer);
            var parser             = new mathParser(tokens);
            var antlrErrorListener = new AntlrErrorListener();

            parser.RemoveErrorListeners();
            parser.AddErrorListener(antlrErrorListener);

            var context = parser.prog();
            var end     = context.Stop.StopIndex;

            if (end + 1 < exp.Length)
            {
                _context  = null;
                LastError = "Parameter exp invalid !";
                return(false);
            }
            if (antlrErrorListener.IsError)
            {
                _context  = null;
                LastError = antlrErrorListener.ErrorMsg;
                return(false);
            }
            _context = context;
            return(true);
            //} catch (Exception ex) {
            //    LastError = ex.Message;
            //    return false;
            //}
        }