/// <summary>
        /// Produces the Javascript and appends it to the StringBuilder passed in the builder argument.
        /// </summary>
        /// <param name="builder">The StringBuilder instance to append the Javascript to.</param>
        /// <param name="options">Specifies options to use while producing Javascript.</param>
        public void AppendScript(StringBuilder builder, ScriptOptions options)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            if (_value != null)
            {
                builder.Append("case ");
                _value.AppendScript(builder, options, false);
                builder.Append(":");
            }
            else
            {
                builder.Append("default:");
            }

            if (_statements != null)
            {
                foreach (Statement statement in _statements.WithConvertedNulls())
                {
                    statement.AppendScript(builder, options, false);
                    statement.AppendRequiredTerminator(builder);
                }
            }
        }
        /// <summary>
        /// Appends the script to represent this object to the StringBuilder.
        /// </summary>
        /// <param name="builder">The StringBuilder to which the Javascript is appended.</param>
        /// <param name="options">The options to use when appending JavaScript</param>
        /// <param name="allowReservedWords"></param>
        internal protected override void AppendScript(StringBuilder builder, ScriptOptions options, bool allowReservedWords)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            if (_variable == null)
            {
                throw new InvalidOperationException();
            }

            if (_collection == null)
            {
                throw new InvalidOperationException();
            }

            if (_statement == null)
            {
                throw new InvalidOperationException();
            }

            builder.Append("for(");
            _variable.AppendScript(builder, options, allowReservedWords);
            builder.Append(" in ");
            _collection.AppendScript(builder, options, allowReservedWords);
            builder.Append(")");
            _statement.AppendScript(builder, options, allowReservedWords);
            _statement.AppendRequiredTerminator(builder);
        }
Пример #3
0
        /// <summary>
        /// Appends the script to represent this object to the StringBuilder.
        /// </summary>
        /// <param name="builder">The StringBuilder to which the Javascript is appended.</param>
        /// <param name="options">The options to use when appending JavaScript</param>
        /// <param name="allowReservedWords"></param>
        internal protected override void AppendScript(StringBuilder builder, ScriptOptions options, bool allowReservedWords)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            if (_operandLeft == null)
            {
                throw new InvalidOperationException();
            }

            if (_operandRight == null)
            {
                throw new InvalidOperationException();
            }

            Expression operandLeft  = _operandLeft;
            Expression operandRight = _operandRight;

            if (operandLeft.PrecedenceLevel.RequiresGrouping(PrecedenceLevel, Association.LeftToRight))
            {
                operandLeft = JS.Group(operandLeft);
            }

            operandLeft.AppendScript(builder, options, allowReservedWords);

            builder.Append(".");

            operandRight.AppendScript(builder, options, allowReservedWords);
        }
        /// <summary>
        /// Appends the script to represent this object to the StringBuilder.
        /// </summary>
        /// <param name="builder">The StringBuilder to which the Javascript is appended.</param>
        /// <param name="options">The options to use when appending JavaScript</param>
        /// <param name="allowReservedWords"></param>
        protected internal override void AppendScript(StringBuilder builder, ScriptOptions options, bool allowReservedWords)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            if (Condition == null)
            {
                throw new InvalidOperationException("The Condition property cannot be null.");
            }

            if (Then == null)
            {
                throw new InvalidOperationException("The Then property cannot be null.");
            }

            if (Else == null)
            {
                throw new InvalidOperationException("The Else property cannot be null.");
            }

            // TODO: Check if we need to support Precedence.
            Condition.AppendScript(builder, options, allowReservedWords);
            builder.Append("?");
            Then.AppendScript(builder, options, allowReservedWords);
            builder.Append(":");
            Else.AppendScript(builder, options, allowReservedWords);
        }
        /// <summary>
        /// Appends the script to represent this object to the StringBuilder.
        /// </summary>
        /// <param name="builder">The StringBuilder to which the Javascript is appended.</param>
        /// <param name="options">The options to use when appending JavaScript</param>
        /// <param name="allowReservedWords"></param>
        internal protected override void AppendScript(StringBuilder builder, ScriptOptions options, bool allowReservedWords)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            builder.Append("return");

            if (_value != null)
            {
                builder.Append(" ");
                _value.AppendScript(builder, options, allowReservedWords);
            }
        }
Пример #6
0
        /// <summary>
        /// Appends the script to represent this object to the StringBuilder.
        /// </summary>
        /// <param name="builder">The StringBuilder to which the Javascript is appended.</param>
        /// <param name="options">The options to use when appending JavaScript</param>
        /// <param name="allowReservedWords"></param>
        internal protected override void AppendScript(StringBuilder builder, ScriptOptions options, bool allowReservedWords)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            if (_expression == null)
            {
                throw new InvalidOperationException();
            }

            builder.Append("throw ");
            _expression.AppendScript(builder, options, allowReservedWords);
        }
