org.mozilla.javascript.Parser.parse()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 JavaScript  
字(11.0k)|赞(0)|评价(0)|浏览(337)

本文整理了Java中org.mozilla.javascript.Parser.parse()方法的一些代码示例,展示了Parser.parse()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Parser.parse()方法的具体详情如下:
包路径:org.mozilla.javascript.Parser
类名称:Parser
方法名:parse

Parser.parse介绍

[英]Builds a parse tree from the given sourcereader.
[中]从给定的sourcereader构建解析树。

代码示例

代码示例来源:origin: pentaho/pentaho-kettle

  1. public static ScriptNode parseVariables( Context cx, Scriptable scope, String source, String sourceName,
  2. int lineno, Object securityDomain ) {
  3. // Interpreter compiler = new Interpreter();
  4. CompilerEnvirons evn = new CompilerEnvirons();
  5. // evn.setLanguageVersion(Context.VERSION_1_5);
  6. evn.setOptimizationLevel( -1 );
  7. evn.setGeneratingSource( true );
  8. evn.setGenerateDebugInfo( true );
  9. ErrorReporter errorReporter = new ToolErrorReporter( false );
  10. Parser p = new Parser( evn, errorReporter );
  11. ScriptNode tree = p.parse( source, "", 0 ); // IOException
  12. new NodeTransformer().transform( tree );
  13. // Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null);
  14. return tree;
  15. }
  16. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. public static ScriptNode parseVariables( Context cx, Scriptable scope, String source, String sourceName,
  2. int lineno, Object securityDomain ) {
  3. // Interpreter compiler = new Interpreter();
  4. CompilerEnvirons evn = new CompilerEnvirons();
  5. // evn.setLanguageVersion(Context.VERSION_1_5);
  6. evn.setOptimizationLevel( -1 );
  7. evn.setGeneratingSource( true );
  8. evn.setGenerateDebugInfo( true );
  9. ErrorReporter errorReporter = new ToolErrorReporter( false );
  10. Parser p = new Parser( evn, errorReporter );
  11. ScriptNode tree = p.parse( source, "", 0 ); // IOException
  12. new NodeTransformer().transform( tree );
  13. // Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null);
  14. return tree;
  15. }
  16. }

代码示例来源:origin: pmd/pmd

  1. protected AstRoot parseEcmascript(final String sourceCode, final List<ParseProblem> parseProblems)
  2. throws ParseException {
  3. final CompilerEnvirons compilerEnvirons = new CompilerEnvirons();
  4. compilerEnvirons.setRecordingComments(parserOptions.isRecordingComments());
  5. compilerEnvirons.setRecordingLocalJsDocComments(parserOptions.isRecordingLocalJsDocComments());
  6. compilerEnvirons.setLanguageVersion(parserOptions.getRhinoLanguageVersion().getVersion());
  7. // Scope's don't appear to get set right without this
  8. compilerEnvirons.setIdeMode(true);
  9. compilerEnvirons.setWarnTrailingComma(true);
  10. // see bug #1150 "EmptyExpression" for valid statements!
  11. compilerEnvirons.setReservedKeywordAsIdentifier(true);
  12. // TODO We should do something with Rhino errors...
  13. final ErrorCollector errorCollector = new ErrorCollector();
  14. final Parser parser = new Parser(compilerEnvirons, errorCollector);
  15. // TODO Fix hardcode
  16. final String sourceURI = "unknown";
  17. final int beginLineno = 1;
  18. AstRoot astRoot = parser.parse(sourceCode, sourceURI, beginLineno);
  19. parseProblems.addAll(errorCollector.getErrors());
  20. return astRoot;
  21. }

代码示例来源:origin: rhino/js

  1. public ScriptOrFnNode parse(Reader sourceReader,
  2. String sourceURI, int lineno)
  3. throws IOException
  4. {
  5. this.sourceURI = sourceURI;
  6. this.ts = new TokenStream(this, sourceReader, null, lineno);
  7. return parse();
  8. }

