Esempio n. 1
0
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("for(");

            string var = m_var.ToCode();

            sb.Append(var);
            if (JSScanner.EndsWithIdentifierPart(var))
            {
                sb.Append(' ');
            }
            sb.Append("in");

            string collection = m_collection.ToCode();

            if (JSScanner.StartsWithIdentifierPart(collection))
            {
                sb.Append(' ');
            }
            sb.Append(m_collection.ToCode());
            sb.Append(')');

            string bodyString = (
                m_body == null
              ? string.Empty
              : m_body.ToCode()
                );

            sb.Append(bodyString);
            return(sb.ToString());
        }
Esempio n. 2
0
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            if (Parser.Settings.LocalRenaming != LocalRenaming.KeepAll &&
                Parser.Settings.IsModificationAllowed(TreeModifications.LocalRenaming))
            {
                // we're hyper-crunching.
                // we want to output our label as per our nested level.
                // top-level is "a", next level is "b", etc.
                // we don't need to worry about collisions with variables.
                sb.Append(CrunchEnumerator.CrunchedLabel(m_nestCount));
            }
            else
            {
                // not hypercrunching -- just output our label
                sb.Append(m_label);
            }
            sb.Append(':');
            if (m_statement != null)
            {
                // don't sent the AlwaysBraces down the chain -- we're handling it here.
                // but send any other formats down -- we don't know why they were sent.
                sb.Append(m_statement.ToCode(format));
            }
            return(sb.ToString());
        }
Esempio n. 3
0
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("throw");
            string exprString = (
                m_operand == null
              ? string.Empty
              : m_operand.ToCode()
                );

            if (exprString.Length > 0)
            {
                if (JSScanner.StartsWithIdentifierPart(exprString))
                {
                    sb.Append(' ');
                }
                sb.Append(exprString);
            }
            if (Parser.Settings.MacSafariQuirks)
            {
                sb.Append(';');
            }
            return(sb.ToString());
        }
Esempio n. 4
0
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("@set@");
            sb.Append(m_variableName);
            sb.Append('=');

            // if the value is an operator of any kind, we need to wrap it in parentheses
            // so it gets properly parsed
            if (m_value is BinaryOperator || m_value is UnaryOperator)
            {
                sb.Append('(');
                sb.Append(m_value.ToCode());
                sb.Append(')');
            }
            else if (m_value != null)
            {
                sb.Append(m_value.ToCode());
            }
            return(sb.ToString());
        }
Esempio n. 5
0
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("return");
            if (m_operand != null)
            {
                string operandText = m_operand.ToCode();
                if (operandText.Length > 0 && JSScanner.StartsWithIdentifierPart(operandText))
                {
                    sb.Append(' ');
                }
                sb.Append(operandText);
            }
            return(sb.ToString());
        }
Esempio n. 6
0
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(m_field.ToString());
            if (m_initializer != null)
            {
                if (m_ccSpecialCase)
                {
                    sb.Append(UseCCOn ? "/*@cc_on=" : "/*@=");
                }
                else
                {
                    if (Parser.Settings.OutputMode == OutputMode.MultipleLines && Parser.Settings.IndentSize > 0)
                    {
                        sb.Append(" = ");
                    }
                    else
                    {
                        sb.Append('=');
                    }
                }

                bool useParen = false;
                // a comma operator is the only thing with a lesser precedence than an assignment
                BinaryOperator binOp = m_initializer as BinaryOperator;
                if (binOp != null && binOp.OperatorToken == JSToken.Comma)
                {
                    useParen = true;
                }
                if (useParen)
                {
                    sb.Append('(');
                }
                sb.Append(m_initializer.ToCode(m_ccSpecialCase ? ToCodeFormat.Preprocessor : ToCodeFormat.Normal));
                if (useParen)
                {
                    sb.Append(')');
                }

                if (m_ccSpecialCase)
                {
                    sb.Append("@*/");
                }
            }
            return(sb.ToString());
        }
Esempio n. 7
0
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("with(");
            sb.Append(m_withObject.ToCode());
            sb.Append(")");

            string bodyString = (
                m_block == null
              ? ";"
              : m_block.ToCode()
                );

            sb.Append(bodyString);
            return(sb.ToString());
        }
