grammar: remove shim again

Signed-off-by: Michael Hoffmann <mhoffm@posteo.de>
This commit is contained in:
Michael Hoffmann
2023-07-25 19:04:47 +02:00
parent becebebd35
commit 636dbe7030
12 changed files with 31068 additions and 31223 deletions

View File

@@ -1,10 +1,9 @@
# Changelog # Changelog
## 1.1.0 - not yet released ## 1.1.0 - 2023-07-25
feature feature
* add dialects so we can have different queries in `nvim-treesitter` * add dialects so we can have different queries in `nvim-treesitter`
* fix structure of comments in block bodies
fix: fix:
* fix ci acceptance workflow * fix ci acceptance workflow

View File

@@ -2,21 +2,12 @@
"name": "terraform", "name": "terraform",
"rules": { "rules": {
"config_file": { "config_file": {
"type": "CHOICE",
"members": [
{
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "body" "name": "body"
}, },
{
"type": "SYMBOL",
"name": "object"
}
]
},
{ {
"type": "BLANK" "type": "BLANK"
} }
@@ -25,25 +16,6 @@
"body": { "body": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "_shim"
},
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_shim"
},
{
"type": "BLANK"
}
]
},
{ {
"type": "REPEAT1", "type": "REPEAT1",
"content": { "content": {
@@ -61,8 +33,6 @@
} }
} }
] ]
}
]
}, },
"attribute": { "attribute": {
"type": "SEQ", "type": "SEQ",
@@ -1805,10 +1775,6 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "heredoc_identifier" "name": "heredoc_identifier"
},
{
"type": "SYMBOL",
"name": "_shim"
} }
], ],
"inline": [], "inline": [],

View File

