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

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

本文整理了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

public static ScriptNode parseVariables( Context cx, Scriptable scope, String source, String sourceName,
  int lineno, Object securityDomain ) {
  // Interpreter compiler = new Interpreter();
  CompilerEnvirons evn = new CompilerEnvirons();
  // evn.setLanguageVersion(Context.VERSION_1_5);
  evn.setOptimizationLevel( -1 );
  evn.setGeneratingSource( true );
  evn.setGenerateDebugInfo( true );
  ErrorReporter errorReporter = new ToolErrorReporter( false );
  Parser p = new Parser( evn, errorReporter );
  ScriptNode tree = p.parse( source, "", 0 ); // IOException
  new NodeTransformer().transform( tree );
  // Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null);
  return tree;
 }
}

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

public static ScriptNode parseVariables( Context cx, Scriptable scope, String source, String sourceName,
  int lineno, Object securityDomain ) {
  // Interpreter compiler = new Interpreter();
  CompilerEnvirons evn = new CompilerEnvirons();
  // evn.setLanguageVersion(Context.VERSION_1_5);
  evn.setOptimizationLevel( -1 );
  evn.setGeneratingSource( true );
  evn.setGenerateDebugInfo( true );
  ErrorReporter errorReporter = new ToolErrorReporter( false );
  Parser p = new Parser( evn, errorReporter );
  ScriptNode tree = p.parse( source, "", 0 ); // IOException
  new NodeTransformer().transform( tree );
  // Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null);
  return tree;
 }
}

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

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

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

public ScriptOrFnNode parse(Reader sourceReader,
              String sourceURI, int lineno)
  throws IOException
{
  this.sourceURI = sourceURI;
  this.ts = new TokenStream(this, sourceReader, null, lineno);
  return parse();
}

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

public ScriptOrFnNode parse(String sourceString,
              String sourceURI, int lineno)
{
  this.sourceURI = sourceURI;
  this.ts = new TokenStream(this, null, sourceString, lineno);
  try {
    return parse();
  } catch (IOException ex) {
    // Should never happen
    throw new IllegalStateException();
  }
}

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

public ScriptOrFnNode parse(Reader sourceReader,
              String sourceURI, int lineno)
  throws IOException
{
  this.sourceURI = sourceURI;
  this.ts = new TokenStream(this, sourceReader, null, lineno);
  return parse();
}

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

public ScriptOrFnNode parse(Reader sourceReader,
              String sourceURI, int lineno)
  throws IOException
{
  this.sourceURI = sourceURI;
  this.ts = new TokenStream(this, sourceReader, null, lineno);
  return parse();
}

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

public ScriptOrFnNode parse(String sourceString,
              String sourceURI, int lineno)
{
  this.sourceURI = sourceURI;
  this.ts = new TokenStream(this, null, sourceString, lineno);
  try {
    return parse();
  } catch (IOException ex) {
    // Should never happen
    throw new IllegalStateException();
  }
}

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

public ScriptOrFnNode parse(String sourceString,
              String sourceURI, int lineno)
{
  this.sourceURI = sourceURI;
  this.ts = new TokenStream(this, null, sourceString, lineno);
  try {
    return parse();
  } catch (IOException ex) {
    // Should never happen
    throw new IllegalStateException();
  }
}

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

/**
 * Builds a parse tree from the given sourcereader.
 * @see #parse(String,String,int)
 * @throws IOException if the {@link Reader} encounters an error
 */
public AstRoot parse(Reader sourceReader, String sourceURI, int lineno)
  throws IOException
{
  if (parseFinished) throw new IllegalStateException("parser reused");
  if (compilerEnv.isIdeMode()) {
    return parse(readFully(sourceReader), sourceURI, lineno);
  }
  try {
    this.sourceURI = sourceURI;
    ts = new TokenStream(this, sourceReader, null, lineno);
    return parse();
  } finally {
    parseFinished = true;
  }
}

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

/**
 * Builds a parse tree from the given sourcereader.
 * @see #parse(String,String,int)
 * @throws IOException if the {@link Reader} encounters an error
 */
public AstRoot parse(Reader sourceReader, String sourceURI, int lineno)
  throws IOException
{
  if (parseFinished) throw new IllegalStateException("parser reused");
  if (compilerEnv.isIdeMode()) {
    return parse(readFully(sourceReader), sourceURI, lineno);
  }
  try {
    this.sourceURI = sourceURI;
    ts = new TokenStream(this, sourceReader, null, lineno);
    return parse();
  } finally {
    parseFinished = true;
  }
}

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

/**
 * Builds a parse tree from the given sourcereader.
 * @see #parse(String,String,int)
 * @throws IOException if the {@link Reader} encounters an error
 */
public AstRoot parse(Reader sourceReader, String sourceURI, int lineno)
  throws IOException
{
  if (parseFinished) throw new IllegalStateException("parser reused");
  if (compilerEnv.isIdeMode()) {
    return parse(readFully(sourceReader), sourceURI, lineno);
  }
  try {
    this.sourceURI = sourceURI;
    ts = new TokenStream(this, sourceReader, null, lineno);
    return parse();
  } finally {
    parseFinished = true;
  }
}

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

/**
 * Builds a parse tree from the given sourcereader.
 * @see #parse(String,String,int)
 * @throws IOException if the {@link Reader} encounters an error
 */
public AstRoot parse(Reader sourceReader, String sourceURI, int lineno)
  throws IOException
{
  if (parseFinished) throw new IllegalStateException("parser reused");
  if (compilerEnv.isIdeMode()) {
    return parse(readFully(sourceReader), sourceURI, lineno);
  }
  try {
    this.sourceURI = sourceURI;
    ts = new TokenStream(this, sourceReader, null, lineno);
    return parse();
  } finally {
    parseFinished = true;
  }
}

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