Esempio n. 8
0
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();
            // switch and value
            sb.Append("switch(");
            sb.Append(m_expression.ToCode());
            sb.Append(')');

            // opening brace
            Parser.Settings.NewLine(sb);
            sb.Append('{');

            // cases
            sb.Append(m_cases.ToCode(ToCodeFormat.Semicolons));

            // closing brace
            Parser.Settings.NewLine(sb);
            sb.Append('}');
            return sb.ToString();
        }
Esempio n. 9
0
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            // the label should be indented
            Parser.Settings.Indent();
            // start a new line
            Parser.Settings.NewLine(sb);
            if (m_caseValue != null)
            {
                sb.Append("case");

                string caseValue = m_caseValue.ToCode();
                if (JSScanner.StartsWithIdentifierPart(caseValue))
                {
                    sb.Append(' ');
                }
                sb.Append(caseValue);
            }
            else
            {
                sb.Append("default");
            }
            sb.Append(':');

            // in pretty-print mode, we indent the statements under the label, too
            Parser.Settings.Indent();

            // output the statements
            if (m_statements != null && m_statements.Count > 0)
            {
                sb.Append(m_statements.ToCode(ToCodeFormat.NoBraces));
            }

            // if we are pretty-printing, we need to unindent twice:
            // once for the label, and again for the statements
            Parser.Settings.Unindent();
            Parser.Settings.Unindent();
            return(sb.ToString());
        }
Esempio n. 10
0
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            // we will only add a space if we KNOW we might need one
            bool needsSpace = false;

            // normal processing...
            if (m_isConstructor)
            {
                sb.Append("new");
                // the new operator might need to be followed by a space, depending
                // on what will actually follow
                needsSpace = true;
            }

            // list of items that DON'T need parens.
            // lookup, call or member don't need parens. All other items
            // are lower-precedence and therefore need to be wrapped in
            // parentheses to keep the right order.
            // function objects take care of their own parentheses.
            CallNode funcCall        = m_func as CallNode;
            bool     encloseInParens = !(
                (m_func is Lookup) ||
                (m_func is Member) ||
                (funcCall != null) ||
                (m_func is ThisLiteral) ||
                (m_func is FunctionObject)
                );

            // because if the new-operator associates to the right and the ()-operator associates
            // to the left, we need to be careful that we don't change the precedence order when the
            // function of a new operator is itself a call. In that case, the call will have it's own
            // parameters (and therefore parentheses) that will need to be associated with the call
            // and NOT the new -- the call will need to be surrounded with parens to keep that association.
            if (m_isConstructor && funcCall != null && !funcCall.InBrackets)
            {
                encloseInParens = true;
            }


            // if the root is a constructor with no arguments, we'll need to wrap it in parens so the
            // member-dot comes out with the right precedence.
            // (don't bother checking if we already are already going to use parens)
            if (!encloseInParens && funcCall != null && funcCall.IsConstructor &&
                (funcCall.Arguments == null || funcCall.Arguments.Count == 0))
            {
                encloseInParens = true;
            }

            if (encloseInParens)
            {
                // we're adding a parenthesis, so no -- we won't need to
                // add a space
                needsSpace = false;
                sb.Append('(');
            }

            string functionString = m_func.ToCode();

            // if we still think we might need a space, check the function we just
            // formatted. If it starts with an identifier part, then we need the space.
            if (needsSpace && JSScanner.StartsWithIdentifierPart(functionString))
            {
                sb.Append(' ');
            }
            sb.Append(functionString);

            if (encloseInParens)
            {
                sb.Append(')');
            }
            // if this isn't a constructor, or if it is and there are parameters,
            // then we want to output the parameters. But if this is a constructor with
            // no parameters, we can skip the whole empty-argument-parens thing altogether.
            if (!m_isConstructor || (m_args != null && m_args.Count > 0))
            {
                sb.Append((m_inBrackets ? '[' : '('));
                if (m_args != null)
                {
                    sb.Append(m_args.ToCode(ToCodeFormat.Commas));
                }
                sb.Append((m_inBrackets ? ']' : ')'));
            }

            return(sb.ToString());
        }
Esempio n. 11
0
 public override string ToCode(ToCodeFormat format)
 {
     return("@elif(" + m_condition.ToCode() + ")");
 }
