Beispiel #1
0
        public void CheckSyntax(string s) {
            _tokenizer = new ExpressionTokenizer();
            _evalMode = EvalMode.ParseOnly;

            _tokenizer.InitTokenizer(s);
            ParseExpression();
            if (_tokenizer.CurrentToken != ExpressionTokenizer.TokenType.EOF) {
                throw BuildParseError("Unexpected token at the end of expression", _tokenizer.CurrentPosition);
            }
        }
Beispiel #2
0
        public object Evaluate(string s) {
            _tokenizer = new ExpressionTokenizer();
            _evalMode = EvalMode.Evaluate;

            _tokenizer.InitTokenizer(s);
            object val = ParseExpression();
            if (_tokenizer.CurrentToken != ExpressionTokenizer.TokenType.EOF) {
                throw BuildParseError("Unexpected token at the end of expression", _tokenizer.CurrentPosition);
            }
            return val;
        }
 public object Evaluate(ExpressionTokenizer tokenizer)
 {
     _evalMode = EvalMode.Evaluate;
     _tokenizer = tokenizer;
     return ParseExpression();
 }
        private object ParseConditional()
        {
            // we're on "if" token - skip it
            _tokenizer.GetNextToken();
            if (_tokenizer.CurrentToken != ExpressionTokenizer.TokenType.LeftParen) {
                throw BuildParseError("'(' expected.", _tokenizer.CurrentPosition);
            }
            _tokenizer.GetNextToken();

            ExpressionTokenizer.Position p0 = _tokenizer.CurrentPosition;
            object val = ParseExpression();
            ExpressionTokenizer.Position p1 = _tokenizer.CurrentPosition;

            bool cond = false;
            if (!SyntaxCheckOnly()) {
                cond = (bool) SafeConvert(typeof(bool), val, "the conditional expression", p0, p1);
            }

            // skip comma between condition value and then
            if (_tokenizer.CurrentToken != ExpressionTokenizer.TokenType.Comma) {
                throw BuildParseError("',' expected.", _tokenizer.CurrentPosition);
            }
            _tokenizer.GetNextToken();

            EvalMode oldEvalMode = _evalMode;

            try {
                if (!cond) {
                    // evaluate 'then' clause without executing functions
                    _evalMode = EvalMode.ParseOnly;
                } else {
                    _evalMode = oldEvalMode;
                }
                object thenValue = ParseExpression();
                _evalMode = oldEvalMode;

                if (_tokenizer.CurrentToken != ExpressionTokenizer.TokenType.Comma) {
                    throw BuildParseError("',' expected.", _tokenizer.CurrentPosition);
                }
                _tokenizer.GetNextToken(); // skip comma

                if (cond) {
                    // evaluate 'else' clause without executing functions
                    _evalMode = EvalMode.ParseOnly;
                } else {
                    _evalMode = oldEvalMode;
                }
                object elseValue = ParseExpression();

                _evalMode = oldEvalMode;

                // skip closing ')'
                if (_tokenizer.CurrentToken != ExpressionTokenizer.TokenType.RightParen) {
                    throw BuildParseError("')' expected.", _tokenizer.CurrentPosition);
                }
                _tokenizer.GetNextToken();

                return cond ? thenValue : elseValue;
            }
            finally {
                // restore evaluation mode - even on exceptions
                _evalMode = oldEvalMode;
            }
        }
        private object ParseBooleanOr()
        {
            ExpressionTokenizer.Position p0 = _tokenizer.CurrentPosition;
            object o = ParseBooleanAnd();
            EvalMode oldEvalMode = _evalMode;
            try {
                while (_tokenizer.IsKeyword("or")) {
                    bool v1 = true;

                    if (!SyntaxCheckOnly()) {
                        v1 = (bool)SafeConvert(typeof(bool), o, "the left hand side of the 'or' operator", p0, _tokenizer.CurrentPosition);

                        if (v1) {
                            // we're lazy - don't evaluate anything from now, we know that the result is 'true'
                            _evalMode = EvalMode.ParseOnly;
                        }
                    }

                    _tokenizer.GetNextToken();
                    ExpressionTokenizer.Position p2 = _tokenizer.CurrentPosition;
                    object o2 = ParseBooleanAnd();
                    ExpressionTokenizer.Position p3 = _tokenizer.CurrentPosition;

                    if (!SyntaxCheckOnly()) {
                        bool v2 = (bool)SafeConvert(typeof(bool), o2, "the right hand side of the 'or' operator", p2, p3);
                        o = v1 || v2;
                    }
                }
                return o;
            } finally {
                _evalMode = oldEvalMode;
            }
        }
Beispiel #6
0
 static void command_mode(string[] args) => EvalMode = (EvalMode)char.Parse(args[0]);