forked from pmd/pmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApexParser.java
50 lines (39 loc) · 1.64 KB
/
ApexParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.apex.ast;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.lang.apex.ApexLanguageProcessor;
import net.sourceforge.pmd.lang.ast.ParseException;
import net.sourceforge.pmd.lang.ast.Parser;
import com.google.summit.SummitAST;
import com.google.summit.translation.Translate;
import com.google.summit.ast.CompilationUnit;
@InternalApi
public final class ApexParser implements Parser {
public ApexParser() {
Logger.getLogger(Translate.class.getName()).setLevel(Level.WARNING);
}
@Override
public ASTApexFile parse(final ParserTask task) {
CompilationUnit astRoot = null;
PrintStream err = System.err;
try {
// Redirect System.err to suppress ANTLR warning about runtime/compilation version mismatch.
// See: org.antlr.v4.runtime.RuntimeMetadata
System.setErr(new PrintStream(new ByteArrayOutputStream()));
astRoot = SummitAST.INSTANCE.parseAndTranslate(task.getTextDocument().getText().toString(), null);
} finally {
System.setErr(err);
}
if (astRoot == null) {
throw new ParseException("Couldn't parse the source - there is not root node - Syntax Error??");
}
final ApexTreeBuilder treeBuilder = new ApexTreeBuilder(task, (ApexLanguageProcessor) task.getLanguageProcessor());
return treeBuilder.buildTree(astRoot);
}
}