Пример #1
0
		private static STNode ReadCompare(ExcelFormulaLexer lexer)
		{
			var node = ReadConnect(lexer);
			if (node == null) return null;

			STNodeType type = STNodeType.NONE;

			if (lexer.IsToken("=") || lexer.IsToken("=="))
				type = STNodeType.EQUALS;
			else if (lexer.IsToken("<>") || lexer.IsToken("!="))
				type = STNodeType.NOT_EQUALS;
			else if (lexer.IsToken(">"))
				type = STNodeType.GREAT_THAN;
			else if (lexer.IsToken("<"))
				type = STNodeType.LESS_THAN;
			else if (lexer.IsToken(">="))
				type = STNodeType.GREAT_EQUALS;
			else if (lexer.IsToken("<="))
				type = STNodeType.LESS_EQUALS;
			else
				return node;

			lexer.NextToken();

			var right = ReadExpr(lexer);
			if (right == null) throw CreateException(lexer, "expect expression");

			node = CreateNode(lexer, type, node.Start, lexer.CommittedLength - node.Start,
				new List<STNode> { node, right });

			return node;
		}
Пример #2
0
		private static STNode ReadFunctionCall(ExcelFormulaLexer lexer)
		{
			var id = ReadSheetName(lexer);
			if (id == null) return null;

			string funName = null;

			// process function name like 'LOG10'
			if (id.Type == STNodeType.CELL)
			{
				Group mg = null;

				if (lexer.CurrentToken == null
					|| (mg = lexer.CurrentToken.Groups["token"]) == null
					|| !mg.Success
					|| mg.Value != "(")
					return id;

				funName = lexer.Input.Substring(id.Start, id.Length);
				lexer.NextToken();
			}
			else
			{
				if (id.Type != STNodeType.IDENTIFIER
					|| !lexer.SkipToken("(")) return id;

				funName = ((STIdentifierNode)id).Identifier;
			}

			var parameterList = ReadParameterList(lexer);

			if (!lexer.SkipToken(")")) throw CreateException(lexer, "expect )");

			return new STFunctionNode(funName, id.Start, lexer.CommittedLength - id.Start, parameterList);
		}
Пример #3
0
		private static STNode ReadMul(ExcelFormulaLexer lexer)
		{
			var node = ReadExponent(lexer);
			if (node == null) return node;

			while (true)
			{
				STNodeType type = STNodeType.NONE;

				if (lexer.IsToken("*"))
					type = STNodeType.MUL;
				else if (lexer.IsToken("/"))
					type = STNodeType.DIV;
				else
					break;

				lexer.NextToken();

				var right = ReadExponent(lexer);
				if (right == null) throw CreateException(lexer, "expect expression");

				node = CreateNode(lexer, type, node.Start, lexer.CommittedLength - node.Start,
					new List<STNode> { node, right });
			}

			return node;
		}
Пример #4
0
        private static STNode ReadPrimary(ExcelFormulaLexer lexer)
        {
            STNode node;

            if (CommitMatchNode(lexer, "string", STNodeType.STRING, out node) ||
                CommitMatchNode(lexer, "identifier", STNodeType.IDENTIFIER, out node) ||
                CommitMatchNode(lexer, "number", STNodeType.NUMBER, out node) ||
                CommitMatchNode(lexer, "cell", STNodeType.CELL, out node) ||
                CommitMatchNode(lexer, "range", STNodeType.RANGE, out node) ||
                CommitMatchNode(lexer, "true", STNodeType.TRUE, out node) ||
                CommitMatchNode(lexer, "false", STNodeType.FALSE, out node) ||
                CommitMatchNode(lexer, "union_ranges", STNodeType.INTERSECTION, out node))
            {
                return(node);
            }
            //else if (lexer.IsMatch("abs_cell"))
            //{
            //	var g = lexer.CurrentToken;

            //	int col = RGUtility.GetNumberOfChar(g.Groups["col"].Value) - 1;
            //	int row = int.Parse(g.Groups["row"].Value) - 1;

            //	lexer.NextToken();

            //	return new STCellNode(lexer.Cell.Worksheet, new ReoGridPos(row, col), STNodeType.CELL, g.Index, g.Length);
            //}
            else if (lexer.IsToken("("))
            {
                int start = lexer.Index;

                lexer.NextToken();

                var expr = ReadExpr(lexer);
                if (expr == null)
                {
                    throw CreateException(lexer, "expect expression");
                }

                if (!lexer.SkipToken(")"))
                {
                    throw CreateException(lexer, "expect )");
                }

                return(CreateNode(lexer, STNodeType.SUB_EXPR, start, lexer.CommittedLength - start,
                                  new List <STNode>()
                {
                    expr
                }));
            }
            else
            {
                return(null);
            }
        }
