Class ExtendedContext

java.lang.Object
org.antlr.v4.runtime.RuleContext
org.antlr.v4.runtime.ParserRuleContext
io.github.douira.glsl_transformer.tree.ExtendedContext
All Implemented Interfaces:
MoveCheckable, TreeMember, ParseTree, RuleNode, SyntaxTree, Tree

public abstract class ExtendedContext extends ParserRuleContext implements TreeMember
Implements custom behavior in parse rule contexts. This class is used as the super class for all contexts in the parser. This class is not meant to be constructed manually but is the base class which ANTLR extends in the generated parser code. The token indexes of the token intervals of new nodes have nothing to do with the token indexes of existing nodes. Therefore, new nodes are registered as local roots that have their own token index domain and a set of intervals in which non-hidden tokens should not be printed. Local root nodes store information about which nodes in their subtree have been removed. The printer then uses this to not print any non-hidden tokens contained within any local root's omission set.
  • Constructor Details

    • ExtendedContext

      public ExtendedContext(ParserRuleContext parent, int invokingStateNumber)
      Creates a new extended parser rule context. This is required for the generated parse code to be valid.
      Parameters:
      parent - The parent node
      invokingStateNumber - The invoking state number
    • ExtendedContext

      public ExtendedContext()
      Creates a new extended parser rule context without a parent. This is also used in the generated code.
  • Method Details

    • getPreviousNode

      public ParseTree getPreviousNode()
      Description copied from interface: MoveCheckable
      Returns the node that this node replaced during a tree transformation.
      Specified by:
      getPreviousNode in interface MoveCheckable
      Returns:
      The replaced node
    • setPreviousNode

      public void setPreviousNode(ParseTree previousNode)
      Description copied from interface: MoveCheckable
      Allows the transformation phase to tell this node which node it replaces.
      Specified by:
      setPreviousNode in interface MoveCheckable
      Parameters:
      previousNode - The node to set as the previous node in this position in the parent's child array that this node replaces
    • getParent

      public ExtendedContext getParent()
      Override to make type more specific
      Specified by:
      getParent in interface ParseTree
      Specified by:
      getParent in interface Tree
      Specified by:
      getParent in interface TreeMember
      Overrides:
      getParent in class ParserRuleContext
    • setParent

      public void setParent(ExtendedContext parent)
      Sets the parent and copies the root reference from the parent. This requires the parent to have been set up before the child.
      Parameters:
      parent - The paren to set on this node
    • makeLocalRoot

      public void makeLocalRoot(BufferedTokenStream tokenStream)
      Registers this node as a new local root node together with its corresponding token stream. This means it has its own omission set and token interval space. The token interval of omitted nodes are added to the omission set of the closest local root ancestor.
      Parameters:
      tokenStream - The token stream from which this node was parsed
    • isLocalRoot

      public boolean isLocalRoot()
      Checks if this node is a local root.
      Returns:
      true if the node is a local root
    • isRoot

      public boolean isRoot()
      Checks if this node is the global root.
      Returns:
      true if this node is the global root
      Implementation Note:
      This is faster to check than finding the local root, taking it's root and checking if it's the same.
    • omitTokens

      public void omitTokens()
      Marks the tokens encompassed by this node as omitted. Non-hidden tokens will not be printed if they are withing the token source token interval of this node. Other nodes will however, still be printed in order to preserve whitespace.
      Specified by:
      omitTokens in interface TreeMember
    • omitTokens

      public void omitTokens(Interval tokenInterval)
      Omits the given token interval on this node's local root.
      Parameters:
      tokenInterval - The token interval to be omitted
      See Also:
    • finishEditingTree

      public void finishEditingTree()
      Marks editing on the whole tree as finished. This modifies the root node's readonly state. After this point no further modifications should be made.
    • getOmissionSet

      public CachingIntervalSet getOmissionSet()
      The omission set is present if this node is a local root. Then it contains the token intervals that should be omitted by the printer when printing this local root's subtree. The omission set is set to be readonly if the tree has been set to readonly.
      Returns:
      This local root's token stream if this node is a local root, null otherwise
    • getTokenStream

      public BufferedTokenStream getTokenStream()
      The token stream is present if this node is a local root. Then it's the token stream that was used to construct this local root's subtree.
      Returns:
      The token stream for this local root if this node is a local root, null otherwise
    • getFullSourceInterval

      public Interval getFullSourceInterval()
      Gets the source interval for this local root that includes the whole token stream with unparsed tokens before and after the parsed tokens that this node encompasses.
      Returns:
      The full source interval of the contained token stream if this node is a local root, null otherwise.
    • getLargestSourceInterval

      public Interval getLargestSourceInterval()
      Gets the source interval for this node that covers the largest space. For local roots this is the full source interval covering all tokens in the token stream. For all other nodes it's the regular source interval covering only the tokens parsed for this node.
      Returns:
      The largest source interval, never null.
    • getChildIndexLike

      public int getChildIndexLike(Class<? extends ParseTree> ctxType)
      Finds the index of the first contained child that matches the given type.
      Parameters:
      ctxType - The child type to look for
      Returns:
      The index of the first child with the given index. If no child with that type was found, the length of the child array is returned as the "last" index.
    • addChild

      public void addChild(int index, ParseTree node)
      Adds a child to the list of children with at the given index.
      Parameters:
      index - The index to add the node at
      node - The node to add