41 import jdk.nashorn.internal.runtime.JSType; 42 import jdk.nashorn.internal.runtime.ParserException; 43 import jdk.nashorn.internal.runtime.ScriptEnvironment; 44 import jdk.nashorn.internal.runtime.Source; 45 import jdk.nashorn.internal.runtime.options.Options; 46 47 final class ParserImpl implements Parser { 48 49 private final ScriptEnvironment env; 50 51 ParserImpl(final String... args) throws IllegalArgumentException { 52 Objects.requireNonNull(args); 53 Options options = new Options("nashorn"); 54 options.process(args); 55 this.env = new ScriptEnvironment(options, 56 new PrintWriter(System.out), new PrintWriter(System.err)); 57 } 58 59 @Override 60 public CompilationUnitTree parse(final File file, final DiagnosticListener listener) throws IOException, NashornException { 61 Objects.requireNonNull(file); 62 final Source src = Source.sourceFor(file.getName(), file); 63 return translate(makeParser(src, listener).parse()); 64 } 65 66 @Override 67 public CompilationUnitTree parse(final Path path, final DiagnosticListener listener) throws IOException, NashornException { 68 Objects.requireNonNull(path); 69 final Source src = Source.sourceFor(path.toString(), path); 70 return translate(makeParser(src, listener).parse()); 71 } 72 73 @Override 74 public CompilationUnitTree parse(final URL url, final DiagnosticListener listener) throws IOException, NashornException { 75 final Source src = Source.sourceFor(url.toString(), url); 76 return translate(makeParser(src, listener).parse()); 77 } 78 79 @Override 80 public CompilationUnitTree parse(final String name, final Reader reader, final DiagnosticListener listener) throws IOException, NashornException { 81 Objects.requireNonNull(name); 82 Objects.requireNonNull(reader); 83 final Source src = Source.sourceFor(name, reader); 84 return translate(makeParser(src, listener).parse()); 85 } 86 87 @Override 88 public CompilationUnitTree parse(final String name, final String code, final DiagnosticListener listener) throws NashornException { 89 final Source src = Source.sourceFor(name, code); 90 return translate(makeParser(src, listener).parse()); 91 } 92 93 @Override 94 public CompilationUnitTree parse(final ScriptObjectMirror scriptObj, final DiagnosticListener listener) throws NashornException { 95 Objects.requireNonNull(scriptObj); 96 final Map<?,?> map = scriptObj; 97 if (map.containsKey("script") && map.containsKey("name")) { 98 final String script = JSType.toString(map.get("script")); 99 final String name = JSType.toString(map.get("name")); 100 final Source src = Source.sourceFor(name, script); 101 return translate(makeParser(src, listener).parse()); 102 } else { 103 throw new IllegalArgumentException("can't find 'script' and 'name' properties"); 104 } 105 } 106 107 private jdk.nashorn.internal.parser.Parser makeParser(final Source source, final DiagnosticListener listener) { 108 final ErrorManager errMgr = listener != null? new ListenerErrorManager(listener) : new Context.ThrowErrorManager(); 109 return new jdk.nashorn.internal.parser.Parser(env, source, errMgr); 110 } 111 112 private static class ListenerErrorManager extends ErrorManager { 113 private final DiagnosticListener listener; 114 115 ListenerErrorManager(final DiagnosticListener listener) { 116 // null check | 41 import jdk.nashorn.internal.runtime.JSType; 42 import jdk.nashorn.internal.runtime.ParserException; 43 import jdk.nashorn.internal.runtime.ScriptEnvironment; 44 import jdk.nashorn.internal.runtime.Source; 45 import jdk.nashorn.internal.runtime.options.Options; 46 47 final class ParserImpl implements Parser { 48 49 private final ScriptEnvironment env; 50 51 ParserImpl(final String... args) throws IllegalArgumentException { 52 Objects.requireNonNull(args); 53 Options options = new Options("nashorn"); 54 options.process(args); 55 this.env = new ScriptEnvironment(options, 56 new PrintWriter(System.out), new PrintWriter(System.err)); 57 } 58 59 @Override 60 public CompilationUnitTree parse(final File file, final DiagnosticListener listener) throws IOException, NashornException { 61 final Source src = Source.sourceFor(Objects.requireNonNull(file).getName(), file); 62 return translate(makeParser(src, listener).parse()); 63 } 64 65 @Override 66 public CompilationUnitTree parse(final Path path, final DiagnosticListener listener) throws IOException, NashornException { 67 final Source src = Source.sourceFor(Objects.requireNonNull(path).toString(), path); 68 return translate(makeParser(src, listener).parse()); 69 } 70 71 @Override 72 public CompilationUnitTree parse(final URL url, final DiagnosticListener listener) throws IOException, NashornException { 73 final Source src = Source.sourceFor(url.toString(), url); 74 return translate(makeParser(src, listener).parse()); 75 } 76 77 @Override 78 public CompilationUnitTree parse(final String name, final Reader reader, final DiagnosticListener listener) throws IOException, NashornException { 79 final Source src = Source.sourceFor(Objects.requireNonNull(name), Objects.requireNonNull(reader)); 80 return translate(makeParser(src, listener).parse()); 81 } 82 83 @Override 84 public CompilationUnitTree parse(final String name, final String code, final DiagnosticListener listener) throws NashornException { 85 final Source src = Source.sourceFor(name, code); 86 return translate(makeParser(src, listener).parse()); 87 } 88 89 @Override 90 public CompilationUnitTree parse(final ScriptObjectMirror scriptObj, final DiagnosticListener listener) throws NashornException { 91 final Map<?,?> map = Objects.requireNonNull(scriptObj); 92 if (map.containsKey("script") && map.containsKey("name")) { 93 final String script = JSType.toString(map.get("script")); 94 final String name = JSType.toString(map.get("name")); 95 final Source src = Source.sourceFor(name, script); 96 return translate(makeParser(src, listener).parse()); 97 } else { 98 throw new IllegalArgumentException("can't find 'script' and 'name' properties"); 99 } 100 } 101 102 private jdk.nashorn.internal.parser.Parser makeParser(final Source source, final DiagnosticListener listener) { 103 final ErrorManager errMgr = listener != null? new ListenerErrorManager(listener) : new Context.ThrowErrorManager(); 104 return new jdk.nashorn.internal.parser.Parser(env, source, errMgr); 105 } 106 107 private static class ListenerErrorManager extends ErrorManager { 108 private final DiagnosticListener listener; 109 110 ListenerErrorManager(final DiagnosticListener listener) { 111 // null check |