@@ -139,7 +139,7 @@
"fields": {}, "fields": {},
"children": { "children": {
"multiple": true, "multiple": true,
"required": false, "required": true,
"types": [ "types": [
{ {
"type": "attribute", "type": "attribute",
@@ -202,10 +202,6 @@
{ {
"type": "body", "type": "body",
"named": true "named": true
},
{
"type": "object",
"named": true
} }
] ]
} }

File diff suppressed because it is too large Load Diff

View File

@@ -20,7 +20,6 @@ enum TokenType {
TEMPLATE_DIRECTIVE_START, TEMPLATE_DIRECTIVE_START,
TEMPLATE_DIRECTIVE_END, TEMPLATE_DIRECTIVE_END,
HEREDOC_IDENTIFIER, HEREDOC_IDENTIFIER,
SHIM,
}; };
enum ContextType { enum ContextType {
@@ -97,13 +96,6 @@ public:
if (lexer->lookahead == '\0') { if (lexer->lookahead == '\0') {
return false; return false;
} }
if (valid_symbols[SHIM]) {
lexer->mark_end(lexer);
while(skip_comment(lexer));
if (lexer->lookahead != '}') {
return accept_inplace(lexer, SHIM);
}
}
// manage quoted context // manage quoted context
if (valid_symbols[QUOTED_TEMPLATE_START] && !in_quoted_context() && if (valid_symbols[QUOTED_TEMPLATE_START] && !in_quoted_context() &&
lexer->lookahead == '"') { lexer->lookahead == '"') {

View File

@@ -24,20 +24,15 @@ module.exports = function make_grammar(dialect) {
$.template_directive_start, $.template_directive_start,
$.template_directive_end, $.template_directive_end,
$.heredoc_identifier, $.heredoc_identifier,
$._shim,
], ],
extras: ($) => [$.comment, $._whitespace], extras: ($) => [$.comment, $._whitespace],
rules: { rules: {
// also allow objects to handle .tfvars in json format // also allow objects to handle .tfvars in json format
config_file: ($) => optional($.body), config_file: ($) => optional(choice($.body, $.object)),
body: ($) => body: ($) => choice(repeat1(choice($.attribute, $.block))),
choice(
$._shim,
seq(optional($._shim), repeat1(choice($.attribute, $.block)))
),
attribute: ($) => seq($.identifier, "=", $.expression), attribute: ($) => seq($.identifier, "=", $.expression),
@@ -47,7 +42,7 @@ module.exports = function make_grammar(dialect) {
repeat(choice($.string_lit, $.identifier)), repeat(choice($.string_lit, $.identifier)),
$.block_start, $.block_start,
optional($.body), optional($.body),
$.block_end $.block_end,
), ),
block_start: ($) => "{", block_start: ($) => "{",
@@ -57,8 +52,8 @@ module.exports = function make_grammar(dialect) {
token( token(
seq( seq(
choice(/\p{ID_Start}/, "_"), choice(/\p{ID_Start}/, "_"),
repeat(choice(/\p{ID_Continue}/, "-")) repeat(choice(/\p{ID_Continue}/, "-")),
) ),
), ),
expression: ($) => prec.right(choice($._expr_term, $.conditional)), expression: ($) => prec.right(choice($._expr_term, $.conditional)),
@@ -78,7 +73,7 @@ module.exports = function make_grammar(dialect) {
seq($._expr_term, $.index), seq($._expr_term, $.index),
seq($._expr_term, $.get_attr), seq($._expr_term, $.get_attr),
seq($._expr_term, $.splat), seq($._expr_term, $.splat),
seq("(", $.expression, ")") seq("(", $.expression, ")"),
), ),
literal_value: ($) => literal_value: ($) =>
@@ -97,8 +92,8 @@ module.exports = function make_grammar(dialect) {
seq( seq(
$.quoted_template_start, $.quoted_template_start,
optional($.template_literal), optional($.template_literal),
$.quoted_template_end $.quoted_template_end,
) ),
), ),
collection_value: ($) => choice($.tuple, $.object), collection_value: ($) => choice($.tuple, $.object),
@@ -114,7 +109,7 @@ module.exports = function make_grammar(dialect) {
seq( seq(
$.expression, $.expression,
repeat(seq($._comma, $.expression)), repeat(seq($._comma, $.expression)),
optional($._comma) optional($._comma),
), ),
object: ($) => object: ($) =>
@@ -127,14 +122,14 @@ module.exports = function make_grammar(dialect) {
seq( seq(
$.object_elem, $.object_elem,
repeat(seq(optional($._comma), $.object_elem)), repeat(seq(optional($._comma), $.object_elem)),
optional($._comma) optional($._comma),
), ),
object_elem: ($) => object_elem: ($) =>
seq( seq(
field("key", $.expression), field("key", $.expression),
choice("=", ":"), choice("=", ":"),
field("val", $.expression) field("val", $.expression),
), ),
index: ($) => choice($.new_index, $.legacy_index), index: ($) => choice($.new_index, $.legacy_index),
@@ -160,7 +155,7 @@ module.exports = function make_grammar(dialect) {
$.for_intro, $.for_intro,
$.expression, $.expression,
optional($.for_cond), optional($.for_cond),
$.tuple_end $.tuple_end,
), ),
for_object_expr: ($) => for_object_expr: ($) =>
@@ -172,7 +167,7 @@ module.exports = function make_grammar(dialect) {
$.expression, $.expression,
optional($.ellipsis), optional($.ellipsis),
optional($.for_cond), optional($.for_cond),
$.object_end $.object_end,
), ),
for_intro: ($) => for_intro: ($) =>
@@ -182,7 +177,7 @@ module.exports = function make_grammar(dialect) {
optional(seq(",", $.identifier)), optional(seq(",", $.identifier)),
"in", "in",
$.expression, $.expression,
":" ":",
), ),
for_cond: ($) => seq("if", $.expression), for_cond: ($) => seq("if", $.expression),
@@ -194,7 +189,7 @@ module.exports = function make_grammar(dialect) {
$.identifier, $.identifier,
$._function_call_start, $._function_call_start,
optional($.function_arguments), optional($.function_arguments),
$._function_call_end $._function_call_end,
), ),
_function_call_start: ($) => "(", _function_call_start: ($) => "(",
@@ -205,8 +200,8 @@ module.exports = function make_grammar(dialect) {
seq( seq(
$.expression, $.expression,
repeat(seq($._comma, $.expression)), repeat(seq($._comma, $.expression)),
optional(choice($._comma, $.ellipsis)) optional(choice($._comma, $.ellipsis)),
) ),
), ),
ellipsis: ($) => token("..."), ellipsis: ($) => token("..."),
@@ -231,8 +226,8 @@ module.exports = function make_grammar(dialect) {
return choice( return choice(
...table.map(([precedence, operator]) => ...table.map(([precedence, operator]) =>
prec.left(precedence, seq($._expr_term, operator, $._expr_term)) prec.left(precedence, seq($._expr_term, operator, $._expr_term)),
) ),
); );
}, },
@@ -244,8 +239,8 @@ module.exports = function make_grammar(dialect) {
seq( seq(
$.quoted_template_start, $.quoted_template_start,
optional($._template), optional($._template),
$.quoted_template_end $.quoted_template_end,
) ),
), ),
heredoc_template: ($) => heredoc_template: ($) =>
@@ -253,7 +248,7 @@ module.exports = function make_grammar(dialect) {
$.heredoc_start, $.heredoc_start,
$.heredoc_identifier, $.heredoc_identifier,
optional($._template), optional($._template),
$.heredoc_identifier $.heredoc_identifier,
), ),
heredoc_start: ($) => choice("<<", "<<-"), heredoc_start: ($) => choice("<<", "<<-"),
@@ -265,8 +260,8 @@ module.exports = function make_grammar(dialect) {
choice( choice(
$.template_interpolation, $.template_interpolation,
$.template_directive, $.template_directive,
$.template_literal $.template_literal,
) ),
), ),
template_literal: ($) => prec.right(repeat1($._template_literal_chunk)), template_literal: ($) => prec.right(repeat1($._template_literal_chunk)),
@@ -277,7 +272,7 @@ module.exports = function make_grammar(dialect) {
optional($.strip_marker), optional($.strip_marker),
optional($.expression), optional($.expression),
optional($.strip_marker), optional($.strip_marker),
$.template_interpolation_end $.template_interpolation_end,
), ),
template_directive: ($) => choice($.template_for, $.template_if), template_directive: ($) => choice($.template_for, $.template_if),
@@ -295,7 +290,7 @@ module.exports = function make_grammar(dialect) {
"in", "in",
$.expression, $.expression,
optional($.strip_marker), optional($.strip_marker),
$.template_directive_end $.template_directive_end,
), ),
template_for_end: ($) => template_for_end: ($) =>
@@ -304,7 +299,7 @@ module.exports = function make_grammar(dialect) {
optional($.strip_marker), optional($.strip_marker),
"endfor", "endfor",
optional($.strip_marker), optional($.strip_marker),
$.template_directive_end $.template_directive_end,
), ),
template_if: ($) => template_if: ($) =>
@@ -312,7 +307,7 @@ module.exports = function make_grammar(dialect) {
$.template_if_intro, $.template_if_intro,
optional($._template), optional($._template),
optional(seq($.template_else_intro, optional($._template))), optional(seq($.template_else_intro, optional($._template))),
$.template_if_end $.template_if_end,
), ),
template_if_intro: ($) => template_if_intro: ($) =>
@@ -322,7 +317,7 @@ module.exports = function make_grammar(dialect) {
"if", "if",
$.expression, $.expression,
optional($.strip_marker), optional($.strip_marker),
$.template_directive_end $.template_directive_end,
), ),
template_else_intro: ($) => template_else_intro: ($) =>
@@ -331,7 +326,7 @@ module.exports = function make_grammar(dialect) {
optional($.strip_marker), optional($.strip_marker),
"else", "else",
optional($.strip_marker), optional($.strip_marker),
$.template_directive_end $.template_directive_end,
), ),
template_if_end: ($) => template_if_end: ($) =>
@@ -340,7 +335,7 @@ module.exports = function make_grammar(dialect) {
optional($.strip_marker), optional($.strip_marker),
"endif", "endif",
optional($.strip_marker), optional($.strip_marker),
$.template_directive_end $.template_directive_end,
), ),
// 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
@@ -349,8 +344,8 @@ module.exports = function make_grammar(dialect) {
choice( choice(
seq("#", /.*/), seq("#", /.*/),
seq("//", /.*/), seq("//", /.*/),
seq("/*", /[^*]*\*+([^/*][^*]*\*+)*/, "/") seq("/*", /[^*]*\*+([^/*][^*]*\*+)*/, "/"),
) ),
), ),
_whitespace: ($) => token(/\s/), _whitespace: ($) => token(/\s/),

View File

@@ -2,12 +2,21 @@
"name": "hcl", "name": "hcl",
"rules": { "rules": {
"config_file": { "config_file": {
"type": "CHOICE",
"members": [
{
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "body" "name": "body"
}, },
{
"type": "SYMBOL",
"name": "object"
}
]
},
{ {
"type": "BLANK" "type": "BLANK"
} }
@@ -16,25 +25,6 @@
"body": { "body": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "_shim"
},
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_shim"
},
{
"type": "BLANK"
}
]
},
{ {
"type": "REPEAT1", "type": "REPEAT1",
"content": { "content": {
@@ -52,8 +42,6 @@
} }
} }
] ]
}
]
}, },
"attribute": { "attribute": {
"type": "SEQ", "type": "SEQ",
@@ -1796,10 +1784,6 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "heredoc_identifier" "name": "heredoc_identifier"
},
{
"type": "SYMBOL",
"name": "_shim"
} }
], ],
"inline": [], "inline": [],

