Skip to content

Commit f395489

Browse files
Merge pull request #21 from amplify-education/fix_identifier_handling
Don't treat identifiers as tokens
2 parents 8fe76a4 + 82b4b57 commit f395489

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

hcl2/hcl2.lark

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
start : new_line_or_comment? body new_line_or_comment?
22
body : (attribute | block | one_line_block)*
3-
attribute : IDENTIFIER "=" expression new_line_or_comment
4-
block : IDENTIFIER (IDENTIFIER | STRING_LIT)* "{" new_line_or_comment body "}" new_line_or_comment
5-
one_line_block : IDENTIFIER (IDENTIFIER | STRING_LIT)* "{" (IDENTIFIER "=" expression)? "}" new_line_or_comment
3+
attribute : identifier "=" expression new_line_or_comment
4+
block : identifier (identifier | STRING_LIT)* "{" new_line_or_comment body "}" new_line_or_comment
5+
one_line_block : identifier (identifier | STRING_LIT)* "{" (identifier "=" expression)? "}" new_line_or_comment
66
new_line_and_or_comma: new_line_or_comment | "," | "," new_line_or_comment
77
new_line_or_comment: ( /\n/ | /#.*\n/ | /\/\/.*\n/ )+
88

9-
IDENTIFIER : /[a-zA-Z_][a-zA-Z0-9_-]*/
9+
identifier : /[a-zA-Z_][a-zA-Z0-9_-]*/
1010

1111
?expression : expr_term | operation | conditional
1212

@@ -30,7 +30,7 @@ expr_term : "(" expression ")"
3030
| function_call
3131
| index_expr_term
3232
| get_attr_expr_term
33-
| IDENTIFIER
33+
| identifier
3434
| heredoc_template
3535
| heredoc_template_trim
3636
| attr_splat_expr_term
@@ -54,26 +54,26 @@ EXP_MARK : ("e" | "E") ("+" | "-")?
5454

5555
tuple : "[" (new_line_or_comment? expression (new_line_or_comment? "," new_line_or_comment? expression)* new_line_or_comment? ","?)? new_line_or_comment? "]"
5656
object : "{" (new_line_or_comment? object_elem (new_line_and_or_comma object_elem )* new_line_and_or_comma?)? "}"
57-
object_elem : (IDENTIFIER | expression) "="? expression
57+
object_elem : (identifier | expression) "="? expression
5858

5959
heredoc_template : /<<(?P<heredoc>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)+?\n+\s*(?P=heredoc)/
6060
heredoc_template_trim : /<<-(?P<heredoc_trim>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)+?\n+\s*(?P=heredoc_trim)/
6161

62-
function_call : IDENTIFIER "(" new_line_or_comment? arguments? new_line_or_comment? ")"
62+
function_call : identifier "(" new_line_or_comment? arguments? new_line_or_comment? ")"
6363
arguments : (expression (new_line_or_comment? "," new_line_or_comment? expression)* ("," | "...")? new_line_or_comment?)
6464

6565
index_expr_term : expr_term index
6666
get_attr_expr_term : expr_term get_attr
6767
attr_splat_expr_term : expr_term attr_splat
6868
full_splat_expr_term : expr_term full_splat
6969
?index : "[" expression "]"
70-
?get_attr : "." IDENTIFIER
70+
?get_attr : "." identifier
7171
?attr_splat : ".*" get_attr*
7272
?full_splat : "[" "*" "]" (get_attr | index)*
7373

7474
!for_tuple_expr : "[" new_line_or_comment? for_intro new_line_or_comment? expression new_line_or_comment? for_cond? new_line_or_comment? "]"
7575
!for_object_expr : "{" new_line_or_comment? for_intro expression "=>" expression "..."? new_line_or_comment? for_cond? new_line_or_comment? "}"
76-
!for_intro : "for" new_line_or_comment? IDENTIFIER ("," IDENTIFIER new_line_or_comment?)? new_line_or_comment? "in" new_line_or_comment? expression ":" new_line_or_comment?
76+
!for_intro : "for" new_line_or_comment? identifier ("," identifier new_line_or_comment?)? new_line_or_comment? "in" new_line_or_comment? expression ":" new_line_or_comment?
7777
!for_cond : "if" new_line_or_comment? expression
7878

7979
%ignore /[ \t]+/

hcl2/transformer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,10 @@ def strip_quotes(self, value: Any) -> Any:
220220
if value.startswith('"') and value.endswith('"'):
221221
return str(value)[1:-1]
222222
return value
223+
224+
def identifier(self, value: Any) -> Any:
225+
# Making identifier a token by capitalizing it to IDENTIFIER
226+
# seems to return a token object instead of the str
227+
# So treat it like a regular rule
228+
# In this case we just convert the whole thing to a string
229+
return str(value[0])

hcl2/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""Place of record for the package version"""
22

3-
__version__ = "0.2.3"
3+
__version__ = "0.2.4"
44
__git_hash__ = "GIT_HASH"

requirements.pip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Place dependencies in this file, following the distutils format:
22
# http://docs.python.org/2/distutils/setupscript.html#relationships-between-distributions-and-packages
33
docopt==0.6.2
4-
lark-parser>=0.7.3,<0.8.0
4+
lark-parser>=0.7.8,<0.8.0

test/helpers/terraform-config-json/variables.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
},
66
{
77
"account": {}
8+
},
9+
{
10+
"azs": {
11+
"default": [
12+
{
13+
"us-west-1": "us-west-1c,us-west-1b",
14+
"us-west-2": "us-west-2c,us-west-2b,us-west-2a",
15+
"us-east-1": "us-east-1c,us-east-1b,us-east-1a",
16+
"eu-central-1": "eu-central-1a,eu-central-1b,eu-central-1c",
17+
"sa-east-1": "sa-east-1a,sa-east-1c",
18+
"ap-northeast-1": "ap-northeast-1a,ap-northeast-1c,ap-northeast-1d",
19+
"ap-southeast-1": "ap-southeast-1a,ap-southeast-1b,ap-southeast-1c",
20+
"ap-southeast-2": "ap-southeast-2a,ap-southeast-2b,ap-southeast-2c"
21+
}
22+
]
23+
}
824
}
925
],
1026
"locals": [

test/helpers/terraform-config/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,15 @@ locals {
88
foo = "${var.account}_bar"
99
}
1010

11+
variable "azs" {
12+
default = {
13+
us-west-1 = "us-west-1c,us-west-1b"
14+
us-west-2 = "us-west-2c,us-west-2b,us-west-2a"
15+
us-east-1 = "us-east-1c,us-east-1b,us-east-1a"
16+
eu-central-1 = "eu-central-1a,eu-central-1b,eu-central-1c"
17+
sa-east-1 = "sa-east-1a,sa-east-1c"
18+
ap-northeast-1 = "ap-northeast-1a,ap-northeast-1c,ap-northeast-1d"
19+
ap-southeast-1 = "ap-southeast-1a,ap-southeast-1b,ap-southeast-1c"
20+
ap-southeast-2 = "ap-southeast-2a,ap-southeast-2b,ap-southeast-2c"
21+
}
22+
}

0 commit comments

Comments
 (0)