本文整理汇总了Java中org.mozilla.javascript.Parser.parse方法的典型用法代码示例。如果您正苦于以下问题:Java Parser.parse方法的具体用法?Java Parser.parse怎么用?Java Parser.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.mozilla.javascript.Parser
的用法示例。
在下文中一共展示了Parser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
示例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;
}
示例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());
}
示例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;
}
示例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;
}
示例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;
}
示例7: 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();
}
}
示例8: 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
}
}
}
示例9: 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;
}
示例10: 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();
}
示例11: 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);
}
示例12: 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());
}
示例13: 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());
}
示例14: 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;
}
示例15: 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;
}