1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 *
26 * @subtest
27 */
28
29var parser = Java.type('jdk.nashorn.api.tree.Parser');
30var tree = Java.type('jdk.nashorn.api.tree.Tree');
31var list = Java.type('java.util.List');
32var visitor = Java.type('jdk.nashorn.api.tree.SimpleTreeVisitorES5_1');
33var visitor_es6 = Java.type('jdk.nashorn.api.tree.SimpleTreeVisitorES6');
34var file = Java.type('java.io.File')
35var cls = Java.type('java.lang.Class')
36
37function convert (value) {
38    if (!value || typeof(value) != 'object') {
39        return value;
40    }
41    var  obj = Object.bindProperties({}, value)
42    var result = {}
43    for (var i in obj) {
44        if (i == "lineMap") {
45            continue;
46        }
47
48        var val = obj[i]
49        // skip these ES6 specific properties to reduce noise
50        // in the output - unless there were set to true
51        if (typeof(val) == 'boolean' && val == false) {
52            switch (i) {
53                case "computed":
54                case "static":
55                case "restParameter":
56                case "this":
57                case "super":
58                case "star":
59                case "default":
60                case "starDefaultStar":
61                case "arrow":
62                case "generator":
63                case "let":
64                case "const":
65                    continue;
66             }
67        }
68
69        if (typeof(val) == 'object') {
70            if (val instanceof cls) {
71                continue;
72            }
73            if (val instanceof tree) {
74                result[i] = convert(val)
75            }
76            else if (val instanceof list) {
77                var lst = []
78                for (var j in val) {
79                    lst.push(convert(val[j]))
80                }
81                result[i] = lst
82            }
83            else {
84                result[i] = String(val)
85            }
86        } else if (typeof(val) != 'function') {
87            result[i] = String(val)
88        }
89    }
90    return result
91}
92
93function parse(name, code, args, visitor, listener) {
94    var tree =  parser.create(args).parse(name, code, listener || null)
95    var results = []
96    tree.accept(visitor, results)
97    print(JSON.stringify(results, null, 2))
98}
99
100function parseModule(name, code) {
101    return parser.create("--es6-module").parse(name, code, null);
102}
103
104function parseDiagnostic (code, args) {
105    var messages = new Array()
106    var tree = parser.create(args).parse("test.js",  code, function (message) {
107        messages.push(convert(message))
108    })
109    print(JSON.stringify(messages, null, 2).replace(/\\r/g, ''))
110}
111