代码示例来源:origin: com.yahoo/yuicompressor

  1. public ScriptOrFnNode parse(String sourceString,
  2. String sourceURI, int lineno)
  3. {
  4. this.sourceURI = sourceURI;
  5. this.ts = new TokenStream(this, null, sourceString, lineno);
  6. try {
  7. return parse();
  8. } catch (IOException ex) {
  9. // Should never happen
  10. throw new IllegalStateException();
  11. }
  12. }

代码示例来源:origin: com.sun.phobos/phobos-rhino

  1. public ScriptOrFnNode parse(Reader sourceReader,
  2. String sourceURI, int lineno)
  3. throws IOException
  4. {
  5. this.sourceURI = sourceURI;
  6. this.ts = new TokenStream(this, sourceReader, null, lineno);
  7. return parse();
  8. }

代码示例来源:origin: com.yahoo/yuicompressor

  1. public ScriptOrFnNode parse(Reader sourceReader,
  2. String sourceURI, int lineno)
  3. throws IOException
  4. {
  5. this.sourceURI = sourceURI;
  6. this.ts = new TokenStream(this, sourceReader, null, lineno);
  7. return parse();
  8. }

代码示例来源:origin: rhino/js

  1. public ScriptOrFnNode parse(String sourceString,
  2. String sourceURI, int lineno)
  3. {
  4. this.sourceURI = sourceURI;
  5. this.ts = new TokenStream(this, null, sourceString, lineno);
  6. try {
  7. return parse();
  8. } catch (IOException ex) {
  9. // Should never happen
  10. throw new IllegalStateException();
  11. }
  12. }

代码示例来源:origin: com.sun.phobos/phobos-rhino

  1. public ScriptOrFnNode parse(String sourceString,
  2. String sourceURI, int lineno)
  3. {
  4. this.sourceURI = sourceURI;
  5. this.ts = new TokenStream(this, null, sourceString, lineno);
  6. try {
  7. return parse();
  8. } catch (IOException ex) {
  9. // Should never happen
  10. throw new IllegalStateException();
  11. }
  12. }

代码示例来源:origin: ro.isdc.wro4j/rhino

  1. /**
  2. * Builds a parse tree from the given sourcereader.
  3. * @see #parse(String,String,int)
  4. * @throws IOException if the {@link Reader} encounters an error
  5. */
  6. public AstRoot parse(Reader sourceReader, String sourceURI, int lineno)
  7. throws IOException
  8. {
  9. if (parseFinished) throw new IllegalStateException("parser reused");
  10. if (compilerEnv.isIdeMode()) {
  11. return parse(readFully(sourceReader), sourceURI, lineno);
  12. }
  13. try {
  14. this.sourceURI = sourceURI;
  15. ts = new TokenStream(this, sourceReader, null, lineno);
  16. return parse();
  17. } finally {
  18. parseFinished = true;
  19. }
  20. }

代码示例来源:origin: geogebra/geogebra

  1. /**
  2. * Builds a parse tree from the given sourcereader.
  3. * @see #parse(String,String,int)
  4. * @throws IOException if the {@link Reader} encounters an error
  5. */
  6. public AstRoot parse(Reader sourceReader, String sourceURI, int lineno)
  7. throws IOException
  8. {
  9. if (parseFinished) throw new IllegalStateException("parser reused");
  10. if (compilerEnv.isIdeMode()) {
  11. return parse(readFully(sourceReader), sourceURI, lineno);
  12. }
  13. try {
  14. this.sourceURI = sourceURI;
  15. ts = new TokenStream(this, sourceReader, null, lineno);
  16. return parse();
  17. } finally {
  18. parseFinished = true;
  19. }
  20. }