/**
 * Builds a parse tree from the given source string.
 *
 * @return an {@link AstRoot} object representing the parsed program.  If
 * the parse fails, {@code null} will be returned.  (The parse failure will
 * result in a call to the {@link ErrorReporter} from
 * {@link CompilerEnvirons}.)
 */
public AstRoot parse(String sourceString, String sourceURI, int lineno)
{
  if (parseFinished) throw new IllegalStateException("parser reused");
  this.sourceURI = sourceURI;
  if (compilerEnv.isIdeMode()) {
    this.sourceChars = sourceString.toCharArray();
  }
  this.ts = new TokenStream(this, null, sourceString, lineno);
  try {
    return parse();
  } catch (IOException iox) {
    // Should never happen
    throw new IllegalStateException();
  } finally {
    parseFinished = true;
  }
}

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

/**
 * Builds a parse tree from the given source string.
 *
 * @return an {@link AstRoot} object representing the parsed program.  If
 * the parse fails, {@code null} will be returned.  (The parse failure will
 * result in a call to the {@link ErrorReporter} from
 * {@link CompilerEnvirons}.)
 */
public AstRoot parse(String sourceString, String sourceURI, int lineno)
{
  if (parseFinished) throw new IllegalStateException("parser reused");
  this.sourceURI = sourceURI;
  if (compilerEnv.isIdeMode()) {
    this.sourceChars = sourceString.toCharArray();
  }
  this.ts = new TokenStream(this, null, sourceString, lineno);
  try {
    return parse();
  } catch (IOException iox) {
    // Should never happen
    throw new IllegalStateException();
  } finally {
    parseFinished = true;
  }
}

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

/**
 * Scan the given file for class definitions and accumulate dependencies.
 */
private void scan(final File source) throws IOException {
 log.debug("Scanning: " + source);
 ErrorReporter errorReporter = new LogErrorReporter(log);
 CompilerEnvirons env = new CompilerEnvirons();
 env.setErrorReporter(errorReporter);
 Parser parser = new Parser(env, errorReporter);
 Reader reader = new BufferedReader(new FileReader(source));
 try {
  AstRoot root = parser.parse(reader, source.getAbsolutePath(), 0);
  DependencyAccumulator visitor = new DependencyAccumulator(source);
  root.visit(visitor);
  // complain if no def was found in this source
  if (visitor.current == null) {
   log.warn("No class definition was found while processing: " + source);
  }
 }
 finally {
  reader.close();
 }
}

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

public void parseScript(String scriptText, TypeDeclarationOptions options)
{
  if(scriptText != null && scriptText.length() > 0)
  {
    CompilerEnvirons env = JavaScriptParser.createCompilerEnvironment(new JavaScriptParser.JSErrorReporter(), provider.getLanguageSupport());
    Parser parser = new Parser(env);
    StringReader r = new StringReader(scriptText);
    try {
      AstRoot root = parser.parse(r, null, 0);
      CodeBlock block = provider.iterateAstRoot(root, preProcessingCompletions, "", Integer.MAX_VALUE, options);
      provider.recursivelyAddLocalVars(preProcessingCompletions, block, 0, null, false, true);
    }
    catch(IOException io) {
      //ignore this
    }
  }
}

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

@Override
  public TreeContext generate(Reader r) throws IOException {
    CompilerEnvirons env = new CompilerEnvirons();
    env.setRecordingLocalJsDocComments(true);
    env.setAllowSharpComments(true);
    env.setRecordingComments(true);
    Parser p = new Parser(env);
    AstRoot root = p.parse(r, null, 1);
    RhinoTreeVisitor visitor = new RhinoTreeVisitor(root);
    root.visitAll(visitor);
    return visitor.getTree(root);
  }
}

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

/**
 * Compiles Text and resolves the type.
 * e.g 
 * "Hello World".length; //resolve as a Number
 * 
 * @param text to compile and resolve  
 */
@Override
public JavaScriptType compileText(String text) throws IOException {
  CompilerEnvirons env = JavaScriptParser.createCompilerEnvironment(new JavaScriptParser.JSErrorReporter(), provider.getLanguageSupport());
  
  String parseText = JavaScriptHelper.removeLastDotFromText(text);
  
  int charIndex = JavaScriptHelper.findIndexOfFirstOpeningBracket(parseText);
  env.setRecoverFromErrors(true);
  Parser parser = new Parser(env);
  StringReader r = new StringReader(parseText);
  AstRoot root = parser.parse(r, null, 0);
  CompilerNodeVisitor visitor = new CompilerNodeVisitor(charIndex == 0);
  root.visitAll(visitor);
  return lastJavaScriptType;
}

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

/**
 * Resolve node type to TypeDeclaration. Called instead of #compileText(String text) when document is already parsed
 * @param text The node to resolve
 * @return TypeDeclaration for node or null if not found.
 */
@Override
public TypeDeclaration resolveParamNode(String text) throws IOException {
  
  if(text != null) {
    CompilerEnvirons env = JavaScriptParser.createCompilerEnvironment(new JavaScriptParser.JSErrorReporter(), provider.getLanguageSupport());
    
    
    int charIndex = JavaScriptHelper.findIndexOfFirstOpeningBracket(text);
    env.setRecoverFromErrors(true);
    Parser parser = new Parser(env);
    StringReader r = new StringReader(text);
    AstRoot root = parser.parse(r, null, 0);
    CompilerNodeVisitor visitor = new CompilerNodeVisitor(charIndex == 0);
    root.visitAll(visitor);
  }
  
  return lastJavaScriptType != null ? lastJavaScriptType.getType()
      : provider.getTypesFactory().getDefaultTypeDeclaration();
}

相关文章

Parser类方法