Beispiel #1
0
        public static GroupExpression Parse(ExpressionWalker ew, ExpressionToken left, ExpressionToken right)
        {
            ew.IsCurrentOrThrow(left);

            ew.NextOrThrow();
            if (ew.Current.Token == right)
            {
                throw new UnexpectedTokenException($"Expected an expression, found end of group of type {right}");
            }
            var expr = ParseExpression(ew, typeof(GroupExpression));
            var grp  = new GroupExpression()
            {
                _matchLeft = AttributeExtensions.GetAttribute <ExpressionTokenAttribute>(left).Emit.AsResult(), _matchRight = AttributeExtensions.GetAttribute <ExpressionTokenAttribute>(right).Emit.AsResult()
            };

            if (ew.IsCurrent(ExpressionToken.Or))
            {
                grp.InnerExpression = TernaryExpression.Parse(ew, expr);
            }
            else
            {
                grp.InnerExpression = expr;
            }

            ew.IsCurrentOrThrow(right);
            ew.Next();
            return(grp);
        }
Beispiel #2
0
        public static Expression ParseExpression(ExpressionWalker ew, Type caller = null)
        {
            Expression ret = null;

_retry:
            bool isOperatorCall = caller == typeof(OperatorExpression) || caller == typeof(RightOperatorExpression) || caller == typeof(ForeachExpression);
            var current = ew.Current.Token;

            if (current == ExpressionToken.Literal)
            {
                //cases like variable(.., variable[.., variable + .., variable
                if (ew.HasNext)
                {
                    var peak = ew.PeakNextOrThrow().Token;
                    if (peak == ExpressionToken.LeftParen)
                    {
                        ret = CallExpression.Parse(ew);
                    }
                    else if (peak == ExpressionToken.Period && caller != typeof(IdentityExpression) && caller != typeof(InteractableExpression))
                    {
                        ret = IdentityExpression.Parse(ew);
                    }
                    else if (peak == ExpressionToken.LeftBracet)
                    {
                        ret = IndexerCallExpression.Parse(ew);
                    }
                    else if (peak == ExpressionToken.New)
                    {
                        ret = NewExpression.Parse(ew);
                    }
                    else if (RightOperatorExpression.IsNextAnRightUniOperation(ew, caller) && caller != typeof(RightOperatorExpression))
                    {
                        ret = RightOperatorExpression.Parse(ew);
                    }
                    else if (OperatorExpression.IsNextAnOperation(ew) && !isOperatorCall)
                    {
                        ret = OperatorExpression.Parse(ew);
                    }
                    else
                    {
                        ret = IdentityExpression.Parse(ew, caller);
                    }
                }
                else
                {
                    ret = IdentityExpression.Parse(ew, caller);
                }
            }
            else if (LeftOperatorExpression.IsCurrentAnLeftUniOperation(ew))
            {
                ret = LeftOperatorExpression.Parse(ew);
            }
            else if (current == ExpressionToken.NumberLiteral)
            {
                ret = NumberLiteral.Parse(ew);
            }
            else if (current == ExpressionToken.StringLiteral)
            {
                ret = StringLiteral.Parse(ew);
            }
            else if (current == ExpressionToken.LeftParen)
            {
                ret = GroupExpression.Parse(ew, ExpressionToken.LeftParen, ExpressionToken.RightParen);
            }
            else if (current == ExpressionToken.LeftBracet)
            {
                ret = ArrayExpression.Parse(ew);
            }
            else if (current == ExpressionToken.New)
            {
                ret = NewExpression.Parse(ew);
            }
            else if (current == ExpressionToken.Boolean)
            {
                ret = BooleanLiteral.Parse(ew);
            }
            else if (current == ExpressionToken.CharLiteral)
            {
                ret = CharLiteral.Parse(ew);
            }
            else if (current == ExpressionToken.Throw)
            {
                ret = ThrowExpression.Parse(ew);
            }
            else if (current == ExpressionToken.Hashtag && ew.HasNext && ew.PeakNext.Token == ExpressionToken.NumberLiteral)
            {
                ret = HashtagReferenceExpression.Parse(ew, caller);
            }
            else if (current == ExpressionToken.Null)
            {
                ret = NullIdentity.Parse(ew);
            }
            else if (current == ExpressionToken.NewLine)
            {
                ew.NextOrThrow();
                goto _retry;
            }
            else
            {
                throw new UnexpectedTokenException($"Token was not expected to be a {ew.Current.Token}");
            }

            //here we parse chained math operations
            while (OperatorExpression.IsCurrentAnOperation(ew) && !isOperatorCall)
            {
                if (RightOperatorExpression.IsCurrentAnRightUniOperation(ew) && caller != typeof(OperatorExpression))
                {
                    ret = RightOperatorExpression.Parse(ew, ret);
                }
                else if (OperatorExpression.IsCurrentAnOperation(ew))
                {
                    ret = OperatorExpression.Parse(ew, ret);
                }
            }

            return(ret);
        }