代码示例来源:origin: com.github.tntim96/rhino

  1. /**
  2. * Builds a parse tree from the given sourcereader.
  3. * @see #parse(String,String,int)
  4. * @throws IOException if the {@link Reader} encounters an error
  5. */
  6. public AstRoot parse(Reader sourceReader, String sourceURI, int lineno)
  7. throws IOException
  8. {
  9. if (parseFinished) throw new IllegalStateException("parser reused");
  10. if (compilerEnv.isIdeMode()) {
  11. return parse(readFully(sourceReader), sourceURI, lineno);
  12. }
  13. try {
  14. this.sourceURI = sourceURI;
  15. ts = new TokenStream(this, sourceReader, null, lineno);
  16. return parse();
  17. } finally {
  18. parseFinished = true;
  19. }
  20. }

代码示例来源:origin: io.apigee/rhino

  1. /**
  2. * Builds a parse tree from the given sourcereader.
  3. * @see #parse(String,String,int)
  4. * @throws IOException if the {@link Reader} encounters an error
  5. */
  6. public AstRoot parse(Reader sourceReader, String sourceURI, int lineno)
  7. throws IOException
  8. {
  9. if (parseFinished) throw new IllegalStateException("parser reused");
  10. if (compilerEnv.isIdeMode()) {
  11. return parse(readFully(sourceReader), sourceURI, lineno);
  12. }
  13. try {
  14. this.sourceURI = sourceURI;
  15. ts = new TokenStream(this, sourceReader, null, lineno);
  16. return parse();
  17. } finally {
  18. parseFinished = true;
  19. }
  20. }

代码示例来源:origin: io.apigee/rhino

  1. /**
  2. * Builds a parse tree from the given source string.
  3. *
  4. * @return an {@link AstRoot} object representing the parsed program. If
  5. * the parse fails, {@code null} will be returned. (The parse failure will
  6. * result in a call to the {@link ErrorReporter} from
  7. * {@link CompilerEnvirons}.)
  8. */
  9. public AstRoot parse(String sourceString, String sourceURI, int lineno)
  10. {
  11. if (parseFinished) throw new IllegalStateException("parser reused");
  12. this.sourceURI = sourceURI;
  13. if (compilerEnv.isIdeMode()) {
  14. this.sourceChars = sourceString.toCharArray();
  15. }
  16. this.ts = new TokenStream(this, null, sourceString, lineno);
  17. try {
  18. return parse();
  19. } catch (IOException iox) {
  20. // Should never happen
  21. throw new IllegalStateException();
  22. } finally {
  23. parseFinished = true;
  24. }
  25. }

代码示例来源:origin: geogebra/geogebra

  1. /**
  2. * Builds a parse tree from the given source string.
  3. *
  4. * @return an {@link AstRoot} object representing the parsed program. If
  5. * the parse fails, {@code null} will be returned. (The parse failure will
  6. * result in a call to the {@link ErrorReporter} from
  7. * {@link CompilerEnvirons}.)
  8. */
  9. public AstRoot parse(String sourceString, String sourceURI, int lineno)
  10. {
  11. if (parseFinished) throw new IllegalStateException("parser reused");
  12. this.sourceURI = sourceURI;
  13. if (compilerEnv.isIdeMode()) {
  14. this.sourceChars = sourceString.toCharArray();
  15. }
  16. this.ts = new TokenStream(this, null, sourceString, lineno);
  17. try {
  18. return parse();
  19. } catch (IOException iox) {
  20. // Should never happen
  21. throw new IllegalStateException();
  22. } finally {
  23. parseFinished = true;
  24. }
  25. }

代码示例来源:origin: sonatype/nexus-public

  1. /**
  2. * Scan the given file for class definitions and accumulate dependencies.
  3. */
  4. private void scan(final File source) throws IOException {
  5. log.debug("Scanning: " + source);
  6. ErrorReporter errorReporter = new LogErrorReporter(log);
  7. CompilerEnvirons env = new CompilerEnvirons();
  8. env.setErrorReporter(errorReporter);
  9. Parser parser = new Parser(env, errorReporter);
  10. Reader reader = new BufferedReader(new FileReader(source));
  11. try {
  12. AstRoot root = parser.parse(reader, source.getAbsolutePath(), 0);
  13. DependencyAccumulator visitor = new DependencyAccumulator(source);
  14. root.visit(visitor);
  15. // complain if no def was found in this source
  16. if (visitor.current == null) {
  17. log.warn("No class definition was found while processing: " + source);
  18. }
  19. }
  20. finally {
  21. reader.close();
  22. }
  23. }

