• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java Parser类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.mozilla.javascript.Parser的典型用法代码示例。如果您正苦于以下问题:Java Parser类的具体用法?Java Parser怎么用?Java Parser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



Parser类属于org.mozilla.javascript包,在下文中一共展示了Parser类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: parse

import org.mozilla.javascript.Parser; //导入依赖的package包/类
private AstRoot parse(String string, boolean jsdoc) {
    CompilerEnvirons environment = new CompilerEnvirons();

    TestErrorReporter testErrorReporter = new TestErrorReporter(null, null);
    environment.setErrorReporter(testErrorReporter);

    environment.setRecordingComments(true);
    environment.setRecordingLocalJsDocComments(jsdoc);

    Parser p = new Parser(environment, testErrorReporter);
    AstRoot script = p.parse(string, null, 0);

    assertTrue(testErrorReporter.hasEncounteredAllErrors());
    assertTrue(testErrorReporter.hasEncounteredAllWarnings());

    return script;
}
 
开发者ID:middle2tw,项目名称:whackpad,代码行数:18,代码来源:ParserTest.java


示例2: parseAsReader

import org.mozilla.javascript.Parser; //导入依赖的package包/类
private AstRoot parseAsReader(String string) throws IOException {
    CompilerEnvirons environment = new CompilerEnvirons();

    TestErrorReporter testErrorReporter = new TestErrorReporter(null, null);
    environment.setErrorReporter(testErrorReporter);

    environment.setRecordingComments(true);
    environment.setRecordingLocalJsDocComments(true);

    Parser p = new Parser(environment, testErrorReporter);
    AstRoot script = p.parse(new StringReader(string), null, 0);

    assertTrue(testErrorReporter.hasEncounteredAllErrors());
    assertTrue(testErrorReporter.hasEncounteredAllWarnings());

    return script;
}
 
开发者ID:middle2tw,项目名称:whackpad,代码行数:18,代码来源:ParserTest.java


示例3: testNodeReplacementInWhileLoopWithBrackets

import org.mozilla.javascript.Parser; //导入依赖的package包/类
@Test
public void testNodeReplacementInWhileLoopWithBrackets() throws IOException {
    String script = "var o = {\n" +
            "  _x: 123, \n" +
            "  get x() {\n" +
            "    return this._x;\n" +
            "  }\n" +
            ", \n" +
            "  set x(value) {\n" +
            "    this._x = value;\n" +
            "  }\n" +
            "};\n";

    Parser parser = new Parser(environment);
    AstRoot astRoot = parser.parse(new StringReader(script), null, 1);
    assertEquals(script, astRoot.toSource());
}
 
开发者ID:F43nd1r,项目名称:rhino-android,代码行数:18,代码来源:BugGetterSetterTest.java


示例4: compile

import org.mozilla.javascript.Parser; //导入依赖的package包/类
/**
 * Compiles {@code source} and returns the transformed and optimized
 * {@link ScriptNode}
 */
protected ScriptNode compile(CharSequence source) {
    final String mainMethodClassName = "Main";
    final String scriptClassName = "Main";

    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    Parser p = new Parser(compilerEnv);
    AstRoot ast = p.parse(source.toString(), "<eval>", 1);
    IRFactory irf = new IRFactory(compilerEnv);
    ScriptNode tree = irf.transformTree(ast);

    Codegen codegen = new Codegen();
    codegen.setMainMethodClass(mainMethodClassName);
    codegen.compileToClassFile(compilerEnv, scriptClassName, tree, tree.getEncodedSource(),
            false);

    return tree;
}
 
开发者ID:F43nd1r,项目名称:rhino-android,代码行数:23,代码来源:Bug782363Test.java


示例5: compile

import org.mozilla.javascript.Parser; //导入依赖的package包/类
/**
 * Compiles {@code source} and returns the transformed and optimized
 * {@link ScriptNode}
 */
