Skip to content

Commit

Permalink
HCL added Scoped Function name support (Terraform 1.8)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkishalmi committed Apr 16, 2024
1 parent d20d7e4 commit 8c75f2d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public enum HCLTokenId implements TokenId {
INTERPOLATION_START("${", "separator"),
INTERPOLATION_END("}", "separator"),
RARROW("=>", "separator"),
SCOPE("::", "separator"),
TEMPLATE_START("%{", "separator"),
TEMPLATE_END("}", "separator"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.netbeans.modules.languages.hcl.grammar.HCLLexer;
import org.netbeans.modules.languages.hcl.grammar.HCLParser;

/**
*
Expand All @@ -41,6 +42,13 @@ protected final HCLIdentifier id(TerminalNode tn) {
return tn != null ? id(tn.getSymbol()) : null;
}

protected final HCLIdentifier id(HCLParser.ScopedIdContext ctx) {
HCLIdentifier parent = ctx.target != null
? id(ctx.target)
: id(ctx.scopedId());
return created(new HCLIdentifier.ScopedId(parent, ctx.ref.getText()), ctx);
}

protected final HCLIdentifier id(Token t) {
return (t != null) && (t.getType() == HCLLexer.IDENTIFIER) ? created(new HCLIdentifier.SimpleId(t.getText()), t) : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.antlr.v4.runtime.NoViableAltException;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.netbeans.modules.languages.hcl.grammar.HCLLexer;
import static org.netbeans.modules.languages.hcl.grammar.HCLLexer.*;
import org.netbeans.modules.languages.hcl.grammar.HCLParser;
Expand Down Expand Up @@ -175,16 +176,22 @@ protected HCLExpression expr(HCLParser.FunctionCallContext ctx) {
if (ctx == null) {
return null;
}
List<HCLExpression> args = Collections.emptyList();
if (ctx.exception != null) {
return null;
}
List<HCLExpression> args = List.of();
boolean expand = false;
if (ctx.arguments() != null) {
args = new ArrayList<>(ctx.arguments().expression().size());
for (HCLParser.ExpressionContext ectx : ctx.arguments().expression()) {
args.add(expr(ectx));
}
args = ctx.arguments().expression().stream()
.map(this::expr)
.toList();
expand = ctx.arguments().ELLIPSIS() != null;
}
return created(new HCLFunction(id(ctx.IDENTIFIER()), args, expand), ctx);
HCLIdentifier name = ctx.IDENTIFIER() != null
? id(ctx.IDENTIFIER())
: id(ctx.scopedId());

return created(new HCLFunction(name, args, expand), ctx);
}

private static HCLArithmeticOperation.Operator binOp(int tokenType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ default List<? extends HCLElement> elements() {
public record SimpleId(String id) implements HCLIdentifier {}

public record StringId(String id) implements HCLIdentifier {}

public record ScopedId(HCLIdentifier parent, String id) implements HCLIdentifier {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,19 @@ heredoc
: HEREDOC_START heredocTemplate HEREDOC_END
;

// This is not part of the HCL spec, though used in Terraform 1.8 for function calls
scopedId
: target=IDENTIFIER SCOPE ref=IDENTIFIER
| scopedId SCOPE ref=IDENTIFIER
;

variableExpr
: IDENTIFIER
;

functionCall
: IDENTIFIER LPAREN arguments RPAREN
| IDENTIFIER LPAREN RPAREN
: (IDENTIFIER|scopedId) LPAREN arguments RPAREN
| (IDENTIFIER|scopedId) LPAREN RPAREN
;

arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ QUESTION
: Question
;

// Used from Terraform 1.8 in provider exported function names
SCOPE
: Colon Colon
;

COLON
: Colon
;
Expand Down Expand Up @@ -123,8 +128,8 @@ ELLIPSIS
;

LEGACY_INDEX
: Dot DecDigit+
;
: Dot DecDigit+
;

DOT
: Dot
Expand Down

0 comments on commit 8c75f2d

Please sign in to comment.