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);
    }
    public void TestTokenProperties()
    {
        BooleanToken             test          = new BooleanToken(true);
        IntegerToken             resultIfTrue  = new IntegerToken(1);
        IntegerToken             resultIfFalse = new IntegerToken(2);
        ConditionalOperatorToken result        = new ConditionalOperatorToken(0, test, resultIfTrue, resultIfFalse);

        Assert.AreEqual(test, result.Test);
        Assert.AreEqual(resultIfTrue, result.ResultIfTrue);
        Assert.AreEqual(resultIfFalse, result.ResultIfFalse);
    }
Example #3
0
    public override bool Equals(object obj, bool includeChildren)
    {
        if (!base.Equals(obj, includeChildren))
        {
            return(false);
        }

        ConditionalOperatorToken other = (ConditionalOperatorToken)obj;

        if (Test != null)
        {
            if (!Test.Equals(other.Test))
            {
                return(false);
            }
        }
        else if (other.Test != null)
        {
            return(false);
        }

        if (ResultIfTrue != null)
        {
            if (!ResultIfTrue.Equals(other.ResultIfTrue))
            {
                return(false);
            }
        }
        else if (other.ResultIfTrue != null)
        {
            return(false);
        }

        if (ResultIfFalse != null)
        {
            if (!ResultIfFalse.Equals(other.ResultIfFalse))
            {
                return(false);
            }
        }
        else if (other.ResultIfFalse != null)
        {
            return(false);
        }

        return(true);
    }
    public void TestValidExpression()
    {
        string expression = "true?1:2";

        InitCompiler(expression, 4);

        BooleanToken test = new BooleanToken(true, 0);

        compiler.Parent.AddChild(test);

        Assert.IsTrue(parser.Parse(compiler));
        Assert.AreEqual(expression.Length, compiler.Pos);
        Assert.AreSame(root, compiler.Parent);
        Assert.AreEqual(1, root.Children.Count);

        ConditionalOperatorToken expected = new ConditionalOperatorToken(4,
                                                                         test, new IntegerToken(1, 5), new IntegerToken(2, 7));

        Assert.AreEqual(expected, root.Children[0]);
    }