Ejemplo n.º 1
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());
        }
Ejemplo n.º 2
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());
        }
Ejemplo n.º 3
0
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("do");

            ToCodeFormat bodyFormat = ((Body != null &&
                                        Body.Count == 1 &&
                                        Body[0].GetType() == typeof(DoWhile))
              ? ToCodeFormat.AlwaysBraces
              : ToCodeFormat.Normal
                                       );

            // if the body is a single statement that ends in a do-while, then we
            // will need to wrap the body in curly-braces to get around an IE bug
            if (Body != null && Body.EncloseBlock(EncloseBlockType.SingleDoWhile))
            {
                bodyFormat = ToCodeFormat.AlwaysBraces;
            }

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

            if (bodyString.Length == 0)
            {
                sb.Append(';');
            }
            else
            {
                // if the first character could be interpreted as a continuation
                // of the "do" keyword, then we need to add a space
                if (JSScanner.StartsWithIdentifierPart(bodyString))
                {
                    sb.Append(' ');
                }
                sb.Append(bodyString);

                // if there is no body, we need a semi-colon
                // OR if we didn't always wrap in braces AND we require a separator, we need a semi-colon.
                // and make sure it doesn't already end in a semicolon -- we don't want two in a row.
                if (Body == null ||
                    (bodyFormat != ToCodeFormat.AlwaysBraces && Body.RequiresSeparator && !bodyString.EndsWith(";", StringComparison.Ordinal)))
                {
                    sb.Append(';');
                }
            }
            // add a space for readability for pretty-print mode
            if (Parser.Settings.OutputMode == OutputMode.MultipleLines && Parser.Settings.IndentSize > 0)
            {
                sb.Append(' ');
            }
            sb.Append("while(");
            sb.Append(Condition.ToCode());
            sb.Append(")");
            return(sb.ToString());
        }
Ejemplo n.º 4
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());
        }
Ejemplo n.º 5
0
        public override string ToCode(ToCodeFormat format)
        {
            string operandString = Operand.ToCode(format);

            if (NeedsParentheses)
            {
                // need parentheses
                return("typeof(" + operandString + ')');
            }
            else if (JSScanner.StartsWithIdentifierPart(operandString))
            {
                // need a space separating them
                return("typeof " + operandString);
            }
            else
            {
                // don't need the space
                return("typeof" + operandString);
            }
        }
Ejemplo n.º 6
0
        public override string ToCode(ToCodeFormat format)
        {
            string operandString = Operand.ToCode(format);

            if (NeedsParentheses)
            {
                // needs parens
                return("delete(" + operandString + ')');
            }
            else if (JSScanner.StartsWithIdentifierPart(operandString))
            {
                // needs a space
                return("delete " + operandString);
            }
            else
            {
                // needs no separator
                return("delete" + operandString);
            }
        }
Ejemplo n.º 7
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());
        }
Ejemplo n.º 8
0
Archivo: if.cs Proyecto: formist/LinkMe
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("if(");
            sb.Append(Condition.ToCode());
            sb.Append(')');

            // if we're in Safari-quirks mode, we will need to wrap the if block
            // in curly braces if it only includes a function declaration. Safari
            // throws parsing errors in those situations
            ToCodeFormat elseFormat = ToCodeFormat.Normal;

            if (FalseBlock != null && FalseBlock.Count == 1)
            {
                if (Parser.Settings.MacSafariQuirks &&
                    FalseBlock[0] is FunctionObject)
                {
                    elseFormat = ToCodeFormat.AlwaysBraces;
                }
                else if (FalseBlock[0] is IfNode)
                {
                    elseFormat = ToCodeFormat.ElseIf;
                }
            }

            // get the else block -- we need to know if there is anything in order
            // to fully determine if the true-branch needs curly-braces
            string elseBlock = (
                FalseBlock == null
                ? string.Empty
                : FalseBlock.ToCode(elseFormat));

            // we'll need to force the true block to be enclosed in curly braces if
            // there is an else block and the true block contains a single statement
            // that ends in an if that doesn't have an else block
            ToCodeFormat trueFormat = (FalseBlock != null &&
                                       TrueBlock != null &&
                                       TrueBlock.EncloseBlock(EncloseBlockType.IfWithoutElse)
                ? ToCodeFormat.AlwaysBraces
                : ToCodeFormat.Normal);

            if (elseBlock.Length > 0 &&
                TrueBlock != null &&
                TrueBlock.EncloseBlock(EncloseBlockType.SingleDoWhile))
            {
                trueFormat = ToCodeFormat.AlwaysBraces;
            }

            // if we're in Safari-quirks mode, we will need to wrap the if block
            // in curly braces if it only includes a function declaration. Safari
            // throws parsing errors in those situations
            if (Parser.Settings.MacSafariQuirks &&
                TrueBlock != null &&
                TrueBlock.Count == 1 &&
                TrueBlock[0] is FunctionObject)
            {
                trueFormat = ToCodeFormat.AlwaysBraces;
            }

            // add the true block
            string trueBlock = (
                TrueBlock == null
                ? string.Empty
                : TrueBlock.ToCode(trueFormat));

            sb.Append(trueBlock);

            if (elseBlock.Length > 0)
            {
                if (trueFormat != ToCodeFormat.AlwaysBraces &&
                    !trueBlock.EndsWith(";", StringComparison.Ordinal) &&
                    (TrueBlock == null || TrueBlock.RequiresSeparator))
                {
                    sb.Append(';');
                }

                // if we are in pretty-print mode, drop the else onto a new line
                Parser.Settings.NewLine(sb);
                sb.Append("else");
                // if the first character could be interpreted as a continuation
                // of the "else" statement, then we need to add a space
                if (JSScanner.StartsWithIdentifierPart(elseBlock))
                {
                    sb.Append(' ');
                }

                sb.Append(elseBlock);
            }
            return(sb.ToString());
        }
Ejemplo n.º 9
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());
        }