Skip to content

Commit

Permalink
feat: support required fields
Browse files Browse the repository at this point in the history
And also required and optional dynamic fields.

Closes #15
  • Loading branch information
eonpatapon committed Jun 1, 2023
1 parent 0deecf4 commit 8327a33
Show file tree
Hide file tree
Showing 7 changed files with 16,181 additions and 15,690 deletions.
28 changes: 16 additions & 12 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,29 +289,33 @@ module.exports = grammar({
$._alias_expr,
),

_label_name: $ => choice(
_label_name: $ => prec(1, choice(
$.identifier,
$.keyword_identifier,
alias($._simple_string_lit, $.string),
$.selector_expression,
),
alias($.parenthesized_expression, $.dynamic),
)),

_label_alias_expr: $ => alias($._alias_expr, $.optional),

required: $ => seq($._label_name, '!'),

optional: $ => seq($._label_name, '?'),

_label_expr: $ => choice(
seq($._label_name, optional('?')),
$._label_name,
$.optional,
$.required,
seq('[', $._label_alias_expr, ']'),
),

label: $ => choice(
seq(
optional(seq(
field('alias', choice($.identifier, $.keyword_identifier)),
'=',
)),
$._label_expr,
),
$.parenthesized_expression,
label: $ => seq(
optional(seq(
field('alias', choice($.identifier, $.keyword_identifier)),
'=',
)),
$._label_expr,
),

field: $ => prec.right(seq(
Expand Down
5 changes: 3 additions & 2 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@

; Fields & Properties

(field
(label
(field
(label
(identifier) @field))

(selector_expression
Expand Down Expand Up @@ -122,6 +122,7 @@
[
(ellipsis)
"?"
"!"
] @punctuation.special

; Literals
Expand Down
163 changes: 92 additions & 71 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1191,30 +1191,43 @@
]
},
"_label_name": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "keyword_identifier"
},
{
"type": "ALIAS",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_simple_string_lit"
"name": "identifier"
},
"named": true,
"value": "string"
},
{
"type": "SYMBOL",
"name": "selector_expression"
}
]
{
"type": "SYMBOL",
"name": "keyword_identifier"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_simple_string_lit"
},
"named": true,
"value": "string"
},
{
"type": "SYMBOL",
"name": "selector_expression"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "parenthesized_expression"
},
"named": true,
"value": "dynamic"
}
]
}
},
"_label_alias_expr": {
"type": "ALIAS",
Expand All @@ -1225,29 +1238,46 @@
"named": true,
"value": "optional"
},
"required": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_label_name"
},
{
"type": "STRING",
"value": "!"
}
]
},
"optional": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_label_name"
},
{
"type": "STRING",
"value": "?"
}
]
},
"_label_expr": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_label_name"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "?"
},
{
"type": "BLANK"
}
]
}
]
"type": "SYMBOL",
"name": "_label_name"
},
{
"type": "SYMBOL",
"name": "optional"
},
{
"type": "SYMBOL",
"name": "required"
},
{
"type": "SEQ",
Expand All @@ -1269,54 +1299,45 @@
]
},
"label": {
"type": "CHOICE",
"type": "SEQ",
"members": [
{
"type": "SEQ",
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"type": "SEQ",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "alias",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "keyword_identifier"
}
]
"type": "FIELD",
"name": "alias",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "keyword_identifier"
}
},
{
"type": "STRING",
"value": "="
}
]
]
}
},
{
"type": "BLANK"
"type": "STRING",
"value": "="
}
]
},
{
"type": "SYMBOL",
"name": "_label_expr"
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "parenthesized_expression"
"name": "_label_expr"
}
]
},
Expand Down
Loading

0 comments on commit 8327a33

Please sign in to comment.