01 /**
02 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03 */
04 package net.sourceforge.pmd.lang.ecmascript.ast;
05
06 import java.io.IOException;
07 import java.io.Reader;
08 import java.util.HashMap;
09 import java.util.Map;
10
11 import net.sourceforge.pmd.lang.ast.ParseException;
12
13 import org.mozilla.javascript.CompilerEnvirons;
14 import org.mozilla.javascript.ErrorReporter;
15 import org.mozilla.javascript.Parser;
16 import org.mozilla.javascript.ast.AstNode;
17 import org.mozilla.javascript.ast.AstRoot;
18 import org.mozilla.javascript.ast.ErrorCollector;
19
20 public class EcmascriptParser {
21
22 protected Map<AstNode, EcmascriptNode> nodeCache = new HashMap<AstNode, EcmascriptNode>();
23
24 protected AstRoot parseEcmascript(final Reader reader) throws ParseException {
25 // TODO Fix hardcode
26 final CompilerEnvirons compilerEnvirons = new CompilerEnvirons();
27 compilerEnvirons.setRecordingComments(true);
28 //compilerEnvirons.setLanguageVersion(Context.VERSION_1_0);
29 // TODO Fix hardcode
30 final ErrorReporter errorReporter = new ErrorCollector();
31 final Parser parser = new Parser(compilerEnvirons, errorReporter);
32 nodeCache.clear();
33 try {
34 // TODO Fix hardcode
35 final String sourceURI = "unknown";
36 // TODO Fix hardcode
37 final int lineno = 0;
38 return parser.parse(reader, sourceURI, lineno);
39 } catch (final IOException e) {
40 throw new ParseException(e);
41 }
42 }
43
44 public EcmascriptNode parse(final Reader reader) {
45 final AstRoot astRoot = parseEcmascript(reader);
46 final EcmascriptTreeBuilder treeBuilder = new EcmascriptTreeBuilder();
47 return treeBuilder.build(astRoot);
48 }
49 }
|