Files
tree-sitter-jhcl/grammar.js

213 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-06-07 20:50:00 +02:00
const
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],
2021-06-08 00:04:05 +02:00
[$.object_elem, $.variable_expr],
2021-06-08 18:50:12 +02:00
[$.attr_splat],
[$.full_splat],
2021-06-12 21:47:30 +02:00
[$.conditional],
2021-06-07 20:50:00 +02:00
],
2021-06-08 22:32:42 +02:00
extras: $ => [
$.comment,
/\s/,
],
2021-06-07 20:50:00 +02:00
rules: {
config_file: $ => $.body,
body: $ => repeat1(seq(
choice(
$.attribute,
$.block,
),
)),
attribute: $ => seq(
2021-06-08 01:28:18 +02:00
field('name', $.identifier),
2021-06-07 20:50:00 +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,
//$.operation,
2021-06-12 21:47:30 +02:00
$.conditional,
2021-06-07 20:50:00 +02:00
),
expr_term: $ => choice(
$.literal_value,
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,
$.string_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
string_lit: $ => seq(
'"',
repeat(choice(token.immediate(prec(1, /[^\\"\n\r\t]+/)), $.escape_sequence)),
'"',
),
escape_sequence: $ => token.immediate(seq(
'\\',
choice(
'\\',
'"',
'n',
'r',
't',
/u[0-9a-fA-F]{4}/,
/U[0-9a-fA-F]{8}/
)
)),
2021-06-08 00:45:23 +02:00
bool_lit: $ => choice('true', 'false'),
null_lit: $ => 'null',
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),
attr_splat: $ => seq('.', '*', repeat($.get_attr)),
2021-06-08 22:32:42 +02:00
2021-06-08 18:50:12 +02:00
full_splat: $ => seq('[', '*', ']', repeat(choice($.get_attr, $.index))),
2021-06-08 22:32:42 +02:00
for_expr: $ => choice($.for_tuple_expr, $.for_object_expr),
for_tuple_expr: $ => seq(
'[',
$.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(
$.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-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
}
});