public static string StringVariantToInvariant(Worksheet worksheet, string variantString) { if (worksheet == null) { return(variantString); } bool flag = worksheet.ReferenceStyle == ReferenceStyle.A1; string str = variantString; bool flag2 = (str != null) && str.StartsWith("="); try { CalcParser parser = new CalcParser(); CalcExpression expr = parser.Parse(variantString, new SpreadCalcParserContext(worksheet, !flag, 0, 0, CultureInfo.CurrentCulture)); str = parser.Unparse(expr, new SpreadCalcParserContext(worksheet, !flag, 0, 0, CultureInfo.InvariantCulture)); if (flag2) { variantString = "=" + variantString; } } catch { } return(str); }
internal static string GetCoord(int rowIndex, int columnIndex) { StringBuilder sb = new StringBuilder(); CalcParser.UnParseCell(false, 0, 0, rowIndex, columnIndex, true, true, sb, CalcRangeType.Cell); return(sb.ToString()); }
internal static string GetCoord(int rowIndex, int columnIndex, int rowCount, int colCount, CalcRangeType rangeType) { StringBuilder sb = new StringBuilder(); CalcParser.UnParseRange(false, 0, 0, rowIndex, columnIndex, (rowIndex + rowCount) - 1, (columnIndex + colCount) - 1, true, true, true, true, sb, rangeType); return(sb.ToString()); }
public void CalcSimpleLength() { var source = new StringSource(@"calc(5px)"); var calc = CalcParser.ParseCalc(source); Assert.AreEqual("calc(5px)", calc.CssText); }
public static void Main(string[] args) { try { CalcLexer lexer = new CalcLexer(new CharBuffer(Console.In)); lexer.setFilename("<stdin>"); CalcParser parser = new CalcParser(lexer); parser.setFilename("<stdin>"); // Parse the input expression parser.expr(); CommonAST t = (CommonAST)parser.getAST(); // Print the resulting tree out in LISP notation Console.Out.WriteLine(t.ToStringTree()); CalcTreeWalker walker = new CalcTreeWalker(); // Traverse the tree created by the parser float r = walker.expr(t); Console.Out.WriteLine("value is "+r); } catch(TokenStreamException e) { Console.Error.WriteLine("exception: "+e); } catch(RecognitionException e) { Console.Error.WriteLine("exception: "+e); } }
public void CalcSimpleAddition() { var source = new StringSource(@"calc(5 + 3)"); var calc = CalcParser.ParseCalc(source); Assert.AreEqual("calc(5 + 3)", calc.CssText); }
public void CalcAdditionWithCalcBracket() { var source = new StringSource(@"calc(5 + calc(3 + 7))"); var calc = CalcParser.ParseCalc(source); Assert.AreEqual("calc(5 + 3 + 7)", calc.CssText); }
public void CalcComplexAddition() { var source = new StringSource(@"calc(50% + 3px / 2em + -1cm)"); var calc = CalcParser.ParseCalc(source); Assert.AreEqual("calc(50% + 3px / 2em + -1cm)", calc.CssText); }
public static void Main(string[] args) { new INTNode(); try { CalcLexer lexer = new CalcLexer(new ByteBuffer(Console.OpenStandardInput())); CalcParser parser = new CalcParser(lexer); // Parse the input expression parser.expr(); CalcAST t = (CalcAST)parser.getAST(); // Print the resulting tree out in LISP notation Console.Out.WriteLine(t.ToStringTree()); // XML serialize the tree, showing // different physical node class types TextWriter w = Console.Out; t.xmlSerialize(w); w.Write("\n"); w.Flush(); // Compute value and return int r = t.Value(); Console.Out.WriteLine("value is " + r); } catch (Exception e) { Console.Error.WriteLine("exception: " + e); Console.Error.WriteLine(e.StackTrace); } }
public static void Main(string[] args) { new INTNode(); try { CalcLexer lexer = new CalcLexer(new ByteBuffer(Console.OpenStandardInput())); CalcParser parser = new CalcParser(lexer); // Parse the input expression parser.expr(); CalcAST t = (CalcAST)parser.getAST(); // Print the resulting tree out in LISP notation Console.Out.WriteLine(t.ToStringTree()); // XML serialize the tree, showing // different physical node class types TextWriter w = Console.Out; t.xmlSerialize(w); w.Write("\n"); w.Flush(); // Compute value and return int r = t.Value(); Console.Out.WriteLine("value is "+r); } catch(Exception e) { Console.Error.WriteLine("exception: "+e); Console.Error.WriteLine(e.StackTrace); } }
public static void Main(string[] args) { try { CalcLexer lexer = new CalcLexer(new CharBuffer(Console.In)); lexer.setFilename("<stdin>"); CalcParser parser = new CalcParser(lexer); parser.setFilename("<stdin>"); // Parse the input expression parser.expr(); CommonAST t = (CommonAST)parser.getAST(); // Print the resulting tree out in LISP notation Console.Out.WriteLine(t.ToStringTree()); CalcTreeWalker walker = new CalcTreeWalker(); // Traverse the tree created by the parser float r = walker.expr(t); Console.Out.WriteLine("value is " + r); } catch (TokenStreamException e) { Console.Error.WriteLine("exception: " + e); } catch (RecognitionException e) { Console.Error.WriteLine("exception: " + e); } }
public string evaluate(string expr) { CalcLexer lexer = new CalcLexer(new AntlrInputStream(expr)); CalcParser parser = new CalcParser(new CommonTokenStream(lexer)); IParseTree tree = parser.start(); double answer = new CalcVisitor().Visit(tree); return(answer.ToString()); }
public void Interpreter_GivenEquationSolve_7_ShouldFail() { //Arrange var text = "2^-(4+9*6/7)a"; var lexer = new CalcLexer(text); var parser = new CalcParser(new CalcLexer(text)); //Act //Assert Assert.Throws <Exception>(() => parser.Parse()); }
public static (ParseTreePattern tree, string) Compile(string input) { var stream = new AntlrInputStream(input); var lexer = new CalcLexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new CalcParser(tokens); parser.BuildParseTree = true; parser.TrimParseTree = true; var listener = new CalculatorListener(default, parser);
private void Setup(string input) { AntlrInputStream inputStream = new AntlrInputStream(input); this._calcLexer = new CalcLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(this._calcLexer); this._calcParser = new CalcParser(commonTokenStream) { BuildParseTree = true }; }
public static double Evaluate(string expression) { var lexer = new CalcLexer(new AntlrInputStream(expression)); lexer.RemoveErrorListeners(); lexer.AddErrorListener(new ThrowExceptionErrorListener()); var tokens = new CommonTokenStream(lexer); var parser = new CalcParser(tokens); var tree = parser.compileUnit(); var visitor = new CalcVisitor(); return(visitor.Visit(tree)); }
static void Main(string[] args) { var a = new StreamReader("./root.txt"); var antlrInput = new AntlrInputStream(a); var lexer = new CalcLexer(antlrInput); var tokens = new BufferedTokenStream(lexer); var parser = new CalcParser(tokens); lexer.PushLexerState(); IParseTree tree = parser.compileUnit(); Console.WriteLine(tree.ToStringTree()); }
public void Interpreter_GivenEquationSolve_6_ShouldPass() { //Arrange var text = "2^-(4+9*6/7)"; var lexer = new CalcLexer(text); var parser = new CalcParser(new CalcLexer(text)); var tree = parser.Parse(); var interpreter = new CalcInterpreter(); //Act var result = interpreter.Interpret(tree); //Assert Assert.AreEqual(0.0002976107554210143, result); }
public void Interpreter_GivenEquationSolve_13_ShouldPass() { //Arrange var text = "!((1.3*4 >= 5/2) & (6^2 >= 7^2))"; var lexer = new CalcLexer(text); var parser = new CalcParser(new CalcLexer(text)); var tree = parser.Parse(); var interpreter = new CalcInterpreter(); //Act var result = interpreter.Interpret(tree); //Assert Assert.AreEqual(1, result); }
private void ButCalc_Click(object sender, EventArgs e) { var reader = new StringReader(Formula.Text); var input = new AntlrInputStream(reader.ReadToEnd()); var lexer = new CalcLexer(input); var tokens = new CommonTokenStream(lexer); var parser = new CalcParser(tokens); IParseTree tree = parser.prog(); Tree.Text = tree.ToStringTree(parser); var visitor = new CalcVisitor(); Result.Text = visitor.Visit(tree).ToString(); }
public void Interpreter_GivenEquationSolve_1_ShouldPass() { //Arrange var text = "8.23987456*0.3+9-2+45"; var lexer = new CalcLexer(text); var parser = new CalcParser(new CalcLexer(text)); var tree = parser.Parse(); var interpreter = new CalcInterpreter(); //Act var result = interpreter.Interpret(tree); //Assert Assert.AreEqual(54.471962368, result); }
internal string GetDataFormula(CalcParserContext context) { if (this.dataReference == null) { return(null); } if ((this.Group.SparklineGroupManager != null) && (this.Group.SparklineGroupManager.CalcEvaluator != null)) { return(this.Group.SparklineGroupManager.CalcEvaluator.Expression2Formula(this.DataReference, 0, 0)); } CalcParser parser = new CalcParser(); return(parser.Unparse(this.dataReference, context)); }
public void Interpreter_GivenEquationSolve_3_ShouldPass() { //Arrange var text = "2^4+9*6/7"; var lexer = new CalcLexer(text); var parser = new CalcParser(new CalcLexer(text)); var tree = parser.Parse(); var interpreter = new CalcInterpreter(); //Act var result = interpreter.Interpret(tree); //Assert Assert.GreaterOrEqual(result, 23.7); }
public void Interpreter_GivenEquationSolve_5_ShouldPass() { //Arrange var text = "2^(-3)"; var lexer = new CalcLexer(text); var parser = new CalcParser(new CalcLexer(text)); var tree = parser.Parse(); var interpreter = new CalcInterpreter(); //Act var result = interpreter.Interpret(tree); //Assert Assert.AreEqual(0.125, result); }
public Calc_node Generate(string text) { text = text.Replace('×', '*').Replace('÷', '/'); // patch TextReader sr = new StringReader(text); stream.Reset(); stream.Load(sr, BaseInputCharStream.InitialBufferSize, BaseInputCharStream.ReadBufferSize); var lexer = new CalcLexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new CalcParser(tokens); var tree = parser.exp(); var result = visitor.Visit(tree); return(result); }
static void Main(string[] args) { var text = "9+5*\n7 \n#this is a comment\n";//args[0]; var lexer = new CalcLexer(text); var tokens = lexer.Tokenize(); tokens.ForEach(token => Console.WriteLine(token)); var parser = new CalcParser(new CalcLexer(text)); // var tree = parser.Parse(); // Console.WriteLine("\nParse Tree"); // Console.WriteLine(tree.ToStringTree()); var interpreter = new CalcInterpreter(); // Console.WriteLine("\nResult"); // Console.WriteLine(interpreter.Interpret(tree)); }
public Argument Calculate(string expression) { AntlrInputStream inputStream = new AntlrInputStream(expression); CalcLexer calcLexer = new CalcLexer(inputStream); calcLexer.RemoveErrorListeners(); //removes the default console listener calcLexer.AddErrorListener(new ThrowExceptionErrorListener()); CommonTokenStream commonTokenStream = new CommonTokenStream(calcLexer); CalcParser parser = new CalcParser(commonTokenStream); parser.RemoveErrorListeners(); //removes the default console listener parser.AddErrorListener(new ThrowExceptionErrorListener()); CalcParser.ExpressionContext context = parser.expression(); return(this._visitor.Visit(context)); }
/// <summary> /// Adds a specified name information object to the NameInfoCollection class. /// </summary> /// <param name="nameInfo">The name information object to add to the <see cref="T:Dt.Cells.Data.NameInfoCollection" /> class.</param> public void Add(NameInfo nameInfo) { if (nameInfo == null) { throw new ArgumentNullException("item"); } if (nameInfo.Name == null) { throw new ArgumentException(ResourceStrings.NamedStyleInfoNameNullError); } if (!CalcParser.ValidateName(nameInfo.Name)) { throw new ArgumentException(ResourceStrings.NamedStyleInfoInvalidNameError); } NameInfo info = this.Find(nameInfo.Name); if (info != null) { this.names.Remove(info.Name); } this.names.Add(nameInfo.Name, nameInfo); this.RaiseNameInfoCollectionChanged(nameInfo, NameInfoCollectionChangedAction.Add); }