Пример #7
0
        /// <summary>
        /// Appends the script to represent this object to the StringBuilder.
        /// </summary>
        /// <param name="builder">The StringBuilder to which the Javascript is appended.</param>
        /// <param name="options">The options to use when appending JavaScript</param>
        /// <param name="allowReservedWords"></param>
        internal protected override void AppendScript(StringBuilder builder, ScriptOptions options, bool allowReservedWords)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            if (_expression == null)
            {
                throw new InvalidOperationException();
            }

            builder.Append("switch(");
            _expression.AppendScript(builder, options, allowReservedWords);
            builder.Append("){");

            bool defaultPassed = false;

            foreach (CaseStatement @case in _cases)
            {
                if (@case == null)
                {
                    continue;
                }

                if (defaultPassed)
                {
                    throw new InvalidOperationException();
                }

                @case.AppendScript(builder, options);

                if (@case.Value == null)
                {
                    defaultPassed = true;
                }
            }

            builder.Append("}");
        }
        /// <summary>
        /// Appends the script to represent this object to the StringBuilder.
        /// </summary>
        /// <param name="builder">The StringBuilder to which the Javascript is appended.</param>
        /// <param name="options">The options to use when appending JavaScript</param>
        /// <param name="allowReservedWords"></param>
        internal protected override void AppendScript(StringBuilder builder, ScriptOptions options, bool allowReservedWords)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            if (OperandLeft == null)
            {
                throw new InvalidOperationException();
            }

            if (OperandRight == null)
            {
                throw new InvalidOperationException();
            }

            _operandLeft.AppendScript(builder, options, allowReservedWords);
            builder.Append("[");
            _operandRight.AppendScript(builder, options, allowReservedWords);
            builder.Append("]");
        }
Пример #9
0
        /// <summary>
        /// Appends the script to represent this object to the StringBuilder.
        /// </summary>
        /// <param name="builder">The StringBuilder to which the Javascript is appended.</param>
        /// <param name="options">The options to use when appending JavaScript</param>
        /// <param name="allowReservedWords"></param>
        internal protected override void AppendScript(StringBuilder builder, ScriptOptions options, bool allowReservedWords)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            Expression operand = _operand;

            if (operand.PrecedenceLevel.RequiresGrouping(PrecedenceLevel, Association.LeftToRight))
            {
                operand = JS.Group(_operand);
            }

            operand.AppendScript(builder, options, allowReservedWords);

            builder.Append("(");

            bool isFirst = true;

            foreach (Expression param in _arguments.WithConvertedNulls())
            {
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    builder.Append(",");
                }

                param.AppendScript(builder, options, allowReservedWords);
            }

            builder.Append(")");
        }
Пример #10
0
        /// <summary>
        /// Appends the script to represent this object to the StringBuilder.
        /// </summary>
        /// <param name="builder">The StringBuilder to which the Javascript is appended.</param>
        /// <param name="options">The options to use when appending JavaScript</param>
        /// <param name="allowReservedWords"></param>
        internal protected override void AppendScript(StringBuilder builder, ScriptOptions options, bool allowReservedWords)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            if (_condition == null)
            {
                throw new InvalidOperationException("Condition cannot be null.");
            }

            if (_statement == null)
            {
                throw new InvalidOperationException("Statement cannot be null.");
            }

            builder.Append("do ");
            _statement.AppendScript(builder, options, allowReservedWords);
            _statement.AppendRequiredTerminator(builder);
            builder.Append("while(");
            _condition.AppendScript(builder, options, allowReservedWords);
            builder.Append(")");
        }
