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

@@ -14,7 +14,7 @@ It is recommended to use `nix` to fulfill all development dependencies. To activ
## Running Tests ## Running Tests
To run tests simply run `nix-shell --run 'tree-sitter test'`. To run tests simply run `nix-shell --run 'tree-sitter test'`.
## Quoted Template Expressions ## Quoted Template Expressions
@@ -24,17 +24,13 @@ In principle it is allowed to contain arbitary expressions in quoted template in
foo = "prefix-${func(\"bar\"}" 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
* [ ] 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
* [ ] add quoted template directives * [ ] add quoted template directives

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": "SYMBOL", "type": "SEQ",
"name": "quoted_template" "members": [
{
"type": "STRING",
"value": "\""
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"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

@@ -6,16 +6,16 @@ foo = [1, 2, "foo"]
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(collection_value (collection_value
(tuple (tuple
(expression (expr_term (literal_value (numeric_lit)))) (expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (literal_value (numeric_lit)))) (expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (template_expr (quoted_template))))))))))) (expression (expr_term (template_expr (quoted_template)))))))))))
================== ==================
@@ -26,18 +26,18 @@ foo = {1: 2, "foo"="bar"}
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(collection_value (collection_value
(object (object
(object_elem (object_elem
(expression (expr_term (literal_value (numeric_lit)))) (expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (literal_value (numeric_lit))))) (expression (expr_term (literal_value (numeric_lit)))))
(object_elem (object_elem
(expression (expr_term (template_expr (quoted_template)))) (expression (expr_term (template_expr (quoted_template))))
(expression (expr_term (template_expr (quoted_template)))))))))))) (expression (expr_term (template_expr (quoted_template))))))))))))

View File

@@ -6,13 +6,13 @@ foo = predicate() ? 1: 2
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(conditional (conditional
(expression (expr_term (function_call (identifier)))) (expression (expr_term (function_call (identifier))))
(expression (expr_term (literal_value (numeric_lit)))) (expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (literal_value (numeric_lit))))))))) (expression (expr_term (literal_value (numeric_lit)))))))))

View File

@@ -6,23 +6,23 @@ foo = [for v in ["a", "b"]: v]
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(for_expr (for_expr
(for_tuple_expr (for_tuple_expr
(for_intro (for_intro
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(collection_value (collection_value
(tuple (tuple
(expression (expr_term (template_expr (quoted_template)))) (expression (expr_term (template_expr (quoted_template))))
(expression (expr_term (template_expr (quoted_template))))))))) (expression (expr_term (template_expr (quoted_template)))))))))
(expression (expr_term (variable_expr (identifier))))))))))) (expression (expr_term (variable_expr (identifier)))))))))))
================== ==================
for tuple expression with index for tuple expression with index
@@ -32,24 +32,24 @@ foo = [for i, v in ["a", "b"]: i]
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(for_expr (for_expr
(for_tuple_expr (for_tuple_expr
(for_intro (for_intro
(identifier) (identifier)
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(collection_value (collection_value
(tuple (tuple
(expression (expr_term (template_expr (quoted_template)))) (expression (expr_term (template_expr (quoted_template))))
(expression (expr_term (template_expr (quoted_template))))))))) (expression (expr_term (template_expr (quoted_template)))))))))
(expression (expr_term (variable_expr (identifier))))))))))) (expression (expr_term (variable_expr (identifier)))))))))))
================== ==================
for tuple expression with predicate for tuple expression with predicate
@@ -59,35 +59,35 @@ foo = [for i, v in ["a", "b", "c"]: v if pred(i)]
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(for_expr (for_expr
(for_tuple_expr (for_tuple_expr
(for_intro (for_intro
(identifier) (identifier)
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(collection_value (collection_value
(tuple (tuple
(expression (expr_term (template_expr (quoted_template)))) (expression (expr_term (template_expr (quoted_template))))
(expression (expr_term (template_expr (quoted_template)))) (expression (expr_term (template_expr (quoted_template))))
(expression (expr_term (template_expr (quoted_template))))))))) (expression (expr_term (template_expr (quoted_template)))))))))
(expression (expr_term (variable_expr (identifier)))) (expression (expr_term (variable_expr (identifier))))
(for_cond (for_cond
(expression (expression
(expr_term (expr_term
(function_call (function_call
(identifier) (identifier)
(function_arguments (expression (expr_term (variable_expr (identifier)))))))))))))))) (function_arguments (expression (expr_term (variable_expr (identifier))))))))))))))))
================== ==================
for object expression for object expression
================== ==================
foo = {for i, v in ["a", "b"]: v => i} foo = {for i, v in ["a", "b"]: v => i}
@@ -96,23 +96,23 @@ foo = {for i, v in ["a", "b"]: v => i}
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(for_expr (for_expr
(for_object_expr (for_object_expr
(for_intro (for_intro
(identifier) (identifier)
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(collection_value (collection_value
(tuple (tuple
(expression (expr_term (template_expr (quoted_template)))) (expression (expr_term (template_expr (quoted_template))))
(expression (expr_term (template_expr (quoted_template))))))))) (expression (expr_term (template_expr (quoted_template)))))))))
(expression (expr_term (variable_expr (identifier)))) (expression (expr_term (variable_expr (identifier))))
(expression (expr_term (variable_expr (identifier))))))))))) (expression (expr_term (variable_expr (identifier)))))))))))
================== ==================
for object expression 2 for object expression 2
@@ -124,20 +124,20 @@ foo = {for i, v in ["a", "b"]: v => i...}
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(for_expr (for_expr
(for_object_expr (for_object_expr
(for_intro (for_intro
(identifier) (identifier)
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(collection_value (collection_value
(tuple (tuple
(expression (expr_term (template_expr (quoted_template)))) (expression (expr_term (template_expr (quoted_template))))
(expression (expr_term (template_expr (quoted_template))))))))) (expression (expr_term (template_expr (quoted_template)))))))))
(expression (expr_term (variable_expr (identifier)))) (expression (expr_term (variable_expr (identifier))))
(expression (expr_term (variable_expr (identifier)))) (ellipsis)))))))) (expression (expr_term (variable_expr (identifier)))) (ellipsis))))))))

