tree-sitter-ucode 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 António Mora
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,158 @@
1
+ # tree-sitter-ucode
2
+
3
+ Tree-sitter grammar for [ucode](https://github.com/jow-/ucode), the ECMAScript-like scripting language used in OpenWrt.
4
+
5
+ Two grammars are provided:
6
+
7
+ | Grammar | Scope | File types |
8
+ |---------|-------|------------|
9
+ | `ucode` | `source.uc` | `.uc` |
10
+ | `ucode_tmpl` | `source.uc.tmpl` | `.uc.tmpl`, `.utpl` |
11
+
12
+ ## Ucode vs JavaScript
13
+
14
+ Ucode is an ECMAScript subset with OpenWrt-specific extensions. Key differences:
15
+
16
+ | Feature | Ucode | JavaScript |
17
+ |---------|-------|------------|
18
+ | Alternative block syntax | `if/elif/else/endif`, `for/endfor`, `while/endwhile`, `function/endfunction` | Not supported |
19
+ | Two-variable for-in | `for (k, v in obj)` | Single variable only |
20
+ | Forward declaration | `function foo;` | Not supported |
21
+ | Removed keywords | `var`, `new`, `typeof`, `void`, `class`, `instanceof`, `do`, `async`, `await`, `yield` | All supported |
22
+ | Removed features | Destructuring, `for...of`, `do-while`, generators | All supported |
23
+ | Added number literals | `0177` (C octal), `0x1.8` (hex float), `0B`/`0O` prefixes | Standard only |
24
+ | Added escape sequences | `\e` (ESC), `\a` (BEL), octal `\177` | Standard only |
25
+ | Regex flags | `g`, `i`, `s` only | Full set |
26
+ | Module system | Static `import`/`export`, dynamic `import(path)`; no `from` on re-exports | Full ES modules |
27
+
28
+ ## Requirements
29
+
30
+ - [tree-sitter CLI](https://github.com/tree-sitter/tree-sitter) ≥ 0.24
31
+ - Node.js ≥ 18 (for the Node.js bindings only)
32
+
33
+ ## Build
34
+
35
+ ```sh
36
+ npm install
37
+ npm run build # generate + compile Node.js bindings
38
+ ```
39
+
40
+ To regenerate parsers after editing a grammar file:
41
+
42
+ ```sh
43
+ # ucode grammar
44
+ npx tree-sitter generate
45
+
46
+ # ucode_tmpl grammar
47
+ npx tree-sitter generate tmpl/grammar.js --output tmpl/src
48
+ ```
49
+
50
+ ## Test
51
+
52
+ ```sh
53
+ npm test # runs tree-sitter test for ucode and ucode_tmpl
54
+ ```
55
+
56
+ To filter by corpus file name:
57
+
58
+ ```sh
59
+ npx tree-sitter test --file-name control_flow
60
+ npx tree-sitter test -p tmpl --file-name template
61
+ ```
62
+
63
+ ## Use in Neovim (nvim-treesitter)
64
+
65
+ Add to your nvim-treesitter config (e.g. `~/.config/nvim/lua/plugins/treesitter.lua`):
66
+
67
+ ```lua
68
+ local parser_config = require("nvim-treesitter.parsers").get_parser_configs()
69
+
70
+ parser_config.ucode = {
71
+ install_info = {
72
+ url = "https://github.com/m00qek/tree-sitter-ucode",
73
+ files = { "src/parser.c", "src/scanner.c" },
74
+ branch = "main",
75
+ },
76
+ filetype = "ucode",
77
+ }
78
+
79
+ parser_config.ucode_tmpl = {
80
+ install_info = {
81
+ url = "https://github.com/m00qek/tree-sitter-ucode",
82
+ files = { "tmpl/src/parser.c", "tmpl/src/scanner.c" },
83
+ branch = "main",
84
+ },
85
+ filetype = "ucode_tmpl",
86
+ }
87
+ ```
88
+
89
+ Associate `.uc` and `.uc.tmpl` files with the right filetypes:
90
+
91
+ ```lua
92
+ vim.filetype.add({
93
+ extension = {
94
+ uc = "ucode",
95
+ utpl = "ucode_tmpl",
96
+ },
97
+ pattern = { [".*%.uc%.tmpl"] = "ucode_tmpl" },
98
+ })
99
+ ```
100
+
101
+ ## Use in Helix
102
+
103
+ Add to `~/.config/helix/languages.toml`:
104
+
105
+ ```toml
106
+ [[language]]
107
+ name = "ucode"
108
+ scope = "source.uc"
109
+ file-types = ["uc"]
110
+ comment-token = "//"
111
+ indent = { tab-width = 2, unit = " " }
112
+ grammar = "ucode"
113
+
114
+ [[language]]
115
+ name = "ucode-tmpl"
116
+ scope = "source.uc.tmpl"
117
+ file-types = ["uc.tmpl", "utpl"]
118
+ grammar = "ucode_tmpl"
119
+
120
+ [[grammar]]
121
+ name = "ucode"
122
+ source = { git = "https://github.com/m00qek/tree-sitter-ucode", rev = "main" }
123
+
124
+ [[grammar]]
125
+ name = "ucode_tmpl"
126
+ source = { git = "https://github.com/m00qek/tree-sitter-ucode", rev = "main", subpath = "tmpl" }
127
+ ```
128
+
129
+ ## Template files (.uc.tmpl / .utpl)
130
+
131
+ Template files mix raw text with code tags. The `ucode_tmpl` grammar produces
132
+ a document tree; editors use language injection to apply ucode highlighting
133
+ inside the code and expression tags.
134
+
135
+ | Tag | Purpose |
136
+ |-----|---------|
137
+ | `{% ... %}` | Execute ucode statements (no output) |
138
+ | `{{ ... }}` | Evaluate expression and emit output |
139
+ | `{# ... #}` | Template comment (discarded) |
140
+ | `{%- ... -%}` | Statement block — strip whitespace on both sides |
141
+ | `{{- ... -}}` | Expression block — strip whitespace on both sides |
142
+ | `{%+ ... %}` | Statement block — suppress `lstrip_blocks` stripping |
143
+ | `{#- ... -#}` | Comment — strip whitespace on both sides |
144
+
145
+ Opener and closer markers are independent: any opener variant may be combined with any closer variant. `{%-` / `{{-` / `{#-` strip the preceding raw text; `-%}` / `-}}` / `-#}` strip the following raw text. `{%+` suppresses `lstrip_blocks` stripping and may be combined with `-%}` (e.g. `{%+ ... -%}`).
146
+
147
+ Example:
148
+
149
+ ```
150
+ Hello, {{ name }}!
151
+ {% for (let i in items): %}
152
+ - {{ items[i] }}
153
+ {% endfor %}
154
+ ```
155
+
156
+ ## License
157
+
158
+ MIT
package/binding.gyp ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "targets": [
3
+ {
4
+ "target_name": "tree_sitter_ucode_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
+ ],
15
+ "variables": {
16
+ "has_scanner": "<!(node -p \"fs.existsSync('src/scanner.c')\")"
17
+ },
18
+ "conditions": [
19
+ ["has_scanner=='true'", {
20
+ "sources+": ["src/scanner.c"],
21
+ }],
22
+ ["OS!='win'", {
23
+ "cflags_c": [
24
+ "-std=c11",
25
+ ],
26
+ }, {
27
+ "cflags_c": [
28
+ "/std:c11",
29
+ "/utf-8",
30
+ ],
31
+ }],
32
+ ],
33
+ }
34
+ ]
35
+ }
@@ -0,0 +1,19 @@
1
+ #include <napi.h>
2
+
3
+ typedef struct TSLanguage TSLanguage;
4
+
5
+ extern "C" TSLanguage *tree_sitter_ucode();
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
+ auto language = Napi::External<TSLanguage>::New(env, tree_sitter_ucode());
14
+ language.TypeTag(&LANGUAGE_TYPE_TAG);
15
+ exports["language"] = language;
16
+ return exports;
17
+ }
18
+
19
+ NODE_API_MODULE(tree_sitter_ucode_binding, Init)
@@ -0,0 +1,27 @@
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
+ language: unknown;
23
+ nodeTypeInfo: NodeInfo[];
24
+ };
25
+
26
+ declare const language: Language;
27
+ export = language;
@@ -0,0 +1,10 @@
1
+ const root = require("path").join(__dirname, "..", "..");
2
+
3
+ module.exports =
4
+ typeof process.versions.bun === "string"
5
+ ? require(`../../prebuilds/${process.platform}-${process.arch}/tree-sitter-ucode.node`)
6
+ : require("node-gyp-build")(root);
7
+
8
+ try {
9
+ module.exports.nodeTypeInfo = require("../../src/node-types.json");
10
+ } catch (_) {}