public bool Parse(ExpressionCompiler compiler)
    {
        int symbolPos = compiler.Pos;

        if (!symbolParser.Parse(compiler))
        {
            return(false);
        }

        // Lenient by default
        if (compiler.Parent.Children.Count == 0)
        {
            // Reset the compiler position
            compiler.Pos = symbolPos;
            return(false);
        }
        TokenImpl lhs = compiler.Parent.PopChild();

        TokenImpl token = CreateToken(compiler, symbolPos, lhs);

        if (token != null)
        {
            compiler.Parent.AddChild(token);
        }

        return(true);
    }
示例#2
0
    public bool Parse(ExpressionCompiler compiler)
    {
        int symbolPos = compiler.Pos;

        if (!symbolParser.Parse(compiler))
        {
            return(false);
        }

        // Parse the right hand side
        if (!compiler.ParseNextToken())
        {
            // Reset the compiler position
            compiler.Pos = symbolPos;

            throw new ParserException(Name, symbolPos, "Missing right-hand operand");
        }
        TokenImpl rhs = compiler.Parent.PopChild();

        TokenImpl token = CreateToken(compiler, symbolPos, rhs);

        if (token != null)
        {
            compiler.Parent.AddChild(token);
        }

        return(true);
    }
    public bool Parse(ExpressionCompiler compiler)
    {
        int startPos = compiler.Pos;

        if (!testSymbol.Parse(compiler))
        {
            return(false);
        }

        // Must have a left-hand side...
        if (compiler.Parent.Children.Count == 0)
        {
            // Reset the compiler position
            compiler.Pos = startPos;

            throw new ParserException(Name, startPos, "Missing operand for the test");
        }
        TokenImpl test = compiler.Parent.PopChild();

        // Parse the result if true token
        if (!compiler.ParseNextToken())
        {
            // Reset the compiler position and restore the removed child
            compiler.Pos = startPos;
            compiler.Parent.AddChild(test);

            throw new ParserException(Name, startPos, "Missing operand for result if true");
        }
        TokenImpl resultIfTrue = compiler.Parent.PopChild();

        // Must have the 'else' token next
        if (!elseSymbol.Parse(compiler))
        {
            // Reset the compiler position and restore the (first) removed child
            compiler.Pos = startPos;
            compiler.Parent.AddChild(test);

            throw new ParserException(Name, startPos, "Missing ':' symbol");
        }

        // Parse the result if false token
        if (!compiler.ParseNextToken())
        {
            // Reset the compiler position and restore the (first) removed child
            compiler.Pos = startPos;
            compiler.Parent.AddChild(test);

            throw new ParserException(Name, startPos, "Missing operand for result if false");
        }
        TokenImpl resultIfFalse = compiler.Parent.PopChild();

        TokenImpl token = new ConditionalOperatorToken(startPos, test, resultIfTrue, resultIfFalse);

        compiler.Parent.AddChild(token);
        return(true);
    }
示例#4
0
    /**
     * Convenience method to remove the last child and return it
     */
    public TokenImpl PopChild()
    {
        if (Children.Count == 0)
        {
            throw new System.InvalidOperationException("No children on this token");
        }

        TokenImpl child = Children[Children.Count - 1];

        Children.RemoveAt(Children.Count - 1);

        return(child);
    }
示例#5
0
    protected virtual TokenImpl GetRhs(T token)
    {
        TokenImpl impl = token;

        if (token is BinaryToken)
        {
            return(((BinaryToken)impl).Rhs);
        }
        else
        {
            return(null);
        }
    }
示例#6
0
    protected virtual TokenImpl GetLhs(T token)
    {
        TokenImpl impl = token;

        if (token is LeftHandUnaryToken)
        {
            return(((LeftHandUnaryToken)impl).Lhs);
        }
        else
        {
            return(null);
        }
    }
示例#7
0
    public static object CoerceToType(System.Type type, TokenImpl context, object value)
    {
        object defaultValue;

        if (type.IsValueType)
        {
            defaultValue = Activator.CreateInstance(type);
        }
        else
        {
            defaultValue = null;
        }

        return(DoCoerce(type, context, value, defaultValue));
    }
示例#8
0
    public bool Parse(ExpressionCompiler compiler)
    {
        int symbolPos = compiler.Pos;

        if (!symbolParser.Parse(compiler))
        {
            return(false);
        }

        // Must have a left-hand side...
        if (compiler.Parent.Children.Count == 0)
        {
            // Reset the compiler position
            compiler.Pos = symbolPos;

            // If we are lenient to the left-hand side missing, just return false
            if (lhsLenient)
            {
                return(false);
            }
            else
            {
                throw new ParserException(Name, symbolPos, "Missing left-hand operand");
            }
        }
        TokenImpl lhs = compiler.Parent.PopChild();

        // Parse the right hand side
        if (!compiler.ParseNextToken())
        {
            // Reset the compiler position and restore the removed child
            compiler.Pos = symbolPos;
            compiler.Parent.AddChild(lhs);

            throw new ParserException(Name, symbolPos, "Missing right-hand operand");
        }
        TokenImpl rhs = compiler.Parent.PopChild();

        TokenImpl token = CreateToken(compiler, symbolPos, lhs, rhs);

        if (token != null)
        {
            compiler.Parent.AddChild(token);
        }

        return(true);
    }
