示例#1
0
        public static List <SymbolToken> Parse(string expression, VariableContext context)
        {
            var pipe1 = new StringToPrimitiveTokenPipe(expression, context);
            var pipe2 = new MatchAllParenthesesProcessor(pipe1);
            var pipe3 = new NegationProcessor(pipe2);
            var pipe4 = new RedundantParenthesesProcessor(pipe3);
            var pipe5 = new TokenValidater(pipe4);

            return(pipe5.PumpAll());
        }
示例#2
0
        private void Hold(SymbolToken val)
        {
            _heldInput.Add(val);
            if (val.Type == SymbolType.OpenBracket)
            {
                _parentheses++;
            }
            if (val.Type == SymbolType.CloseBracket)
            {
                _parentheses--;
            }

            if (_parentheses == 0)
            {
                // Remove all surrounding parentheses
                while (_heldInput.HasRedundantParentheses())
                {
                    if (_heldInput.Count == 2)
                    {
                        var close = _heldInput[_heldInput.Count - 1];
                        throw new InvalidParenthesisException(close.Position);
                    }
                    _heldInput = _heldInput.GetRange(1, _heldInput.Count - 2);
                }

                var pp     = new RedundantParenthesesProcessor(_heldInput);
                var output = pp.PumpAll();

                // Decide if parentheses should be added back in
                if (output.Count == 1 && !_lastOutput.Type.IsUnaryOperation())
                {
                    Output(output);
                }
                else
                {
                    Output(SymbolToken.OpenBracket);
                    Output(output);
                    Output(SymbolToken.CloseBracket);
                }

                _heldInput = new List <SymbolToken>();
                _holdInput = false;
            }
        }