Files
tree-sitter-jhcl/grammar.js

311 lines
6.3 KiB
JavaScript
Raw Normal View History

2021-06-07 20:50:00 +02:00
const
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: $ => [
// string literals are just quoted template without template stuff
[$.string_lit, $.quoted_template],
],
externals: $ => [
$._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,
$._whitespace,
2021-06-08 22:32:42 +02:00
],
2021-06-07 20:50:00 +02:00
rules: {
config_file: $ => optional($.body),
2021-06-07 20:50:00 +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-07 20:50:00 +02:00
attribute: $ => seq(
$.identifier,
2021-06-13 09:33:34 +02:00
'=',
2021-06-08 22:32:42 +02:00
$.expression,
),
2021-06-07 20:50:00 +02:00
block: $ => seq(
$.identifier,
2021-06-07 20:50:00 +02:00
repeat(choice($.string_lit, $.identifier)),
$._block_start,
2021-06-07 20:50:00 +02:00
optional($.body),
$._block_end,
2021-06-19 21:01:03 +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(
choice(/\p{L}/, '_'),
repeat(choice(/\p{L}/, /[0-9]/, /(-|_)/)),
2021-06-08 00:12:25 +02:00
)),
expression: $ => prec.right(choice(
$._expr_term,
2021-06-12 21:47:30 +02:00
$.conditional,
)),
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 != ""
_expr_term: $ => choice(
2021-06-07 20:50:00 +02:00
$.literal_value,
$.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,
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,
$.string_lit,
2021-06-07 20:50:00 +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',
string_lit: $ => seq(
$._quoted_template_start,
$.template_literal,
$._quoted_template_end,
),
2021-06-08 00:04:05 +02:00
collection_value: $ => choice(
$.tuple,
$.object,
),
_comma: $ => ',',
2021-06-08 00:04:05 +02:00
tuple: $ => seq(
$._tuple_start,
optional($._tuple_elems),
$._tuple_end,
2021-06-08 00:04:05 +02:00
),
_tuple_start: $ => '[',
_tuple_end: $ => ']',
_tuple_elems: $ => seq(
$.expression,
repeat(seq(
$._comma,
$.expression,
)),
optional($._comma),
),
2021-06-08 00:04:05 +02:00
object: $ => seq(
$._object_start,
optional($._object_elems),
$._object_end,
2021-06-08 00:04:05 +02:00
),
_object_start: $ => '{',
_object_end: $ => '}',
_object_elems: $ => seq(
$.object_elem,
repeat(seq(
optional($._comma),
$.object_elem
)),
optional($._comma),
),
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,
),
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),
attr_splat: $ => prec.right(seq(
'.*',
2021-06-13 09:33:34 +02:00
repeat($.get_attr),
)),
2021-06-08 22:32:42 +02:00
full_splat: $ => prec.right(seq(
'[*]',
2021-06-13 09:33:34 +02:00
repeat(choice($.get_attr, $.index)),
)),
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(
$._tuple_start,
2021-06-08 22:32:42 +02:00
$.for_intro,
$.expression,
optional($.for_cond),
$._tuple_end,
2021-06-08 22:32:42 +02:00
),
for_object_expr: $ => seq(
$._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),
$._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
variable_expr: $ => prec.right($.identifier),
2021-06-07 20:50:00 +02:00
2021-06-11 23:41:02 +02:00
function_call: $ => seq(
$.identifier,
$._function_call_start,
2021-06-11 23:41:02 +02:00
optional($.function_arguments),
$._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: $ => ')',
function_arguments: $ => prec.right(seq(
2021-06-13 09:33:34 +02:00
$.expression,
repeat(seq($._comma, $.expression,)),
optional(choice(',', $.ellipsis)),
)),
2021-06-11 23:32:22 +02:00
2021-06-11 23:57:34 +02:00
ellipsis: $ => token('...'),
conditional: $ => prec.left(seq(
2021-06-12 21:47:30 +02:00
$.expression,
'?',
$.expression,
':',
$.expression,
)),
2021-06-12 21:47:30 +02:00
2021-06-13 09:17:52 +02:00
operation: $ => choice($.unary_operation, $.binary_operation),
unary_operation: $ => prec.left(PREC.unary, seq(choice('-', '!'), $._expr_term)),
2021-06-13 09:17:52 +02:00
binary_operation: $ => {
const table = [
[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]) =>
prec.left(precedence, seq($._expr_term, operator, $._expr_term),
2021-06-13 09:17:52 +02:00
))
);
},
template_expr: $ => choice(
$.quoted_template,
// $.heredoc_template,
),
quoted_template: $ => seq(
$._quoted_template_start,
repeat(choice(
$.template_literal,
$.template_interpolation,
$.template_directive,
)),
$._quoted_template_end,
),
strip_marker: $ => '~',
template_literal: $ => prec.right(repeat1(
$._template_literal_chunk,
)),
template_interpolation: $ => seq(
$._template_interpolation_start,
optional($.strip_marker),
$.expression,
optional($.strip_marker),
$._template_interpolation_end,
),
// TODO
template_directive: $ => choice(
//$.template_for,
//$.template_if,
),
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
)),
_whitespace: $ => token(/\s/),
2021-06-07 20:50:00 +02:00
}
});