@konomanoasa/tree-sitter-toml 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 konomanoasa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # tree-sitter-toml
2
+
3
+ [![CI](https://github.com/konomanoasa/tree-sitter-toml/actions/workflows/ci.yaml/badge.svg)](https://github.com/konomanoasa/tree-sitter-toml/actions/workflows/ci.yaml)
4
+ [![npm](https://img.shields.io/npm/v/@konomanoasa/tree-sitter-toml)](https://www.npmjs.com/package/@konomanoasa/tree-sitter-toml)
5
+
6
+ [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) grammar for
7
+ TOML 1.1.0.
8
+
9
+ ## Installation
10
+
11
+ ```sh
12
+ npm install @konomanoasa/tree-sitter-toml
13
+ ```
14
+
15
+ ## Development
16
+
17
+ ```sh
18
+ npm install
19
+ npm run parse -- script.toml
20
+ ```
21
+
22
+ ## Specifications
23
+
24
+ - [TOML 1.1.0](https://toml.io/en/v1.1.0)
25
+ - [TOML 1.1.0 ABNF](https://github.com/toml-lang/toml/blob/1.1.0/toml.abnf)
26
+
27
+ ## License
28
+
29
+ [MIT](LICENSE)
package/grammar.js ADDED
@@ -0,0 +1,197 @@
1
+ const sign = optional(choice("-", "+"));
2
+
3
+ const digit = ($) => alias($._digit, $.digit);
4
+
5
+ const hexdig = ($) => alias($._hexdig, $.hexdig);
6
+
7
+ const times = (count, leaf) => Array(count).fill(leaf);
8
+
9
+ const underscored = (leaf) => choice(leaf, seq("_", leaf));
10
+
11
+ const separated = (leaf) => seq(leaf, repeat(underscored(leaf)));
12
+
13
+ const layout = ($) => optional($._ws_comment_newline);
14
+
15
+ const elements = ($, element) =>
16
+ seq(
17
+ layout($),
18
+ element,
19
+ repeat(seq(layout($), ",", layout($), element)),
20
+ optional(seq(layout($), ",")),
21
+ );
22
+
23
+ const header = ($, open, close) =>
24
+ seq(open, optional($._ws), field("key", $.key), optional($._ws), close);
25
+
26
+ const multiline = ($, delimiter, body) =>
27
+ prec(1, seq(delimiter, optional($.newline), optional(body), delimiter));
28
+
29
+ const quotes = (quote, mark) =>
30
+ prec.right(seq(alias(quote, mark), optional(alias(quote, mark))));
31
+
32
+ export default grammar({
33
+ name: "toml",
34
+ extras: () => [],
35
+ externals: ($) => [$._mlb_quote, $._mll_quote],
36
+ conflicts: ($) => [
37
+ [$.key, $.dotted_key],
38
+ [$.dotted_key],
39
+ [$.unsigned_dec_int, $._digit],
40
+ [$.offset_date_time, $.local_date_time, $.local_date],
41
+ [$.array_values],
42
+ [$.inline_table_keyvals],
43
+ ],
44
+ rules: {
45
+ toml: ($) =>
46
+ seq(
47
+ optional($.expression),
48
+ repeat(seq($.newline, optional($.expression))),
49
+ ),
50
+ expression: ($) =>
51
+ choice(
52
+ $._ws,
53
+ seq(optional($._ws), $.comment),
54
+ seq(
55
+ optional($._ws),
56
+ choice($.keyval, $.table),
57
+ optional($._ws),
58
+ optional($.comment),
59
+ ),
60
+ ),
61
+ _ws: ($) => prec.right(repeat1(choice($._space, /\t/))),
62
+ _space: () => / /,
63
+ newline: () => /\r?\n/,
64
+ comment: () => /#[\t\x20-\x7e\u0080-\u{d7ff}\u{e000}-\u{10ffff}]*/u,
65
+ keyval: ($) =>
66
+ seq(
67
+ field("key", $.key),
68
+ optional($._ws),
69
+ "=",
70
+ optional($._ws),
71
+ field("value", $.val),
72
+ ),
73
+ key: ($) => choice($.simple_key, $.dotted_key),
74
+ simple_key: ($) => choice($.quoted_key, $.unquoted_key),
75
+ unquoted_key: () => /[A-Za-z0-9_-]+/,
76
+ quoted_key: ($) => choice($.basic_string, $.literal_string),
77
+ dotted_key: ($) =>
78
+ seq(
79
+ $.simple_key,
80
+ repeat1(seq(optional($._ws), ".", optional($._ws), $.simple_key)),
81
+ ),
82
+ val: ($) =>
83
+ choice(
84
+ $.string,
85
+ $.boolean,
86
+ $.array,
87
+ $.inline_table,
88
+ $.date_time,
89
+ $.float,
90
+ $.integer,
91
+ ),
92
+ string: ($) =>
93
+ choice(
94
+ $.ml_basic_string,
95
+ $.basic_string,
96
+ $.ml_literal_string,
97
+ $.literal_string,
98
+ ),
99
+ basic_string: ($) => seq('"', repeat($.basic_char), '"'),
100
+ basic_char: ($) => choice($.basic_unescaped, $.escaped),
101
+ basic_unescaped: () =>
102
+ /[\t\x20-\x21\x23-\x5b\x5d-\x7e\u0080-\u{d7ff}\u{e000}-\u{10ffff}]/u,
103
+ escaped: ($) => seq("\\", $.escape_seq_char),
104
+ escape_seq_char: ($) =>
105
+ choice(
106
+ '"',
107
+ "\\",
108
+ "b",
109
+ "e",
110
+ "f",
111
+ "n",
112
+ "r",
113
+ "t",
114
+ seq("x", ...times(2, hexdig($))),
115
+ seq("u", ...times(4, hexdig($))),
116
+ seq("U", ...times(8, hexdig($))),
117
+ ),
118
+ ml_basic_string: ($) => multiline($, '"""', $.ml_basic_body),
119
+ ml_basic_body: ($) => repeat1(choice($.mlb_content, $.mlb_quotes)),
120
+ mlb_content: ($) => choice($.basic_char, $.newline, $.mlb_escaped_nl),
121
+ mlb_quotes: ($) => quotes($._mlb_quote, '"'),
122
+ mlb_escaped_nl: ($) =>
123
+ prec.right(
124
+ seq("\\", optional($._ws), $.newline, repeat(choice($._ws, $.newline))),
125
+ ),
126
+ literal_string: ($) => seq("'", repeat($.literal_char), "'"),
127
+ literal_char: () =>
128
+ /[\t\x20-\x26\x28-\x7e\u0080-\u{d7ff}\u{e000}-\u{10ffff}]/u,
129
+ ml_literal_string: ($) => multiline($, "'''", $.ml_literal_body),
130
+ ml_literal_body: ($) => repeat1(choice($.mll_content, $.mll_quotes)),
131
+ mll_content: ($) => choice($.literal_char, $.newline),
132
+ mll_quotes: ($) => quotes($._mll_quote, "'"),
133
+ integer: ($) => choice($.dec_int, $.hex_int, $.oct_int, $.bin_int),
134
+ dec_int: ($) => seq(sign, $.unsigned_dec_int),
135
+ unsigned_dec_int: ($) =>
136
+ choice(
137
+ digit($),
138
+ seq(alias($._digit1_9, $.digit1_9), repeat1(underscored(digit($)))),
139
+ ),
140
+ hex_int: ($) => seq("0x", separated(hexdig($))),
141
+ oct_int: ($) => seq("0o", separated(alias($._digit0_7, $.digit0_7))),
142
+ bin_int: ($) => seq("0b", separated(alias($._digit0_1, $.digit0_1))),
143
+ _digit: ($) => choice(/0/, $._digit1_9),
144
+ _digit1_9: () => /[1-9]/,
145
+ _digit0_7: () => /[0-7]/,
146
+ _digit0_1: () => /[01]/,
147
+ _hexdig: ($) => choice($._digit, /[A-Fa-f]/),
148
+ float: ($) =>
149
+ choice(
150
+ seq($.float_int_part, choice($.exp, seq($.frac, optional($.exp)))),
151
+ $.special_float,
152
+ ),
153
+ float_int_part: ($) => $.dec_int,
154
+ frac: ($) => seq(".", $.zero_prefixable_int),
155
+ zero_prefixable_int: ($) => separated(digit($)),
156
+ exp: ($) => seq(choice("e", "E"), $.float_exp_part),
157
+ float_exp_part: ($) => seq(sign, $.zero_prefixable_int),
158
+ special_float: () => seq(sign, choice("inf", "nan")),
159
+ boolean: () => choice("true", "false"),
160
+ date_time: ($) =>
161
+ choice($.offset_date_time, $.local_date_time, $.local_date, $.local_time),
162
+ date_fullyear: ($) => seq(...times(4, digit($))),
163
+ date_month: ($) => seq(...times(2, digit($))),
164
+ date_mday: ($) => seq(...times(2, digit($))),
165
+ time_delim: ($) => choice("T", "t", alias($._space, " ")),
166
+ time_hour: ($) => seq(...times(2, digit($))),
167
+ time_minute: ($) => seq(...times(2, digit($))),
168
+ time_second: ($) => seq(...times(2, digit($))),
169
+ time_secfrac: ($) => seq(".", repeat1(digit($))),
170
+ time_numoffset: ($) =>
171
+ seq(choice("+", "-"), $.time_hour, ":", $.time_minute),
172
+ time_offset: ($) => choice("Z", "z", $.time_numoffset),
173
+ partial_time: ($) =>
174
+ seq(
175
+ $.time_hour,
176
+ ":",
177
+ $.time_minute,
178
+ optional(seq(":", $.time_second, optional($.time_secfrac))),
179
+ ),
180
+ full_date: ($) => seq($.date_fullyear, "-", $.date_month, "-", $.date_mday),
181
+ full_time: ($) => seq($.partial_time, $.time_offset),
182
+ offset_date_time: ($) => seq($.full_date, $.time_delim, $.full_time),
183
+ local_date_time: ($) => seq($.full_date, $.time_delim, $.partial_time),
184
+ local_date: ($) => $.full_date,
185
+ local_time: ($) => $.partial_time,
186
+ array: ($) => seq("[", optional($.array_values), layout($), "]"),
187
+ array_values: ($) => elements($, $.val),
188
+ _ws_comment_newline: ($) =>
189
+ repeat1(choice($._ws, $.newline, seq($.comment, $.newline))),
190
+ table: ($) => choice($.std_table, $.array_table),
191
+ std_table: ($) => header($, "[", "]"),
192
+ inline_table: ($) =>
193
+ seq("{", optional($.inline_table_keyvals), layout($), "}"),
194
+ inline_table_keyvals: ($) => elements($, $.keyval),
195
+ array_table: ($) => header($, "[[", "]]"),
196
+ },
197
+ });
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@konomanoasa/tree-sitter-toml",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Tree-sitter grammar for TOML.",
6
+ "license": "MIT",
7
+ "repository": "git+https://github.com/konomanoasa/tree-sitter-toml.git",
8
+ "files": [
9
+ "grammar.js",
10
+ "queries/*.scm",
11
+ "src/*.c",
12
+ "src/*.json",
13
+ "src/tree_sitter/*.h",
14
+ "tree-sitter.json"
15
+ ],
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "scripts": {
20
+ "check": "biome check . && node scripts/check-c.js && node scripts/check-generated.js && npm test",
21
+ "format": "biome check --write . && node scripts/check-c.js --write",
22
+ "generate": "npm run tree-sitter -- generate-all",
23
+ "parse": "npm run tree-sitter -- parse --scope source.toml",
24
+ "test": "npm run tree-sitter -- test-corpus --rebuild && node --test --test-concurrency=1 \"test/*.test.js\"",
25
+ "test:fuzz": "node --test --test-name-pattern=\"fixed-seed generated histories\" test/incremental.test.js",
26
+ "test:fuzz:corpus": "npm run tree-sitter -- fuzz-all --iterations 100 --edits 5",
27
+ "test:highlights": "node --test test/highlight.test.js",
28
+ "test:incremental": "node --test test/incremental.test.js",
29
+ "test:sanitize": "node scripts/check-c.js --sanitize",
30
+ "test:scanner": "node scripts/check-c.js",
31
+ "tree-sitter": "node scripts/tree-sitter.js"
32
+ },
33
+ "devDependencies": {
34
+ "@biomejs/biome": "2.5.14",
35
+ "@types/node": "26.5.1",
36
+ "tree-sitter-cli": "0.27.0"
37
+ },
38
+ "allowScripts": {
39
+ "tree-sitter-cli": true
40
+ }
41
+ }
@@ -0,0 +1,222 @@
1
+ (comment) @comment
2
+
3
+ (unquoted_key) @property
4
+
5
+ "=" @operator
6
+
7
+ "," @punctuation.delimiter
8
+
9
+ [
10
+ "["
11
+ "]"
12
+ "[["
13
+ "]]"
14
+ "{"
15
+ "}"
16
+ ] @punctuation.bracket
17
+
18
+ [
19
+ "\"\"\""
20
+ "'''"
21
+ ] @punctuation.delimiter
22
+
23
+ [
24
+ "0x"
25
+ "0o"
26
+ "0b"
27
+ ] @number
28
+
29
+ [
30
+ "inf"
31
+ "nan"
32
+ ] @number.float
33
+
34
+ [
35
+ "true"
36
+ "false"
37
+ ] @boolean
38
+
39
+ [
40
+ ":"
41
+ "Z"
42
+ "z"
43
+ ] @string.special
44
+
45
+ (dotted_key
46
+ "." @punctuation.delimiter)
47
+
48
+ (basic_string
49
+ "\"" @punctuation.delimiter)
50
+
51
+ (literal_string
52
+ "'" @punctuation.delimiter)
53
+
54
+ (quoted_key
55
+ (basic_string
56
+ (basic_char
57
+ (basic_unescaped) @property)))
58
+
59
+ (quoted_key
60
+ (literal_string
61
+ (literal_char) @property))
62
+
63
+ (string
64
+ (basic_string
65
+ (basic_char
66
+ (basic_unescaped) @string)))
67
+
68
+ (string
69
+ (literal_string
70
+ (literal_char) @string))
71
+
72
+ (mlb_content
73
+ (basic_char
74
+ (basic_unescaped) @string))
75
+
76
+ (mll_content
77
+ (literal_char) @string)
78
+
79
+ (mlb_quotes
80
+ "\"" @string)
81
+
82
+ (mll_quotes
83
+ "'" @string)
84
+
85
+ (escaped
86
+ "\\" @string.escape)
87
+
88
+ (escape_seq_char
89
+ [
90
+ "\""
91
+ "\\"
92
+ "b"
93
+ "e"
94
+ "f"
95
+ "n"
96
+ "r"
97
+ "t"
98
+ "x"
99
+ "u"
100
+ "U"
101
+ (hexdig)
102
+ ] @string.escape)
103
+
104
+ (mlb_escaped_nl
105
+ "\\" @punctuation.special)
106
+
107
+ (integer
108
+ (dec_int
109
+ [
110
+ "-"
111
+ "+"
112
+ ] @number))
113
+
114
+ (integer
115
+ (dec_int
116
+ (unsigned_dec_int
117
+ [
118
+ (digit)
119
+ (digit1_9)
120
+ "_"
121
+ ] @number)))
122
+
123
+ (hex_int
124
+ [
125
+ (hexdig)
126
+ "_"
127
+ ] @number)
128
+
129
+ (oct_int
130
+ [
131
+ (digit0_7)
132
+ "_"
133
+ ] @number)
134
+
135
+ (bin_int
136
+ [
137
+ (digit0_1)
138
+ "_"
139
+ ] @number)
140
+
141
+ (float_int_part
142
+ (dec_int
143
+ [
144
+ "-"
145
+ "+"
146
+ ] @number.float))
147
+
148
+ (float_int_part
149
+ (dec_int
150
+ (unsigned_dec_int
151
+ [
152
+ (digit)
153
+ (digit1_9)
154
+ "_"
155
+ ] @number.float)))
156
+
157
+ (frac
158
+ "." @number.float)
159
+
160
+ (zero_prefixable_int
161
+ [
162
+ (digit)
163
+ "_"
164
+ ] @number.float)
165
+
166
+ (exp
167
+ [
168
+ "e"
169
+ "E"
170
+ ] @number.float)
171
+
172
+ (float_exp_part
173
+ [
174
+ "-"
175
+ "+"
176
+ ] @number.float)
177
+
178
+ (special_float
179
+ [
180
+ "-"
181
+ "+"
182
+ ] @number.float)
183
+
184
+ (full_date
185
+ "-" @string.special)
186
+
187
+ (time_delim
188
+ [
189
+ "T"
190
+ "t"
191
+ " "
192
+ ] @string.special)
193
+
194
+ (time_secfrac
195
+ [
196
+ "."
197
+ (digit)
198
+ ] @string.special)
199
+
200
+ (time_numoffset
201
+ [
202
+ "+"
203
+ "-"
204
+ ] @string.special)
205
+
206
+ (date_fullyear
207
+ (digit) @string.special)
208
+
209
+ (date_month
210
+ (digit) @string.special)
211
+
212
+ (date_mday
213
+ (digit) @string.special)
214
+
215
+ (time_hour
216
+ (digit) @string.special)
217
+
218
+ (time_minute
219
+ (digit) @string.special)
220
+
221
+ (time_second
222
+ (digit) @string.special)