コード例 #1
0
        public MathParserTermResult Parse(string expression, int startPos)
        {
            var i              = startPos;
            var termType       = TermType.Const;
            int numberOfParams = 0;

            MathParserTermResult internalTermResult = null;

            while (i < expression.Length && Char.IsLetter(expression[i]))
            {
                i++;
            }

            if (i < expression.Length && expression[i] == '(')
            {
                var subParser = new SubExpressionTermParser(true);
                internalTermResult = subParser.Parse(expression, i);
                numberOfParams     = subParser.NumberOfParameters;

                termType = TermType.Function;
            }

            var term = expression.Substring(startPos, i - startPos);

            var result = new ComplexMathParserResult
            {
                StartPos       = startPos,
                EndPos         = internalTermResult != null ? internalTermResult.EndPos : i,
                Term           = term,
                TermType       = termType,
                InternalTerm   = internalTermResult,
                NumberOfParams = numberOfParams
            };

            return(result);
        }
コード例 #2
0
        public MathParserTermResult Parse(string expression, int startPos)
        {
            var i = startPos;
            var openedParenthesis = 0;

            if (expression[i] == '(')
            {
                i++;

                while (i < expression.Length)
                {
                    if (expression[i] == '(')
                    {
                        openedParenthesis++;
                    }
                    else
                    if (expression[i] == ')')
                    {
                        if (openedParenthesis > 0)
                        {
                            openedParenthesis--;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (_isFunctionExpression && expression[i] == ',' && openedParenthesis == 0)
                    {
                        NumberOfParameters++;
                    }

                    i++;
                }

                if (openedParenthesis > 0)
                {
                    throw new InvalidOperationException("No matching ')'!");
                }

                if (i == expression.Length || expression[i] != ')')
                {
                    throw new InvalidOperationException("Expression should be ended with ')'!");
                }
            }

            if (startPos + 1 == i)
            {
                throw new InvalidOperationException($"Subexpression is empty!");
            }

            var result = new ComplexMathParserResult
            {
                StartPos = startPos + 1,
                EndPos   = i + 1,
                Term     = expression.Substring(startPos + 1, i - startPos - 1),
                TermType = TermType.Expression
            };

            return(result);
        }