2025-05-09 10:06:33 +03:00
|
|
|
/// <reference types="tree-sitter-cli/dsl" />
|
|
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
|
|
/** @param {string} dialect */
|
2022-12-03 00:40:51 +01:00
|
|
|
module.exports = function make_grammar(dialect) {
|
2023-04-08 20:13:43 +02:00
|
|
|
const PREC = {
|
|
|
|
|
unary: 7,
|
|
|
|
|
binary_mult: 6,
|
|
|
|
|
binary_add: 5,
|
|
|
|
|
binary_ord: 4,
|
|
|
|
|
binary_comp: 3,
|
|
|
|
|
binary_and: 2,
|
|
|
|
|
binary_or: 1,
|
|
|
|
|
|
|
|
|
|
// if possible prefer string_literals to quoted templates
|
|
|
|
|
string_lit: 2,
|
|
|
|
|
quoted_template: 1,
|
|
|
|
|
};
|
2025-03-30 14:02:12 +02:00
|
|
|
|
2022-12-03 00:40:51 +01:00
|
|
|
return grammar({
|
|
|
|
|
name: dialect,
|
|
|
|
|
|
2023-04-08 20:13:43 +02:00
|
|
|
externals: ($) => [
|
2022-12-03 00:40:51 +01:00
|
|
|
$.quoted_template_start,
|
|
|
|
|
$.quoted_template_end,
|
|
|
|
|
$._template_literal_chunk,
|
|
|
|
|
$.template_interpolation_start,
|
|
|
|
|
$.template_interpolation_end,
|
|
|
|
|
$.template_directive_start,
|
|
|
|
|
$.template_directive_end,
|
|
|
|
|
$.heredoc_identifier,
|
|
|
|
|
],
|
|
|
|
|
|
2023-04-08 20:13:43 +02:00
|
|
|
extras: ($) => [$.comment, $._whitespace],
|
2022-12-03 00:40:51 +01:00
|
|
|
|
|
|
|
|
rules: {
|
|
|
|
|
// also allow objects to handle .tfvars in json format
|
2023-07-25 19:04:47 +02:00
|
|
|
config_file: ($) => optional(choice($.body, $.object)),
|
2023-04-08 20:13:43 +02:00
|
|
|
|
2023-07-25 19:04:47 +02:00
|
|
|
body: ($) => choice(repeat1(choice($.attribute, $.block))),
|
2023-04-08 20:13:43 +02:00
|
|
|
|
|
|
|
|
attribute: ($) => seq($.identifier, "=", $.expression),
|
2022-12-03 00:40:51 +01:00
|
|
|
|
2023-04-08 20:13:43 +02:00
|
|
|
block: ($) =>
|
2023-04-08 15:26:24 +03:00
|
|
|
seq(
|
2023-04-08 20:13:43 +02:00
|
|
|
$.identifier,
|
|
|
|
|
repeat(choice($.string_lit, $.identifier)),
|
|
|
|
|
$.block_start,
|
|
|
|
|
optional($.body),
|
2023-07-25 19:04:47 +02:00
|
|
|
$.block_end,
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
block_start: ($) => "{",
|
|
|
|
|
block_end: ($) => "}",
|
|
|
|
|
|
|
|
|
|
identifier: ($) =>
|
|
|
|
|
token(
|
|
|
|
|
seq(
|
2025-05-09 10:06:33 +03:00
|
|
|
choice(/\p{ID_Start}/u, "_"),
|
|
|
|
|
repeat(choice(/\p{ID_Continue}/u, "-", "::")),
|
2023-07-25 19:04:47 +02:00
|
|
|
),
|
2022-12-03 00:40:51 +01:00
|
|
|
),
|
2023-04-08 20:13:43 +02:00
|
|
|
|
|
|
|
|
expression: ($) => prec.right(choice($._expr_term, $.conditional)),
|
2022-12-03 00:40:51 +01:00
|
|
|
|
|
|
|
|
// operations are documented as expressions, but our real world samples
|
|
|
|
|
// contain instances of operations without parentheses. think for example:
|
|
|
|
|
// x = a == "" && b != ""
|
2023-04-08 20:13:43 +02:00
|
|
|
_expr_term: ($) =>
|
|
|
|
|
choice(
|
|
|
|
|
$.literal_value,
|
|
|
|
|
$.template_expr,
|
|
|
|
|
$.collection_value,
|
|
|
|
|
$.variable_expr,
|
|
|
|
|
$.function_call,
|
|
|
|
|
$.for_expr,
|
|
|
|
|
$.operation,
|
|
|
|
|
seq($._expr_term, $.index),
|
|
|
|
|
seq($._expr_term, $.get_attr),
|
|
|
|
|
seq($._expr_term, $.splat),
|
2023-07-25 19:04:47 +02:00
|
|
|
seq("(", $.expression, ")"),
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
literal_value: ($) =>
|
|
|
|
|
choice($.numeric_lit, $.bool_lit, $.null_lit, $.string_lit),
|
|
|
|
|
|
|
|
|
|
numeric_lit: ($) =>
|
|
|
|
|
choice(/[0-9]+(\.[0-9]+([eE][-+]?[0-9]+)?)?/, /0x[0-9a-zA-Z]+/),
|
|
|
|
|
|
|
|
|
|
bool_lit: ($) => choice("true", "false"),
|
|
|
|
|
|
|
|
|
|
null_lit: ($) => "null",
|
|
|
|
|
|
|
|
|
|
string_lit: ($) =>
|
|
|
|
|
prec(
|
|
|
|
|
PREC.string_lit,
|
|
|
|
|
seq(
|
|
|
|
|
$.quoted_template_start,
|
|
|
|
|
optional($.template_literal),
|
2023-07-25 19:04:47 +02:00
|
|
|
$.quoted_template_end,
|
|
|
|
|
),
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
collection_value: ($) => choice($.tuple, $.object),
|
|
|
|
|
|
|
|
|
|
_comma: ($) => ",",
|
|
|
|
|
|
|
|
|
|
tuple: ($) => seq($.tuple_start, optional($._tuple_elems), $.tuple_end),
|
|
|
|
|
|
|
|
|
|
tuple_start: ($) => "[",
|
|
|
|
|
tuple_end: ($) => "]",
|
|
|
|
|
|
|
|
|
|
_tuple_elems: ($) =>
|
|
|
|
|
seq(
|
2022-12-03 00:40:51 +01:00
|
|
|
$.expression,
|
2023-04-08 20:13:43 +02:00
|
|
|
repeat(seq($._comma, $.expression)),
|
2023-07-25 19:04:47 +02:00
|
|
|
optional($._comma),
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
object: ($) =>
|
|
|
|
|
seq($.object_start, optional($._object_elems), $.object_end),
|
|
|
|
|
|
|
|
|
|
object_start: ($) => "{",
|
|
|
|
|
object_end: ($) => "}",
|
|
|
|
|
|
|
|
|
|
_object_elems: ($) =>
|
|
|
|
|
seq(
|
|
|
|
|
$.object_elem,
|
|
|
|
|
repeat(seq(optional($._comma), $.object_elem)),
|
2023-07-25 19:04:47 +02:00
|
|
|
optional($._comma),
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
object_elem: ($) =>
|
|
|
|
|
seq(
|
|
|
|
|
field("key", $.expression),
|
|
|
|
|
choice("=", ":"),
|
2023-07-25 19:04:47 +02:00
|
|
|
field("val", $.expression),
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
index: ($) => choice($.new_index, $.legacy_index),
|
|
|
|
|
|
|
|
|
|
new_index: ($) => seq("[", $.expression, "]"),
|
|
|
|
|
legacy_index: ($) => seq(".", /[0-9]+/),
|
|
|
|
|
|
|
|
|
|
get_attr: ($) => seq(".", $.identifier),
|
|
|
|
|
|
|
|
|
|
splat: ($) => choice($.attr_splat, $.full_splat),
|
|
|
|
|
|
|
|
|
|
attr_splat: ($) =>
|
|
|
|
|
prec.right(seq(".*", repeat(choice($.get_attr, $.index)))),
|
|
|
|
|
|
|
|
|
|
full_splat: ($) =>
|
|
|
|
|
prec.right(seq("[*]", repeat(choice($.get_attr, $.index)))),
|
|
|
|
|
|
|
|
|
|
for_expr: ($) => choice($.for_tuple_expr, $.for_object_expr),
|
|
|
|
|
|
|
|
|
|
for_tuple_expr: ($) =>
|
|
|
|
|
seq(
|
|
|
|
|
$.tuple_start,
|
|
|
|
|
$.for_intro,
|
|
|
|
|
$.expression,
|
|
|
|
|
optional($.for_cond),
|
2023-07-25 19:04:47 +02:00
|
|
|
$.tuple_end,
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
for_object_expr: ($) =>
|
|
|
|
|
seq(
|
|
|
|
|
$.object_start,
|
|
|
|
|
$.for_intro,
|
|
|
|
|
$.expression,
|
|
|
|
|
"=>",
|
|
|
|
|
$.expression,
|
|
|
|
|
optional($.ellipsis),
|
|
|
|
|
optional($.for_cond),
|
2023-07-25 19:04:47 +02:00
|
|
|
$.object_end,
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
for_intro: ($) =>
|
|
|
|
|
seq(
|
|
|
|
|
"for",
|
|
|
|
|
$.identifier,
|
|
|
|
|
optional(seq(",", $.identifier)),
|
|
|
|
|
"in",
|
|
|
|
|
$.expression,
|
2023-07-25 19:04:47 +02:00
|
|
|
":",
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
for_cond: ($) => seq("if", $.expression),
|
|
|
|
|
|
|
|
|
|
variable_expr: ($) => prec.right($.identifier),
|
|
|
|
|
|
|
|
|
|
function_call: ($) =>
|
|
|
|
|
seq(
|
|
|
|
|
$.identifier,
|
|
|
|
|
$._function_call_start,
|
|
|
|
|
optional($.function_arguments),
|
2023-07-25 19:04:47 +02:00
|
|
|
$._function_call_end,
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
_function_call_start: ($) => "(",
|
|
|
|
|
_function_call_end: ($) => ")",
|
|
|
|
|
|
|
|
|
|
function_arguments: ($) =>
|
|
|
|
|
prec.right(
|
|
|
|
|
seq(
|
|
|
|
|
$.expression,
|
|
|
|
|
repeat(seq($._comma, $.expression)),
|
2023-07-25 19:04:47 +02:00
|
|
|
optional(choice($._comma, $.ellipsis)),
|
|
|
|
|
),
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
ellipsis: ($) => token("..."),
|
|
|
|
|
|
|
|
|
|
conditional: ($) =>
|
|
|
|
|
prec.left(seq($.expression, "?", $.expression, ":", $.expression)),
|
|
|
|
|
|
|
|
|
|
operation: ($) => choice($.unary_operation, $.binary_operation),
|
|
|
|
|
|
|
|
|
|
unary_operation: ($) =>
|
|
|
|
|
prec.left(PREC.unary, seq(choice("-", "!"), $._expr_term)),
|
|
|
|
|
|
|
|
|
|
binary_operation: ($) => {
|
2022-12-03 00:40:51 +01:00
|
|
|
const table = [
|
2023-04-08 20:13:43 +02:00
|
|
|
[PREC.binary_mult, choice("*", "/", "%")],
|
|
|
|
|
[PREC.binary_add, choice("+", "-")],
|
|
|
|
|
[PREC.binary_ord, choice(">", ">=", "<", "<=")],
|
|
|
|
|
[PREC.binary_comp, choice("==", "!=")],
|
2025-03-30 12:46:26 +02:00
|
|
|
[PREC.binary_and, "&&"],
|
|
|
|
|
[PREC.binary_or, "||"],
|
2022-12-03 00:40:51 +01:00
|
|
|
];
|
|
|
|
|
|
2023-04-08 20:13:43 +02:00
|
|
|
return choice(
|
|
|
|
|
...table.map(([precedence, operator]) =>
|
2023-07-25 19:04:47 +02:00
|
|
|
prec.left(precedence, seq($._expr_term, operator, $._expr_term)),
|
|
|
|
|
),
|
2022-12-03 00:40:51 +01:00
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
|
2023-04-08 20:13:43 +02:00
|
|
|
template_expr: ($) => choice($.quoted_template, $.heredoc_template),
|
2022-12-03 00:40:51 +01:00
|
|
|
|
2023-04-08 20:13:43 +02:00
|
|
|
quoted_template: ($) =>
|
|
|
|
|
prec(
|
|
|
|
|
PREC.quoted_template,
|
|
|
|
|
seq(
|
|
|
|
|
$.quoted_template_start,
|
|
|
|
|
optional($._template),
|
2023-07-25 19:04:47 +02:00
|
|
|
$.quoted_template_end,
|
|
|
|
|
),
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
heredoc_template: ($) =>
|
|
|
|
|
seq(
|
|
|
|
|
$.heredoc_start,
|
|
|
|
|
$.heredoc_identifier,
|
|
|
|
|
optional($._template),
|
2023-07-25 19:04:47 +02:00
|
|
|
$.heredoc_identifier,
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
heredoc_start: ($) => choice("<<", "<<-"),
|
|
|
|
|
|
|
|
|
|
strip_marker: ($) => "~",
|
|
|
|
|
|
|
|
|
|
_template: ($) =>
|
|
|
|
|
repeat1(
|
|
|
|
|
choice(
|
|
|
|
|
$.template_interpolation,
|
|
|
|
|
$.template_directive,
|
2023-07-25 19:04:47 +02:00
|
|
|
$.template_literal,
|
|
|
|
|
),
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
template_literal: ($) => prec.right(repeat1($._template_literal_chunk)),
|
|
|
|
|
|
|
|
|
|
template_interpolation: ($) =>
|
|
|
|
|
seq(
|
|
|
|
|
$.template_interpolation_start,
|
|
|
|
|
optional($.strip_marker),
|
|
|
|
|
optional($.expression),
|
|
|
|
|
optional($.strip_marker),
|
2023-07-25 19:04:47 +02:00
|
|
|
$.template_interpolation_end,
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
template_directive: ($) => choice($.template_for, $.template_if),
|
|
|
|
|
|
|
|
|
|
template_for: ($) =>
|
|
|
|
|
seq($.template_for_start, optional($._template), $.template_for_end),
|
|
|
|
|
|
|
|
|
|
template_for_start: ($) =>
|
|
|
|
|
seq(
|
|
|
|
|
$.template_directive_start,
|
|
|
|
|
optional($.strip_marker),
|
|
|
|
|
"for",
|
|
|
|
|
$.identifier,
|
|
|
|
|
optional(seq(",", $.identifier)),
|
|
|
|
|
"in",
|
|
|
|
|
$.expression,
|
|
|
|
|
optional($.strip_marker),
|
2023-07-25 19:04:47 +02:00
|
|
|
$.template_directive_end,
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
template_for_end: ($) =>
|
|
|
|
|
seq(
|
|
|
|
|
$.template_directive_start,
|
|
|
|
|
optional($.strip_marker),
|
|
|
|
|
"endfor",
|
|
|
|
|
optional($.strip_marker),
|
2023-07-25 19:04:47 +02:00
|
|
|
$.template_directive_end,
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
template_if: ($) =>
|
|
|
|
|
seq(
|
|
|
|
|
$.template_if_intro,
|
|
|
|
|
optional($._template),
|
|
|
|
|
optional(seq($.template_else_intro, optional($._template))),
|
2023-07-25 19:04:47 +02:00
|
|
|
$.template_if_end,
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
template_if_intro: ($) =>
|
2022-12-03 00:40:51 +01:00
|
|
|
seq(
|
2023-04-08 20:13:43 +02:00
|
|
|
$.template_directive_start,
|
|
|
|
|
optional($.strip_marker),
|
|
|
|
|
"if",
|
|
|
|
|
$.expression,
|
|
|
|
|
optional($.strip_marker),
|
2023-07-25 19:04:47 +02:00
|
|
|
$.template_directive_end,
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
template_else_intro: ($) =>
|
|
|
|
|
seq(
|
|
|
|
|
$.template_directive_start,
|
|
|
|
|
optional($.strip_marker),
|
|
|
|
|
"else",
|
|
|
|
|
optional($.strip_marker),
|
2023-07-25 19:04:47 +02:00
|
|
|
$.template_directive_end,
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
template_if_end: ($) =>
|
|
|
|
|
seq(
|
|
|
|
|
$.template_directive_start,
|
|
|
|
|
optional($.strip_marker),
|
|
|
|
|
"endif",
|
|
|
|
|
optional($.strip_marker),
|
2023-07-25 19:04:47 +02:00
|
|
|
$.template_directive_end,
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment/36328890#36328890
|
|
|
|
|
comment: ($) =>
|
|
|
|
|
token(
|
|
|
|
|
choice(
|
|
|
|
|
seq("#", /.*/),
|
|
|
|
|
seq("//", /.*/),
|
2023-07-25 19:04:47 +02:00
|
|
|
seq("/*", /[^*]*\*+([^/*][^*]*\*+)*/, "/"),
|
|
|
|
|
),
|
2023-04-08 20:13:43 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
_whitespace: ($) => token(/\s/),
|
|
|
|
|
},
|
2022-12-03 00:40:51 +01:00
|
|
|
});
|
2023-04-08 20:13:43 +02:00
|
|
|
};
|