View File

@@ -20,16 +20,16 @@ foo = bar("foo")
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(function_call (function_call
(identifier) (identifier)
(function_arguments (function_arguments
(expression (expr_term (template_expr (quoted_template))))))))))) (expression (expr_term (template_expr (quoted_template)))))))))))
================== ==================
variadic function call variadic function call
@@ -39,16 +39,16 @@ foo = bar(x...)
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(function_call (function_call
(identifier) (identifier)
(function_arguments (function_arguments
(expression (expr_term (variable_expr (identifier)))) (ellipsis)))))))) (expression (expr_term (variable_expr (identifier)))) (ellipsis))))))))
================== ==================
multiline function call multiline function call
@@ -62,16 +62,16 @@ foo = bar(
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(function_call (function_call
(identifier) (identifier)
(function_arguments (function_arguments
(expression (expr_term (template_expr (quoted_template)))) (expression (expr_term (template_expr (quoted_template))))
(expression (expr_term (template_expr (quoted_template)))) (expression (expr_term (template_expr (quoted_template))))
(expression (expr_term (template_expr (quoted_template))))))))))) (expression (expr_term (template_expr (quoted_template)))))))))))

View File

@@ -6,13 +6,13 @@ foo = -3
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(operation (operation
(unary_operation (expr_term (literal_value (numeric_lit))))))))) (unary_operation (expr_term (literal_value (numeric_lit)))))))))
================== ==================
unary operator ! unary operator !
@@ -22,13 +22,13 @@ foo = !true
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(operation (operation
(unary_operation (expr_term (literal_value (bool_lit))))))))) (unary_operation (expr_term (literal_value (bool_lit)))))))))
================== ==================
binary operators + binary operators +
@@ -38,13 +38,13 @@ foo = 1+2
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(operation (operation
(binary_operation (binary_operation
(expr_term (literal_value (numeric_lit))) (expr_term (literal_value (numeric_lit)))
(expr_term (literal_value (numeric_lit))))))))) (expr_term (literal_value (numeric_lit)))))))))

View File

@@ -6,16 +6,16 @@ foo = bar.baz
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(expr_term (expr_term
(variable_expr (variable_expr
(identifier))) (identifier)))
(get_attr (get_attr
(identifier))))))) (identifier)))))))
================== ==================
@@ -26,15 +26,15 @@ foo = bar[1]
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(expr_term (expr_term
(variable_expr (variable_expr
(identifier))) (identifier)))
(index (index
(expression (expression
(expr_term (expr_term
@@ -49,19 +49,19 @@ foo = bar.*.foo
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(expr_term (expr_term
(variable_expr (variable_expr
(identifier))) (identifier)))
(splat (splat
(attr_splat (attr_splat
(get_attr (get_attr
(identifier))))))))) (identifier)))))))))
================== ==================
full splat full splat
@@ -71,16 +71,16 @@ foo = bar[*].foo
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expression
(expr_term (expr_term
(expr_term (expr_term
(variable_expr (variable_expr
(identifier))) (identifier)))
(splat (splat
(full_splat (full_splat
(get_attr (get_attr
(identifier))))))))) (identifier)))))))))

View File

@@ -90,11 +90,11 @@ foo = "bar\pbaz"
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expr_term (template_expr (quoted_template (ERROR (UNEXPECTED '\'))))))))) (expression (expr_term (template_expr (quoted_template (ERROR (UNEXPECTED '\')))))))))
================== ==================
string bad escape sequence 2 string bad escape sequence 2
@@ -104,11 +104,11 @@ foo = "bar\uZZ"
--- ---
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expr_term (template_expr (quoted_template (ERROR (UNEXPECTED '\'))))))))) (expression (expr_term (template_expr (quoted_template (ERROR (UNEXPECTED '\')))))))))
================== ==================
string literal multi line error string literal multi line error
@@ -221,10 +221,10 @@ foo = "%\n\t"
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expr_term (template_expr (quoted_template (expression (expr_term (template_expr (quoted_template
(escape_sequence) (escape_sequence)
(escape_sequence)))))))) (escape_sequence))))))))
================== ==================
string literal template chars but no template 4 string literal template chars but no template 4
@@ -237,10 +237,10 @@ foo = "%%\n\t"
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expr_term (template_expr (quoted_template (expression (expr_term (template_expr (quoted_template
(escape_sequence) (escape_sequence)
(escape_sequence)))))))) (escape_sequence))))))))
================== ==================
string literal template chars but no template 5 string literal template chars but no template 5
@@ -253,8 +253,8 @@ foo = "$$"
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expr_term (template_expr (quoted_template))))))) (expression (expr_term (template_expr (quoted_template)))))))
================== ==================
string literal template chars but no template 6 string literal template chars but no template 6
@@ -267,11 +267,11 @@ foo = "%%{\n\t"
(config_file (config_file
(body (body
(attribute (attribute
(identifier) (identifier)
(expression (expr_term (template_expr (quoted_template (expression (expr_term (template_expr (quoted_template
(escape_sequence) (escape_sequence)
(escape_sequence) (escape_sequence)
(escape_sequence)))))))) (escape_sequence))))))))
================== ==================
string literal escaped template string literal escaped template
@@ -284,5 +284,20 @@ foo = "$${ var.bar }"
(config_file (config_file
(body (body
(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)))))