Class PositionConfigureUtils
java.lang.Object
org.apache.groovy.parser.antlr4.util.PositionConfigureUtils
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends ASTNode>
TconfigureAST(T astNode, org.antlr.v4.runtime.Token token) Sets the start and end ofastNodefrom a single token.static <T extends ASTNode>
TconfigureAST(T astNode, org.antlr.v4.runtime.tree.TerminalNode terminalNode) Copies the position ofterminalNode's symbol ontoastNode.static <T extends ASTNode>
TconfigureAST(T astNode, GroovyParser.GroovyParserRuleContext ctx) Sets location(lineNumber, colNumber, lastLineNumber, lastColumnNumber) for node using standard context information.static <T extends ASTNode>
TconfigureAST(T astNode, GroovyParser.GroovyParserRuleContext ctx, ASTNode initialStop) Sets the start fromctxand the end frominitialStopwhen that node has an authored end position.static <T extends ASTNode>
TconfigureAST(T astNode, ASTNode source) Copies line, column, last line, and last column fromsourceontoastNode, including a-1that marks a missing position.static <T extends ASTNode>
TconfigureAST(T astNode, ASTNode start, ASTNode stop) Sets the start fromstartand the end fromstopwhen that node has an authored end position.static <T extends ASTNode>
voidconfigureEndPosition(T astNode, org.antlr.v4.runtime.Token token) Sets the last line and last column ofastNodefromendPosition(Token).endPosition(org.antlr.v4.runtime.Token token) Returns the exclusive end line and column oftoken.
-
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
Returns the exclusive end line and column oftoken. 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 ischarPositionInLine + 1plus 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
v1and exclusive end column inv2
-
configureAST
public static <T extends ASTNode> T configureAST(T astNode, org.antlr.v4.runtime.tree.TerminalNode terminalNode) Copies the position ofterminalNode's symbol ontoastNode. This isconfigureAST(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 overwrittenterminalNode- the terminal whose symbol supplies the token- Returns:
astNode
-
configureAST
Sets the start and end ofastNodefrom a single token. The start column ischarPositionInLine + 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 overwrittentoken- the token that supplies every coordinate- Returns:
astNode
-
configureAST
Copies line, column, last line, and last column fromsourceontoastNode, including a-1that 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 overwrittensource- 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 fromctxand the end frominitialStopwhen that node has an authored end position. Otherwise the end comes fromctx's stop token.Groovy truth is non-null, so a synthetic
ConstantExpression(default-1coordinates) must not be treated as a stop: it would wipe the parent's end. The same applies toEmptyExpression.INSTANCE.- Parameters:
astNode- node to modifyctx- parse context supplying the start (and fallback end)initialStop- authored initializer whose end should be used, ornull/unpositioned to usectx'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 ofastNodefromendPosition(Token). The start line and start column are left unchanged.- Type Parameters:
T- the node type- Parameters:
astNode- the node whose end position is overwrittentoken- the token measured byendPosition(Token)
-
configureAST
Sets the start fromstartand the end fromstopwhen that node has an authored end position. Otherwise the end is copied fromstart. SeeconfigureAST(ASTNode, GroovyParser.GroovyParserRuleContext, ASTNode).- Parameters:
astNode- node to modifystart- node supplying the start (and fallback end)stop- authored node whose end should be used, ornull/unpositioned to usestart's end- Returns:
astNode
-