protected ScriptNode compile(CharSequence source) {
    final String mainMethodClassName = "Main";
    final String scriptClassName = "Main";

    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv
            .getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    AstRoot ast = p.parse(source.toString(), "<eval>", 1);
    IRFactory irf = new IRFactory(compilerEnv);
    ScriptNode tree = irf.transformTree(ast);

    Codegen codegen = new Codegen();
    codegen.setMainMethodClass(mainMethodClassName);
    codegen.compileToClassFile(compilerEnv, scriptClassName, tree,
            tree.getEncodedSource(), false);

    return tree;
}
 
开发者ID:F43nd1r,项目名称:rhino-android,代码行数:25,代码来源:Bug708801Test.java


示例6: parse

import org.mozilla.javascript.Parser; //导入依赖的package包/类
/**
 * @return a tree representation of the parsed JavaScript file
 * @throws IOException
 */
public ScriptOrFnNode parse() throws IOException {

  if (nodeTree == null) {
    Reader reader = new FileReader(jsFile);

    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    ErrorReporter errorReporter = compilerEnv.getErrorReporter();

    Parser parser = new Parser(compilerEnv, errorReporter);

    String sourceURI;

    try {
      sourceURI = jsFile.getCanonicalPath();
    } catch (IOException e) {
      sourceURI = jsFile.toString();
    }

    nodeTree = parser.parse(reader, sourceURI, 1);
  }
  return nodeTree;
}
 
开发者ID:mauyr,项目名称:openbravo-brazil,代码行数:27,代码来源:JavaScriptParser.java


示例7: explainErrors

import org.mozilla.javascript.Parser; //导入依赖的package包/类
private static String explainErrors(JSEnvironment env, String sourceCode) {
    AstRoot root = new Parser().parse(sourceCode, "", 1);
    SatSolver sat = new Sat4J();
    SJSTypeTheory theory = new SJSTypeTheory(env, null, root);
    List<Integer> hard = new ArrayList<>();
    List<Integer> soft = new ArrayList<>();
    List<ITypeConstraint> constraints = theory.getConstraints();
    for (int i = 0; i < constraints.size(); ++i) {
        (theory.hackyGenerator().hasExplanation(constraints.get(i)) ? soft : hard).add(i);
    }
    Pair<TypeAssignment, Collection<Integer>> result =
            TheorySolver.solve(
                theory, new SatFixingSetFinder<>(sat),
                hard, soft);
    ConstraintGenerator g = theory.hackyGenerator();
    StringBuilder buf = new StringBuilder();
    for (int broken : result.getRight()) {
        ITypeConstraint c = theory.hackyConstraintAccess().get(broken);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        g.explainFailure(c, result.getLeft()).prettyprint(new PrintStream(stream));
        buf.append(stream.toString());
    }
    return buf.toString();
}
 
开发者ID:Samsung,项目名称:SJS,代码行数:25,代码来源:ExplanationsTest.java


示例8: scan

import org.mozilla.javascript.Parser; //导入依赖的package包/类
/**
 * 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();
  }
}
 
开发者ID:sonatype,项目名称:nexus-public,代码行数:28,代码来源:ClassDefScanner.java


示例9: parse

import org.mozilla.javascript.Parser; //导入依赖的package包/类
@Override
public Bundle parse(InputStream inStream) throws IOException {
    LinkedHashMap<String, String> resultMap = null;
    Bundle result = new Bundle();
    try (InputStreamReader reader = new InputStreamReader(new BomInputStream(inStream), "UTF-8")) {
        AstRoot root = new Parser().parse(reader, null, 1);
        KeyValueVisitor visitor = new KeyValueVisitor();
        root.visitAll(visitor);
        resultMap = visitor.elements;
        int sequenceNum = 1;
        for (Entry<String, String> entry : resultMap.entrySet()) {
            result.addResourceString(entry.getKey(), entry.getValue(), sequenceNum++);
        }
    } catch (Exception e) {
        throw new IllegalResourceFormatException(e);
    }
    return result;
}
 
开发者ID:IBM-Cloud,项目名称:gp-java-tools,代码行数:19,代码来源:AmdJsResource.java


示例10: parseFile

import org.mozilla.javascript.Parser; //导入依赖的package包/类
/**
 *
 * @param fileName
 * @param staticMethods
 * @param staticFields
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 */
Collection<JsFile> parseFile(final String fileName,
    final List<JsMethod> staticMethods,
    final Map<String, JsElement> staticFields)
        throws FileNotFoundException, IOException {
  try (final Reader reader = new FileReader(fileName)) {
    final CompilerEnvirons env = new CompilerEnvirons();
    env.setRecordingLocalJsDocComments(true);
    env.setAllowSharpComments(true);
    env.setRecordingComments(true);
    final AstRoot node = new Parser(env).parse(reader, fileName, 1);
    final JavaScriptFileParser parser = new JavaScriptFileParser(fileName);
    node.visitAll(parser);
    staticMethods.addAll(parser.getStaticMethods());
    staticFields.putAll(parser.getConsts());
    return parser.getFiles();
  }
}
 
