Interface ParseTree<NT extends Enum<NT>>

Type Parameters:
NT - is an Enum type with one value for every nonterminal in the grammar. See Parser for more information.

public interface ParseTree<NT extends Enum<NT>>
Represents an immutable parse tree. Each node represents the application of a production rule from the grammar, "name ::= expr". The node's name() is the nonterminal on the left side of the production rule, and the node's children() are the production rules that were matched for each nonterminal used in the righthand side (expr). A node also has text() representing the substring matched by this node's subtree, and start()/end() indicating where that substring is relative to the entire string parsed.

If the grammar used @skip to skip over some nonterminals automatically, then the skipped subtrees do not appear among this node's children(), but can be found by using allChildren() or childrenByName().

Author:
6.005/6.031 course staff
  • Method Summary

    Modifier and Type Method Description
    List<ParseTree<NT>> allChildren()
    Get all of this node's children, including @skip rules.
    List<ParseTree<NT>> children()
    Get this node's children.
    List<ParseTree<NT>> childrenByName​(NT name)
    Get the children that correspond to a particular production rule
    int end()
    Get the offset where this subtree ends in the entire parsed string.
    boolean isSkipped()
    Test if this node is in a subtree that was @skipped.
    NT name()
    Get this node's name.
    int start()
    Get the offset where this subtree starts in the entire parsed string.
    String text()
    Get this subtree's text.
  • Method Details

    • name

      NT name()
      Get this node's name.
      Returns:
      the nonterminal corresponding to this node in the parse tree.
    • children

      List<ParseTree<NT>> children()
      Get this node's children.
      Returns:
      the children of this node, in order, excluding @skipped subtrees
    • text

      String text()
      Get this subtree's text.
      Returns:
      the substring of the entire parsed string that this subtree matched
    • start

      int start()
      Get the offset where this subtree starts in the entire parsed string.
      Returns:
      the offset [0,...length-1] in the entire parsed string where this subtree started to match
    • end

      int end()
      Get the offset where this subtree ends in the entire parsed string.
      Returns:
      the offset [0,...length-1] in the entire parsed string where this subtree ends
    • childrenByName

      List<ParseTree<NT>> childrenByName​(NT name)
      Get the children that correspond to a particular production rule
      Parameters:
      name - Name of the nonterminal corresponding to the desired production rule.
      Returns:
      children that represent matches of name's production rule.
    • allChildren

      List<ParseTree<NT>> allChildren()
      Get all of this node's children, including @skip rules.
      Returns:
      all the children of this node, in order, including @skipped subtrees
    • isSkipped

      boolean isSkipped()
      Test if this node is in a subtree that was @skipped.
      Returns:
      true iff this node is part of a subtree that was automatically skipped over by a @skip rule.