代码示例来源:origin: com.fifesoft/languagesupport

  1. public void parseScript(String scriptText, TypeDeclarationOptions options)
  2. {
  3. if(scriptText != null && scriptText.length() > 0)
  4. {
  5. CompilerEnvirons env = JavaScriptParser.createCompilerEnvironment(new JavaScriptParser.JSErrorReporter(), provider.getLanguageSupport());
  6. Parser parser = new Parser(env);
  7. StringReader r = new StringReader(scriptText);
  8. try {
  9. AstRoot root = parser.parse(r, null, 0);
  10. CodeBlock block = provider.iterateAstRoot(root, preProcessingCompletions, "", Integer.MAX_VALUE, options);
  11. provider.recursivelyAddLocalVars(preProcessingCompletions, block, 0, null, false, true);
  12. }
  13. catch(IOException io) {
  14. //ignore this
  15. }
  16. }
  17. }

代码示例来源:origin: GumTreeDiff/gumtree

  1. @Override
  2. public TreeContext generate(Reader r) throws IOException {
  3. CompilerEnvirons env = new CompilerEnvirons();
  4. env.setRecordingLocalJsDocComments(true);
  5. env.setAllowSharpComments(true);
  6. env.setRecordingComments(true);
  7. Parser p = new Parser(env);
  8. AstRoot root = p.parse(r, null, 1);
  9. RhinoTreeVisitor visitor = new RhinoTreeVisitor(root);
  10. root.visitAll(visitor);
  11. return visitor.getTree(root);
  12. }
  13. }

代码示例来源:origin: com.fifesoft/languagesupport

  1. /**
  2. * Compiles Text and resolves the type.
  3. * e.g
  4. * "Hello World".length; //resolve as a Number
  5. *
  6. * @param text to compile and resolve
  7. */
  8. @Override
  9. public JavaScriptType compileText(String text) throws IOException {
  10. CompilerEnvirons env = JavaScriptParser.createCompilerEnvironment(new JavaScriptParser.JSErrorReporter(), provider.getLanguageSupport());
  11. String parseText = JavaScriptHelper.removeLastDotFromText(text);
  12. int charIndex = JavaScriptHelper.findIndexOfFirstOpeningBracket(parseText);
  13. env.setRecoverFromErrors(true);
  14. Parser parser = new Parser(env);
  15. StringReader r = new StringReader(parseText);
  16. AstRoot root = parser.parse(r, null, 0);
  17. CompilerNodeVisitor visitor = new CompilerNodeVisitor(charIndex == 0);
  18. root.visitAll(visitor);
  19. return lastJavaScriptType;
  20. }

代码示例来源:origin: com.fifesoft/languagesupport

  1. /**
  2. * Resolve node type to TypeDeclaration. Called instead of #compileText(String text) when document is already parsed
  3. * @param text The node to resolve
  4. * @return TypeDeclaration for node or null if not found.
  5. */
  6. @Override
  7. public TypeDeclaration resolveParamNode(String text) throws IOException {
  8. if(text != null) {
  9. CompilerEnvirons env = JavaScriptParser.createCompilerEnvironment(new JavaScriptParser.JSErrorReporter(), provider.getLanguageSupport());
  10. int charIndex = JavaScriptHelper.findIndexOfFirstOpeningBracket(text);
  11. env.setRecoverFromErrors(true);
  12. Parser parser = new Parser(env);
  13. StringReader r = new StringReader(text);
  14. AstRoot root = parser.parse(r, null, 0);
  15. CompilerNodeVisitor visitor = new CompilerNodeVisitor(charIndex == 0);
  16. root.visitAll(visitor);
  17. }
  18. return lastJavaScriptType != null ? lastJavaScriptType.getType()
  19. : provider.getTypesFactory().getDefaultTypeDeclaration();
  20. }

相关文章

Parser类方法