-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.js
More file actions
460 lines (409 loc) · 16.6 KB
/
grammar.js
File metadata and controls
460 lines (409 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// SPDX-License-Identifier: PMPL-1.0-or-later
// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>
//
// tree-sitter-k9 — Tree-sitter grammar for the K9 format.
//
// K9 has two syntax variants:
// 1. Kennel level (.k9) — YAML-like with key: value pairs, pedigree blocks
// 2. Yard/Hunt level (.k9.ncl) — Nickel syntax with let bindings, contracts,
// type annotations, record literals, and recipe blocks
//
// Both variants share:
// - Magic number: K9! (first line)
// - Trust levels: kennel, yard, hunt (as enum tags 'Kennel, 'Yard, 'Hunt)
// - Pedigree blocks: name, version, description, security_level
//
// This grammar handles the full K9 format including the Nickel-derived syntax.
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
// Precedence levels for expression parsing (higher = tighter binding).
const PREC = {
OR: 1,
AND: 2,
COMPARE: 3,
ADD: 4,
CONCAT: 4,
MUL: 5,
MERGE: 6,
PIPE: 0,
APP: 7,
};
module.exports = grammar({
name: "k9",
extras: ($) => [/\s/, $.line_comment],
word: ($) => $.identifier,
conflicts: ($) => [],
rules: {
// ---------------------------------------------------------------------------
// Top-level document: begins with the K9! magic number, followed by optional
// comment headers, then the document body.
// ---------------------------------------------------------------------------
document: ($) =>
seq(
$.magic_number,
optional($.header_block),
choice(
$.record_literal, // Nickel-style (.k9.ncl)
$.kennel_body, // YAML-like (.k9)
),
),
// ---------------------------------------------------------------------------
// Magic number: K9! — mandatory first line identifying the file as K9 format.
// ---------------------------------------------------------------------------
magic_number: ($) => /K9!\n/,
// ---------------------------------------------------------------------------
// Header block: comment lines after the magic number, before document body.
// Includes SPDX headers, security level annotations, and documentation.
// ---------------------------------------------------------------------------
header_block: ($) =>
prec.right(repeat1($.header_comment)),
header_comment: ($) => token(prec(2, /#[^\n]*\n/)),
// =========================================================================
// KENNEL-LEVEL SYNTAX (.k9) — YAML-like key-value format
// =========================================================================
// ---------------------------------------------------------------------------
// Kennel body: flat or nested key-value pairs, pedigree blocks, and
// list items in YAML-like syntax.
// ---------------------------------------------------------------------------
kennel_body: ($) =>
repeat1($._kennel_entry),
_kennel_entry: ($) =>
choice(
$.pedigree_block,
$.kennel_pair,
$.kennel_list_item,
),
// ---------------------------------------------------------------------------
// Pedigree block: structured metadata block declaring component identity.
// pedigree:
// name: my-component
// version: 1.0.0
// description: A brief description
// security_level: kennel
// ---------------------------------------------------------------------------
pedigree_block: ($) =>
prec.right(seq(
"pedigree",
":",
/\n/,
repeat1($.kennel_nested_pair),
)),
// ---------------------------------------------------------------------------
// Kennel key-value pair: simple key: value at the top level.
// ---------------------------------------------------------------------------
kennel_pair: ($) =>
seq(
$.identifier,
":",
/[ \t]+/,
$.kennel_value,
/\n/,
),
// ---------------------------------------------------------------------------
// Kennel nested pair: indented key: value within a block.
// ---------------------------------------------------------------------------
kennel_nested_pair: ($) =>
seq(
/[ \t]+/,
$.identifier,
":",
/[ \t]+/,
$.kennel_value,
/\n/,
),
// ---------------------------------------------------------------------------
// Kennel value: value types in YAML-like syntax.
// ---------------------------------------------------------------------------
kennel_value: ($) =>
choice(
$.string_literal,
$.number_literal,
$.boolean_literal,
$.trust_level,
$.kennel_unquoted,
),
// ---------------------------------------------------------------------------
// Kennel list item: YAML-style list entry.
// ---------------------------------------------------------------------------
kennel_list_item: ($) =>
seq(
optional(/[ \t]+/),
"-",
/[ \t]+/,
$.kennel_value,
/\n/,
),
// ---------------------------------------------------------------------------
// Kennel unquoted string: bare string value continuing to end of line.
// ---------------------------------------------------------------------------
kennel_unquoted: ($) => /[^\n#"'\[\]{}]+/,
// =========================================================================
// YARD/HUNT-LEVEL SYNTAX (.k9.ncl) — Nickel-derived format
// =========================================================================
// ---------------------------------------------------------------------------
// Record literal: Nickel-style { key = value, ... } blocks.
// The primary structural element of .k9.ncl files.
// ---------------------------------------------------------------------------
record_literal: ($) =>
seq(
"{",
optional($.record_body),
"}",
),
record_body: ($) =>
seq(
$._record_entry,
repeat(seq(optional(","), $._record_entry)),
optional(","),
),
_record_entry: ($) =>
choice(
$.record_field,
$.let_binding,
),
// ---------------------------------------------------------------------------
// Record field: key = value assignment within a record.
// Supports optional type contracts (| Type) and default values.
// name = "my-component",
// port | Num = 8080,
// target_dir | String | std.string.NonEmpty = "/tmp",
// ---------------------------------------------------------------------------
record_field: ($) =>
seq(
$.field_name,
repeat($.type_contract),
optional(seq("=", $._expression)),
),
// ---------------------------------------------------------------------------
// Field name: identifier or quoted string used as a record key.
// ---------------------------------------------------------------------------
field_name: ($) =>
choice(
$.identifier,
$.string_literal,
),
// ---------------------------------------------------------------------------
// Type contract: Nickel type annotation using | operator.
// | String
// | Num
// | Bool
// | std.string.NonEmpty
// ---------------------------------------------------------------------------
type_contract: ($) =>
seq("|", $.type_expression),
// ---------------------------------------------------------------------------
// Type expression: a type name, possibly qualified with module paths.
// ---------------------------------------------------------------------------
type_expression: ($) =>
choice(
$.builtin_type,
$.qualified_identifier,
$.identifier,
),
// ---------------------------------------------------------------------------
// Built-in types: Nickel primitive types.
// ---------------------------------------------------------------------------
builtin_type: ($) =>
choice("String", "Num", "Bool", "Dyn"),
// ---------------------------------------------------------------------------
// Let binding: Nickel let expression for local definitions.
// let name = "value" in
// let config = { ... } in
// ---------------------------------------------------------------------------
let_binding: ($) =>
seq(
"let",
$.identifier,
optional($.type_contract),
"=",
$._expression,
optional("in"),
),
// ---------------------------------------------------------------------------
// Expression: any value or computation in the Nickel dialect.
// Uses explicit precedence levels to avoid ambiguity.
// ---------------------------------------------------------------------------
_expression: ($) =>
choice(
$._primary_expression,
$.binary_expression,
$.if_expression,
$.function_expression,
$.import_expression,
),
// ---------------------------------------------------------------------------
// Primary expression: atomic or grouped expressions.
// ---------------------------------------------------------------------------
_primary_expression: ($) =>
choice(
$.string_literal,
$.number_literal,
$.boolean_literal,
$.null_literal,
$.trust_level,
$.enum_tag,
$.identifier,
$.qualified_identifier,
$.record_literal,
$.array_literal,
$.multiline_string,
$.parenthesized_expression,
),
// ---------------------------------------------------------------------------
// Parenthesized expression: (expr)
// ---------------------------------------------------------------------------
parenthesized_expression: ($) =>
seq("(", $._expression, ")"),
// ---------------------------------------------------------------------------
// Binary expression: Nickel operators with explicit precedence.
// ---------------------------------------------------------------------------
binary_expression: ($) =>
choice(
prec.left(PREC.OR, seq($._expression, "||", $._expression)),
prec.left(PREC.AND, seq($._expression, "&&", $._expression)),
prec.left(PREC.COMPARE, seq($._expression, "==", $._expression)),
prec.left(PREC.COMPARE, seq($._expression, "!=", $._expression)),
prec.left(PREC.COMPARE, seq($._expression, "<", $._expression)),
prec.left(PREC.COMPARE, seq($._expression, ">", $._expression)),
prec.left(PREC.COMPARE, seq($._expression, "<=", $._expression)),
prec.left(PREC.COMPARE, seq($._expression, ">=", $._expression)),
prec.left(PREC.ADD, seq($._expression, "+", $._expression)),
prec.left(PREC.ADD, seq($._expression, "-", $._expression)),
prec.left(PREC.CONCAT, seq($._expression, "++", $._expression)),
prec.left(PREC.MUL, seq($._expression, "*", $._expression)),
prec.left(PREC.MUL, seq($._expression, "/", $._expression)),
prec.left(PREC.MERGE, seq($._expression, "&", $._expression)),
prec.left(PREC.PIPE, seq($._expression, "|>", $._expression)),
),
// ---------------------------------------------------------------------------
// If expression: Nickel conditional.
// if condition then expr1 else expr2
// ---------------------------------------------------------------------------
if_expression: ($) =>
prec.right(seq(
"if",
$._expression,
"then",
$._expression,
optional(seq("else", $._expression)),
)),
// ---------------------------------------------------------------------------
// Function expression: Nickel lambda syntax.
// fun x => x + 1
// ---------------------------------------------------------------------------
function_expression: ($) =>
prec.right(seq(
"fun",
repeat1($.identifier),
"=>",
$._expression,
)),
// ---------------------------------------------------------------------------
// Import expression: Nickel import statement.
// import "path/to/file.ncl"
// ---------------------------------------------------------------------------
import_expression: ($) =>
seq("import", $.string_literal),
// ---------------------------------------------------------------------------
// String literal: double or single-quoted strings with escape sequences.
// ---------------------------------------------------------------------------
string_literal: ($) =>
choice(
seq('"', optional($.string_content), '"'),
),
string_content: ($) =>
repeat1(
choice(
$.string_fragment,
$.escape_sequence,
$.string_interpolation,
),
),
string_fragment: ($) => token.immediate(prec(1, /[^"\\%]+/)),
escape_sequence: ($) => token.immediate(/\\[nrt\\'"0]/),
// ---------------------------------------------------------------------------
// String interpolation: %{expression} within double-quoted strings.
// "mkdir -p %{config.target_dir}"
// ---------------------------------------------------------------------------
string_interpolation: ($) =>
seq(
token.immediate("%{"),
$._expression,
"}",
),
// ---------------------------------------------------------------------------
// Multiline string: Nickel m%"..."% syntax for heredoc-like strings.
// m%"
// This is a multiline string.
// "%
// ---------------------------------------------------------------------------
multiline_string: ($) =>
seq(
'm%"',
optional($.multiline_content),
'"%',
),
multiline_content: ($) => /[^"]+/,
// ---------------------------------------------------------------------------
// Number literal: integer or floating-point number.
// ---------------------------------------------------------------------------
number_literal: ($) => /\-?[0-9]+(\.[0-9]+)?/,
// ---------------------------------------------------------------------------
// Boolean literal: true or false.
// ---------------------------------------------------------------------------
boolean_literal: ($) => choice("true", "false"),
// ---------------------------------------------------------------------------
// Null literal: null value.
// ---------------------------------------------------------------------------
null_literal: ($) => "null",
// ---------------------------------------------------------------------------
// Trust level: K9 security tier enum tags.
// Kennel = data-only, Yard = validated, Hunt = full execution.
// Uses Nickel enum tag syntax: 'Name
// ---------------------------------------------------------------------------
trust_level: ($) =>
token(prec(2, choice(
"'Kennel",
"'Yard",
"'Hunt",
))),
// ---------------------------------------------------------------------------
// Enum tag: generic Nickel enum variant (beyond trust levels).
// ---------------------------------------------------------------------------
enum_tag: ($) => token(prec(1, /\'[A-Z][a-zA-Z0-9_]*/)),
// ---------------------------------------------------------------------------
// Array literal: Nickel array syntax.
// ["item1", "item2", "item3"]
// ---------------------------------------------------------------------------
array_literal: ($) =>
seq(
"[",
optional(
seq(
$._expression,
repeat(seq(",", $._expression)),
optional(","),
),
),
"]",
),
// ---------------------------------------------------------------------------
// Identifier: variable or field name.
// ---------------------------------------------------------------------------
identifier: ($) => /[a-zA-Z_][a-zA-Z0-9_\-]*/,
// ---------------------------------------------------------------------------
// Qualified identifier: dotted path for module access.
// std.string.NonEmpty
// config.target_dir
// ---------------------------------------------------------------------------
qualified_identifier: ($) =>
prec.left(PREC.APP, seq(
$.identifier,
repeat1(seq(".", $.identifier)),
)),
// ---------------------------------------------------------------------------
// Line comment: # to end of line.
// ---------------------------------------------------------------------------
line_comment: ($) => token(prec(-1, /#[^\n]*/)),
},
});