Skip to content

Commit

Permalink
✨ : add HCL parsing for boolean and number variables
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Nov 12, 2019
1 parent 908ee7c commit 27e5876
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
24 changes: 22 additions & 2 deletions src/main/antlr4/io/codeka/gaia/hcl/antlr/hcl.g4
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,37 @@ variableBlock
;

type
: 'type' '=' STRING
: 'type' '=' TYPE
;

description
: 'description' '=' STRING
;

r_default
: 'default' '=' STRING
: 'default' '=' defaultValue
;

defaultValue
: STRING
| NUMBER
| 'true'
| 'false'
;

identifier
: STRING
;

TYPE
: 'string'
| '"string"'
| 'number'
| '"number"'
| 'bool'
| '"bool"'
;

/**
* STRING Lexer Rule comes from the JSON grammar
* https://github.com/antlr/grammars-v4/blob/master/json/JSON.g4
Expand All @@ -53,6 +69,10 @@ fragment SAFECODEPOINT
: ~ ["\\\u0000-\u001F]
;

NUMBER
: '0' | [1-9] [0-9]*
;

WS
: [ \t\r\n]+ -> skip
; // skip spaces, tabs, newlines
4 changes: 2 additions & 2 deletions src/main/java/io/codeka/gaia/hcl/HclTreeListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ class HclTreeListener : hclBaseListener() {
}

override fun enterType(ctx: hclParser.TypeContext) {
this.current.type = ctx.STRING().text
this.current.type = ctx.TYPE().text
}

override fun enterDescription(ctx: hclParser.DescriptionContext) {
this.current.description = ctx.STRING().text
}

override fun enterR_default(ctx: hclParser.R_defaultContext) {
this.current.default = ctx.STRING().text
this.current.default = ctx.defaultValue().text
}
}
11 changes: 5 additions & 6 deletions src/test/java/io/codeka/gaia/hcl/HCLParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ void parsing_variable_shouldWork() throws IOException {
walker.walk(hclTreeListener, parser.file());

// then
assertThat(hclTreeListener.getVariables()).hasSize(1);
assertThat(hclTreeListener.getVariables()).hasSize(3);

Variable foo = new Variable();
foo.setName("\"foo\"");
foo.setDescription("\"bar\"");
foo.setDefault("\"baz\"");
Variable stringVar = new Variable("\"string_var\"", "\"string\"", "\"a test string var\"", "\"foo\"");
Variable numberVar = new Variable("\"number_var\"", "number", "\"a test number var\"", "42");
Variable boolVar = new Variable("\"bool_var\"", "", "", "false");

assertThat(hclTreeListener.getVariables()).contains(foo);
assertThat(hclTreeListener.getVariables()).contains(stringVar, numberVar, boolVar);
}

}
17 changes: 14 additions & 3 deletions src/test/resources/hcl/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
variable "foo" {
description = "bar"
default = "baz"
variable "string_var" {
type = "string"
description = "a test string var"
default = "foo"
}

variable "number_var" {
type = number
description = "a test number var"
default = 42
}

variable "bool_var" {
default = false
}

0 comments on commit 27e5876

Please sign in to comment.