示例#9
0
    protected override TokenImpl CreateToken(ExpressionCompiler compiler, int symbolPos, TokenImpl lhs, TokenImpl rhs)
    {
        if (!(rhs is IdentifierToken))
        {
            throw new ParserException(Name, symbolPos, "Right-hand side of a property access token must be an identifier");
        }

        // The lhs must be a token which supports Hosting. If it's not see if we can do anything
        if (lhs is HostSupport)
        {
            return(new PropertyAccessToken(symbolPos, lhs, (IdentifierToken)rhs));
        }
        else if (lhs is BinaryToken)
        {
            // If the RHS of the binary token is a token which allows hosting we can join to it
            BinaryToken binaryLhs = (BinaryToken)lhs;
            TokenImpl   lhsRhs    = binaryLhs.Rhs;

            if (lhsRhs is HostSupport)
            {
                // We can join...
                binaryLhs.Rhs = new PropertyAccessToken(symbolPos, lhsRhs, (IdentifierToken)rhs);
                return(binaryLhs);
            }
        }
        else if (lhs is UnaryToken)
        {
            // If the RHS of the unary token is a token which allows hosting we can join to and replace it
            UnaryToken unaryLhs = (UnaryToken)lhs;
            TokenImpl  lhsRhs   = unaryLhs.Rhs;

            if (lhsRhs is HostSupport)
            {
                // We can join it
                unaryLhs.Rhs = new PropertyAccessToken(symbolPos, lhsRhs, (IdentifierToken)rhs);
                return(unaryLhs);
            }
        }

        // Can't work with the lhs
        throw new ParserException(Name, symbolPos, $"Left-hand side of a property access token cannot be: {lhs.Name}");
    }
示例#10
0
    public virtual bool Equals(object obj, bool includeChildren)
    {
        if (obj == null)
        {
            return(false);
        }
        if (obj.GetType() != GetType())
        {
            return(false);
        }

        TokenImpl other = (TokenImpl)obj;

        if (other.Position != Position)
        {
            return(false);
        }

        if (includeChildren)
        {
            if (other.Children.Count != Children.Count)
            {
                return(false);
            }

            for (int i = 0; i < other.Children.Count; ++i)
            {
                if (!other.Children[i].Equals(Children[i]))
                {
                    return(false);
                }
            }
        }

        return(true);
    }
示例#11
0
 public DecrementAndReturnToken(int position, TokenImpl rhs) : base(position, rhs)
 {
 }
示例#12
0
 public ConditionalOperatorToken(int position, TokenImpl test, TokenImpl resultIfTrue, TokenImpl resultIfFalse) : base(position)
 {
     this.Test          = test;
     this.ResultIfTrue  = resultIfTrue;
     this.ResultIfFalse = resultIfFalse;
 }
示例#13
0
 public NotToken(int position, TokenImpl rhs) : base(position, rhs)
 {
 }
示例#14
0
    public override object Evaluate(UnityELEvaluator context)
    {
        TokenImpl child = Children[0];

        return(child.Evaluate(context));
    }
示例#15
0
 public NoSuchFunctionException(TokenImpl source, string message, Exception cause) : base(source, message, cause)
 {
 }
示例#16
0
 public KeyedAccessToken(int position, TokenImpl hostToken) : base(position)
 {
     this.Host = hostToken;
 }
示例#17
0
 public FunctionToken(int position, TokenImpl functionName) : base(position)
 {
     this.FunctionName = functionName;
 }
示例#18
0
 protected override TokenImpl CreateToken(ExpressionCompiler compiler, int symbolPos, TokenImpl lhs, TokenImpl rhs)
 {
     if (lhs is BinaryToken)
     {
         return(ApplyPrecedence(compiler, (BinaryToken)lhs, new AsToken(symbolPos, lhs, rhs)));
     }
     else
     {
         return(new AsToken(symbolPos, lhs, rhs));
     }
 }
示例#19
0
 public AndToken(int position, TokenImpl lhs, TokenImpl rhs) : base(position, lhs, rhs)
 {
 }
示例#20
0
 public IsNotEmptyToken(int position, TokenImpl rhs) : base(position, rhs)
 {
 }
示例#21
0
 public EqualsToken(int position, TokenImpl lhs, TokenImpl rhs) : base(position, lhs, rhs)
 {
 }
示例#22
0
 protected abstract TokenImpl CreateToken(ExpressionCompiler compiler, int symbolPos, TokenImpl lhs);
示例#23
0
 public SubtractAndAssignToken(int position, TokenImpl lhs, TokenImpl rhs) : base(position, lhs, rhs)
 {
 }
示例#24
0
 public UnaryMinusToken(int position, TokenImpl rhs) : base(position, rhs)
 {
 }
示例#25
0
 public AddAndAssignToken(int position, TokenImpl lhs, TokenImpl rhs) : base(position, lhs, rhs)
 {
 }
示例#26
0
 public ModulusToken(int position, TokenImpl lhs, TokenImpl rhs) : base(position, lhs, rhs)
 {
 }
示例#27
0
 protected override TokenImpl CreateToken(ExpressionCompiler compiler, int symbolPos, TokenImpl rhs)
 {
     return(new ComplementToken(symbolPos, rhs));
 }
示例#28
0
 public ExponentAndAssignToken(int position, TokenImpl lhs, TokenImpl rhs) : base(position, lhs, rhs)
 {
 }
示例#29
0
 public LeftHandUnaryToken(int position, TokenImpl Lhs) : base(position)
 {
     this.Lhs = Lhs;
 }
示例#30
0
 public ParserException(TokenImpl source, string message, Exception cause) :
     base($"{source.DebugName}{message}", cause)
 {
 }