nashorn/test/script/nosecurity/parserapi.js
author chegar
Wed, 10 Feb 2016 22:58:27 +0000
changeset 35743 80ba74046cf1
parent 34974 94a13629c390
child 39662 e2b36a3779b9
permissions -rw-r--r--
8148861: Update jaxws to use the new non-inheriting thread-local Thread constructor Reviewed-by: mkos, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     1
/*
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     4
 * 
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     8
 * 
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    13
 * accompanied this code).
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    14
 * 
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    18
 * 
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    21
 * questions.
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    22
 */
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    23
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    24
/**
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    25
 * Nashorn parser API usage.
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    26
 *
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    27
 * @test
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    28
 * @option -scripting
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    29
 * @run
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    30
 */
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    31
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    32
function Parser() {
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    33
    // create nashorn parser
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    34
    this._parser = Parser.create();
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    35
}
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    36
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    37
// Java types used
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    38
Parser.Diagnostic = Java.type("jdk.nashorn.api.tree.Diagnostic");
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    39
Parser.SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1");
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    40
Parser.Tree = Java.type("jdk.nashorn.api.tree.Tree");
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    41
Parser.List = Java.type("java.util.List");
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    42
Parser.Enum = Java.type("java.lang.Enum");
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    43
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    44
// function to parse the script and return script friendly object
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    45
Parser.prototype.parse = function(name, script, listener) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    46
    var tree = this._parser.parse(name, script, listener);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    47
    tree.accept(new Parser.SimpleTreeVisitor(), null);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    48
    return this.convert(tree);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    49
}
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    50
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    51
Parser.create = function() {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    52
    return Java.type("jdk.nashorn.api.tree.Parser").create();
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    53
}
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    54
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    55
// convert Nashorn parser Tree, Diagnostic as a script friendly object
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    56
Parser.prototype.convert = function(tree) {
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    57
    if (!tree || typeof tree != 'object' || tree instanceof java.lang.Long) {
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    58
        return tree;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    59
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    60
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    61
    var obj = Object.bindProperties({}, tree);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    62
    var result = {};
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    63
    for (var i in obj) {
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    64
        var val = obj[i];
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    65
        if (val instanceof Parser.Tree) {
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    66
            result[i] = this.convert(val);
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    67
        } else if (val instanceof Parser.List) {
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    68
            var arr = new Array(val.size());
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    69
            for (var j in val) {
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    70
                arr[j] = this.convert(val[j]);
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    71
            }
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    72
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    73
            result[i] = arr;
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    74
        } else {
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    75
            switch (typeof val) {
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    76
                case 'number':
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    77
                case 'string':
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    78
                case 'boolean':
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    79
                    result[i] = String(val);
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    80
                    break;
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    81
                default:
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    82
                    if (val instanceof java.lang.Long || val instanceof Parser.Enum) {
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    83
                        result[i] = String(val);
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    84
                    }
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    85
            }
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    86
        }
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    87
    }
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    88
    return result;
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    89
}
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    90
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    91
function processFiles(subdir) {
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    92
    var File = Java.type("java.io.File");
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    93
    var files = new File(__DIR__ + subdir).listFiles();
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    94
    java.util.Arrays.sort(files);
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    95
    for each (var file in files) {
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    96
        if (file.name.endsWith(".js")) {
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    97
            var script = readFully(file);
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    98
            var parser = new Parser();
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
    99
            var tree = parser.parse(subdir + "/" + file.name, script,
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
   100
                function(diagnostic) {
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
   101
                    print(JSON.stringify(parser.convert(diagnostic), null, 2).replace(/\\r/g, ''));
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
   102
                    print(",");
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
   103
                });
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   104
34974
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
   105
            if (tree != null) {
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
   106
                print(JSON.stringify(tree, null, 2));
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
   107
                print(",");
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
   108
            }
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
   109
        }
94a13629c390 8143896: java.lang.Long is implicitly converted to double
hannesw
parents: 29626
diff changeset
   110
    }
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   111
}
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   112
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   113
// parse files in parsertests directory
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   114
function main() {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   115
    print("[");
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   116
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   117
    processFiles("parsertests");
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   118
    processFiles("parsernegativetests");
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   119
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   120
    // parse this file first!
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   121
    var script = readFully(__FILE__);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   122
    var tree = new Parser().parse("parserapi.js", script, null);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   123
    print(JSON.stringify(tree, null, 2));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   124
    print("]");
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   125
}
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   126
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   127
main();