Пример #11
0
        /// <summary>
        /// Appends the script to represent this object to the StringBuilder.
        /// </summary>
        /// <param name="builder">The StringBuilder to which the Javascript is appended.</param>
        /// <param name="options">The options to use when appending JavaScript</param>
        /// <param name="allowReservedWords"></param>
        internal protected override void AppendScript(StringBuilder builder, ScriptOptions options, bool allowReservedWords)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            bool noLeftSide  = false;
            bool noRightSide = false;

            switch (_operator)
            {
            case UnaryOperator.Number:
                builder.Append("+");
                break;

            case UnaryOperator.Negative:
                builder.Append("-");
                break;

            case UnaryOperator.BitwiseNot:
                builder.Append("~");
                break;

            case UnaryOperator.LogicalNot:
                builder.Append("!");
                break;

            case UnaryOperator.PreIncrement:
                builder.Append("++");
                break;

            case UnaryOperator.PreDecrement:
                builder.Append("--");
                break;

            case UnaryOperator.TypeOf:
                builder.Append("typeof ");
                break;

            case UnaryOperator.New:
                builder.Append("new ");
                break;

            case UnaryOperator.Delete:
                builder.Append("delete ");
                break;

            case UnaryOperator.Group:
                builder.Append("(");
                break;

            default:
                noLeftSide = true;
                break;
            }

            Expression operand = _operand ?? new NullExpression();

            operand.AppendScript(builder, options, allowReservedWords);

            switch (_operator)
            {
            case UnaryOperator.PostIncrement:
                builder.Append("++");
                break;

            case UnaryOperator.PostDecrement:
                builder.Append("--");
                break;

            case UnaryOperator.Group:
                builder.Append(")");
                break;

            default:
                noRightSide = true;
                break;
            }

            if (noLeftSide && noRightSide)
            {
                throw new InvalidOperationException("This operator yielded no result.");
            }
        }
        internal protected override void AppendScript(StringBuilder builder, ScriptOptions options, bool allowReservedWords)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            Expression operandLeft  = _operandLeft ?? new NullExpression();
            Expression operandRight = _operandRight ?? new NullExpression();

            if (operandLeft.PrecedenceLevel.RequiresGrouping(PrecedenceLevel, Association.LeftToRight))
            {
                operandLeft = JS.Group(operandLeft);
            }

            if (operandRight.PrecedenceLevel.RequiresGrouping(PrecedenceLevel, Association.RightToLeft))
            {
                operandRight = JS.Group(operandRight);
            }

            operandLeft.AppendScript(builder, options, allowReservedWords);

            switch (_operator)
            {
            case BinaryOperator.Assign:
                builder.Append("=");
                break;

            case BinaryOperator.Add:
                builder.Append("+");
                break;

            case BinaryOperator.AddAndAssign:
                builder.Append("+=");
                break;

            case BinaryOperator.Subtract:
                builder.Append("-");
                break;

            case BinaryOperator.SubtractAndAssign:
                builder.Append("-=");
                break;

            case BinaryOperator.Multiply:
                builder.Append("*");
                break;

            case BinaryOperator.MultiplyAndAssign:
                builder.Append("*=");
                break;

            case BinaryOperator.Divide:
                builder.Append("/");
                break;

            case BinaryOperator.DivideAndAssign:
                builder.Append("/=");
                break;

            case BinaryOperator.Remain:
                builder.Append("%");
                break;

            case BinaryOperator.RemainAndAssign:
                builder.Append("%=");
                break;

            case BinaryOperator.BitwiseAnd:
                builder.Append("&");
                break;

            case BinaryOperator.BitwiseAndAndAssign:
                builder.Append("&=");
                break;

            case BinaryOperator.BitwiseOr:
                builder.Append("|");
                break;

            case BinaryOperator.BitwiseOrAndAssign:
                builder.Append("|=");
                break;

            case BinaryOperator.BitwiseXor:
                builder.Append("^");
                break;

            case BinaryOperator.BitwiseXorAndAssign:
                builder.Append("^=");
                break;

            case BinaryOperator.ShiftLeft:
                builder.Append("<<");
                break;

            case BinaryOperator.ShiftLeftAndAssign:
                builder.Append("<<=");
                break;

            case BinaryOperator.ShiftRight:
                builder.Append(">>");
                break;

            case BinaryOperator.ShiftRightAndAssign:
                builder.Append(">>=");
                break;

            case BinaryOperator.Equals:
                builder.Append("==");
                break;

            case BinaryOperator.Identical:
                builder.Append("===");
                break;

            case BinaryOperator.NotEqual:
                builder.Append("!=");
                break;

            case BinaryOperator.NotIdentical:
                builder.Append("!==");
                break;

            case BinaryOperator.GreaterThan:
                builder.Append(">");
                break;

            case BinaryOperator.GreaterThanOrEqualTo:
                builder.Append(">=");
                break;

            case BinaryOperator.LessThan:
                builder.Append("<");
                break;

            case BinaryOperator.LessThanOrEqualTo:
                builder.Append("<=");
                break;

            case BinaryOperator.LogicalAnd:
                builder.Append("&&");
                break;

            case BinaryOperator.LogicalOr:
                builder.Append("||");
                break;

            case BinaryOperator.InstanceOf:
                builder.Append(" instanceof ");
                break;

            case BinaryOperator.In:
                builder.Append(" in ");
                break;

            case BinaryOperator.MultipleEvaluation:
                builder.Append(",");
                break;

            default:
                throw new InvalidOperationException("What is this?");
            }

            operandRight.AppendScript(builder, options, allowReservedWords);
        }