Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HCL - support for legacy syntax for attribute expressions #4899

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
16 changes: 16 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,22 @@ 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.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.withBase((Expression) visit(li.getBase(), p));
li = li.withIndex(li.getIndex());
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,17 @@ public Hcl visitIndexAccessExpression(HCLParser.IndexAccessExpressionContext ctx
));
}

@Override
public Hcl visitLegacyIndexAttributeExpression(HCLParser.LegacyIndexAttributeExpressionContext ctx) {
return convert(ctx, (c, prefix) -> new Hcl.LegacyIndexAttributeAccess(
randomId(),
Space.format(prefix),
Markers.EMPTY,
(Expression) visit(c.exprTerm()),
c.legacyIndexAttr().NumericLiteral().getText()
));
}

@Override
public Hcl visitLiteralValue(HCLParser.LiteralValueContext ctx) {
return convert(ctx, (c, prefix) -> {
Original file line number Diff line number Diff line change
@@ -288,6 +288,16 @@ public Hcl visitIndexPosition(Hcl.Index.Position indexPosition, PrintOutputCaptu
return indexPosition;
}

@Override
public Hcl visitLegacyIndexAttribute(Hcl.LegacyIndexAttributeAccess laccess, PrintOutputCapture<P> p) {
beforeSyntax(laccess, Space.Location.ATTRIBUTE_ACCESS, p);
visit(laccess.getBase(), p);
p.append(".");
p.append(laccess.getIndex());
afterSyntax(laccess, p);
return laccess;
}

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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -219,6 +219,18 @@ public class HCLParserBaseListener implements HCLParserListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void exitIndexAccessExpression(HCLParser.IndexAccessExpressionContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterLegacyIndexAttributeExpression(HCLParser.LegacyIndexAttributeExpressionContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitLegacyIndexAttributeExpression(HCLParser.LegacyIndexAttributeExpressionContext ctx) { }
/**
* {@inheritDoc}
*
@@ -447,6 +459,18 @@ public class HCLParserBaseListener implements HCLParserListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void exitGetAttr(HCLParser.GetAttrContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterLegacyIndexAttr(HCLParser.LegacyIndexAttrContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitLegacyIndexAttr(HCLParser.LegacyIndexAttrContext ctx) { }
/**
* {@inheritDoc}
*
Original file line number Diff line number Diff line change
@@ -139,6 +139,13 @@ public class HCLParserBaseVisitor<T> extends AbstractParseTreeVisitor<T> impleme
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitIndexAccessExpression(HCLParser.IndexAccessExpressionContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitLegacyIndexAttributeExpression(HCLParser.LegacyIndexAttributeExpressionContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
@@ -272,6 +279,13 @@ public class HCLParserBaseVisitor<T> extends AbstractParseTreeVisitor<T> impleme
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitGetAttr(HCLParser.GetAttrContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitLegacyIndexAttr(HCLParser.LegacyIndexAttrContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
Original file line number Diff line number Diff line change
@@ -202,6 +202,18 @@ public interface HCLParserListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitIndexAccessExpression(HCLParser.IndexAccessExpressionContext ctx);
/**
* Enter a parse tree produced by the {@code LegacyIndexAttributeExpression}
* labeled alternative in {@link HCLParser#exprTerm}.
* @param ctx the parse tree
*/
void enterLegacyIndexAttributeExpression(HCLParser.LegacyIndexAttributeExpressionContext ctx);
/**
* Exit a parse tree produced by the {@code LegacyIndexAttributeExpression}
* labeled alternative in {@link HCLParser#exprTerm}.
* @param ctx the parse tree
*/
void exitLegacyIndexAttributeExpression(HCLParser.LegacyIndexAttributeExpressionContext ctx);
/**
* Enter a parse tree produced by the {@code ForExpression}
* labeled alternative in {@link HCLParser#exprTerm}.
@@ -398,6 +410,16 @@ public interface HCLParserListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitGetAttr(HCLParser.GetAttrContext ctx);
/**
* Enter a parse tree produced by {@link HCLParser#legacyIndexAttr}.
* @param ctx the parse tree
*/
void enterLegacyIndexAttr(HCLParser.LegacyIndexAttrContext ctx);
/**
* Exit a parse tree produced by {@link HCLParser#legacyIndexAttr}.
* @param ctx the parse tree
*/
void exitLegacyIndexAttr(HCLParser.LegacyIndexAttrContext ctx);
/**
* Enter a parse tree produced by {@link HCLParser#splat}.
* @param ctx the parse tree
Original file line number Diff line number Diff line change
@@ -131,6 +131,13 @@ public interface HCLParserVisitor<T> extends ParseTreeVisitor<T> {
* @return the visitor result
*/
T visitIndexAccessExpression(HCLParser.IndexAccessExpressionContext ctx);
/**
* Visit a parse tree produced by the {@code LegacyIndexAttributeExpression}
* labeled alternative in {@link HCLParser#exprTerm}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitLegacyIndexAttributeExpression(HCLParser.LegacyIndexAttributeExpressionContext ctx);
/**
* Visit a parse tree produced by the {@code ForExpression}
* labeled alternative in {@link HCLParser#exprTerm}.
@@ -248,6 +255,12 @@ public interface HCLParserVisitor<T> extends ParseTreeVisitor<T> {
* @return the visitor result
*/
T visitGetAttr(HCLParser.GetAttrContext ctx);
/**
* Visit a parse tree produced by {@link HCLParser#legacyIndexAttr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitLegacyIndexAttr(HCLParser.LegacyIndexAttrContext ctx);
/**
* Visit a parse tree produced by {@link HCLParser#splat}.
* @param ctx the parse tree
62 changes: 61 additions & 1 deletion rewrite-hcl/src/main/java/org/openrewrite/hcl/tree/Hcl.java
Original file line number Diff line number Diff line change
@@ -26,7 +26,6 @@
import org.openrewrite.hcl.internal.HclPrinter;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.marker.Markers;
import org.openrewrite.template.SourceTemplate;

import java.lang.ref.WeakReference;
import java.nio.charset.Charset;
@@ -236,6 +235,67 @@ public AttributeAccess withName(HclLeftPadded<Identifier> name) {
}
}

@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
class LegacyIndexAttributeAccess implements Expression, Label {
@Nullable
@NonFinal
transient WeakReference<Padding> padding;

@With
@EqualsAndHashCode.Include
@Getter
UUID id;

@With
@Getter
Space prefix;

@With
@Getter
Markers markers;

@With
@Getter
Expression base;

@With
@Getter
String index;

@Override
public <P> Hcl acceptHcl(HclVisitor<P> v, P p) {
return v.visitLegacyIndexAttribute(this, p);
}

@Override
public String toString() {
return "LegacyIndexAttributeAccess{" + base + "." + index + "}";
}

public Padding getPadding() {
Padding p;
if (this.padding == null) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
} else {
p = this.padding.get();
if (p == null || p.t != this) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
}
}
return p;
}

@RequiredArgsConstructor
public static class Padding {
private final LegacyIndexAttributeAccess t;
}
}

@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
Original file line number Diff line number Diff line change
@@ -33,4 +33,17 @@ void variableExpression() {
)
);
}

@Test
void legacyIndexOperator() {
rewriteRun(
hcl(
"""
locals {
dns_record = aws_acm_certificate.google_dot_com.0.resource_record_name
}
"""
)
);
}
}