Class 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);