fix operations; add one line blocks

This commit is contained in:
mhoffm
2021-06-19 21:01:03 +02:00
parent d61fefced0
commit b1c3109853
5 changed files with 10946 additions and 8990 deletions

View File

@@ -15,6 +15,8 @@ module.exports = grammar({
conflicts: $ => [ conflicts: $ => [
// string literals are just quoted template without template stuff // string literals are just quoted template without template stuff
[$.string_lit, $.quoted_template], [$.string_lit, $.quoted_template],
// empty block may be both
[$.block, $.one_line_block],
], ],
externals: $ => [ externals: $ => [
@@ -39,6 +41,7 @@ module.exports = grammar({
choice( choice(
$.attribute, $.attribute,
$.block, $.block,
$.one_line_block,
$._newlines, $._newlines,
), ),
)), )),
@@ -60,6 +63,15 @@ module.exports = grammar({
optional($._newlines), optional($._newlines),
)), )),
one_line_block: $ => seq(
$.identifier,
repeat(choice($.string_lit, $.identifier)),
$._block_start,
optional(seq($.identifier, '=', $.expression)),
$._block_end,
$._newlines,
),
_block_start: $ => '{', _block_start: $ => '{',
_block_end: $ => '}', _block_end: $ => '}',
@@ -74,6 +86,9 @@ module.exports = grammar({
$.conditional, $.conditional,
), ),
// operations are documented as expressions, but our real world samples
// contain instances of operations without parentheses. think for example:
// x = a == "" && b != ""
_expr_term: $ => choice( _expr_term: $ => choice(
$.literal_value, $.literal_value,
$.template_expr, $.template_expr,
@@ -181,22 +196,28 @@ module.exports = grammar({
for_expr: $ => choice($.for_tuple_expr, $.for_object_expr), for_expr: $ => choice($.for_tuple_expr, $.for_object_expr),
// newlines
for_tuple_expr: $ => seq( for_tuple_expr: $ => seq(
$._tuple_start, $._tuple_start,
optional($._newlines),
$.for_intro, $.for_intro,
$.expression, $.expression,
optional($.for_cond), optional($.for_cond),
optional($._newlines),
$._tuple_end, $._tuple_end,
), ),
// newlines
for_object_expr: $ => seq( for_object_expr: $ => seq(
$._object_start, $._object_start,
optional($._newlines),
$.for_intro, $.for_intro,
$.expression, $.expression,
'=>', '=>',
$.expression, $.expression,
optional($.ellipsis), optional($.ellipsis),
optional($.for_cond), optional($.for_cond),
optional($._newlines),
$._object_end, $._object_end,
), ),

View File

@@ -21,6 +21,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "block" "name": "block"
}, },
{
"type": "SYMBOL",
"name": "one_line_block"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_newlines" "name": "_newlines"
@@ -127,6 +131,68 @@
] ]
} }
}, },
"one_line_block": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "string_lit"
},
{
"type": "SYMBOL",
"name": "identifier"
}
]
}
},
{
"type": "SYMBOL",
"name": "_block_start"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": "="
},
{
"type": "SYMBOL",
"name": "expression"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_block_end"
},
{
"type": "SYMBOL",
"name": "_newlines"
}
]
},
"_block_start": { "_block_start": {
"type": "STRING", "type": "STRING",
"value": "{" "value": "{"
@@ -763,6 +829,18 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "_tuple_start" "name": "_tuple_start"
}, },
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_newlines"
},
{
"type": "BLANK"
}
]
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "for_intro" "name": "for_intro"
@@ -783,6 +861,18 @@
} }
] ]
}, },
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_newlines"
},
{
"type": "BLANK"
}
]
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_tuple_end" "name": "_tuple_end"
@@ -796,6 +886,18 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "_object_start" "name": "_object_start"
}, },
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_newlines"
},
{
"type": "BLANK"
}
]
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "for_intro" "name": "for_intro"
@@ -836,6 +938,18 @@
} }
] ]
}, },
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_newlines"
},
{
"type": "BLANK"
}
]
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_object_end" "name": "_object_end"
@@ -1487,6 +1601,10 @@
[ [
"string_lit", "string_lit",
"quoted_template" "quoted_template"
],
[
"block",
"one_line_block"
] ]
], ],
"precedences": [], "precedences": [],

View File

@@ -126,6 +126,10 @@
{ {
"type": "block", "type": "block",
"named": true "named": true
},
{
"type": "one_line_block",
"named": true
} }
] ]
} }
@@ -514,6 +518,29 @@
] ]
} }
}, },
{
"type": "one_line_block",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "string_lit",
"named": true
}
]
}
},
{ {
"type": "operation", "type": "operation",
"named": true, "named": true,

19741
src/parser.c

File diff suppressed because it is too large Load Diff

View File

@@ -110,3 +110,32 @@ block_1 {
(identifier)) (identifier))
(block (block
(identifier)))))) (identifier))))))
================================================================================
one line block
================================================================================
locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
--------------------------------------------------------------------------------
(config_file
(body
(one_line_block
(identifier)
(identifier)
(expression
(function_call
(identifier)
(function_arguments
(expression
(function_call
(identifier)))
(expression
(literal_value
(string_lit
(template_literal))))
(expression
(template_expr
(quoted_template)))))))))