public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append("var "); Parser.Settings.Indent(); bool useNewline = !(Parent is ForNode || Parent is ForIn); bool first = true; for (int ndx = 0; ndx < m_list.Count; ++ndx) { VariableDeclaration vdecl = m_list[ndx]; if (vdecl != null) { if (!first) { sb.Append(','); if (useNewline) { Parser.Settings.NewLine(sb); } } sb.Append(vdecl.ToCode()); first = false; } } Parser.Settings.Unindent(); return(sb.ToString()); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append(m_isGetter ? "get " : "set "); sb.Append(Value); return sb.ToString(); }
public override string ToCode(ToCodeFormat format) { string operandString = Operand.ToCode(format); if (NeedsParentheses) { operandString = "(" + operandString + ")"; } switch (m_postOrPrefixOperator) { case PostOrPrefix.PostfixDecrement: return(operandString + "--"); case PostOrPrefix.PostfixIncrement: return(operandString + "++"); case PostOrPrefix.PrefixDecrement: return("--" + operandString); case PostOrPrefix.PrefixIncrement: return("++" + operandString); default: throw new UnexpectedTokenException(); } }
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()); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append(JSScanner.GetOperatorString(OperatorToken)); if (Operand != null) { string operandString = Operand.ToCode(format); if (NeedsParentheses) { sb.Append('('); sb.Append(operandString); sb.Append(')'); } else { if (operandString.Length > 0) { // make sure that - - or + + doesn't get crunched to -- or ++ // (which would totally change the meaning of the code) if ((OperatorToken == JSToken.Minus && operandString[0] == '-') || (OperatorToken == JSToken.Plus && operandString[0] == '+')) { sb.Append(' '); } } sb.Append(operandString); } } return(sb.ToString()); }
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()); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append(JSScanner.GetOperatorString(OperatorToken)); if (Operand != null) { string operandString = Operand.ToCode(format); if (NeedsParentheses) { sb.Append('('); sb.Append(operandString); sb.Append(')'); } else { if (operandString.Length > 0) { // make sure that - - or + + doesn't get crunched to -- or ++ // (which would totally change the meaning of the code) if ((OperatorToken == JSToken.Minus && operandString[0] == '-') || (OperatorToken == JSToken.Plus && operandString[0] == '+')) { sb.Append(' '); } } sb.Append(operandString); } } return sb.ToString(); }
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()); }
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()); }
/* * public override AstNode Clone() * { * return new GetterSetter(m_identifier, m_isGetter, Context.Clone(), Parser); * } */ public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append(m_isGetter ? "get " : "set "); sb.Append(Value); return(sb.ToString()); }
public override string ToCode(ToCodeFormat format) { // if we have a local field pointer that has a crunched name, // the return the crunched name. Otherwise just return our given name; return(VariableField != null ? VariableField.ToString() : m_name); }
public override string ToCode(ToCodeFormat format) { return string.Format( CultureInfo.InvariantCulture, "/{0}/{1}", Pattern, (PatternSwitches == null ? string.Empty : PatternSwitches) ); }
public override string ToCode(ToCodeFormat format) { return(string.Format( CultureInfo.InvariantCulture, "/{0}/{1}", m_pattern, (m_patternSwitches == null ? string.Empty : m_patternSwitches) )); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append('['); if (m_elements != null) { sb.Append(m_elements.ToCode(ToCodeFormat.Commas)); } sb.Append(']'); return(sb.ToString()); }
public override string ToCode(ToCodeFormat format) { bool requiresSeparator = false; string separator; switch (format) { case ToCodeFormat.Commas: separator = ","; break; case ToCodeFormat.Semicolons: separator = ";"; break; default: separator = string.Empty; break; } StringBuilder sb = new StringBuilder(); for (int ndx = 0; ndx < m_list.Count; ++ndx) { // the crunched code for the item, passing in the separator // we are using (in case it affects the code by needing to add parens or something) string itemText = m_list[ndx].ToCode(format); // see if we need to add the separator if (separator.Length > 0) { // don't add it if this is the first item; we only need it between them // (and only then if it's required) if (ndx > 0 && requiresSeparator) { sb.Append(separator); } // see if we'll need one for the next iteration (if any). // if we're separating with commas, then we will always add a comma unless there already is one. // otherwise we'll only add a separator if there isn't already one AND we require a separator. requiresSeparator = !itemText.EndsWith(separator, StringComparison.Ordinal) && (format == ToCodeFormat.Commas || m_list[ndx].RequiresSeparator); } // add the item to the stream sb.Append(itemText); } return(sb.ToString()); }
//code in parser relies on the member string (x.y.z...) being returned from here public override string ToCode(ToCodeFormat format) { // pass P to the root object so it knows we might want parentheses string rootCrunched = Root.ToCode(); // these tests are for items that DON'T need parens, and then we NOT the results. // non-numeric constant wrappers don't need parens (boolean, string, null). // numeric constant wrappers need parens IF there is no decimal point. // function expressions will take care of their own parens. bool needParen = !( (Root is Lookup) || (Root is Member) || (Root is CallNode) || (Root is ThisLiteral) || (Root is ArrayLiteral) || (Root is ObjectLiteral) || (Root is RegExpLiteral) || (Root is FunctionObject) || (Root is ConstantWrapper && !((ConstantWrapper)Root).IsNumericLiteral) || (Root is ConstantWrapper && ((ConstantWrapper)Root).IsNumericLiteral && rootCrunched.Contains(".")) ); // 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 (!needParen) { CallNode callNode = Root as CallNode; if (callNode != null && callNode.IsConstructor && (callNode.Arguments == null || callNode.Arguments.Count == 0)) { needParen = true; } } StringBuilder sb = new StringBuilder(); if (needParen) { sb.Append('('); sb.Append(rootCrunched); sb.Append(')'); } else { sb.Append(rootCrunched); } sb.Append('.'); sb.Append(Name); return(sb.ToString()); }
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(); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); if (m_forceComments) { sb.Append("/*"); } sb.Append('@'); sb.Append(m_varName); if (m_forceComments) { sb.Append("@*/"); } return sb.ToString(); }
public override string ToCode(ToCodeFormat format) { if (PrimitiveType == PrimitiveType.String) { string rawValue = Value.ToString(); // if the raw value is safe to be an identifier, then go ahead and ditch the quotes and just output // the raw value. Otherwise call ToCode to wrap the string in quotes. return(JSScanner.IsSafeIdentifier(rawValue) && !JSScanner.IsKeyword(rawValue) ? rawValue : base.ToCode(format)); } else { // call the base to format the value return(base.ToCode(format)); } }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); if (m_forceComments) { sb.Append("/*"); } sb.Append('@'); sb.Append(m_varName); if (m_forceComments) { sb.Append("@*/"); } return(sb.ToString()); }
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()); }
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()); }
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()); }
public override string ToCode(ToCodeFormat format) { if (PrimitiveType == PrimitiveType.String && Parser.Settings.IsModificationAllowed(TreeModifications.RemoveQuotesFromObjectLiteralNames)) { string rawValue = Value.ToString(); // if the raw value is safe to be an identifier, then go ahead and ditch the quotes and just output // the raw value. Otherwise call ToCode to wrap the string in quotes. return JSScanner.IsSafeIdentifier(rawValue) && !JSScanner.IsKeyword(rawValue, EnclosingScope.UseStrict) ? rawValue : base.ToCode(format); } else { // call the base to format the value return base.ToCode(format); } }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append("while("); sb.Append(Condition.ToCode()); sb.Append(')'); string bodyString = ( Body == null ? string.Empty : Body.ToCode() ); sb.Append(bodyString); return(sb.ToString()); }
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; } }
public override string ToCode(ToCodeFormat format) { string operandString = Operand.ToCode(format); if (NeedsParentheses) { // need parens return "void(" + operandString + ')'; } else if (JSScanner.StartsWithIdentifierPart(operandString)) { // need the space return "void " + operandString; } else { // no separator needed return "void" + operandString; } }
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(); }
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); } }
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); } }
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()); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append("continue"); if (Label != null) { sb.Append(' '); if (Parser.Settings.LocalRenaming != LocalRenaming.KeepAll && Parser.Settings.IsModificationAllowed(TreeModifications.LocalRenaming)) { // hypercrunched -- only depends on nesting level sb.Append(CrunchEnumerator.CrunchedLabel(NestLevel)); } else { // not hypercrunched -- just output label sb.Append(Label); } } return sb.ToString(); }
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()); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append("for("); if (Initializer != null) { sb.Append(Initializer.ToCode()); } sb.Append(';'); CodeSettings codeSettings = Parser.Settings; if (codeSettings.OutputMode == OutputMode.MultipleLines && codeSettings.IndentSize > 0) { sb.Append(' '); } if (Condition != null) { sb.Append(Condition.ToCode()); } sb.Append(';'); if (codeSettings.OutputMode == OutputMode.MultipleLines && codeSettings.IndentSize > 0) { sb.Append(' '); } if (Incrementer != null) { sb.Append(Incrementer.ToCode()); } sb.Append(')'); string bodyString = ( Body == null ? string.Empty : Body.ToCode() ); sb.Append(bodyString); return(sb.ToString()); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); // if there aren't any statements, we don't need this comment if (m_statements.Count > 0) { sb.Append("/*"); // a first conditional compilation statement will take care of the opening @-sign, // so if the first statement is NOT a conditional compilation statement, then we // need to take care of it outselves if (!(m_statements[0] is ConditionalCompilationStatement)) { sb.Append("@ "); } sb.Append(m_statements.ToCode(ToCodeFormat.NoBraces)); sb.Append("@*/"); } return(sb.ToString()); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append('{'); int count = (m_keys.Length < m_values.Length ? m_keys.Length : m_values.Length); if (count > 0) { Parser.Settings.Indent(); for (int ndx = 0; ndx < count; ++ndx) { if (ndx > 0) { sb.Append(','); } Parser.Settings.NewLine(sb); sb.Append(m_keys[ndx].ToCode()); if (m_keys[ndx] is GetterSetter) { sb.Append(m_values[ndx].ToCode(ToCodeFormat.NoFunction)); } else { // the key is always an identifier, string or numeric literal sb.Append(':'); sb.Append(m_values[ndx].ToCode()); } } Parser.Settings.Unindent(); Parser.Settings.NewLine(sb); } sb.Append('}'); return(sb.ToString()); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append("continue"); if (m_label != null) { sb.Append(' '); if (Parser.Settings.LocalRenaming != LocalRenaming.KeepAll && Parser.Settings.IsModificationAllowed(TreeModifications.LocalRenaming)) { // hypercrunched -- only depends on nesting level sb.Append(CrunchEnumerator.CrunchedLabel(m_nestLevel)); } else { // not hypercrunched -- just output label sb.Append(m_label); } } return(sb.ToString()); }
public override string ToCode(ToCodeFormat format) { return "this"; }
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(); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); // passing a "T" format means nested try's don't actually nest -- they // just add the catch clauses to the end if (format != ToCodeFormat.NestedTry) { sb.Append("try"); if (TryBlock == null) { // empty body sb.Append("{}"); } else { sb.Append(TryBlock.ToCode(ToCodeFormat.NestedTry)); } } else { sb.Append(TryBlock.ToCode(ToCodeFormat.NestedTry)); } // handle the catch clause (if any) // catch should always have braces around it string catchString = ( CatchBlock == null ? string.Empty : CatchBlock.Count == 0 ? "{}" : CatchBlock.ToCode(ToCodeFormat.AlwaysBraces) ); if (catchString.Length > 0) { Parser.Settings.NewLine(sb); sb.Append("catch("); if (m_catchVariable != null) { sb.Append(m_catchVariable.ToString()); } else if (m_catchVarName != null) { sb.Append(m_catchVarName); } sb.Append(')'); sb.Append(catchString); } // handle the finally, if any // finally should always have braces around it string finallyString = ( FinallyBlock == null ? string.Empty : FinallyBlock.ToCode(ToCodeFormat.AlwaysBraces) ); if (finallyString.Length > 0) { Parser.Settings.NewLine(sb); sb.Append("finally"); sb.Append(finallyString); } return(sb.ToString()); }
public override string ToCode(ToCodeFormat format) { string str; switch(PrimitiveType) { case PrimitiveType.Null: str = "null"; break; case PrimitiveType.Number: if (Context == null || Parser.Settings.IsModificationAllowed(TreeModifications.MinifyNumericLiterals)) { // apply minification to the literal to get it as small as possible str = NumericToString(); } else { // just use the original literal from the context str = Context.Code; } break; case PrimitiveType.Boolean: str = Convert.ToBoolean(Value, CultureInfo.InvariantCulture) ? "true" : "false"; break; case PrimitiveType.String: if (Context == null || Parser.Settings.IsModificationAllowed(TreeModifications.MinifyStringLiterals)) { // escape the string str = EscapeString( Value.ToString(), IsParameterToRegExp, false, EnclosingScope.UseStrict); } else { // just use the original literal from the context str = Context.Code; } if (Parser.Settings.InlineSafeStrings) { // if there are ANY closing script tags... if (str.IndexOf("</script>", StringComparison.OrdinalIgnoreCase) >= 0) { // replace all of them with an escaped version so a text-compare won't match str = str.Replace("</script>", @"<\/script>"); } // if there are ANY closing CDATA strings... if (str.IndexOf("]]>", StringComparison.Ordinal) >= 0) { // replace all of them with an escaped version so a text-compare won't match str = str.Replace("]]>", @"]\]>"); } } break; default: // other.... str = Value.ToString(); break; } return str; }
public override string ToCode(ToCodeFormat format) { return "@else"; }
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(); }
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(); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append("for("); if (Initializer != null) { sb.Append(Initializer.ToCode()); } sb.Append(';'); CodeSettings codeSettings = Parser.Settings; if (codeSettings.OutputMode == OutputMode.MultipleLines && codeSettings.IndentSize > 0) { sb.Append(' '); } if (Condition != null) { sb.Append(Condition.ToCode()); } sb.Append(';'); if (codeSettings.OutputMode == OutputMode.MultipleLines && codeSettings.IndentSize > 0) { sb.Append(' '); } if (Incrementer != null) { sb.Append(Incrementer.ToCode()); } sb.Append(')'); string bodyString = ( Body == null ? string.Empty : Body.ToCode() ); sb.Append(bodyString); return sb.ToString(); }
public override string ToCode(ToCodeFormat format) { return "@elif(" + Condition.ToCode() + ")"; }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); bool parens = NeedsParens(Condition, JSToken.ConditionalIf); if (parens) { sb.Append('('); } sb.Append(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(TrueExpression, JSToken.Assign); if (parens) { sb.Append('('); } sb.Append(TrueExpression.ToCode()); if (parens) { sb.Append(')'); } if (codeSettings.OutputMode == OutputMode.MultipleLines && codeSettings.IndentSize > 0) { sb.Append(" : "); } else { sb.Append(':'); } parens = NeedsParens(FalseExpression, JSToken.Assign); if (parens) { sb.Append('('); } sb.Append(FalseExpression.ToCode()); if (parens) { sb.Append(')'); } return sb.ToString(); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append("for("); string var = Variable.ToCode(); sb.Append(var); if (JSScanner.EndsWithIdentifierPart(var)) { sb.Append(' '); } sb.Append("in"); string collection = Collection.ToCode(); if (JSScanner.StartsWithIdentifierPart(collection)) { sb.Append(' '); } sb.Append(Collection.ToCode()); sb.Append(')'); string bodyString = ( Body == null ? string.Empty : Body.ToCode() ); sb.Append(bodyString); return sb.ToString(); }
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(); }
public override string ToCode(ToCodeFormat format) { return("@else"); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); // switch and value sb.Append("switch("); sb.Append(Expression.ToCode()); sb.Append(')'); // opening brace Parser.Settings.NewLine(sb); sb.Append('{'); // cases sb.Append(Cases.ToCode(ToCodeFormat.Semicolons)); // closing brace Parser.Settings.NewLine(sb); sb.Append('}'); return sb.ToString(); }
public override string ToCode(ToCodeFormat format) { return m_comment; }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append('{'); int count = (m_keys.Length < m_values.Length ? m_keys.Length : m_values.Length); if (count > 0) { Parser.Settings.Indent(); for (int ndx = 0; ndx < count; ++ndx) { if (ndx > 0) { sb.Append(','); } Parser.Settings.NewLine(sb); sb.Append(m_keys[ndx].ToCode()); if (m_keys[ndx] is GetterSetter) { sb.Append(m_values[ndx].ToCode(ToCodeFormat.NoFunction)); } else { // the key is always an identifier, string or numeric literal sb.Append(':'); sb.Append(m_values[ndx].ToCode()); } } Parser.Settings.Unindent(); Parser.Settings.NewLine(sb); } sb.Append('}'); return sb.ToString(); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append("var "); Parser.Settings.Indent(); bool first = true; for (int ndx = 0; ndx < m_list.Count; ++ndx) { VariableDeclaration vdecl = m_list[ndx]; if (vdecl != null) { if (!first) { sb.Append(','); Parser.Settings.NewLine(sb); } sb.Append(vdecl.ToCode()); first = false; } } Parser.Settings.Unindent(); return sb.ToString(); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); if (!Parser.Settings.MinifyCode || !HideFromOutput) { if (LeftHandFunctionExpression) { sb.Append('('); } if (format != ToCodeFormat.NoFunction) { sb.Append("function"); if (Identifier != null) { // we don't want to show the name for named function expressions where the // name is never referenced. Don't use IsReferenced because that will always // return true for function expressions. Since we really only want to know if // the field name is referenced, check the refcount directly on the field. // also output the function expression name if this is debug mode if (FunctionType != FunctionType.Expression || !(Parser.Settings.RemoveFunctionExpressionNames && Parser.Settings.IsModificationAllowed(TreeModifications.RemoveFunctionExpressionNames)) || m_variableField.RefCount > 0) { sb.Append(' '); sb.Append(Identifier.ToCode()); } } } if (m_parameterDeclarations != null) { sb.Append('('); if (m_parameterDeclarations.Length > 0) { // figure out the last referenced argument so we can skip // any that aren't actually referenced int lastRef = m_parameterDeclarations.Length - 1; // if we're not known at compile time, then we can't leave off unreferenced parameters // (also don't leave things off if we're not hypercrunching) // (also check the kill flag for removing unused parameters) if (Parser.Settings.RemoveUnneededCode && m_functionScope.IsKnownAtCompileTime && Parser.Settings.MinifyCode && Parser.Settings.IsModificationAllowed(TreeModifications.RemoveUnusedParameters)) { while (lastRef >= 0) { // we want to loop backwards until we either find a parameter that is referenced. // at that point, lastRef will be the index of the last referenced parameter so // we can output from 0 to lastRef JSArgumentField argumentField = m_parameterDeclarations[lastRef].Field; if (argumentField != null && !argumentField.IsReferenced) { --lastRef; } else { // found a referenced parameter, or something weird -- stop looking break; } } } for (int ndx = 0; ndx <= lastRef; ++ndx) { if (ndx > 0) { sb.Append(','); } sb.Append(m_parameterDeclarations[ndx].Name); } } sb.Append(')'); } if (Body != null) { if (Body.Count == 0) { sb.Append("{}"); } else { sb.Append(Body.ToCode(ToCodeFormat.AlwaysBraces)); } } if (LeftHandFunctionExpression) { sb.Append(')'); } } return sb.ToString(); }
public override string ToCode(ToCodeFormat format) { return "@cc_on"; }
//code in parser relies on the member string (x.y.z...) being returned from here public override string ToCode(ToCodeFormat format) { // pass P to the root object so it knows we might want parentheses string rootCrunched = Root.ToCode(); // these tests are for items that DON'T need parens, and then we NOT the results. // non-numeric constant wrappers don't need parens (boolean, string, null). // numeric constant wrappers need parens IF there is no decimal point. // function expressions will take care of their own parens. bool needParen = !( (Root is Lookup) || (Root is Member) || (Root is CallNode) || (Root is ThisLiteral) || (Root is ArrayLiteral) || (Root is ObjectLiteral) || (Root is RegExpLiteral) || (Root is FunctionObject) || (Root is ConstantWrapper && !((ConstantWrapper)Root).IsNumericLiteral) || (Root is ConstantWrapper && ((ConstantWrapper)Root).IsNumericLiteral && rootCrunched.Contains(".")) ); // 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 (!needParen) { CallNode callNode = Root as CallNode; if (callNode != null && callNode.IsConstructor && (callNode.Arguments == null || callNode.Arguments.Count == 0)) { needParen = true; } } StringBuilder sb = new StringBuilder(); if (needParen) { sb.Append('('); sb.Append(rootCrunched); sb.Append(')'); } else { sb.Append(rootCrunched); } sb.Append('.'); sb.Append(Name); return sb.ToString(); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); sb.Append("with("); sb.Append(m_withObject.ToCode()); sb.Append(")"); if (Body != null) { sb.Append(Body.ToCode()); } return sb.ToString(); }
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(); }
public override string ToCode(ToCodeFormat format) { StringBuilder sb = new StringBuilder(); // passing a "T" format means nested try's don't actually nest -- they // just add the catch clauses to the end if (format != ToCodeFormat.NestedTry) { sb.Append("try"); if (TryBlock == null) { // empty body sb.Append("{}"); } else { sb.Append(TryBlock.ToCode(ToCodeFormat.NestedTry)); } } else { sb.Append(TryBlock.ToCode(ToCodeFormat.NestedTry)); } // handle the catch clause (if any) // catch should always have braces around it string catchString = ( CatchBlock == null ? string.Empty : CatchBlock.Count == 0 ? "{}" : CatchBlock.ToCode(ToCodeFormat.AlwaysBraces) ); if (catchString.Length > 0) { Parser.Settings.NewLine(sb); sb.Append("catch("); if (m_catchVariable != null) { sb.Append(m_catchVariable.ToString()); } else if (CatchVarName != null) { sb.Append(CatchVarName); } sb.Append(')'); sb.Append(catchString); } // handle the finally, if any // finally should always have braces around it string finallyString = ( FinallyBlock == null ? string.Empty : FinallyBlock.ToCode(ToCodeFormat.AlwaysBraces) ); if (finallyString.Length > 0) { Parser.Settings.NewLine(sb); sb.Append("finally"); sb.Append(finallyString); } return sb.ToString(); }