@gram-data/tree-sitter-gram 0.1.11 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/grammar.js CHANGED
@@ -1,208 +1,394 @@
1
1
  module.exports = grammar({
2
- name: 'gram',
2
+ name: "gram",
3
3
 
4
- rules: {
4
+ extras: ($) => [
5
+ /\s/,
6
+ $.comment,
7
+ ],
5
8
 
6
- gram: $ => seq(
7
- field('root', optional($.record)),
8
- repeat($.pattern)
9
- ),
10
-
11
- pattern: $ => commaSep1($.pattern_element),
12
-
13
- pattern_element: $ => seq(
14
- field("annotations", optional(repeat($.annotation))),
15
- field("element", choice(
16
- $.subject,
17
- $._path,
18
- $._reference
19
- ))
20
- ),
21
-
22
- // ABKNOTE -- consider the naming of the two parts of a subject
23
- // attributes & association
24
- // information & association
25
- subject: $ => seq("[", field("attributes", optional($._attributes)), field("association", optional($._association)),"]"),
26
-
27
- annotation: $ => seq(
28
- "@",
29
- field('key', $.symbol),
30
- "(",
31
- field('value', $._value),
32
- ")"
33
- ),
34
-
35
- _path: $ => choice(
36
- $.relationship,
37
- $.node
38
- ),
39
-
40
- node: $ => seq(token("("), field("attributes", optional($._attributes)), token(")")),
41
-
42
- relationship: $ => seq(field("left", $.node), field("value", $._relationship_value), field("right", $._path)),
43
-
44
- _association: $ => seq(
45
- token("|"),
46
- $.pattern
47
- ),
48
-
49
- _reference: $ => $._value,
50
-
51
- _attributes: $ => choice(
52
- choice(field("identifier", $._value), field("labels", $.labels), field("record", $.record)),
53
- seq(field("identifier", $._value), field("labels", $.labels)),
54
- seq(field("identifier", $._value), field("record", $.record)),
55
- seq(field("labels", $.labels), field("record", $.record)),
56
- seq(field("identifier", $._value), field("labels", $.labels), field("record", $.record))
57
- ),
58
-
59
- _value: $ => choice(
60
- prec.right(2, $.range),
61
- prec.right(1, $._numeric_literal),
62
- $.symbol,
63
- $._string_literal,
64
- $.math_symbol,
65
- $.greek,
66
- $.pictograph
67
- ),
68
-
69
- _binder: $ => token(/::?/),
70
-
71
- labels: $ => repeat1($.label),
72
-
73
- label: $ => seq($._binder, field("symbol", $.symbol)),
74
-
75
- record: $ => seq("{", commaSep($.property), "}"),
76
-
77
- _key: $ => choice(
78
- $.symbol,
79
- $._string_literal
80
- ),
81
-
82
- property: $ => seq(
83
- field('key', $._key),
84
- $._binder,
85
- field('value', $._value)
86
- ),
87
-
88
- symbol: $ => token(/[a-zA-Z_][0-9a-zA-Z_.\-@]*/),
89
-
90
-
91
- greek: $ => token(/[\u03B1-\u03C9\u0391-\u03A9]/),
92
- math_symbol: $ => token(/\p{Other_Math}/),
93
- pictograph: $ => token(/[\u2650-\u26FF]/),
94
-
95
- range: $ => choice(
96
- seq(field("lower", $._numeric_literal), "..", field("upper", $._numeric_literal)),
97
- seq(field("lower", $._numeric_literal), "..."),
98
- seq("...", field("upper", $._numeric_literal))
99
- ),
100
-
101
- _numeric_literal: $ => choice(
102
- $.integer,
103
- $.decimal,
104
- $.hexadecimal,
105
- $.octal,
106
- $.measurement
107
- ),
108
-
109
- integer: $ => {
110
- const integer = /-?(0|[1-9]\d*)/;
9
+ rules: {
10
+ gram: ($) => seq(field("root", optional($.record)), repeat($.pattern)),
11
+
12
+ pattern: ($) =>
13
+ seq(
14
+ field("annotations", optional($.annotations)),
15
+ field("elements", commaSep1($._pattern_element)),
16
+ ),
17
+ _pattern_element: ($) => choice($.subject, $._path),
18
+
19
+ subject: ($) =>
20
+ seq(
21
+ "[",
22
+ field("attributes", optional($._attributes)),
23
+ field("pattern", optional(seq("|", $.sub_pattern))),
24
+ "]",
25
+ ),
26
+
27
+ sub_pattern: ($) => commaSep1($._sub_pattern_element),
28
+
29
+ _sub_pattern_element: ($) => choice($.subject, $._path, $.reference),
30
+
31
+ annotations: ($) => repeat1($.annotation),
32
+
33
+ annotation: ($) =>
34
+ seq("@", field("key", $.symbol), "(", field("value", $._value), ")"),
35
+
36
+ _path: ($) => choice($.relationship, $.node),
37
+
38
+ node: ($) =>
39
+ seq(
40
+ "(",
41
+ field("annotations", optional($.annotations)),
42
+ field("attributes", optional($._attributes)),
43
+ ")",
44
+ ),
45
+
46
+ relationship: ($) =>
47
+ seq(
48
+ field("left", $.node),
49
+ field("kind", $._relationship_kind),
50
+ field("right", $._path),
51
+ ),
52
+
53
+ reference: ($) => field("identifier", $._identifier),
54
+
55
+ _identifier: ($) => choice($.symbol, $.string_literal, $.integer),
56
+
57
+ _attributes: ($) =>
58
+ choice(
59
+ choice(
60
+ field("identifier", $._identifier),
61
+ field("labels", $.labels),
62
+ field("record", $.record),
63
+ ),
64
+ seq(field("identifier", $._identifier), field("labels", $.labels)),
65
+ seq(field("identifier", $._identifier), field("record", $.record)),
66
+ seq(field("labels", $.labels), field("record", $.record)),
67
+ seq(
68
+ field("identifier", $._identifier),
69
+ field("labels", $.labels),
70
+ field("record", $.record),
71
+ ),
72
+ ),
73
+
74
+ _value: ($) =>
75
+ choice(
76
+ prec.right(2, $.range),
77
+ prec.right(1, $._numeric_literal),
78
+ prec(3, $.boolean_literal),
79
+ prec(2, $.tagged_string),
80
+ prec(1, $.symbol),
81
+ $.string_literal,
82
+ $.array,
83
+ $.map,
84
+ ),
85
+
86
+ _scalar_value: ($) =>
87
+ choice(
88
+ prec.right(2, $.range),
89
+ prec.right(1, $._numeric_literal),
90
+ prec(3, $.boolean_literal),
91
+ prec(2, $.tagged_string),
92
+ prec(1, $.symbol),
93
+ $.string_literal,
94
+ ),
95
+
96
+ array: ($) => seq("[", commaSep1($._scalar_value), "]"),
97
+
98
+ labels: ($) => repeat1($._label),
99
+
100
+ _label: ($) => seq(choice(":", "::"), $.symbol),
101
+
102
+ record: ($) => seq("{", commaSep($.property), "}"),
103
+
104
+ property: ($) =>
105
+ seq(
106
+ field("key", $._identifier),
107
+ choice(":", "::"),
108
+ field("value", $._value),
109
+ ),
110
+
111
+ map: ($) => seq("{", commaSep($.mapping), "}"),
112
+
113
+ mapping: ($) =>
114
+ seq(field("key", $._identifier), ":", field("value", $._scalar_value)),
115
+
116
+ symbol: ($) => token(/[a-zA-Z_][0-9a-zA-Z_.\-@]*/),
117
+
118
+ range: ($) =>
119
+ choice(
120
+ seq(
121
+ field("lower", $._numeric_literal),
122
+ "..",
123
+ field("upper", $._numeric_literal),
124
+ ),
125
+ seq(field("lower", $._numeric_literal), "..."),
126
+ seq("...", field("upper", $._numeric_literal)),
127
+ ),
128
+
129
+ boolean_literal: ($) => choice("true", "false"),
130
+
131
+ _numeric_literal: ($) =>
132
+ choice($.integer, $.decimal, $.hexadecimal, $.octal, $.measurement),
133
+
134
+ integer: ($) => {
135
+ const integer = /-?(0|[1-9]\d*)/;
111
136
  return token(integer);
112
137
  },
113
138
 
114
- decimal: $ => {
115
- const decimal = /-?(0|[1-9]\d*)\.\d+/;
139
+ decimal: ($) => {
140
+ const decimal = /-?(0|[1-9]\d*)\.\d+/;
116
141
  return token(decimal);
117
142
  },
118
143
 
119
- hexadecimal: $ => {
120
- const hexadecimal = /0x[0-9a-fA-F]+/;
144
+ hexadecimal: ($) => {
145
+ const hexadecimal = /0x[0-9a-fA-F]+/;
121
146
  return token(hexadecimal);
122
147
  },
123
148
 
124
- octal: $ => {
125
- const octal = /0[0-7]+/;
149
+ octal: ($) => {
150
+ const octal = /0[0-7]+/;
126
151
  return token(octal);
127
152
  },
128
153
 
129
- measurement: $ => {
154
+ measurement: ($) => {
130
155
  // /-?(?:[0-9]|[1-9][0-9]+)(?:\.[0-9]+)?[a-zA-Z]+\b(?!@)/
131
- const measurement = /-?(0|[1-9]\d*)([a-zA-Z]+)/;
156
+ const measurement = /-?(0|[1-9]\d*)([a-zA-Z]+)/;
132
157
  return token(measurement);
133
158
  },
134
159
 
135
- _string_literal: $ => choice(
136
- $.single_quoted_string,
137
- $.double_quoted_string,
138
- $.backticked_string,
139
- $.tagged_string,
140
- $.fenced_string
141
- ),
142
-
143
- single_quoted_string: $ => {
144
- const quoted = /'(\\['bfnrt/\\]|[^'\n])*'/;
145
- return token(quoted);
146
- },
147
-
148
- double_quoted_string: $ => {
149
- const quoted = /"(\\["bfnrt/\\]|[^"\n])*"/;
150
- return token(quoted);
151
- },
152
-
153
- backticked_string: $ => {
154
- const quoted = /`(\\[`bfnrt/\\]|[^`\n])*`/;
155
- return token(quoted);
156
- },
157
-
158
- tagged_string: $ => {
159
- const tagged = /[a-zA-Z@][0-9a-zA-Z_.@]*`[^`\n]*`/;
160
- return token(tagged);
161
- },
162
-
163
- fenced_string: $ => {
164
- const fenced = /```(\\[`bfnrt/\\]|[^`])*```/;
165
- return token(fenced);
166
- },
167
-
168
- _relationship_value: $ => choice(
169
- alias($.single_undirected, $.undirected),
170
- alias($.double_undirected, $.undirected),
171
- alias($.squiggle_undirected, $.undirected),
172
- alias($.single_bidirectional, $.bidirectional),
173
- alias($.double_bidirectional, $.bidirectional),
174
- alias($.squiggle_bidirectional, $.bidirectional),
175
- alias($.single_left, $.left),
176
- alias($.double_left, $.left),
177
- alias($.squiggle_left, $.left),
178
- alias($.single_right, $.right),
179
- alias($.double_right, $.right),
180
- alias($.squiggle_right, $.right)
181
- ),
182
-
183
- // undirected: $ => alias(choice(
184
- // $.single_undirected,
185
- // $.double_undirected,
186
- // $.squiggle_undirected
187
- // ), $.undirected),
188
-
189
- single_undirected: $ => seq("-", optional(seq("[", $._attributes, "]")), "-"),
190
- single_bidirectional: $ => seq("<-", optional(seq("[", $._attributes, "]")), "->"),
191
- single_right: $ => seq("-", optional(seq("[", $._attributes, "]")), "->"),
192
- single_left: $ => seq("<-", optional(seq("[", $._attributes, "]")), "-"),
193
-
194
- double_undirected: $ => seq("=", optional(seq("[", $._attributes, "]")), "="),
195
- double_bidirectional: $ => seq("<=", optional(seq("[", $._attributes, "]")), "=>"),
196
- double_right: $ => seq("=", optional(seq("[", $._attributes, "]")), "=>"),
197
- double_left: $ => seq("<=", optional(seq("[", $._attributes, "]")), "="),
198
-
199
- squiggle_undirected: $ => seq("~", optional(seq("[", $._attributes, "]")), "~"),
200
- squiggle_bidirectional: $ => seq("<~", optional(seq("[", $._attributes, "]")), "~>"),
201
- squiggle_right: $ => seq("~", optional(seq("[", $._attributes, "]")), "~>"),
202
- squiggle_left: $ => seq("<~", optional(seq("[", $._attributes, "]")), "~"),
203
- }
160
+ string_literal: ($) =>
161
+ choice(
162
+ $._single_quoted_string,
163
+ $._double_quoted_string,
164
+ $._backticked_string,
165
+ $._fenced_string,
166
+ ),
167
+
168
+ _single_quoted_string: ($) =>
169
+ delimit_string(alias($._single_quoted_text, $.string_content), "'"),
170
+
171
+ _single_quoted_text: ($) => token.immediate(/(\\['bfnrt/\\]|[^'\\\n])*/),
172
+
173
+ _double_quoted_string: ($) =>
174
+ delimit_string(alias($._double_quoted_text, $.string_content), '"'),
175
+
176
+ _double_quoted_text: ($) => token.immediate(/(\\["bfnrt/\\]|[^"\\\n])*/),
177
+
178
+ _backticked_string: ($) =>
179
+ delimit_string(alias($._backticked_text, $.string_content), "`"),
180
+
181
+ _backticked_text: ($) => token.immediate(/(\\[`bfnrt/\\]|[^`\\\n])*/),
182
+
183
+ _fenced_string: ($) => seq("```", $._fenced_string_body),
184
+
185
+ _fenced_string_body: ($) =>
186
+ seq(
187
+ "\n",
188
+ field(
189
+ "content",
190
+ optional(alias($._fenced_string_content, $.string_content)),
191
+ ),
192
+ "```",
193
+ ),
194
+
195
+ _fenced_string_content: ($) => token.immediate(/([^`]|`[^`]|``[^`])+/),
196
+
197
+ tagged_string: ($) =>
198
+ choice(
199
+ seq(
200
+ field("tag", $.symbol),
201
+ "`",
202
+ field("content", alias($._backticked_text, $.string_content)),
203
+ "`",
204
+ ),
205
+ seq(
206
+ "```",
207
+ field("tag", $.symbol),
208
+ "\n",
209
+ field(
210
+ "content",
211
+ optional(alias($._fenced_string_content, $.string_content)),
212
+ ),
213
+ "```",
214
+ ),
215
+ ),
216
+
217
+ _relationship_kind: ($) =>
218
+ choice(
219
+ $.undirected_arrow,
220
+ $.left_arrow,
221
+ $.right_arrow,
222
+ $.bidirectional_arrow,
223
+ ),
224
+
225
+ undirected_arrow: ($) =>
226
+ choice(
227
+ seq(
228
+ "-",
229
+ optional(
230
+ seq(
231
+ "[",
232
+ field("annotations", optional($.annotations)),
233
+ field("attributes", optional($._attributes)),
234
+ "]",
235
+ ),
236
+ ),
237
+ "-",
238
+ ),
239
+ seq(
240
+ "=",
241
+ optional(
242
+ seq(
243
+ "[",
244
+ field("annotations", optional($.annotations)),
245
+ field("attributes", optional($._attributes)),
246
+ "]",
247
+ ),
248
+ ),
249
+ "=",
250
+ ),
251
+ seq(
252
+ "~",
253
+ optional(
254
+ seq(
255
+ "[",
256
+ field("annotations", optional($.annotations)),
257
+ field("attributes", optional($._attributes)),
258
+ "]",
259
+ ),
260
+ ),
261
+ "~",
262
+ ),
263
+ ),
264
+
265
+ right_arrow: ($) =>
266
+ choice(
267
+ seq(
268
+ "-",
269
+ optional(
270
+ seq(
271
+ "[",
272
+ field("annotations", optional($.annotations)),
273
+ field("attributes", optional($._attributes)),
274
+ "]",
275
+ ),
276
+ ),
277
+ "->",
278
+ ),
279
+ seq(
280
+ "=",
281
+ optional(
282
+ seq(
283
+ "[",
284
+ field("annotations", optional($.annotations)),
285
+ field("attributes", optional($._attributes)),
286
+ "]",
287
+ ),
288
+ ),
289
+ "=>",
290
+ ),
291
+ seq(
292
+ "~",
293
+ optional(
294
+ seq(
295
+ "[",
296
+ field("annotations", optional($.annotations)),
297
+ field("attributes", optional($._attributes)),
298
+ "]",
299
+ ),
300
+ ),
301
+ "~>",
302
+ ),
303
+ ),
304
+
305
+ left_arrow: ($) =>
306
+ choice(
307
+ seq(
308
+ "<-",
309
+ optional(
310
+ seq(
311
+ "[",
312
+ field("annotations", optional($.annotations)),
313
+ field("attributes", optional($._attributes)),
314
+ "]",
315
+ ),
316
+ ),
317
+ "-",
318
+ ),
319
+ seq(
320
+ "<=",
321
+ optional(
322
+ seq(
323
+ "[",
324
+ field("annotations", optional($.annotations)),
325
+ field("attributes", optional($._attributes)),
326
+ "]",
327
+ ),
328
+ ),
329
+ "=",
330
+ ),
331
+ seq(
332
+ "<~",
333
+ optional(
334
+ seq(
335
+ "[",
336
+ field("annotations", optional($.annotations)),
337
+ field("attributes", optional($._attributes)),
338
+ "]",
339
+ ),
340
+ ),
341
+ "~",
342
+ ),
343
+ ),
344
+
345
+ bidirectional_arrow: ($) =>
346
+ choice(
347
+ seq(
348
+ "<-",
349
+ optional(
350
+ seq(
351
+ "[",
352
+ field("annotations", optional($.annotations)),
353
+ field("attributes", optional($._attributes)),
354
+ "]",
355
+ ),
356
+ ),
357
+ "->",
358
+ ),
359
+ seq(
360
+ "<=",
361
+ optional(
362
+ seq(
363
+ "[",
364
+ field("annotations", optional($.annotations)),
365
+ field("attributes", optional($._attributes)),
366
+ "]",
367
+ ),
368
+ ),
369
+ "=>",
370
+ ),
371
+ seq(
372
+ "<~",
373
+ optional(
374
+ seq(
375
+ "[",
376
+ field("annotations", optional($.annotations)),
377
+ field("attributes", optional($._attributes)),
378
+ "]",
379
+ ),
380
+ ),
381
+ "~>",
382
+ ),
383
+ ),
384
+
385
+ comment: ($) => token(seq("//", /.*/)),
386
+ },
204
387
  });
205
388
 
389
+ function delimit_string(rule, delimiter) {
390
+ return seq(delimiter, field("content", rule), delimiter);
391
+ }
206
392
  /**
207
393
  * Creates a rule to match one or more of the rules separated by a comma
208
394
  *
@@ -212,7 +398,7 @@ module.exports = grammar({
212
398
  *
213
399
  */
214
400
  function commaSep1(rule) {
215
- return seq(rule, repeat(seq(',', rule)));
401
+ return seq(rule, repeat(seq(",", rule)));
216
402
  }
217
403
 
218
404
  /**
@@ -227,7 +413,6 @@ function commaSep(rule) {
227
413
  return optional(commaSep1(rule));
228
414
  }
229
415
 
230
-
231
416
  /**
232
417
  * Creates a rule to match one or more of the rules separated by a colon
233
418
  *
@@ -237,10 +422,9 @@ function commaSep(rule) {
237
422
  *
238
423
  */
239
424
  function colonSep1(rule) {
240
- return seq(rule, repeat(seq(':', rule)));
425
+ return seq(rule, repeat(seq(":", rule)));
241
426
  }
242
427
 
243
-
244
428
  /**
245
429
  * Creates a rule to match one or more of the rules separated by another rule
246
430
  *
@@ -251,4 +435,4 @@ function colonSep1(rule) {
251
435
  */
252
436
  function ruleSep1(rule, separator) {
253
437
  return seq(rule, repeat(seq(separator, rule)));
254
- }
438
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gram-data/tree-sitter-gram",
3
- "version": "0.1.11",
3
+ "version": "0.2.1",
4
4
  "description": "subject-oriented notation for structured data",
5
5
  "homepage": "https://gram-data.github.io",
6
6
  "repository": {
@@ -13,13 +13,18 @@
13
13
  "install": "node-gyp-build",
14
14
  "prestart": "tree-sitter build --wasm",
15
15
  "start": "tree-sitter playground",
16
- "test": "node --test bindings/node/*_test.js"
16
+ "test": "node --test bindings/node/*_test.js",
17
+ "zed:dev": "ZED_REPO_MODE=dev bash scripts/prepare-zed-extension.sh",
18
+ "zed:publish": "ZED_REPO_MODE=pub bash scripts/prepare-zed-extension.sh"
17
19
  },
18
20
  "keywords": [
19
21
  "tree-sitter",
20
22
  "parser",
21
23
  "gram",
22
- "json"
24
+ "json",
25
+ "zed",
26
+ "editor",
27
+ "syntax-highlighting"
23
28
  ],
24
29
  "files": [
25
30
  "grammar.js",
@@ -27,7 +32,8 @@
27
32
  "prebuilds/**",
28
33
  "bindings/node/*",
29
34
  "queries/*",
30
- "src/**"
35
+ "src/**",
36
+ "editors/**"
31
37
  ],
32
38
  "author": "",
33
39
  "license": "ISC",