public Boolean IsValid(String strExp, ref String strRetMsg) { Boolean bRet = true; calcExprLexer lex = new calcExprLexer(new AntlrInputStream("=" + strExp)); CommonTokenStream tokens = new CommonTokenStream(lex); calcExprParser parser = new calcExprParser(tokens); IParseTree tree = null; try { tree = parser.parse(); if (parser.NumberOfSyntaxErrors > 0) { bRet = false; strRetMsg = "公式:" + "'" + strExp + "'有语法错误,请查看帮助中公式的语法要求"; } } catch (RecognitionException ex) { bRet = false; strRetMsg = "位置:" + parser.GetErrorHeader(ex) + ":公式:" + "'" + strExp + "'有语法错误,请查看帮助中公式的语法要求"; } return(bRet); }
private String Computing(String strExp, Hashtable hashAllVars, ref Hashtable refVarsHash, ref String strMsg) { calcExprLexer lex = new calcExprLexer(new AntlrInputStream("=" + strExp)); CommonTokenStream tokens = new CommonTokenStream(lex); calcExprParser parser = new calcExprParser(tokens); // ParseTreeWalker walker = new ParseTreeWalker(); // calcExprBaseListener listener = new calcExprBaseListener(); EvalExprVisitor visitor = new EvalExprVisitor(); visitor.SetRefVarsHash(refVarsHash); visitor.SetVarHash(hashAllVars);//m_varsHash); IParseTree tree = null; EvalResult ret = null; try { tree = parser.parse(); if (tree != null) { ret = visitor.Visit(tree); } } catch (RecognitionException ex) { strMsg = ex.Message; System.Console.Error.WriteLine(ex.StackTrace); return(null); } if (ret == null) { return(null); } if (ret.bInvalid) { return(ret.strExceptionMsg); } return(ret.Value1.ToString()); }