2021-06-07 20:50:00 +02:00
|
|
|
const
|
2021-06-14 00:55:24 +02:00
|
|
|
PREC = {
|
|
|
|
|
unary: 7,
|
|
|
|
|
binary_mult: 6,
|
|
|
|
|
binary_add: 5,
|
|
|
|
|
binary_ord: 4,
|
|
|
|
|
binary_comp: 3,
|
|
|
|
|
binary_and: 2,
|
|
|
|
|
binary_or: 1,
|
|
|
|
|
}
|
2021-06-07 20:50:00 +02:00
|
|
|
|
|
|
|
|
module.exports = grammar({
|
|
|
|
|
name: 'hcl',
|
|
|
|
|
|
|
|
|
|
conflicts: $ => [
|
2021-06-18 07:17:21 +02:00
|
|
|
// string literals are just quoted template without template stuff
|
2021-06-14 23:33:28 +02:00
|
|
|
[$.string_lit, $.quoted_template],
|
2021-06-14 00:55:24 +02:00
|
|
|
],
|
|
|
|
|
|
|
|
|
|
externals: $ => [
|
2021-06-17 19:39:46 +02:00
|
|
|
$._quoted_template_start,
|
|
|
|
|
$._quoted_template_end,
|
|
|
|
|
$._template_literal_chunk,
|
|
|
|
|
$._template_interpolation_start,
|
|
|
|
|
$._template_interpolation_end,
|
2021-06-07 20:50:00 +02:00
|
|
|
],
|
|
|
|
|
|
2021-06-08 22:32:42 +02:00
|
|
|
extras: $ => [
|
2021-06-13 09:33:34 +02:00
|
|
|
$.comment,
|
2021-06-20 00:03:58 +02:00
|
|
|
$._whitespace,
|
2021-06-08 22:32:42 +02:00
|
|
|
],
|
2021-06-07 20:50:00 +02:00
|
|
|
|
|
|
|
|
rules: {
|
2021-06-20 00:03:58 +02:00
|
|
|
config_file: $ => optional($.body),
|
2021-06-07 20:50:00 +02:00
|
|
|
|
2021-06-20 00:03:58 +02:00
|
|
|
body: $ => repeat1(
|
2021-06-07 20:50:00 +02:00
|
|
|
choice(
|
2021-06-13 09:33:34 +02:00
|
|
|
$.attribute,
|
2021-06-07 20:50:00 +02:00
|
|
|
$.block,
|
|
|
|
|
),
|
2021-06-20 00:03:58 +02:00
|
|
|
),
|
2021-06-07 20:50:00 +02:00
|
|
|
|
2021-06-20 00:03:58 +02:00
|
|
|
attribute: $ => seq(
|
2021-06-19 16:56:35 +02:00
|
|
|
$.identifier,
|
2021-06-13 09:33:34 +02:00
|
|
|
'=',
|
2021-06-08 22:32:42 +02:00
|
|
|
$.expression,
|
2021-06-20 00:03:58 +02:00
|
|
|
),
|
2021-06-07 20:50:00 +02:00
|
|
|
|
2021-06-20 00:03:58 +02:00
|
|
|
block: $ => seq(
|
2021-06-19 16:56:35 +02:00
|
|
|
$.identifier,
|
2021-06-07 20:50:00 +02:00
|
|
|
repeat(choice($.string_lit, $.identifier)),
|
2021-06-17 19:39:46 +02:00
|
|
|
$._block_start,
|
2021-06-07 20:50:00 +02:00
|
|
|
optional($.body),
|
2021-06-17 19:39:46 +02:00
|
|
|
$._block_end,
|
2021-06-19 21:01:03 +02:00
|
|
|
),
|
|
|
|
|
|
2021-06-17 19:39:46 +02:00
|
|
|
_block_start: $ => '{',
|
|
|
|
|
_block_end: $ => '}',
|
|
|
|
|
|
2021-06-08 00:45:23 +02:00
|
|
|
// TODO: not to spec but good enough for now
|
2021-06-08 00:12:25 +02:00
|
|
|
identifier: $ => token(seq(
|
2021-06-20 00:03:58 +02:00
|
|
|
choice(/\p{L}/, '_'),
|
2021-06-19 16:56:35 +02:00
|
|
|
repeat(choice(/\p{L}/, /[0-9]/, /(-|_)/)),
|
2021-06-08 00:12:25 +02:00
|
|
|
)),
|
|
|
|
|
|
2021-06-20 00:03:58 +02:00
|
|
|
expression: $ => prec.right(choice(
|
2021-06-17 19:39:46 +02:00
|
|
|
$._expr_term,
|
2021-06-12 21:47:30 +02:00
|
|
|
$.conditional,
|
2021-06-20 00:03:58 +02:00
|
|
|
)),
|
2021-06-07 20:50:00 +02:00
|
|
|
|
2021-06-19 21:01:03 +02:00
|
|
|
// operations are documented as expressions, but our real world samples
|
|
|
|
|
// contain instances of operations without parentheses. think for example:
|
|
|
|
|
// x = a == "" && b != ""
|
2021-06-17 19:39:46 +02:00
|
|
|
_expr_term: $ => choice(
|
2021-06-07 20:50:00 +02:00
|
|
|
$.literal_value,
|
2021-06-14 00:55:24 +02:00
|
|
|
$.template_expr,
|
2021-06-08 00:04:05 +02:00
|
|
|
$.collection_value,
|
2021-06-07 20:50:00 +02:00
|
|
|
$.variable_expr,
|
2021-06-11 23:32:22 +02:00
|
|
|
$.function_call,
|
2021-06-08 22:32:42 +02:00
|
|
|
$.for_expr,
|
2021-06-19 18:10:47 +02:00
|
|
|
$.operation,
|
2021-06-17 19:39:46 +02:00
|
|
|
seq($._expr_term, $.index),
|
|
|
|
|
seq($._expr_term, $.get_attr),
|
|
|
|
|
seq($._expr_term, $.splat),
|
2021-06-07 20:50:00 +02:00
|
|
|
seq('(', $.expression, ')'),
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
literal_value: $ => choice(
|
|
|
|
|
$.numeric_lit,
|
2021-06-08 00:45:23 +02:00
|
|
|
$.bool_lit,
|
|
|
|
|
$.null_lit,
|
2021-06-17 19:39:46 +02:00
|
|
|
$.string_lit,
|
2021-06-07 20:50:00 +02:00
|
|
|
),
|
|
|
|
|
|
2021-06-20 00:03:58 +02:00
|
|
|
numeric_lit: $ => choice(
|
|
|
|
|
/[0-9]+(\.[0-9]+([eE][-+]?[0-9]+)?)?/,
|
|
|
|
|
/0x[0-9a-zA-Z]+/
|
|
|
|
|
),
|
2021-06-07 20:50:00 +02:00
|
|
|
|
2021-06-08 00:45:23 +02:00
|
|
|
bool_lit: $ => choice('true', 'false'),
|
|
|
|
|
|
|
|
|
|
null_lit: $ => 'null',
|
|
|
|
|
|
2021-06-17 19:39:46 +02:00
|
|
|
string_lit: $ => seq(
|
|
|
|
|
$._quoted_template_start,
|
|
|
|
|
$.template_literal,
|
|
|
|
|
$._quoted_template_end,
|
|
|
|
|
),
|
|
|
|
|
|
2021-06-14 00:55:24 +02:00
|
|
|
|
2021-06-08 00:04:05 +02:00
|
|
|
collection_value: $ => choice(
|
|
|
|
|
$.tuple,
|
|
|
|
|
$.object,
|
|
|
|
|
),
|
|
|
|
|
|
2021-06-19 16:56:35 +02:00
|
|
|
_comma: $ => ',',
|
|
|
|
|
|
2021-06-08 00:04:05 +02:00
|
|
|
tuple: $ => seq(
|
2021-06-19 16:56:35 +02:00
|
|
|
$._tuple_start,
|
|
|
|
|
optional($._tuple_elems),
|
|
|
|
|
$._tuple_end,
|
2021-06-08 00:04:05 +02:00
|
|
|
),
|
|
|
|
|
|
2021-06-19 16:56:35 +02:00
|
|
|
_tuple_start: $ => '[',
|
|
|
|
|
_tuple_end: $ => ']',
|
|
|
|
|
|
2021-06-20 00:03:58 +02:00
|
|
|
_tuple_elems: $ => seq(
|
2021-06-19 16:56:35 +02:00
|
|
|
$.expression,
|
2021-06-20 00:03:58 +02:00
|
|
|
repeat(seq(
|
|
|
|
|
$._comma,
|
|
|
|
|
$.expression,
|
|
|
|
|
)),
|
2021-06-19 16:56:35 +02:00
|
|
|
optional($._comma),
|
2021-06-20 00:03:58 +02:00
|
|
|
),
|
2021-06-19 16:56:35 +02:00
|
|
|
|
2021-06-08 00:04:05 +02:00
|
|
|
object: $ => seq(
|
2021-06-17 19:39:46 +02:00
|
|
|
$._object_start,
|
2021-06-19 16:56:35 +02:00
|
|
|
optional($._object_elems),
|
2021-06-17 19:39:46 +02:00
|
|
|
$._object_end,
|
2021-06-08 00:04:05 +02:00
|
|
|
),
|
|
|
|
|
|
2021-06-17 19:39:46 +02:00
|
|
|
_object_start: $ => '{',
|
|
|
|
|
_object_end: $ => '}',
|
|
|
|
|
|
2021-06-20 00:03:58 +02:00
|
|
|
_object_elems: $ => seq(
|
2021-06-19 16:56:35 +02:00
|
|
|
$.object_elem,
|
|
|
|
|
repeat(seq(
|
2021-06-20 00:03:58 +02:00
|
|
|
optional($._comma),
|
2021-06-19 16:56:35 +02:00
|
|
|
$.object_elem
|
|
|
|
|
)),
|
|
|
|
|
optional($._comma),
|
2021-06-20 00:03:58 +02:00
|
|
|
),
|
2021-06-19 16:56:35 +02:00
|
|
|
|
2021-06-08 00:04:05 +02:00
|
|
|
object_elem: $ => seq(
|
2021-06-18 07:20:55 +02:00
|
|
|
$.expression,
|
2021-06-08 00:04:05 +02:00
|
|
|
choice('=', ':'),
|
|
|
|
|
$.expression,
|
|
|
|
|
),
|
|
|
|
|
|
2021-06-19 16:56:35 +02:00
|
|
|
index: $ => choice($.new_index, $.legacy_index),
|
|
|
|
|
|
|
|
|
|
new_index: $ => seq('[', $.expression, ']'),
|
|
|
|
|
legacy_index: $ => seq('.', /[0-9]+/),
|
2021-06-08 18:50:12 +02:00
|
|
|
|
|
|
|
|
get_attr: $ => seq('.', $.identifier),
|
|
|
|
|
|
|
|
|
|
splat: $ => choice($.attr_splat, $.full_splat),
|
|
|
|
|
|
2021-06-18 07:29:00 +02:00
|
|
|
attr_splat: $ => prec.right(seq(
|
|
|
|
|
'.*',
|
2021-06-13 09:33:34 +02:00
|
|
|
repeat($.get_attr),
|
2021-06-18 07:29:00 +02:00
|
|
|
)),
|
2021-06-08 22:32:42 +02:00
|
|
|
|
2021-06-18 07:29:00 +02:00
|
|
|
full_splat: $ => prec.right(seq(
|
|
|
|
|
'[*]',
|
2021-06-13 09:33:34 +02:00
|
|
|
repeat(choice($.get_attr, $.index)),
|
2021-06-18 07:29:00 +02:00
|
|
|
)),
|
2021-06-08 18:50:12 +02:00
|
|
|
|
2021-06-08 22:32:42 +02:00
|
|
|
for_expr: $ => choice($.for_tuple_expr, $.for_object_expr),
|
|
|
|
|
|
|
|
|
|
for_tuple_expr: $ => seq(
|
2021-06-19 16:56:35 +02:00
|
|
|
$._tuple_start,
|
2021-06-08 22:32:42 +02:00
|
|
|
$.for_intro,
|
|
|
|
|
$.expression,
|
|
|
|
|
optional($.for_cond),
|
2021-06-19 16:56:35 +02:00
|
|
|
$._tuple_end,
|
2021-06-08 22:32:42 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
for_object_expr: $ => seq(
|
2021-06-17 19:39:46 +02:00
|
|
|
$._object_start,
|
2021-06-08 22:32:42 +02:00
|
|
|
$.for_intro,
|
|
|
|
|
$.expression,
|
|
|
|
|
'=>',
|
|
|
|
|
$.expression,
|
2021-06-11 23:57:34 +02:00
|
|
|
optional($.ellipsis),
|
2021-06-08 22:32:42 +02:00
|
|
|
optional($.for_cond),
|
2021-06-17 19:39:46 +02:00
|
|
|
$._object_end,
|
2021-06-08 22:32:42 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
for_intro: $ => seq(
|
|
|
|
|
'for',
|
|
|
|
|
$.identifier,
|
|
|
|
|
optional(seq(',', $.identifier)),
|
|
|
|
|
'in',
|
|
|
|
|
$.expression,
|
|
|
|
|
':',
|
|
|
|
|
),
|
|
|
|
|
|
2021-06-12 21:21:35 +02:00
|
|
|
for_cond: $ => seq(
|
|
|
|
|
'if',
|
|
|
|
|
$.expression,
|
|
|
|
|
),
|
2021-06-08 22:32:42 +02:00
|
|
|
|
2021-06-20 00:03:58 +02:00
|
|
|
variable_expr: $ => prec.right($.identifier),
|
2021-06-07 20:50:00 +02:00
|
|
|
|
2021-06-11 23:41:02 +02:00
|
|
|
function_call: $ => seq(
|
2021-06-19 16:56:35 +02:00
|
|
|
$.identifier,
|
2021-06-17 19:39:46 +02:00
|
|
|
$._function_call_start,
|
2021-06-11 23:41:02 +02:00
|
|
|
optional($.function_arguments),
|
2021-06-17 19:39:46 +02:00
|
|
|
$._function_call_end,
|
2021-06-11 23:41:02 +02:00
|
|
|
),
|
2021-06-11 23:32:22 +02:00
|
|
|
|
2021-06-19 18:10:47 +02:00
|
|
|
_function_call_start: $ => '(',
|
|
|
|
|
_function_call_end: $ => ')',
|
2021-06-17 19:39:46 +02:00
|
|
|
|
2021-06-19 16:56:35 +02:00
|
|
|
function_arguments: $ => prec.right(seq(
|
2021-06-13 09:33:34 +02:00
|
|
|
$.expression,
|
2021-06-20 00:03:58 +02:00
|
|
|
repeat(seq($._comma, $.expression,)),
|
2021-06-19 16:56:35 +02:00
|
|
|
optional(choice(',', $.ellipsis)),
|
|
|
|
|
)),
|
2021-06-11 23:32:22 +02:00
|
|
|
|
2021-06-11 23:57:34 +02:00
|
|
|
ellipsis: $ => token('...'),
|
|
|
|
|
|
2021-06-18 07:17:21 +02:00
|
|
|
conditional: $ => prec.left(seq(
|
2021-06-12 21:47:30 +02:00
|
|
|
$.expression,
|
|
|
|
|
'?',
|
|
|
|
|
$.expression,
|
|
|
|
|
':',
|
|
|
|
|
$.expression,
|
2021-06-18 07:17:21 +02:00
|
|
|
)),
|
2021-06-12 21:47:30 +02:00
|
|
|
|
2021-06-13 09:17:52 +02:00
|
|
|
operation: $ => choice($.unary_operation, $.binary_operation),
|
|
|
|
|
|
2021-06-17 19:39:46 +02:00
|
|
|
unary_operation: $ => prec.left(PREC.unary, seq(choice('-', '!'), $._expr_term)),
|
2021-06-13 09:17:52 +02:00
|
|
|
|
|
|
|
|
binary_operation: $ => {
|
|
|
|
|
const table = [
|
2021-06-14 00:55:24 +02:00
|
|
|
[PREC.binary_mult, choice('*', '/', '%')],
|
|
|
|
|
[PREC.binary_add, choice('+', '-')],
|
|
|
|
|
[PREC.binary_ord, choice('>', '>=', '<', '<=')],
|
|
|
|
|
[PREC.binary_comp, choice('==', '!=')],
|
|
|
|
|
[PREC.binary_and, choice('&&')],
|
|
|
|
|
[PREC.binary_or, choice('||')],
|
2021-06-13 09:17:52 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return choice(...table.map(([precedence, operator]) =>
|
2021-06-17 19:39:46 +02:00
|
|
|
prec.left(precedence, seq($._expr_term, operator, $._expr_term),
|
2021-06-13 09:17:52 +02:00
|
|
|
))
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
|
2021-06-14 00:55:24 +02:00
|
|
|
template_expr: $ => choice(
|
|
|
|
|
$.quoted_template,
|
|
|
|
|
// $.heredoc_template,
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
quoted_template: $ => seq(
|
2021-06-17 19:39:46 +02:00
|
|
|
$._quoted_template_start,
|
2021-06-14 00:55:24 +02:00
|
|
|
repeat(choice(
|
2021-06-17 19:39:46 +02:00
|
|
|
$.template_literal,
|
2021-06-14 00:55:24 +02:00
|
|
|
$.template_interpolation,
|
2021-06-17 19:39:46 +02:00
|
|
|
$.template_directive,
|
|
|
|
|
)),
|
|
|
|
|
$._quoted_template_end,
|
2021-06-14 00:55:24 +02:00
|
|
|
),
|
|
|
|
|
|
2021-06-17 19:39:46 +02:00
|
|
|
strip_marker: $ => '~',
|
|
|
|
|
|
|
|
|
|
template_literal: $ => prec.right(repeat1(
|
|
|
|
|
$._template_literal_chunk,
|
|
|
|
|
)),
|
|
|
|
|
|
2021-06-14 00:55:24 +02:00
|
|
|
template_interpolation: $ => seq(
|
2021-06-17 19:39:46 +02:00
|
|
|
$._template_interpolation_start,
|
|
|
|
|
optional($.strip_marker),
|
|
|
|
|
$.expression,
|
|
|
|
|
optional($.strip_marker),
|
|
|
|
|
$._template_interpolation_end,
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
template_directive: $ => choice(
|
|
|
|
|
//$.template_for,
|
|
|
|
|
//$.template_if,
|
2021-06-14 00:55:24 +02:00
|
|
|
),
|
|
|
|
|
|
2021-06-07 20:50:00 +02:00
|
|
|
// http://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment/36328890#36328890
|
|
|
|
|
comment: $ => token(choice(
|
|
|
|
|
seq('#', /.*/),
|
|
|
|
|
seq('//', /.*/),
|
|
|
|
|
seq(
|
|
|
|
|
'/*',
|
|
|
|
|
/[^*]*\*+([^/*][^*]*\*+)*/,
|
|
|
|
|
'/'
|
|
|
|
|
)
|
2021-06-08 00:04:05 +02:00
|
|
|
)),
|
2021-06-20 00:03:58 +02:00
|
|
|
|
|
|
|
|
_whitespace: $ => token(/\s/),
|
2021-06-07 20:50:00 +02:00
|
|
|
}
|
|
|
|
|
});
|