add collections; improve identifier

This commit is contained in:
mhoffm
2021-06-08 00:04:05 +02:00
parent e67d183d71
commit fdb45d9ddd
4 changed files with 2740 additions and 493 deletions

View File

@@ -1,12 +1,15 @@
const
space = ' ',
decimal = /[0-9]/
//TODO figure out how to subtact regex sets
unicodeLetter = /\p{L}/
unicodePunctuation = /\p{Pc}/
unicodeDigit = /[0-9]/
module.exports = grammar({
name: 'hcl',
conflicts: $ => [
[$.body],
[$.object_elem, $.variable_expr],
],
extras: $ => [
@@ -46,7 +49,7 @@ module.exports = grammar({
expr_term: $ => choice(
$.literal_value,
// $.collection_value,
$.collection_value,
// $.template_expr,
$.variable_expr,
// $.function_call,
@@ -68,6 +71,35 @@ module.exports = grammar({
numeric_lit: $ => /[0-9]+(\.[0-9]+([eE][-+]?[0-9]+)?)?/,
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,
),
variable_expr: $ => $.identifier,
// TODO: template expressions
@@ -81,8 +113,11 @@ module.exports = grammar({
// TODO: string_literals are special template literals
string_lit: $ => seq('"', /\w+/, '"'),
// TODO: unicode identifier
identifier: $ => /\w+/,
// TODO: not to spec, but maybe good enough
identifier: $ => token(seq(
unicodeLetter,
repeat(choice(unicodeLetter, unicodeDigit, unicodePunctuation))
)),
// http://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment/36328890#36328890
comment: $ => token(choice(
@@ -93,6 +128,6 @@ module.exports = grammar({
/[^*]*\*+([^/*][^*]*\*+)*/,
'/'
)
))
)),
}
});