-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.js
More file actions
149 lines (132 loc) · 3.96 KB
/
grammar.js
File metadata and controls
149 lines (132 loc) · 3.96 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
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
// Tree-sitter grammar for NATS server configuration files.
//
// The NATS server config format is a JSON-like superset that supports:
// - Multiple comment styles (# and //)
// - Three key-value separators (=, :, whitespace)
// - Block definitions with { }
// - Include directives
// - Variable definitions and $references
// - Unquoted strings, booleans, numbers with size/duration suffixes
//
// Reference: https://docs.nats.io/running-a-nats-service/configuration
module.exports = grammar({
name: "nats_server_conf",
extras: ($) => [/\s/],
// External tokens handled by src/scanner.c:
// - bare_string: unquoted values like nats-route://host:6222
// - comment: # and // comments (external to resolve // in URLs)
// - identifier: keys and block names
// - boolean: true/false/yes/no/on/off
externals: ($) => [
$.bare_string,
$.comment,
$.identifier,
$.boolean,
],
rules: {
source_file: ($) => repeat(choice($._statement, $.comment)),
_statement: ($) =>
choice($.include_directive, $.block_definition, $.pair),
// ---------------------------------------------------------------
// Include directive: include ./path/to/file.conf
// ---------------------------------------------------------------
include_directive: ($) =>
seq(
field("keyword", alias("include", $.include_keyword)),
field("path", $.string)
),
// ---------------------------------------------------------------
// Block definition: identifier [= | :] { ... }
// ---------------------------------------------------------------
block_definition: ($) =>
prec(
1,
seq(
field("name", $.identifier),
optional(choice("=", ":")),
field("body", $.block)
)
),
block: ($) =>
seq(
"{",
repeat(choice($._statement, $.comment, ",")),
"}"
),
// ---------------------------------------------------------------
// Key-value pair
// Requires = or : separator to avoid ambiguity with bare_string values.
// The whitespace-only separator (key value) is rare in practice;
// the include directive is the primary user of that pattern.
// ---------------------------------------------------------------
pair: ($) =>
seq(
field("key", $.identifier),
choice("=", ":"),
field("value", $._value)
),
// ---------------------------------------------------------------
// Values
// ---------------------------------------------------------------
_value: ($) =>
choice(
$.string,
$.number,
$.boolean,
$.variable_reference,
$.array
),
// Strings: quoted or unquoted (bare)
string: ($) =>
choice($._quoted_string, $.bare_string),
_quoted_string: ($) =>
token(seq('"', repeat(choice(/[^"\\]/, /\\./)), '"')),
// Numbers: integers and floats, with optional size/duration suffixes
number: ($) =>
token(
seq(
optional("-"),
choice(
seq(/[0-9]+/, ".", /[0-9]+/),
/[0-9]+/
),
optional(
token.immediate(
choice(
/[kK][bB]?/,
/[mM][bB]/,
/[gG][bB]?/,
/[tT][bB]?/,
/[nN][sS]/,
/[uU][sS]/,
"µs",
/[mM][sS]/,
/[sSmMhHdD]/
)
)
)
)
),
// Variable reference: $VARNAME
variable_reference: ($) =>
token(seq("$", /[a-zA-Z_][a-zA-Z0-9_]*/)),
// Array: [val1, val2, ...]
array: ($) =>
seq(
"[",
repeat(choice($._array_value, ",", $.comment)),
"]"
),
_array_value: ($) =>
choice(
$.string,
$.number,
$.boolean,
$.variable_reference,
$.array,
$.block
),
},
});