nashorn/samples/prettyprinter.js
author xuelei
Thu, 10 Nov 2016 15:46:40 +0000
changeset 43212 5aa719c66677
parent 39662 e2b36a3779b9
permissions -rw-r--r--
8168728: DSA signing improvments Reviewed-by: valeriep, vinnie, ahgross, asmotrak, robm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29541
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
     1
/*
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
     3
 *
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
     6
 * are met:
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
     7
 *
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    10
 *
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    14
 *
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    18
 *
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    30
 */
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    31
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    32
/**
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    33
 * This script is a AST pretty printer for ECMAScript. It uses
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    34
 * Nashorn parser API to parser given script and uses tree visitor
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    35
 * to pretty print the AST to stdout as a script string.
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    36
 */
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    37
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    38
var File = Java.type("java.io.File");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    39
var file = arguments.length == 0? new File(__FILE__) : new File(arguments[0]);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    40
if (! file.isFile()) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    41
    print(arguments[0] + " is not a file");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    42
    exit(1);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    43
}
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    44
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    45
// Java classes used
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    46
var ArrayAccess = Java.type("jdk.nashorn.api.tree.ArrayAccessTree");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    47
var Block = Java.type("jdk.nashorn.api.tree.BlockTree");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    48
var FunctionDeclaration = Java.type("jdk.nashorn.api.tree.FunctionDeclarationTree");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    49
var FunctionExpression = Java.type("jdk.nashorn.api.tree.FunctionExpressionTree");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    50
var Identifier = Java.type("jdk.nashorn.api.tree.IdentifierTree");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    51
var Kind = Java.type("jdk.nashorn.api.tree.Tree.Kind");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    52
var MemberSelect = Java.type("jdk.nashorn.api.tree.MemberSelectTree");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    53
var ObjectLiteral = Java.type("jdk.nashorn.api.tree.ObjectLiteralTree");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    54
var Parser = Java.type("jdk.nashorn.api.tree.Parser");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    55
var SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    56
var System = Java.type("java.lang.System");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    57
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    58
// make a nashorn parser
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    59
var parser = Parser.create("-scripting", "--const-as-var");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    60
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    61
// symbols for nashorn operators
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    62
var operatorSymbols = {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    63
    POSTFIX_INCREMENT: "++", 
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    64
    POSTFIX_DECREMENT: "--",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    65
    PREFIX_INCREMENT: "++", 
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    66
    PREFIX_DECREMENT: "--",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    67
    UNARY_PLUS: "+",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    68
    UNARY_MINUS: "-",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    69
    BITWISE_COMPLEMENT: "~",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    70
    LOGICAL_COMPLEMENT: "!",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    71
    DELETE: "delete ",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    72
    TYPEOF: "typeof ",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    73
    VOID: "void ", 
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    74
    COMMA: ",",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    75
    MULTIPLY: "*", 
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    76
    DIVIDE: "/", 
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    77
    REMINDER: "%", 
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    78
    PLUS: "+",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    79
    MINUS: "-",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    80
    LEFT_SHIFT: "<<",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    81
    RIGHT_SHIFT: ">>",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    82
    UNSIGNED_RIGHT_SHIFT: ">>>",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    83
    LESS_THAN: "<",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    84
    GREATER_THAN: ">",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    85
    LESS_THAN_EQUAL: "<=",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    86
    GREATER_THAN_EQUAL: ">=", 
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    87
    IN: "in", 
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    88
    EQUAL_TO: "==",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    89
    NOT_EQUAL_TO: "!=",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    90
    STRICT_EQUAL_TO: "===",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    91
    STRICT_NOT_EQUAL_TO: "!==",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    92
    AND: "&",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    93
    XOR: "^",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    94
    OR: "|", 
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    95
    CONDITIONAL_AND: "&&", 
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    96
    CONDITIONAL_OR: "||",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    97
    MULTIPLY_ASSIGNMENT: "*=",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    98
    DIVIDE_ASSIGNMENT: "/=",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
    99
    REMINDER_ASSIGNMENT: "%=",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   100
    PLUS_ASSIGNMENT: "+=",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   101
    MINUS_ASSIGNMENT: "-=",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   102
    LEFT_SHIFT_ASSIGNMENT: "<<=",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   103
    RIGHT_SHIFT_ASSIGNMENT: ">>=",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   104
    UNSIGNED_RIGHT_SHIFT_ASSIGNMENT: ">>>=",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   105
    AND_ASSIGNMENT: "&=",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   106
    XOR_ASSIGNMENT: "^=",
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   107
    OR_ASSIGNMENT: "|="
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   108
};
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   109
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   110
function operatorOf(kind) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   111
     var name = kind.name();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   112
     if (name in operatorSymbols) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   113
         return operatorSymbols[name];
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   114
     }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   115
     throw "invalid operator: " + name;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   116
}
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   117
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   118
var gprint = print;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   119
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   120
function prettyPrint(file) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   121
    var ast = parser.parse(file, gprint);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   122
    if (!ast) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   123
        // failed to parse. don't print anything!
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   124
        return;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   125
    }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   126
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   127
    // AST visitor
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   128
    var visitor;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   129
    // current indent level
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   130
    var indentLevel = 0;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   131
    var out = System.out;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   132
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   133
    function print(obj) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   134
        out.print(String(obj));
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   135
    }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   136
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   137
    function println(obj) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   138
        obj?  out.println(String(obj)) : out.println();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   139
    }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   140
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   141
    // semicolon and end-of-line
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   142
    function eol() {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   143
        println(";");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   144
    }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   145
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   146
    // print indentation - 4 spaces per level
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   147
    function indent() {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   148
        for (var i = 0; i < indentLevel; i++) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   149
            // 4 spaces per indent level
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   150
            print("    ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   151
        }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   152
    }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   153
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   154
    // escape string literals
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   155
    function escapeString(str) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   156
        // FIXME: incomplete, revisit again!
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   157
        return str.replace(/[\\"']/g, '\\$&')
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   158
    }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   159
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   160
    // print a single statement (could be a block too)
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   161
    function printStatement(stat, extra, end) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   162
        if (stat instanceof Block) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   163
            println(" {");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   164
            printStatements(stat.statements, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   165
            indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   166
            print('}');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   167
            typeof end != "undefined"? print(end) : println();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   168
        } else {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   169
            println();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   170
            indentLevel++;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   171
            try {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   172
                stat.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   173
            } finally {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   174
                indentLevel--;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   175
            }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   176
        }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   177
    }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   178
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   179
    // print a statement list
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   180
    function printStatements(stats, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   181
        indentLevel++;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   182
        try {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   183
            for each (var stat in stats) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   184
                stat.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   185
            }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   186
        } finally {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   187
            indentLevel--;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   188
        }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   189
    }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   190
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   191
    // function arguments, array literal elements.
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   192
    function printCommaList(args, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   193
        var len = args.length;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   194
        for (var i = 0; i < len; i++) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   195
            args[i].accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   196
            if (i != len - 1) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   197
                print(", ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   198
            }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   199
        }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   200
    }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   201
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   202
    // print function declarations and expressions
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   203
    function printFunction(func, extra, end) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   204
        // extra lines around function declarations for clarity
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   205
        var funcDecl = (func instanceof FunctionDeclaration);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   206
        if (funcDecl) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   207
            println();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   208
            indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   209
        }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   210
        print("function ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   211
        if (func.name) {
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 29541
diff changeset
   212
            print(func.name.name);
29541
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   213
        }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   214
        printFunctionBody(func, extra, end);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   215
        if (funcDecl) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   216
            println();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   217
        }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   218
    }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   219
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   220
    // print function declaration/expression body
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   221
    function printFunctionBody(func, extra, end) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   222
        print('(');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   223
        var params = func.parameters;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   224
        if (params) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   225
            printCommaList(params);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   226
        }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   227
        print(')');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   228
        printStatement(func.body, extra, end);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   229
    }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   230
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   231
    // print object literal property
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   232
    function printProperty(node, extra, comma) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   233
        var key = node.key;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   234
        var val = node.value;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   235
        var getter = node.getter;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   236
        var setter = node.setter;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   237
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   238
        if (getter) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   239
            print("get ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   240
        } else if (setter) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   241
            print("set ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   242
        }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   243
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   244
        if (typeof key == "string") {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   245
            print(key);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   246
        } else {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   247
            key.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   248
        }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   249
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   250
        if (val) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   251
            print(": ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   252
            if (val instanceof FunctionExpression) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   253
                printFunction(val, extra, comma? ',' : undefined);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   254
            } else {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   255
                val.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   256
                if (comma) print(',');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   257
            }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   258
        } else if (getter) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   259
            printFunctionBody(getter, extra, comma? ',' : undefined);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   260
        } else if (setter) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   261
            printFunctionBody(setter, extra, comma? ',' : undefined);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   262
        }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   263
    }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   264
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   265
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   266
    ast.accept(visitor = new (Java.extend(SimpleTreeVisitor)) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   267
         visitAssignment: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   268
             node.variable.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   269
             print(" = ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   270
             node.expression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   271
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   272
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   273
         visitCompoundAssignment: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   274
             node.variable.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   275
             print(' ' + operatorOf(node.kind) + ' ');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   276
             node.expression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   277
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   278
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   279
         visitBinary: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   280
             node.leftOperand.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   281
             print(' ' + operatorOf(node.kind) + ' ');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   282
             node.rightOperand.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   283
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   284
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   285
         visitBlock: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   286
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   287
             println('{');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   288
             printStatements(node.statements, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   289
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   290
             println('}');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   291
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   292
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   293
         visitBreak: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   294
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   295
             print("break");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   296
             if (node.label) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   297
                 print(' ' + node.label);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   298
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   299
             eol();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   300
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   301
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   302
         visitCase: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   303
             var expr = node.expression;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   304
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   305
             if (expr) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   306
                 print("case ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   307
                 expr.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   308
                 println(':');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   309
             } else {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   310
                 println("default:");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   311
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   312
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   313
             printStatements(node.statements, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   314
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   315
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   316
         visitCatch: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   317
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   318
             print("catch (" + node.parameter.name);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   319
             var cond = node.condition;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   320
             if (cond) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   321
                 print(" if ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   322
                 cond.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   323
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   324
             print(')');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   325
             printStatement(node.block);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   326
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   327
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   328
         visitConditionalExpression: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   329
             print('(');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   330
             node.condition.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   331
             print(" ? ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   332
             node.trueExpression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   333
             print(" : ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   334
             node.falseExpression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   335
             print(')');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   336
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   337
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   338
         visitContinue: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   339
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   340
             print("continue");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   341
             if (node.label) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   342
                 print(' ' + node.label);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   343
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   344
             eol();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   345
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   346
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   347
         visitDebugger: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   348
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   349
             print("debugger");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   350
             eol();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   351
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   352
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   353
         visitDoWhileLoop: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   354
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   355
             print("do");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   356
             printStatement(node.statement, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   357
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   358
             print("while (");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   359
             node.condition.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   360
             print(')');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   361
             eol();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   362
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   363
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   364
         visitExpressionStatement: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   365
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   366
             var expr = node.expression;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   367
             var objLiteral = expr instanceof ObjectLiteral;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   368
             if (objLiteral) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   369
                 print('(');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   370
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   371
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   372
             expr.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   373
             if (objLiteral) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   374
                 print(')');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   375
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   376
             eol();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   377
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   378
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   379
         visitForLoop: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   380
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   381
             print("for (");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   382
             if (node.initializer) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   383
                node.initializer.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   384
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   385
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   386
             print(';');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   387
             if (node.condition) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   388
                node.condition.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   389
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   390
             print(';');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   391
             if (node.update) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   392
                node.update.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   393
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   394
             print(')');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   395
             printStatement(node.statement);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   396
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   397
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   398
         visitForInLoop: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   399
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   400
             print("for ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   401
             if (node.forEach) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   402
                 print("each ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   403
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   404
             print('(');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   405
             node.variable.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   406
             print(" in ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   407
             node.expression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   408
             print(')');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   409
             printStatement(node.statement);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   410
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   411
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   412
         visitFunctionCall: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   413
             var func = node.functionSelect;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   414
             // We need parens around function selected
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   415
             // in many non-simple cases. Eg. function
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   416
             // expression created and called immediately.
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   417
             // Such parens are not preserved in AST and so
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   418
             // introduce here.
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   419
             var simpleFunc =
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   420
                 (func instanceof ArrayAccess) ||
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   421
                 (func instanceof Identifier) ||
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   422
                 (func instanceof MemberSelect);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   423
             if (! simpleFunc) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   424
                 print('(');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   425
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   426
             func.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   427
             if (! simpleFunc) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   428
                 print(')');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   429
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   430
             print('(');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   431
             printCommaList(node.arguments, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   432
             print(')');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   433
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   434
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   435
         visitFunctionDeclaration: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   436
             printFunction(node, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   437
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   438
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   439
         visitFunctionExpression: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   440
             printFunction(node, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   441
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   442
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   443
         visitIdentifier: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   444
             print(node.name);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   445
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   446
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   447
         visitIf: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   448
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   449
             print("if (");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   450
             node.condition.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   451
             print(')');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   452
             printStatement(node.thenStatement);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   453
             var el = node.elseStatement;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   454
             if (el) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   455
                 indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   456
                 print("else");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   457
                 printStatement(el);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   458
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   459
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   460
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   461
         visitArrayAccess: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   462
             node.expression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   463
             print('[');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   464
             node.index.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   465
             print(']');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   466
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   467
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   468
         visitArrayLiteral: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   469
             print('[');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   470
             printCommaList(node.elements);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   471
             print(']');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   472
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   473
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   474
         visitLabeledStatement: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   475
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   476
             print(node.label);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   477
             print(':');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   478
             printStatement(node.statement);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   479
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   480
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   481
         visitLiteral: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   482
             var val = node.value;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   483
             if (typeof val == "string") {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   484
                 print("'" + escapeString(val) + "'");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   485
             } else {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   486
                 print(val);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   487
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   488
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   489
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   490
         visitParenthesized: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   491
             print('(');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   492
             node.expression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   493
             print(')');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   494
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   495
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   496
         visitReturn: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   497
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   498
             print("return");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   499
             if (node.expression) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   500
                 print(' ');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   501
                 node.expression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   502
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   503
             eol();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   504
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   505
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   506
         visitMemberSelect: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   507
             node.expression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   508
             print('.' + node.identifier);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   509
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   510
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   511
         visitNew: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   512
             print("new ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   513
             node.constructorExpression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   514
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   515
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   516
         visitObjectLiteral: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   517
             println('{');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   518
             indentLevel++;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   519
             try {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   520
                 var props = node.properties;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   521
                 var len = props.length;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   522
                 for (var p = 0; p < len; p++) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   523
                     var last = (p == len - 1);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   524
                     indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   525
                     printProperty(props[p], extra, !last);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   526
                     println();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   527
                 }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   528
             } finally {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   529
                 indentLevel--;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   530
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   531
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   532
             print('}');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   533
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   534
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   535
         visitRegExpLiteral: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   536
             print('/' + node.pattern + '/');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   537
             print(node.options);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   538
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   539
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   540
         visitEmptyStatement: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   541
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   542
             eol();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   543
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   544
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   545
         visitSwitch: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   546
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   547
             print("switch (");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   548
             node.expression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   549
             println(") {");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   550
             indentLevel++;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   551
             try {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   552
                 for each (var c in node.cases) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   553
                     c.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   554
                 }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   555
             } finally {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   556
                 indentLevel--;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   557
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   558
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   559
             println('}');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   560
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   561
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   562
         visitThrow: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   563
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   564
             print("throw ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   565
             node.expression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   566
             eol();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   567
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   568
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   569
         visitCompilationUnit: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   570
             for each (var stat in node.sourceElements) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   571
                 stat.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   572
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   573
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   574
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   575
         visitTry: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   576
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   577
             print("try");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   578
             printStatement(node.block);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   579
             var catches = node.catches;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   580
             for each (var c in catches) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   581
                 c.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   582
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   583
             var finallyBlock = node.finallyBlock;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   584
             if (finallyBlock) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   585
                 indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   586
                 print("finally");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   587
                 printStatement(finallyBlock);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   588
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   589
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   590
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   591
         visitInstanceOf: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   592
             node.expression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   593
             print(" instanceof ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   594
             node.type.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   595
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   596
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   597
         visitUnary: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   598
             var kind = node.kind;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   599
             var prefix = kind != Kind.POSTFIX_INCREMENT && kind != Kind.POSTFIX_DECREMENT;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   600
             if (prefix) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   601
                 print(operatorOf(kind));
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   602
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   603
             node.expression.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   604
             if (!prefix) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   605
                 print(operatorOf(kind));
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   606
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   607
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   608
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   609
         visitVariable: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   610
             indent();
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 29541
diff changeset
   611
             print("var " + node.binding.name);
29541
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   612
             var init = node.initializer;
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   613
             if (init) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   614
                 print(" = ");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   615
                 if (init instanceof FunctionExpression) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   616
                     printFunction(init, extra, "");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   617
                 } else {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   618
                     init.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   619
                 }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   620
             }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   621
             eol();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   622
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   623
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   624
         visitWhileLoop: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   625
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   626
             print("while (");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   627
             node.condition.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   628
             print(')');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   629
             printStatement(node.statement);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   630
         },
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   631
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   632
         visitWith: function(node, extra) {
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   633
             indent();
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   634
             print("with (");
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   635
             node.scope.accept(visitor, extra);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   636
             print(')');
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   637
             printStatement(node.statement);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   638
         }
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   639
    }, null);
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   640
}
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   641
efc10427bddc 8006996: Add a pretty printer that prints script source in nice form
sundar
parents:
diff changeset
   642
prettyPrint(file);