Пример #5
0
        internal static STNode ParseInterCompareExp(Cell cell, string input)
        {
            ExcelFormulaLexer lexer = new ExcelFormulaLexer(
                cell.Worksheet == null ? null : cell.Worksheet.workbook, cell, input);

            STNodeType type = STNodeType.NONE;

            if (lexer.IsToken("=") || lexer.IsToken("=="))
            {
                type = STNodeType.EQUALS;
            }
            else if (lexer.IsToken("<>") || lexer.IsToken("!="))
            {
                type = STNodeType.NOT_EQUALS;
            }
            else if (lexer.IsToken(">"))
            {
                type = STNodeType.GREAT_THAN;
            }
            else if (lexer.IsToken("<"))
            {
                type = STNodeType.LESS_THAN;
            }
            else if (lexer.IsToken(">="))
            {
                type = STNodeType.GREAT_EQUALS;
            }
            else if (lexer.IsToken("<="))
            {
                type = STNodeType.LESS_EQUALS;
            }

            if (type != STNodeType.NONE)
            {
                lexer.NextToken();

                var right = ReadExpr(lexer);
                if (right != null)
                {
                    return(CreateNode(lexer, type, 0, lexer.CommittedLength, new List <STNode> {
                        null, right
                    }));
                }
            }

            STNode node = ReadConnect(lexer);

            return(new STNode(STNodeType.EQUALS, 0, 0,
                              new List <STNode> {
                null,
                node != null && node.Type != STNodeType.IDENTIFIER ? node :
                new STStringNode(input, 0, input.Length)
            }));
        }
Пример #6
0
		private static STNode ReadMinus(ExcelFormulaLexer lexer)
		{
			if (!lexer.IsToken("-")) return ReadFunctionCall(lexer);

			int start = lexer.Index;
			lexer.NextToken();

			var node = ReadFunctionCall(lexer);
			if (node == null) throw CreateException(lexer, "expect expression");

			return CreateNode(lexer, STNodeType.UNARY_MINUS, start, lexer.CommittedLength - start, new List<STNode> { node });
		}
Пример #7
0
        private static STNode ReadAdd(ExcelFormulaLexer lexer)
        {
            var node = ReadMul(lexer);

            if (node == null)
            {
                return(null);
            }

            while (true)
            {
                STNodeType type = STNodeType.NONE;

                if (lexer.IsToken("+"))
                {
                    type = STNodeType.ADD;
                }
                else if (lexer.IsToken("-"))
                {
                    type = STNodeType.SUB;
                }
                else
                {
                    break;
                }

                lexer.NextToken();

                var right = ReadMul(lexer);
                if (right == null)
                {
                    throw CreateException(lexer, "expect expression");
                }

                node = CreateNode(lexer, type, node.Start, lexer.CommittedLength - node.Start,
                                  new List <STNode> {
                    node, right
                });
            }

            return(node);
        }
Пример #8
0
 private static STNode CommitRunAndCreateNode(ExcelFormulaLexer lexer, STNodeType type,
                                              int start, int len, List <STNode> nodes)
 {
     lexer.NextToken();
     return(CreateNode(lexer, type, start, len, nodes));
 }