public Evaluator(string polynomial) { if (string.IsNullOrWhiteSpace(polynomial)) { throw new ArgumentException($"{nameof(polynomial)} cannot be null or empty"); } if (polynomial.StartsWith('-')) { polynomial = "0" + polynomial; } var inputStream = new AntlrInputStream(polynomial); var lexer = new PolynomialLexer(inputStream); var commonTokenStream = new CommonTokenStream(lexer); var parser = new PolynomialParser(commonTokenStream); var context = parser.expr(); var visitor = new VisitorImpl(); _expr = visitor.Visit(context); }
private static (IParseTree tree, string parseErrorMessage) TryParseExpression(string expression) { ICharStream stream = CharStreams.fromstring(expression); PolynomialLexer lexer = new PolynomialLexer(stream); ITokenStream tokens = new CommonTokenStream(lexer); PolynomialParser parser = new PolynomialParser(tokens) { BuildParseTree = true }; parser.RemoveErrorListeners(); parser.AddErrorListener(new PolynomialErrorListener()); try { var tree = parser.parse(); return(tree, string.Empty); } catch (ParseCanceledException pce) { return(null, pce.Message); } }