Skip to content

Commit

Permalink
HCL - support for legacy syntax for attribute expressions (#4901)
Browse files Browse the repository at this point in the history
* Support for LegacyIndexAttributeExpression

* licenseFormat

* Space.Location.LEGACY_INDEX_ATTRIBUTE_ACCESS

* LegacyIndexAttributeAccess.index is int

* Padding for LegacyAttributeAccess.base
greg-at-moderne authored Jan 15, 2025
1 parent 8a060c6 commit db9f188
Showing 18 changed files with 895 additions and 584 deletions.
3 changes: 2 additions & 1 deletion rewrite-hcl/src/main/antlr/HCLLexer.g4
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ fragment HexDigit
// https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#numeric-literals

NumericLiteral
: [0-9]+ '.' [0-9]* ExponentPart?
: [0-9]+ '.' [0-9]+ ExponentPart?
| [0-9]+ ExponentPart
| [0-9]+
;
@@ -129,6 +129,7 @@ MOD : '%';
ELLIPSIS : '...';
TILDE : '~';


// ----------------------------------------------------------------------------------------------
mode TEMPLATE;
// ----------------------------------------------------------------------------------------------
9 changes: 9 additions & 0 deletions rewrite-hcl/src/main/antlr/HCLParser.g4
Original file line number Diff line number Diff line change
@@ -52,6 +52,7 @@ exprTerm
| functionCall #FunctionCallExpression
| exprTerm index #IndexAccessExpression
| exprTerm getAttr #AttributeAccessExpression
| exprTerm legacyIndexAttr #LegacyIndexAttributeExpression
| exprTerm splat #SplatExpression
| LPAREN expression RPAREN #ParentheticalExpression
;
@@ -144,6 +145,14 @@ getAttr
: DOT Identifier
;

// Legacy Index Access Operator
// https://github.com/hashicorp/hcl/blob/923b06b5d6adf61a647101c605f7463a1bd56cbf/hclsyntax/spec.md#L547
// Interestingly not mentioned in hcl2 syntax specification anymore

legacyIndexAttr
: DOT NumericLiteral
;

// Splat Operators
// https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#splat-operators

Original file line number Diff line number Diff line change
@@ -102,6 +102,11 @@ public Hcl.Index.Position visitIndexPosition(Hcl.Index.Position indexPosition, P
return (Hcl.Index.Position) super.visitIndexPosition(indexPosition, p);
}

@Override
public Hcl.LegacyIndexAttributeAccess visitLegacyIndexAttribute(Hcl.LegacyIndexAttributeAccess legacyIndexAttributeAccess, P p) {
return (Hcl.LegacyIndexAttributeAccess) super.visitLegacyIndexAttribute(legacyIndexAttributeAccess, p);
}

@Override
public Hcl.Literal visitLiteral(Hcl.Literal literal, P p) {
return (Hcl.Literal) super.visitLiteral(literal, p);
17 changes: 17 additions & 0 deletions rewrite-hcl/src/main/java/org/openrewrite/hcl/HclVisitor.java
Original file line number Diff line number Diff line change
@@ -77,6 +77,23 @@ public Hcl visitAttributeAccess(Hcl.AttributeAccess attributeAccess, P p) {
return a;
}

public Hcl visitLegacyIndexAttribute(Hcl.LegacyIndexAttributeAccess legacyIndexAttributeAccess, P p) {
Hcl.LegacyIndexAttributeAccess li = legacyIndexAttributeAccess;
li = li.withPrefix(visitSpace(li.getPrefix(), Space.Location.LEGACY_INDEX_ATTRIBUTE_ACCESS, p));
li = li.withMarkers(visitMarkers(li.getMarkers(), p));
Expression temp = (Expression) visitExpression(li, p);
if (!(temp instanceof Hcl.LegacyIndexAttributeAccess)) {
return temp;
} else {
li = (Hcl.LegacyIndexAttributeAccess) temp;
}
li = li.getPadding().withBase(
visitRightPadded(li.getPadding().getBase(), HclRightPadded.Location.LEGACY_INDEX_ATTRIBUTE_ACCESS_BASE, p));
li = li.withIndex((Hcl.Literal) visitLiteral(li.getIndex(), p));
return li;
}


public Hcl visitBinary(Hcl.Binary binary, P p) {
Hcl.Binary b = binary;
b = b.withPrefix(visitSpace(b.getPrefix(), Space.Location.BINARY, p));
Original file line number Diff line number Diff line change
@@ -409,6 +409,25 @@ public Hcl visitIndexAccessExpression(HCLParser.IndexAccessExpressionContext ctx
));
}

@Override
public Hcl visitLegacyIndexAttributeExpression(HCLParser.LegacyIndexAttributeExpressionContext ctx) {
return convert(ctx, (c, prefix) -> {
String valueSource = c.legacyIndexAttr().NumericLiteral().getText();
Integer value = Integer.parseInt(valueSource);
return new Hcl.LegacyIndexAttributeAccess(
randomId(),
Space.format(prefix),
Markers.EMPTY,
new HclRightPadded<>(
(Expression) visit(c.exprTerm()),
sourceBefore("."),
Markers.EMPTY
),
new Hcl.Literal(randomId(), Space.format(prefix(c.legacyIndexAttr().NumericLiteral())), Markers.EMPTY, value, valueSource)
);
});
}

@Override
public Hcl visitLiteralValue(HCLParser.LiteralValueContext ctx) {
return convert(ctx, (c, prefix) -> {
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@
import org.openrewrite.marker.Marker;
import org.openrewrite.marker.Markers;

import java.util.Collections;
import java.util.List;
import java.util.function.UnaryOperator;

@@ -288,6 +289,18 @@ public Hcl visitIndexPosition(Hcl.Index.Position indexPosition, PrintOutputCaptu
return indexPosition;
}

@Override
public Hcl visitLegacyIndexAttribute(Hcl.LegacyIndexAttributeAccess laccess, PrintOutputCapture<P> p) {
beforeSyntax(laccess, Space.Location.LEGACY_INDEX_ATTRIBUTE_ACCESS, p);
visitRightPadded(
Collections.singletonList(laccess.getPadding().getBase()),
HclRightPadded.Location.LEGACY_INDEX_ATTRIBUTE_ACCESS_BASE, "", p);
p.append(".");
visitLiteral(laccess.getIndex(), p);
afterSyntax(laccess, p);
return laccess;
}

@Override
public Hcl visitLiteral(Hcl.Literal literal, PrintOutputCapture<P> p) {
beforeSyntax(literal, Space.Location.LITERAL, p);
Loading

0 comments on commit db9f188

Please sign in to comment.