Esempio n. 12
0
        public override string ToCode(ToCodeFormat format)
        {
            if (format == ToCodeFormat.NoBraces && m_list.Count == 0)
            {
                return(string.Empty);
            }

            StringBuilder sb         = new StringBuilder();
            bool          closeBrace = false;
            bool          unindent   = false;

            // if the format is N, then we never enclose in braces.
            // if the format is B or T, then we always enclose in braces.
            // anything else, then we enclose in braces if there's more than
            // one enclosing line
            if (format != ToCodeFormat.NoBraces && Parent != null)
            {
                if (format == ToCodeFormat.AlwaysBraces ||
                    format == ToCodeFormat.NestedTry ||
                    m_list.Count > 1)
                {
                    // opening brace on a new line
                    Parser.Settings.NewLine(sb);

                    // up the indent level for the content within
                    Parser.Settings.Indent();

                    sb.Append("{");
                    closeBrace = true;
                }
                else if (m_list.Count == 1 && format != ToCodeFormat.ElseIf)
                {
                    // we're pretty-printing a single-line block.
                    // we still won't enclose in brackets, but we need to indent it
                    Parser.Settings.Indent();
                    unindent = true;
                }
            }

            bool requireSeparator   = true;
            bool endsWithEmptyBlock = false;
            bool mightNeedSpace     = false;

            for (int ndx = 0; ndx < m_list.Count; ++ndx)
            {
                AstNode item = m_list[ndx];
                if (item != null)
                {
                    // see if we need to add a semi-colon
                    if (ndx > 0 && requireSeparator)
                    {
                        sb.Append(';');
                        if (Parser.Settings.OutputMode == OutputMode.SingleLine && item is ImportantComment)
                        {
                            // if this item is an important comment and we're in single-line mode,
                            // we'll start on a new line
                            sb.Append('\n');
                        }
                        // we no longer require a separator next time around
                        requireSeparator = false;
                    }

                    string itemText = item.ToCode();
                    if (itemText.Length > 0)
                    {
                        // if this is an else-if construct, we don't want to break to a new line.
                        // but all other formats put the next statement on a newline
                        if (format != ToCodeFormat.ElseIf)
                        {
                            Parser.Settings.NewLine(sb);
                        }

                        if (mightNeedSpace && JSScanner.IsValidIdentifierPart(itemText[0]))
                        {
                            sb.Append(' ');
                        }

                        sb.Append(itemText);
                        requireSeparator   = (item.RequiresSeparator && !itemText.EndsWith(";", StringComparison.Ordinal));
                        endsWithEmptyBlock = item.EndsWithEmptyBlock;

                        mightNeedSpace =
                            (item is ConditionalCompilationStatement) &&
                            JSScanner.IsValidIdentifierPart(itemText[itemText.Length - 1]);
                    }
                }
            }
            if (endsWithEmptyBlock)
            {
                sb.Append(';');
            }

            if (closeBrace)
            {
                // unindent now that the block is done
                Parser.Settings.Unindent();
                Parser.Settings.NewLine(sb);
                sb.Append("}");
            }
            else if (unindent)
            {
                Parser.Settings.Unindent();
            }
            return(sb.ToString());
        }
Esempio n. 13
0
 public override string ToCode(ToCodeFormat format)
 {
     return("eval(" + m_operand.ToCode() + ")");
 }
Esempio n. 14
0
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb     = new StringBuilder();
            bool          parens = NeedsParens(m_condition, JSToken.ConditionalIf);

            if (parens)
            {
                sb.Append('(');
            }

            sb.Append(m_condition.ToCode());
            if (parens)
            {
                sb.Append(')');
            }

            CodeSettings codeSettings = Parser.Settings;

            if (codeSettings.OutputMode == OutputMode.MultipleLines && codeSettings.IndentSize > 0)
            {
                sb.Append(" ? ");
            }
            else
            {
                sb.Append('?');
            }

            // the true and false operands are parsed as assignment operators, so use that token as the
            // reference token to compare against for operator precedence to determine if we need parens
            parens = NeedsParens(m_trueExpression, JSToken.Assign);
            if (parens)
            {
                sb.Append('(');
            }

            sb.Append(m_trueExpression.ToCode());
            if (parens)
            {
                sb.Append(')');
            }

            if (codeSettings.OutputMode == OutputMode.MultipleLines && codeSettings.IndentSize > 0)
            {
                sb.Append(" : ");
            }
            else
            {
                sb.Append(':');
            }

            parens = NeedsParens(m_falseExpression, JSToken.Assign);
            if (parens)
            {
                sb.Append('(');
            }

            sb.Append(m_falseExpression.ToCode());
            if (parens)
            {
                sb.Append(')');
            }
            return(sb.ToString());
        }