开发者ID:gruifo,项目名称:gruifo,代码行数:27,代码来源:Controller.java


示例11: getReferencedScriptObject

import org.mozilla.javascript.Parser; //导入依赖的package包/类
/**
 * 
 * @param expr
 * @param objectName
 * @return
 */
private static String getReferencedScriptObject( String expr,
		String objectName )
{
	if ( expr == null )
		return null;
	try
	{
		Context cx = Context.enter( );
		CompilerEnvirons ce = new CompilerEnvirons( );
		Parser p = new Parser( ce, cx.getErrorReporter( ) );
		AstRoot tree = p.parse( expr, null, 0 );
		IRFactory ir = new IRFactory( ce );
		ScriptNode script = ir.transformTree( tree );
		return getScriptObjectName( script, objectName );
	}
	finally
	{
		Context.exit( );
	}
}
 
开发者ID:eclipse,项目名称:birt,代码行数:27,代码来源:CubeQueryUtil.java


示例12: getReferencedScriptObject

import org.mozilla.javascript.Parser; //导入依赖的package包/类
/**
 * 
 * @param expr
 * @param objectName
 * @return
 */
public static String getReferencedScriptObject( String expr,
		String objectName )
{
	if ( expr == null )
		return null;
	try
	{
		Context cx = Context.enter( );
		CompilerEnvirons ce = new CompilerEnvirons( );
		Parser p = new Parser( ce, cx.getErrorReporter( ) );
		AstRoot tree = p.parse( expr, null, 0 );
		Node root = new IRFactory( ce).transformTree(tree);

		return getScriptObjectName( root, objectName );
	}
	catch( Exception ex )
	{
		return null;
	}
	finally
	{
		Context.exit( );
	}
}
 
开发者ID:eclipse,项目名称:birt,代码行数:31,代码来源:OlapExpressionCompiler.java


示例13: parseScript

import org.mozilla.javascript.Parser; //导入依赖的package包/类
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
		}
	}
}
 
开发者ID:bobbylight,项目名称:RSTALanguageSupport,代码行数:18,代码来源:PreProcessingScripts.java


示例14: compileText

import org.mozilla.javascript.Parser; //导入依赖的package包/类
/**
 * 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;
}
 
开发者ID:bobbylight,项目名称:RSTALanguageSupport,代码行数:23,代码来源:JavaScriptCompletionResolver.java


示例15: resolveParamNode

import org.mozilla.javascript.Parser; //导入依赖的package包/类
/**
 * Resolve node type to TypeDeclaration. Called instead of #compileText(String text) when document is already parsed
 * @param node AstNode 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();
}
 
开发者ID:bobbylight,项目名称:RSTALanguageSupport,代码行数:25,代码来源:JavaScriptCompletionResolver.java


示例16: parse

import org.mozilla.javascript.Parser; //导入依赖的package包/类
private AstRoot parse(CharSequence cs) {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    return p.parse(cs.toString(), "<eval>", 1);
}
 
开发者ID:F43nd1r,项目名称:rhino-android,代码行数:8,代码来源:Bug689314Test.java


示例17: assertSource

import org.mozilla.javascript.Parser; //导入依赖的package包/类
/**
 * Asserts that the value returned by {@link AstRoot#toSource()} after
 * the given input source was parsed equals the specified expected output source.
 *
 * @param source the JavaScript source to be parsed
 * @param expectedOutput the JavaScript source that is expected to be
 *                       returned by {@link AstRoot#toSource()}
 */
