@gram-data/tree-sitter-gram 0.1.6

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/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Gram - a lightweight, flexible graph notation
2
+
3
+ Gram describes the structure and content of graphs, without semantics.
4
+
5
+ A graph is composed from a sequence of patterns, where each pattern
6
+ is a sequence of graph elements.
7
+
8
+ ## Graph elements - nodes
9
+
10
+ The smallest graph pattern is an empty node.
11
+ ```
12
+ ()
13
+ ```
14
+
15
+ Nodes may include properties using classic curly-braces-styled notation. This expression
16
+ is like an anonymous object that appears inside some other data structure, perhaps a list
17
+ like `const people = [{name:ABK}]`:
18
+
19
+ ```
20
+ ({name:"ABK"})
21
+ ```
22
+
23
+ Nodes may self-identify themselves. This is like an object that is aware of the variable
24
+ name you've given it in a programming language -- `const abk = {name:"ABK"}`
25
+ ```
26
+ (abk {name:"ABK"})
27
+ ```
28
+
29
+ Or, the identity could be considered the representative value of the node as in these examples:
30
+ ```
31
+ (1), (true), (`heir to the Iron Throne`)
32
+ ```
33
+
34
+ Nodes may also provide labels to associate with other nodes. This is like having an
35
+ introspectable object in a programming language -- `const abk:Person = {name:"ABK"}`:
36
+ ```
37
+ (abk:Person {name:"ABK"}), (michael:Person {name:"Michael"})
38
+ ```
39
+
40
+ It's ok to use multiple labels, which could be subsets, singletons, or intersections:
41
+ ```
42
+ (abk:Person:Author {name:"ABK"}), (one:Person:King:Fictional {name: "John Snow"}),
43
+ (drogon:Dragon:Fictional {name:"Drogon"})
44
+ ```
45
+
46
+
47
+ ## FAQ
48
+
49
+ - Annotation vs self-loop?
50
+
51
+ Q: What is the difference between an annotation and a self-loop?
52
+ A: An annotation provides extra information about a single target, while a self-loop provides
53
+ information about a source,target pair where either the source or target may have been different,
54
+ but happen to be the same.
55
+
56
+ For example, `@physics("rigid")(a:Player)` annotates a player entity with information for the physics subsystem.
57
+ The information is particular to the player and not about how the player relates to itself.
58
+
59
+ As a self-loop, `(a:Player)-[:MESSAGE]->(a:Player)` is a message a player sent to themselves that could've
60
+ been sent to another player. The message has a specific source and target, which happen to be the same.
61
+
package/binding.gyp ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "targets": [
3
+ {
4
+ "target_name": "tree_sitter_gram_binding",
5
+ "dependencies": [
6
+ "<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
7
+ ],
8
+ "include_dirs": [
9
+ "src",
10
+ ],
11
+ "sources": [
12
+ "bindings/node/binding.cc",
13
+ "src/parser.c",
14
+ # NOTE: if your language has an external scanner, add it here.
15
+ ],
16
+ "conditions": [
17
+ ["OS!='win'", {
18
+ "cflags_c": [
19
+ "-std=c11",
20
+ ],
21
+ }, { # OS == "win"
22
+ "cflags_c": [
23
+ "/std:c11",
24
+ "/utf-8",
25
+ ],
26
+ }],
27
+ ],
28
+ }
29
+ ]
30
+ }
@@ -0,0 +1,20 @@
1
+ #include <napi.h>
2
+
3
+ typedef struct TSLanguage TSLanguage;
4
+
5
+ extern "C" TSLanguage *tree_sitter_gram();
6
+
7
+ // "tree-sitter", "language" hashed with BLAKE2
8
+ const napi_type_tag LANGUAGE_TYPE_TAG = {
9
+ 0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16
10
+ };
11
+
12
+ Napi::Object Init(Napi::Env env, Napi::Object exports) {
13
+ exports["name"] = Napi::String::New(env, "gram");
14
+ auto language = Napi::External<TSLanguage>::New(env, tree_sitter_gram());
15
+ language.TypeTag(&LANGUAGE_TYPE_TAG);
16
+ exports["language"] = language;
17
+ return exports;
18
+ }
19
+
20
+ NODE_API_MODULE(tree_sitter_gram_binding, Init)
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" />
2
+
3
+ const assert = require("node:assert");
4
+ const { test } = require("node:test");
5
+
6
+ test("can load grammar", () => {
7
+ const parser = new (require("tree-sitter"))();
8
+ assert.doesNotThrow(() => parser.setLanguage(require(".")));
9
+ });
@@ -0,0 +1,28 @@
1
+ type BaseNode = {
2
+ type: string;
3
+ named: boolean;
4
+ };
5
+
6
+ type ChildNode = {
7
+ multiple: boolean;
8
+ required: boolean;
9
+ types: BaseNode[];
10
+ };
11
+
12
+ type NodeInfo =
13
+ | (BaseNode & {
14
+ subtypes: BaseNode[];
15
+ })
16
+ | (BaseNode & {
17
+ fields: { [name: string]: ChildNode };
18
+ children: ChildNode[];
19
+ });
20
+
21
+ type Language = {
22
+ name: string;
23
+ language: unknown;
24
+ nodeTypeInfo: NodeInfo[];
25
+ };
26
+
27
+ declare const language: Language;
28
+ export = language;
@@ -0,0 +1,7 @@
1
+ const root = require("path").join(__dirname, "..", "..");
2
+
3
+ module.exports = require("node-gyp-build")(root);
4
+
5
+ try {
6
+ module.exports.nodeTypeInfo = require("../../src/node-types.json");
7
+ } catch (_) {}
package/grammar.js ADDED
@@ -0,0 +1,251 @@
1
+ module.exports = grammar({
2
+ name: 'gram',
3
+
4
+ rules: {
5
+
6
+ gram: $ => seq(
7
+ field('root', optional($.record)),
8
+ repeat($.pattern)
9
+ ),
10
+
11
+ pattern: $ => seq(optional(repeat($.annotation)), commaSep1($._patternComponent)),
12
+
13
+ _patternComponent: $ => choice(
14
+ $.subject,
15
+ $._path
16
+ ),
17
+
18
+ subject: $ => seq("[", optional($._attributes), optional(field("association", $._association)),"]"),
19
+
20
+ annotation: $ => prec(9999,seq(
21
+ "@",
22
+ field('key', $.symbol),
23
+ "(",
24
+ field('value', $._value),
25
+ ")"
26
+ )),
27
+
28
+ _path: $ => choice(
29
+ $.relationship,
30
+ $.node
31
+ ),
32
+
33
+ node: $ => seq("(", optional($._attributes),")"),
34
+
35
+ relationship: $ => seq(field("left", $.node), field("value", $._relationship_value), field("right", $._path)),
36
+
37
+ _association: $ => choice($.membership, $.ordering),
38
+
39
+ membership: $ => seq(
40
+ "|",
41
+ optional(seq(optional(field("labels", $.labels)), optional(field("record", $.record)), "|")),
42
+ commaSep1($.member)),
43
+
44
+ ordering: $ => seq(
45
+ "-",
46
+ optional(seq("[", optional(field("labels", $.labels)), optional(field("record", $.record)), "]-")), ">",
47
+ commaSep1($.member)),
48
+
49
+ _reference: $ => $._value,
50
+
51
+ member: $ => seq(
52
+ optional(repeat($.annotation)),
53
+ choice(
54
+ $._reference,
55
+ $._patternComponent
56
+ ),
57
+ ),
58
+
59
+ _attributes: $ => choice(
60
+ choice(field("identifier", $._value), field("labels", $.labels), field("record", $.record)),
61
+ seq(field("identifier", $._value), field("labels", $.labels)),
62
+ seq(field("identifier", $._value), field("record", $.record)),
63
+ seq(field("labels", $.labels), field("record", $.record)),
64
+ seq(field("identifier", $._value), field("labels", $.labels), field("record", $.record))
65
+ ),
66
+
67
+ _value: $ => choice(
68
+ $.symbol,
69
+ $._numeric_literal,
70
+ $._string_literal,
71
+ $.range,
72
+ $.math_symbol,
73
+ $.greek,
74
+ $.pictograph
75
+ ),
76
+
77
+ labels: $ => repeat1($.label),
78
+
79
+ label: $ => seq(field("binder", $.binder), $.symbol),
80
+
81
+ binder: $ => choice(token(":"), token("::"), token("@")),
82
+
83
+ record: $ => seq("{", commaSep($.property), "}"),
84
+
85
+ property: $ => seq(
86
+ field('key', $.symbol),
87
+ field('binder', $.binder),
88
+ field('value', $._value),
89
+ optional(field('cardinality', choice('!', '?', '*', '+')))
90
+ ),
91
+
92
+ symbol: $ => token(/[a-zA-Z_][0-9a-zA-Z_.\-]*/),
93
+
94
+ greek: $ => token(/[\u03B1-\u03C9\u0391-\u03A9]/),
95
+ math_symbol: $ => token(/\p{Other_Math}/),
96
+ pictograph: $ => token(/[\u2650-\u26FF]/),
97
+
98
+ range: $ => choice(
99
+ seq(field("lower", $._numeric_literal), "..", field("upper", $._numeric_literal)),
100
+ seq(field("lower", $._numeric_literal), "..."),
101
+ seq("...", field("upper", $._numeric_literal))
102
+ ),
103
+
104
+ _numeric_literal: $ => choice(
105
+ $.integer,
106
+ $.decimal,
107
+ $.hexadecimal,
108
+ $.octal,
109
+ $.measurement
110
+ ),
111
+
112
+ integer: $ => {
113
+ const integer = /-?(0|[1-9]\d*)/;
114
+ return token(integer);
115
+ },
116
+
117
+ decimal: $ => {
118
+ const decimal = /-?(0|[1-9]\d*)\.\d+/;
119
+ return token(decimal);
120
+ },
121
+
122
+ hexadecimal: $ => {
123
+ const hexadecimal = /0x[0-9a-fA-F]+/;
124
+ return token(hexadecimal);
125
+ },
126
+
127
+ octal: $ => {
128
+ const octal = /0[0-7]+/;
129
+ return token(octal);
130
+ },
131
+
132
+ measurement: $ => {
133
+ // /-?(?:[0-9]|[1-9][0-9]+)(?:\.[0-9]+)?[a-zA-Z]+\b(?!@)/
134
+ const measurement = /-?(0|[1-9]\d*)([a-zA-Z]+)/;
135
+ return token(measurement);
136
+ },
137
+
138
+ _string_literal: $ => choice(
139
+ $.single_quoted_string,
140
+ $.double_quoted_string,
141
+ $.backticked_string,
142
+ $.tagged_string,
143
+ $.fenced_string
144
+ ),
145
+
146
+ single_quoted_string: $ => {
147
+ const quoted = /'(\\['bfnrt/\\]|[^'\n])*'/;
148
+ return token(quoted);
149
+ },
150
+
151
+ double_quoted_string: $ => {
152
+ const quoted = /"(\\["bfnrt/\\]|[^"\n])*"/;
153
+ return token(quoted);
154
+ },
155
+
156
+ backticked_string: $ => {
157
+ const quoted = /`(\\[`bfnrt/\\]|[^`\n])*`/;
158
+ return token(quoted);
159
+ },
160
+
161
+ tagged_string: $ => {
162
+ const tagged = /[a-zA-Z][0-9a-zA-Z_.@]*`[^`\n]*`/;
163
+ return token(tagged);
164
+ },
165
+
166
+ fenced_string: $ => {
167
+ const fenced = /```(\\[`bfnrt/\\]|[^`])*```/;
168
+ return token(fenced);
169
+ },
170
+
171
+ _relationship_value: $ => choice(
172
+ $.single_undirected,
173
+ $.single_bidirectional,
174
+ $.single_right,
175
+ $.single_left,
176
+ $.double_undirected,
177
+ $.double_bidirectional,
178
+ $.double_right,
179
+ $.double_left,
180
+ $.squiggle_undirected,
181
+ $.squiggle_bidirectional,
182
+ $.squiggle_right,
183
+ $.squiggle_left,
184
+ ),
185
+
186
+ single_undirected: $ => seq("-", optional(seq("[", $._attributes, "]")), "-"),
187
+ single_bidirectional: $ => seq("<-", optional(seq("[", $._attributes, "]")), "->"),
188
+ single_right: $ => seq("-", optional(seq("[", $._attributes, "]")), "->"),
189
+ single_left: $ => seq("<-", optional(seq("[", $._attributes, "]")), "-"),
190
+
191
+ double_undirected: $ => seq("=", optional(seq("[", $._attributes, "]")), "="),
192
+ double_bidirectional: $ => seq("<=", optional(seq("[", $._attributes, "]")), "=>"),
193
+ double_right: $ => seq("=", optional(seq("[", $._attributes, "]")), "=>"),
194
+ double_left: $ => seq("<=", optional(seq("[", $._attributes, "]")), "="),
195
+
196
+ squiggle_undirected: $ => seq("~", optional(seq("[", $._attributes, "]")), "~"),
197
+ squiggle_bidirectional: $ => seq("<~", optional(seq("[", $._attributes, "]")), "~>"),
198
+ squiggle_right: $ => seq("~", optional(seq("[", $._attributes, "]")), "~>"),
199
+ squiggle_left: $ => seq("<~", optional(seq("[", $._attributes, "]")), "~"),
200
+ }
201
+ });
202
+
203
+ /**
204
+ * Creates a rule to match one or more of the rules separated by a comma
205
+ *
206
+ * @param {RuleOrLiteral} rule
207
+ *
208
+ * @return {SeqRule}
209
+ *
210
+ */
211
+ function commaSep1(rule) {
212
+ return seq(rule, repeat(seq(',', rule)));
213
+ }
214
+
215
+ /**
216
+ * Creates a rule to optionally match one or more of the rules separated by a comma
217
+ *
218
+ * @param {RuleOrLiteral} rule
219
+ *
220
+ * @return {ChoiceRule}
221
+ *
222
+ */
223
+ function commaSep(rule) {
224
+ return optional(commaSep1(rule));
225
+ }
226
+
227
+
228
+ /**
229
+ * Creates a rule to match one or more of the rules separated by a colon
230
+ *
231
+ * @param {RuleOrLiteral} rule
232
+ *
233
+ * @return {SeqRule}
234
+ *
235
+ */
236
+ function colonSep1(rule) {
237
+ return seq(rule, repeat(seq(':', rule)));
238
+ }
239
+
240
+
241
+ /**
242
+ * Creates a rule to match one or more of the rules separated by another rule
243
+ *
244
+ * @param {RuleOrLiteral} rule
245
+ *
246
+ * @return {SeqRule}
247
+ *
248
+ */
249
+ function ruleSep1(rule, separator) {
250
+ return seq(rule, repeat(seq(separator, rule)));
251
+ }
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@gram-data/tree-sitter-gram",
3
+ "private": false,
4
+ "version": "0.1.6",
5
+ "description": "subject-oriented notation for structured data",
6
+ "main": "bindings/node",
7
+ "types": "bindings/node",
8
+ "scripts": {
9
+ "install": "node-gyp-build",
10
+ "prestart": "tree-sitter build --wasm",
11
+ "start": "tree-sitter playground",
12
+ "test": "node --test bindings/node/*_test.js"
13
+ },
14
+ "keywords": [],
15
+ "files": [
16
+ "grammar.js",
17
+ "binding.gyp",
18
+ "prebuilds/**",
19
+ "bindings/node/*",
20
+ "queries/*",
21
+ "src/**"
22
+ ],
23
+ "author": "",
24
+ "license": "ISC",
25
+ "dependencies": {
26
+ "node-gyp-build": "^4.8.2",
27
+ "node-addon-api": "^8.1.0"
28
+ },
29
+ "peerDependencies": {
30
+ "tree-sitter": "^0.21.0"
31
+ },
32
+ "peerDependenciesMeta": {
33
+ "tree_sitter": {
34
+ "optional": true
35
+ }
36
+ },
37
+ "devDependencies": {
38
+ "eslint": "^9.12.0",
39
+ "node-gyp": "^10.2.0",
40
+ "prebuildify": "^6.0.1",
41
+ "tree-sitter-cli": "^0.24.2"
42
+ },
43
+ "tree-sitter": [
44
+ {
45
+ "scope": "source.gram",
46
+ "file-types": [
47
+ "gram"
48
+ ]
49
+ }
50
+ ]
51
+ }