Class Parser
- java.lang.Object
-
- com.google.javascript.jscomp.parsing.parser.Parser
-
public class Parser extends java.lang.Object
Parses a javascript file.The various parseX() methods never return null - even when parse errors are encountered. Typically parseX() will return a XTree ParseTree. Each ParseTree that is created includes its source location. The typical pattern for a parseX() method is:
XTree parseX() { SourcePosition start = getTreeStartLocation(); parse X grammar element and its children return new XTree(getTreeLocation(start), children); }
parseX() methods must consume at least 1 token - even in error cases. This prevents infinite loops in the parser.
Many parseX() methods are matched by a 'boolean peekX()' method which will return true if the beginning of an X appears at the current location. There are also peek() methods which examine the next token. peek() methods must not consume any tokens.
The eat() method consumes a token and reports an error if the consumed token is not of the expected type. The eatOpt() methods consume the next token iff the next token is of the expected type and return the consumed token or null if no token was consumed.
When parse errors are encountered, an error should be reported and the parse should return a best guess at the current parse tree.
When parsing lists, the preferred pattern is:
eat(LIST_START); ImmutableList.Builder<ParseTree> elements = ImmutableList.builder(); while (peekListElement()) { elements.add(parseListElement()); } eat(LIST_END);
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Parser.Config
-
Constructor Summary
Constructors Constructor Description Parser(Parser.Config config, ErrorReporter errorReporter, SourceFile source)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.List<Comment>
getComments()
FeatureSet
getFeatures()
java.lang.String
getSourceMapURL()
Returns the url provided by the sourceMappingURL if any was found.ProgramTree
parseProgram()
-
-
-
Constructor Detail
-
Parser
public Parser(Parser.Config config, ErrorReporter errorReporter, SourceFile source)
-
-
Method Detail
-
getComments
public java.util.List<Comment> getComments()
-
getFeatures
public FeatureSet getFeatures()
-
getSourceMapURL
@Nullable public java.lang.String getSourceMapURL()
Returns the url provided by the sourceMappingURL if any was found.
-
parseProgram
public ProgramTree parseProgram()
-
-