handle identifiers better

This commit is contained in:
mhoffm
2021-06-27 12:22:06 +02:00
parent 71aaa2a4aa
commit edac6a2beb
5 changed files with 15048 additions and 11133 deletions

View File

@@ -31,7 +31,8 @@ The aim is to build unit testcases from selected failure classes and slowly get
## Todo ## Todo
* [ ] use [Unicode® Standard Annex #31](https://www.unicode.org/reports/tr31/) (augmented with '-') for identifiers * [x] use [Unicode® Standard Annex #31](https://www.unicode.org/reports/tr31/) (augmented with '-') for identifiers
* allow starting '_' also, not to spec but terraform allows it
* [ ] add [template expressions](https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#template-expressions) * [ ] add [template expressions](https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#template-expressions)
* [x] add quoted templates * [x] add quoted templates
* [x] add quoted template interpolations * [x] add quoted template interpolations

View File

@@ -34,14 +34,13 @@ module.exports = grammar({
], ],
rules: { rules: {
config_file: $ => optional($.body), // also allow objects to handle .tfvars in json format
config_file: $ => optional(choice($.body, $.object)),
body: $ => repeat1( body: $ => repeat1(
choice( choice(
$.attribute, $.attribute,
$.block, $.block,
// not to spec but handles .tfvars in json format
$.object,
), ),
), ),
@@ -62,10 +61,9 @@ module.exports = grammar({
_block_start: $ => '{', _block_start: $ => '{',
_block_end: $ => '}', _block_end: $ => '}',
// TODO: not to spec but good enough for now
identifier: $ => token(seq( identifier: $ => token(seq(
choice(/\p{L}/, '_'), choice(/\p{ID_Start}/, '_'),
repeat(choice(/\p{L}/, /[0-9]/, /(-|_)/)), repeat(choice(/\p{ID_Continue}/, '-')),
)), )),
expression: $ => prec.right(choice( expression: $ => prec.right(choice(

View File

@@ -2,12 +2,21 @@
"name": "hcl", "name": "hcl",
"rules": { "rules": {
"config_file": { "config_file": {
"type": "CHOICE",
"members": [
{
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "body" "name": "body"
}, },
{
"type": "SYMBOL",
"name": "object"
}
]
},
{ {
"type": "BLANK" "type": "BLANK"
} }
@@ -25,10 +34,6 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "block" "name": "block"
},
{
"type": "SYMBOL",
"name": "object"
} }
] ]
} }
@@ -113,7 +118,7 @@
"members": [ "members": [
{ {
"type": "PATTERN", "type": "PATTERN",
"value": "\\p{L}" "value": "\\p{ID_Start}"
}, },
{ {
"type": "STRING", "type": "STRING",
@@ -128,15 +133,11 @@
"members": [ "members": [
{ {
"type": "PATTERN", "type": "PATTERN",
"value": "\\p{L}" "value": "\\p{ID_Continue}"
}, },
{ {
"type": "PATTERN", "type": "STRING",
"value": "[0-9]" "value": "-"
},
{
"type": "PATTERN",
"value": "(-|_)"
} }
] ]
} }

View File

@@ -130,10 +130,6 @@
{ {
"type": "block", "type": "block",
"named": true "named": true
},
{
"type": "object",
"named": true
} }
] ]
} }
@@ -188,6 +184,10 @@
{ {
"type": "body", "type": "body",
"named": true "named": true
},
{
"type": "object",
"named": true
} }
] ]
} }

25301
src/parser.c

File diff suppressed because it is too large Load Diff