private void assertSource(String source, String expectedOutput)
{
    CompilerEnvirons env = new CompilerEnvirons();
    env.setLanguageVersion(Context.VERSION_1_7);
    Parser parser = new Parser(env);
    AstRoot root = parser.parse(source, null, 0);
    Assert.assertEquals(expectedOutput, root.toSource());
}
 
开发者ID:F43nd1r,项目名称:rhino-android,代码行数:17,代码来源:Bug491621Test.java


示例18: assertSource

import org.mozilla.javascript.Parser; //导入依赖的package包/类
private void assertSource(String source, String expectedOutput) {
  CompilerEnvirons env = new CompilerEnvirons();
  env.setLanguageVersion(Context.VERSION_ES6);
  Parser parser = new Parser(env);
  AstRoot root = parser.parse(source, null, 0);
  Assert.assertEquals(expectedOutput, root.toSource());
}
 
开发者ID:F43nd1r,项目名称:rhino-android,代码行数:8,代码来源:ToSourceTest.java


示例19: parse

import org.mozilla.javascript.Parser; //导入依赖的package包/类
private AstRoot parse(
    String string, final String [] errors, final String [] warnings,
    boolean jsdoc) {
    TestErrorReporter testErrorReporter =
        new TestErrorReporter(errors, warnings) {
      @Override
      public EvaluatorException runtimeError(
           String message, String sourceName, int line, String lineSource,
           int lineOffset) {
         if (errors == null) {
           throw new UnsupportedOperationException();
         }
         return new EvaluatorException(
           message, sourceName, line, lineSource, lineOffset);
       }
    };
    environment.setErrorReporter(testErrorReporter);

    environment.setRecordingComments(true);
    environment.setRecordingLocalJsDocComments(jsdoc);

    Parser p = new Parser(environment, testErrorReporter);
    AstRoot script = null;
    try {
      script = p.parse(string, null, 0);
    } catch (EvaluatorException e) {
      if (errors == null) {
        // EvaluationExceptions should not occur when we aren't expecting
        // errors.
        throw e;
      }
    }

    assertTrue(testErrorReporter.hasEncounteredAllErrors());
    assertTrue(testErrorReporter.hasEncounteredAllWarnings());

    return script;
}
 
开发者ID:F43nd1r,项目名称:rhino-android,代码行数:39,代码来源:ParserTest.java


示例20: parseAsReader

import org.mozilla.javascript.Parser; //导入依赖的package包/类
private AstRoot parseAsReader(String string) throws IOException {
    TestErrorReporter testErrorReporter = new TestErrorReporter(null, null);
    environment.setErrorReporter(testErrorReporter);

    environment.setRecordingComments(true);
    environment.setRecordingLocalJsDocComments(true);

    Parser p = new Parser(environment, testErrorReporter);
    AstRoot script = p.parse(new StringReader(string), null, 0);

    assertTrue(testErrorReporter.hasEncounteredAllErrors());
    assertTrue(testErrorReporter.hasEncounteredAllWarnings());

    return script;
}
 
开发者ID:F43nd1r,项目名称:rhino-android,代码行数:16,代码来源:ParserTest.java



注:本文中的org.mozilla.javascript.Parser类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java NegotiationType类代码示例发布时间:2022-05-21
下一篇:
Java ICombinatoricsVector类代码示例发布时间:2022-05-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap