Skip to content

Commit 6a619b0

Browse files
authored
feat(grammar): Finalize extend/annotate syntax (#56)
For #17
1 parent 2c8ebbe commit 6a619b0

File tree

7 files changed

+71441
-70759
lines changed

7 files changed

+71441
-70759
lines changed

grammar.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ module.exports = grammar({
100100
$.extend_artifact,
101101
),
102102
),
103-
seq(
104-
kw('annotate'),
105-
$.annotate_artifact,
106-
),
103+
$.annotate_artifact,
107104
),
108105
),
109106

@@ -827,7 +824,7 @@ module.exports = grammar({
827824

828825
extend_context_or_service: $ => seq(
829826
field('kind', choice(kw('context'), kw('service'))),
830-
def_path_name($),
827+
ref_definition($),
831828
optional(kw('with')),
832829
repeat($.annotation),
833830
choice(
@@ -838,7 +835,7 @@ module.exports = grammar({
838835

839836
extend_structure: $ => seq(
840837
choice(kw('type'), kw('aspect'), kw('entity')),
841-
def_path_name($),
838+
ref_definition($),
842839
choice(
843840
seq(
844841
kw('with'),
@@ -857,7 +854,7 @@ module.exports = grammar({
857854

858855
extend_projection: $ => seq(
859856
kw('projection'),
860-
def_path_name($),
857+
ref_definition($),
861858
optional_kw('with'),
862859
repeat($.annotation),
863860
choice(
@@ -877,8 +874,7 @@ module.exports = grammar({
877874
),
878875

879876
extend_artifact: $ => seq(
880-
def_path_name($),
881-
field('element', optional(seq(':', $.simple_path))),
877+
ref_definition_with_optional_element($),
882878
choice(
883879
seq(
884880
repeat($.annotation),
@@ -1176,12 +1172,18 @@ module.exports = grammar({
11761172
),
11771173

11781174
annotate_artifact: $ => seq(
1179-
$.simple_path,
1180-
optional(seq(':', $.simple_path)),
1175+
kw('annotate'),
1176+
ref_definition_with_optional_element($),
11811177
optional_kw('with'),
11821178
repeat($.annotation),
11831179
choice(
1184-
seq('{', repeat($.annotate_element), '}', optional($.annotate_action), optional(';')),
1180+
seq(
1181+
'{',
1182+
repeat($.annotate_element),
1183+
'}',
1184+
optional($.annotate_action),
1185+
optional(';'),
1186+
),
11851187
seq($.annotate_action, optional(';')),
11861188
seq(
11871189
'(',
@@ -1435,6 +1437,34 @@ function def_path_name($) {
14351437
return field('name', alias($.simple_path, $.name));
14361438
}
14371439

1440+
/**
1441+
* Convenience function for a reference that is either a definition (`E`)
1442+
* or an element (`E:elem`).
1443+
*
1444+
* @param {object} $
1445+
* @return {SeqRule}
1446+
*/
1447+
function ref_definition_with_optional_element($) {
1448+
if (arguments.length !== 1)
1449+
throw new InvalidArgument('incorrect number of arguments');
1450+
return seq(
1451+
alias($.simple_path, $.definition_reference),
1452+
optional(seq(':', alias($.simple_path, $.element_reference))),
1453+
);
1454+
}
1455+
1456+
/**
1457+
* Convenience function for a reference that is a definition (`E`).
1458+
*
1459+
* @param {object} $
1460+
* @return {AliasRule}
1461+
*/
1462+
function ref_definition($) {
1463+
if (arguments.length !== 1)
1464+
throw new InvalidArgument('incorrect number of arguments');
1465+
return alias($.simple_path, $.definition_reference);
1466+
}
1467+
14381468
/**
14391469
* A list of `rule`. At least one rule must be parsed.
14401470
*

nvim/highlights.scm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@
163163

164164
(from_path) @variable
165165
(simple_path) @variable
166+
(definition_reference) @variable
167+
(element_reference) @variable
166168

167169
(identifier) @variable
168170

queries/highlights.scm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@
153153

154154
(from_path) @variable
155155
(simple_path) @variable
156+
(definition_reference) @variable
157+
(element_reference) @variable
156158

157159
(identifier) @variable
158160

src/grammar.json

Lines changed: 92 additions & 92 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)