Class PositionConfigureUtils

java.lang.Object
org.apache.groovy.parser.antlr4.util.PositionConfigureUtils

public class PositionConfigureUtils extends Object
Utilities for configuring AST source positions.

Line and column numbers are 1-based; the end column is exclusive. -1 means the node is synthetic or generated and has no span in the source. Authored nodes should be stamped through these helpers so tooling can tell them apart (lineNumber > 0).

  • Constructor Details

    • PositionConfigureUtils

      public PositionConfigureUtils()
  • Method Details

    • configureAST

      public static <T extends ASTNode> T configureAST(T astNode, GroovyParser.GroovyParserRuleContext ctx)
      Sets location(lineNumber, colNumber, lastLineNumber, lastColumnNumber) for node using standard context information. Note: this method is implemented to be closed over ASTNode. It returns same node as it received in arguments.
      Parameters:
      astNode - Node to be modified.
      ctx - Context from which information is obtained.
      Returns:
      Modified astNode.
    • endPosition

      public static Tuple2<Integer,Integer> endPosition(org.antlr.v4.runtime.Token token)
      Returns the exclusive end line and column of token. Lines are 1-based. A null token text is zero-width: the end is the token's line and its 1-based start column. With no newline, the column is charPositionInLine + 1 plus the text's code-point count. With newlines, the line advances by that count and the column is the code-point count from the last newline, inclusive, through the end of the text.
      Parameters:
      token - the token whose end is measured; a null text is tolerated, a null token is not
      Returns:
      end line in v1 and exclusive end column in v2
    • configureAST

      public static <T extends ASTNode> T configureAST(T astNode, org.antlr.v4.runtime.tree.TerminalNode terminalNode)
      Copies the position of terminalNode's symbol onto astNode. This is configureAST(ASTNode, Token) and does not advance the end line when the token text contains newlines.
      Type Parameters:
      T - the node type, returned as the same instance
      Parameters:
      astNode - the node whose positions are overwritten
      terminalNode - the terminal whose symbol supplies the token
      Returns:
      astNode
    • configureAST

      public static <T extends ASTNode> T configureAST(T astNode, org.antlr.v4.runtime.Token token)
      Sets the start and end of astNode from a single token. The start column is charPositionInLine + 1. The end line stays the token's line even when the text contains newlines. The end column is that start column plus the text's code-point count, or the start column when the text is null. The same node is returned.
      Type Parameters:
      T - the node type, returned as the same instance
      Parameters:
      astNode - the node whose positions are overwritten
      token - the token that supplies every coordinate
      Returns:
      astNode
    • configureAST

      public static <T extends ASTNode> T configureAST(T astNode, ASTNode source)
      Copies line, column, last line, and last column from source onto astNode, including a -1 that marks a missing position. The same node is returned.
      Type Parameters:
      T - the node type, returned as the same instance
      Parameters:
      astNode - the node whose positions are overwritten
      source - the node whose four position fields are copied
      Returns:
      astNode
    • configureAST

      public static <T extends ASTNode> T configureAST(T astNode, GroovyParser.GroovyParserRuleContext ctx, ASTNode initialStop)
      Sets the start from ctx and the end from initialStop when that node has an authored end position. Otherwise the end comes from ctx's stop token.

      Groovy truth is non-null, so a synthetic ConstantExpression (default -1 coordinates) must not be treated as a stop: it would wipe the parent's end. The same applies to EmptyExpression.INSTANCE.

      Parameters:
      astNode - node to modify
      ctx - parse context supplying the start (and fallback end)
      initialStop - authored initializer whose end should be used, or null/unpositioned to use ctx's stop
      Returns:
      astNode
    • configureEndPosition

      public static <T extends ASTNode> void configureEndPosition(T astNode, org.antlr.v4.runtime.Token token)
      Sets the last line and last column of astNode from endPosition(Token). The start line and start column are left unchanged.
      Type Parameters:
      T - the node type
      Parameters:
      astNode - the node whose end position is overwritten
      token - the token measured by endPosition(Token)
    • configureAST

      public static <T extends ASTNode> T configureAST(T astNode, ASTNode start, ASTNode stop)
      Sets the start from start and the end from stop when that node has an authored end position. Otherwise the end is copied from start. See configureAST(ASTNode, GroovyParser.GroovyParserRuleContext, ASTNode).
      Parameters:
      astNode - node to modify
      start - node supplying the start (and fallback end)
      stop - authored node whose end should be used, or null/unpositioned to use start's end
      Returns:
      astNode