Files
tree-sitter-jhcl/grammar.js

274 lines
5.2 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,
quoted_string: 2,
quoted_template: 1,
}
2021-06-08 00:04:05 +02:00
unicodeLetter = /\p{L}/
unicodePunctuation = /\p{Pc}/
unicodeDigit = /[0-9]/
2021-06-07 20:50:00 +02:00
module.exports = grammar({
name: 'hcl',
conflicts: $ => [
[$.body],
[$.object_elem, $.variable_expr],
[$.attr_splat],
[$.full_splat],
[$.conditional],
[$.string_lit, $.quoted_template],
],
externals: $ => [
$._template_char,
$._template_char_in_interpolation,
$.escape_sequence,
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-08 22:32:42 +02:00
/\s/,
],
2021-06-07 20:50:00 +02:00
rules: {
config_file: $ => $.body,
body: $ => repeat1(seq(
choice(
2021-06-13 09:33:34 +02:00
$.attribute,
2021-06-07 20:50:00 +02:00
$.block,
),
)),
attribute: $ => seq(
2021-06-08 01:28:18 +02:00
field('name', $.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(
2021-06-08 01:28:18 +02:00
field('name', $.identifier),
2021-06-07 20:50:00 +02:00
repeat(choice($.string_lit, $.identifier)),
'{',
optional($.body),
'}',
),
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(
unicodeLetter,
2021-06-08 22:32:42 +02:00
repeat(choice(unicodeLetter, unicodeDigit, unicodePunctuation)),
2021-06-08 00:12:25 +02:00
)),
2021-06-07 20:50:00 +02:00
expression: $ => choice(
$.expr_term,
2021-06-13 09:17:52 +02:00
$.operation,
2021-06-12 21:47:30 +02:00
$.conditional,
2021-06-07 20:50:00 +02:00
),
expr_term: $ => choice(
$.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-08 18:50:12 +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-07 20:50:00 +02:00
),
2021-06-07 22:32:14 +02:00
numeric_lit: $ => /[0-9]+(\.[0-9]+([eE][-+]?[0-9]+)?)?/,
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 is defined as quoted template
2021-06-08 00:04:05 +02:00
collection_value: $ => choice(
$.tuple,
$.object,
),
tuple: $ => seq(
'[',
optional(seq(
$.expression,
repeat(seq(',', $.expression)),
)),
']',
),
object: $ => seq(
'{',
optional(seq(
$.object_elem,
repeat(seq(',', $.object_elem)),
)),
'}',
),
object_elem: $ => seq(
choice($.identifier, $.expression),
choice('=', ':'),
$.expression,
),
2021-06-08 18:50:12 +02:00
index: $ => seq('[', $.expression, ']'),
get_attr: $ => seq('.', $.identifier),
splat: $ => choice($.attr_splat, $.full_splat),
2021-06-13 09:33:34 +02:00
attr_splat: $ => seq(
'.',
'*',
repeat($.get_attr),
),
2021-06-08 22:32:42 +02:00
2021-06-13 09:33:34 +02:00
full_splat: $ => seq(
'[',
'*',
']',
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(
2021-06-13 09:33:34 +02:00
'[',
2021-06-08 22:32:42 +02:00
$.for_intro,
$.expression,
optional($.for_cond),
']',
),
for_object_expr: $ => seq(
'{',
$.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),
'}',
),
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-07 20:50:00 +02:00
variable_expr: $ => $.identifier,
2021-06-11 23:41:02 +02:00
function_call: $ => seq(
field('name', $.identifier),
'(',
optional($.function_arguments),
')'
),
2021-06-11 23:32:22 +02:00
function_arguments: $ => seq(
2021-06-13 09:33:34 +02:00
$.expression,
repeat(seq(',', $.expression)),
2021-06-11 23:57:34 +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-12 21:47:30 +02:00
conditional: $ => seq(
$.expression,
'?',
$.expression,
':',
$.expression,
),
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),
))
);
},
template_expr: $ => choice(
$.quoted_template,
// $.heredoc_template,
),
string_lit: $ => seq(
'"',
repeat(choice(
$._template_char,
$.escape_sequence,
)),
'"',
),
quoted_template: $ => seq(
'"',
repeat(choice(
$._template_char,
$.escape_sequence,
$.template_interpolation,
)),
'"',
),
template_interpolation: $ => seq(
choice('${', '${~'),
repeat(choice($._template_char_in_interpolation, $.escape_sequence)),
choice('}', '~}'),
),
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-07 20:50:00 +02:00
}
});