View File

@@ -139,7 +139,7 @@
"fields": {}, "fields": {},
"children": { "children": {
"multiple": true, "multiple": true,
"required": false, "required": true,
"types": [ "types": [
{ {
"type": "attribute", "type": "attribute",
@@ -202,6 +202,10 @@
{ {
"type": "body", "type": "body",
"named": true "named": true
},
{
"type": "object",
"named": true
} }
] ]
} }

29836
src/parser.c

File diff suppressed because it is too large Load Diff

View File

@@ -20,7 +20,6 @@ enum TokenType {
TEMPLATE_DIRECTIVE_START, TEMPLATE_DIRECTIVE_START,
TEMPLATE_DIRECTIVE_END, TEMPLATE_DIRECTIVE_END,
HEREDOC_IDENTIFIER, HEREDOC_IDENTIFIER,
SHIM,
}; };
enum ContextType { enum ContextType {
@@ -97,13 +96,6 @@ public:
if (lexer->lookahead == '\0') { if (lexer->lookahead == '\0') {
return false; return false;
} }
if (valid_symbols[SHIM]) {
lexer->mark_end(lexer);
while(skip_comment(lexer));
if (lexer->lookahead != '}') {
return accept_inplace(lexer, SHIM);
}
}
// manage quoted context // manage quoted context
if (valid_symbols[QUOTED_TEMPLATE_START] && !in_quoted_context() && if (valid_symbols[QUOTED_TEMPLATE_START] && !in_quoted_context() &&
lexer->lookahead == '"') { lexer->lookahead == '"') {

View File

@@ -33,7 +33,6 @@ block {
(block (block
(identifier) (identifier)
(block_start) (block_start)
(body)
(comment) (comment)
(block_end)))) (block_end))))
@@ -55,8 +54,8 @@ block {
(block (block
(identifier) (identifier)
(block_start) (block_start)
(body
(comment) (comment)
(body
(attribute (attribute
(identifier) (identifier)
(expression (expression
@@ -80,8 +79,8 @@ block {
(block (block
(identifier) (identifier)
(block_start) (block_start)
(body
(comment) (comment)
(body
(attribute (attribute
(identifier) (identifier)
(expression (expression
@@ -153,5 +152,4 @@ comment at end of file
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(config_file (config_file
(body)
(comment)) (comment))

27
test/corpus/json.txt Normal file
View File

@@ -0,0 +1,27 @@
================================================================================
top level object
================================================================================
{
"foo": "bar"
}
--------------------------------------------------------------------------------
(config_file
(object
(object_start)
(object_elem
(expression
(literal_value
(string_lit
(quoted_template_start)
(template_literal)
(quoted_template_end))))
(expression
(literal_value
(string_lit
(quoted_template_start)
(template_literal)
(quoted_template_end)))))
(object_end)))