add conditionals

This commit is contained in:
mhoffm
2021-06-12 21:47:30 +02:00
parent 0b8859a794
commit 22b921f405
5 changed files with 2899 additions and 2407 deletions

View File

@@ -9,3 +9,9 @@ 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'`.
## todo
* use [Unicode® Standard Annex #31](https://www.unicode.org/reports/tr31/) (augmented with '-')for identifiers
* add [operations](https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#operations)
* add [template expressions](https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#template-expressions) and express string literals using them

View File

@@ -11,6 +11,7 @@ module.exports = grammar({
[$.object_elem, $.variable_expr], [$.object_elem, $.variable_expr],
[$.attr_splat], [$.attr_splat],
[$.full_splat], [$.full_splat],
[$.conditional],
], ],
extras: $ => [ extras: $ => [
@@ -51,7 +52,7 @@ module.exports = grammar({
expression: $ => choice( expression: $ => choice(
$.expr_term, $.expr_term,
//$.operation, //$.operation,
//$.conditional, $.conditional,
), ),
expr_term: $ => choice( expr_term: $ => choice(
@@ -172,6 +173,14 @@ module.exports = grammar({
ellipsis: $ => token('...'), ellipsis: $ => token('...'),
conditional: $ => seq(
$.expression,
'?',
$.expression,
':',
$.expression,
),
// http://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment/36328890#36328890 // http://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment/36328890#36328890
comment: $ => token(choice( comment: $ => token(choice(
seq('#', /.*/), seq('#', /.*/),

View File

@@ -134,6 +134,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "expr_term" "name": "expr_term"
},
{
"type": "SYMBOL",
"name": "conditional"
} }
] ]
}, },
@@ -776,6 +780,31 @@
"value": "..." "value": "..."
} }
}, },
"conditional": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": "?"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": ":"
},
{
"type": "SYMBOL",
"name": "expression"
}
]
},
"comment": { "comment": {
"type": "TOKEN", "type": "TOKEN",
"content": { "content": {
@@ -816,7 +845,7 @@
}, },
{ {
"type": "PATTERN", "type": "PATTERN",
"value": "[^*]*\\*+([^\\/*][^*]*\\*+)*" "value": "[^*]*\\*+([^/*][^*]*\\*+)*"
}, },
{ {
"type": "STRING", "type": "STRING",
@@ -851,6 +880,9 @@
], ],
[ [
"full_splat" "full_splat"
],
[
"conditional"
] ]
], ],
"precedences": [], "precedences": [],

View File

@@ -117,6 +117,21 @@
] ]
} }
}, },
{
"type": "conditional",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
}
]
}
},
{ {
"type": "config_file", "type": "config_file",
"named": true, "named": true,
@@ -191,6 +206,10 @@
"multiple": false, "multiple": false,
"required": true, "required": true,
"types": [ "types": [
{
"type": "conditional",
"named": true
},
{ {
"type": "expr_term", "type": "expr_term",
"named": true "named": true
@@ -546,6 +565,10 @@
"type": "=>", "type": "=>",
"named": false "named": false
}, },
{
"type": "?",
"named": false
},
{ {
"type": "[", "type": "[",
"named": false "named": false

File diff suppressed because it is too large Load Diff