fix string literals; trim trailing whitespace

This commit is contained in:
mhoffm
2021-06-14 23:33:28 +02:00
parent 9606c5fa49
commit 01a4a981da
12 changed files with 3239 additions and 3142 deletions

View File

@@ -27,10 +27,6 @@ foo = "prefix-${func(\"bar\"}"
To make parsing a little easier, this parser only checks for valid escape sequences and template chars. To make parsing a little easier, this parser only checks for valid escape sequences and template chars.
When using this parser one would have to take the content of a template interpolation, unescape it and parse it again to get the syntax tree. The same applies to template directives. When using this parser one would have to take the content of a template interpolation, unescape it and parse it again to get the syntax tree. The same applies to template directives.
## String Literals
String literals are parsed as quoted templates. The calling application should check if the node contains any template interpolations or directives.
## Todo ## Todo
* [ ] use [Unicode® Standard Annex #31](https://www.unicode.org/reports/tr31/) (augmented with '-') for identifiers * [ ] use [Unicode® Standard Annex #31](https://www.unicode.org/reports/tr31/) (augmented with '-') for identifiers

View File

@@ -24,6 +24,7 @@ module.exports = grammar({
[$.attr_splat], [$.attr_splat],
[$.full_splat], [$.full_splat],
[$.conditional], [$.conditional],
[$.string_lit, $.quoted_template],
], ],
externals: $ => [ externals: $ => [
@@ -233,8 +234,14 @@ module.exports = grammar({
// $.heredoc_template, // $.heredoc_template,
), ),
// application should check that no template interpolation is contained string_lit: $ => seq(
string_lit: $ => $.quoted_template, '"',
repeat(choice(
$._template_char,
$.escape_sequence,
)),
'"',
),
quoted_template: $ => seq( quoted_template: $ => seq(
'"', '"',

View File

@@ -1015,8 +1015,33 @@
] ]
}, },
"string_lit": { "string_lit": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\""
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL", "type": "SYMBOL",
"name": "quoted_template" "name": "_template_char"
},
{
"type": "SYMBOL",
"name": "escape_sequence"
}
]
}
},
{
"type": "STRING",
"value": "\""
}
]
}, },
"quoted_template": { "quoted_template": {
"type": "SEQ", "type": "SEQ",
@@ -1176,6 +1201,10 @@
], ],
[ [
"conditional" "conditional"
],
[
"string_lit",
"quoted_template"
] ]
], ],
"precedences": [], "precedences": [],

View File

@@ -556,11 +556,11 @@
"named": true, "named": true,
"fields": {}, "fields": {},
"children": { "children": {
"multiple": false, "multiple": true,
"required": true, "required": false,
"types": [ "types": [
{ {
"type": "quoted_template", "type": "escape_sequence",
"named": true "named": true
} }
] ]

File diff suppressed because it is too large Load Diff

View File

@@ -286,3 +286,18 @@ foo = "$${ var.bar }"
(attribute (attribute
(identifier) (identifier)
(expression (expr_term (template_expr (quoted_template (escape_sequence)))))))) (expression (expr_term (template_expr (quoted_template (escape_sequence))))))))
==================
proper quoted template in string literal position errors
==================
resource "${var.bar}" {
}
---
(config_file
(body
(block
(identifier)
(string_lit (ERROR)))))