コード例 #1
0
        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);
        }
コード例 #2
0
        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());
        }
コード例 #3
0
        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());
        }
コード例 #4
0
        public void CalcSimpleLength()
        {
            var source = new StringSource(@"calc(5px)");
            var calc   = CalcParser.ParseCalc(source);

            Assert.AreEqual("calc(5px)", calc.CssText);
        }
コード例 #5
0
ファイル: Calc.cs プロジェクト: 0xb1dd1e/boo
	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);
		}
	}  
コード例 #6
0
        public void CalcSimpleAddition()
        {
            var source = new StringSource(@"calc(5 + 3)");
            var calc   = CalcParser.ParseCalc(source);

            Assert.AreEqual("calc(5 + 3)", calc.CssText);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
    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);
        }
    }
コード例 #10
0
ファイル: Main.cs プロジェクト: 0xb1dd1e/boo
	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);
		}
	}
コード例 #11
0
ファイル: Calc.cs プロジェクト: TRXMaik/lg3d-escher
    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);
        }
    }
コード例 #12
0
        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());
        }
コード例 #13
0
        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());
        }
コード例 #14
0
        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);
コード例 #15
0
        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
            };
        }
コード例 #16
0
        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));
        }
コード例 #17
0
        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());
        }
コード例 #18
0
        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);
        }
コード例 #19
0
        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);
        }
コード例 #20
0
        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();
        }
コード例 #21
0
        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);
        }
コード例 #22
0
ファイル: Sparkline.cs プロジェクト: Daoting/dt
        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));
        }
コード例 #23
0
        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);
        }
コード例 #24
0
        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);
        }
コード例 #25
0
        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);
        }
コード例 #26
0
ファイル: Program.cs プロジェクト: camerontbelt/calc
        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));
        }
コード例 #27
0
        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));
        }
コード例 #28
0
ファイル: NameInfoCollection.cs プロジェクト: Daoting/dt
        /// <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);
        }