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
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
@@ -24,17 +24,13 @@ In principle it is allowed to contain arbitary expressions in quoted template in
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.
## String Literals
String literals are parsed as quoted templates. The calling application should check if the node contains any template interpolations or directives.
## Todo
* [ ] 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 template interpolations
* [ ] add quoted template directives

View File

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

View File

@@ -1015,8 +1015,33 @@
]
},
"string_lit": {
"type": "SYMBOL",
"name": "quoted_template"
"type": "SEQ",
"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": {
"type": "SEQ",
@@ -1176,6 +1201,10 @@
],
[
"conditional"
],
[
"string_lit",
"quoted_template"
]
],
"precedences": [],

View File

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

File diff suppressed because it is too large Load Diff

View File

@@ -6,16 +6,16 @@ foo = [1, 2, "foo"]
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(collection_value
(tuple
(expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (literal_value (numeric_lit))))
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(collection_value
(tuple
(expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (template_expr (quoted_template)))))))))))
==================
@@ -26,18 +26,18 @@ foo = {1: 2, "foo"="bar"}
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(collection_value
(object
(object_elem
(expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (literal_value (numeric_lit)))))
(object_elem
(expression (expr_term (template_expr (quoted_template))))
(expression (expr_term (template_expr (quoted_template))))))))))))
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(collection_value
(object
(object_elem
(expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (literal_value (numeric_lit)))))
(object_elem
(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
(body
(attribute
(identifier)
(expression
(conditional
(config_file
(body
(attribute
(identifier)
(expression
(conditional
(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
(body
(attribute
(identifier)
(expression
(expr_term
(for_expr
(for_tuple_expr
(for_intro
(identifier)
(expression
(expr_term
(collection_value
(tuple
(expression (expr_term (template_expr (quoted_template))))
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(for_expr
(for_tuple_expr
(for_intro
(identifier)
(expression
(expr_term
(collection_value
(tuple
(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
@@ -32,24 +32,24 @@ foo = [for i, v in ["a", "b"]: i]
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(for_expr
(for_tuple_expr
(for_intro
(identifier)
(identifier)
(expression
(expr_term
(collection_value
(tuple
(expression (expr_term (template_expr (quoted_template))))
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(for_expr
(for_tuple_expr
(for_intro
(identifier)
(identifier)
(expression
(expr_term
(collection_value
(tuple
(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
@@ -59,35 +59,35 @@ foo = [for i, v in ["a", "b", "c"]: v if pred(i)]
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(for_expr
(for_tuple_expr
(for_intro
(identifier)
(identifier)
(expression
(expr_term
(collection_value
(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 (variable_expr (identifier))))
(for_cond
(expression
(expr_term
(function_call
(identifier)
(function_arguments (expression (expr_term (variable_expr (identifier))))))))))))))))
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(for_expr
(for_tuple_expr
(for_intro
(identifier)
(identifier)
(expression
(expr_term
(collection_value
(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 (variable_expr (identifier))))
(for_cond
(expression
(expr_term
(function_call
(identifier)
(function_arguments (expression (expr_term (variable_expr (identifier))))))))))))))))
==================
for object expression
for object expression
==================
foo = {for i, v in ["a", "b"]: v => i}
@@ -96,23 +96,23 @@ foo = {for i, v in ["a", "b"]: v => i}
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(for_expr
(for_object_expr
(for_intro
(attribute
(identifier)
(expression
(expr_term
(for_expr
(for_object_expr
(for_intro
(identifier)
(identifier)
(expression
(expr_term
(collection_value
(tuple
(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)))))))))))
(identifier)
(expression
(expr_term
(collection_value
(tuple
(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 object expression 2
@@ -124,20 +124,20 @@ foo = {for i, v in ["a", "b"]: v => i...}
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(for_expr
(for_object_expr
(for_intro
(attribute
(identifier)
(expression
(expr_term
(for_expr
(for_object_expr
(for_intro
(identifier)
(identifier)
(expression
(expr_term
(collection_value
(tuple
(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)))) (ellipsis))))))))
(identifier)
(expression
(expr_term
(collection_value
(tuple
(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)))) (ellipsis))))))))

View File

@@ -20,16 +20,16 @@ foo = bar("foo")
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(function_call
(identifier)
(function_arguments
(expression (expr_term (template_expr (quoted_template)))))))))))
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(function_call
(identifier)
(function_arguments
(expression (expr_term (template_expr (quoted_template)))))))))))
==================
variadic function call
@@ -39,16 +39,16 @@ foo = bar(x...)
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(function_call
(identifier)
(function_arguments
(expression (expr_term (variable_expr (identifier)))) (ellipsis))))))))
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(function_call
(identifier)
(function_arguments
(expression (expr_term (variable_expr (identifier)))) (ellipsis))))))))
==================
multiline function call
@@ -62,16 +62,16 @@ foo = bar(
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(function_call
(identifier)
(function_arguments
(expression (expr_term (template_expr (quoted_template))))
(expression (expr_term (template_expr (quoted_template))))
(expression (expr_term (template_expr (quoted_template)))))))))))
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(function_call
(identifier)
(function_arguments
(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
(body
(attribute
(identifier)
(expression
(operation
(unary_operation (expr_term (literal_value (numeric_lit)))))))))
(config_file
(body
(attribute
(identifier)
(expression
(operation
(unary_operation (expr_term (literal_value (numeric_lit)))))))))
==================
unary operator !
@@ -22,13 +22,13 @@ foo = !true
---
(config_file
(body
(attribute
(identifier)
(expression
(operation
(unary_operation (expr_term (literal_value (bool_lit)))))))))
(config_file
(body
(attribute
(identifier)
(expression
(operation
(unary_operation (expr_term (literal_value (bool_lit)))))))))
==================
binary operators +
@@ -38,13 +38,13 @@ foo = 1+2
---
(config_file
(body
(attribute
(identifier)
(expression
(operation
(binary_operation
(expr_term (literal_value (numeric_lit)))
(expr_term (literal_value (numeric_lit)))))))))
(config_file
(body
(attribute
(identifier)
(expression
(operation
(binary_operation
(expr_term (literal_value (numeric_lit)))
(expr_term (literal_value (numeric_lit)))))))))

View File

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

View File

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