@sshadows/tree-sitter-al 2.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) 2024 Torben Leth
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,127 @@
1
+ # tree-sitter-al
2
+
3
+ A [tree-sitter](https://tree-sitter.github.io/tree-sitter/) parser for the AL programming language used in Microsoft Dynamics 365 Business Central.
4
+
5
+ [![PyPI](https://img.shields.io/pypi/v/tree-sitter-al)](https://pypi.org/project/tree-sitter-al/)
6
+
7
+ ## Parser Status
8
+
9
+ Validated against **15,358 production AL files** from the Business Central codebase:
10
+
11
+ | Metric | Value |
12
+ |--------|-------|
13
+ | **Success rate** | **99.95%** (15,351 / 15,358 files) |
14
+ | Tests | 1,404 |
15
+ | parser.c size | 10.6 MB |
16
+ | grammar.js | ~3,000 lines |
17
+ | Named keywords | 80 (queryable via highlights/tags) |
18
+ | Query files | 5 (highlights, locals, tags, indents, folds) |
19
+
20
+ ## Installation
21
+
22
+ ### Python (tree-sitter 0.24+)
23
+
24
+ ```bash
25
+ pip install tree-sitter-al
26
+ ```
27
+
28
+ ```python
29
+ import tree_sitter
30
+ import tree_sitter_al
31
+
32
+ lang = tree_sitter.Language(tree_sitter_al.language())
33
+ parser = tree_sitter.Parser(lang)
34
+ tree = parser.parse(b'codeunit 50100 MyCodeunit { }')
35
+ print(tree.root_node.sexp())
36
+ ```
37
+
38
+ ### Node.js
39
+
40
+ ```bash
41
+ npm install tree-sitter-al
42
+ ```
43
+
44
+ ### Pre-built binaries
45
+
46
+ Download from [GitHub Releases](https://github.com/SShadowS/tree-sitter-al/releases):
47
+
48
+ | File | Platform | Use case |
49
+ |------|----------|----------|
50
+ | `tree-sitter-al.wasm` | All | web-tree-sitter |
51
+ | `tree-sitter-al.so` | Linux x86_64 | ast-grep, native bindings |
52
+ | `tree-sitter-al.dll` | Windows x86_64 | ast-grep, native bindings |
53
+ | `tree-sitter-al.dylib` | macOS ARM64 | ast-grep, native bindings |
54
+
55
+ ## V2 Architecture
56
+
57
+ The grammar was rewritten from scratch in March 2026, achieving a **10x reduction in parser size** while improving correctness.
58
+
59
+ ### Before / After
60
+
61
+ | Metric | V1 | V2 |
62
+ |--------|-----|-----|
63
+ | parser.c | 106 MB (can't push to GitHub) | **10.6 MB** |
64
+ | Errors | 14 | **7** |
65
+ | Success rate | 99.91% | **99.95%** |
66
+ | Symbols | 2,249 | **724** |
67
+ | States | 29,126 | **5,179** |
68
+ | grammar.js | 8,500 lines | **~3,000 lines** |
69
+ | Tests | 1,225 | **1,404** |
70
+ | Keywords | invisible in queries | **80 named nodes** |
71
+ | Query files | 3 (partial) | **5 (comprehensive)** |
72
+
73
+ ### Key design decisions
74
+
75
+ - **Scanner-based property disambiguation** — One `PROPERTY_NAME` scanner token replaces 291 individual property rules. The scanner checks if `identifier` is followed by `=` (not `:=`) with a single-character lookahead.
76
+ - **Parse structure, don't validate** — Accept any `Name = Value ;` as a property. Semantic validation belongs in linters/LSP servers, not the parser.
77
+ - **Generic preprocessor** — One `preproc_conditional` rule + ~12 dedicated rules for genuinely complex split constructs, replacing 63 specialized rules.
78
+ - **80 named keyword nodes** — All keywords except `begin`/`end` are named nodes, enabling proper syntax highlighting and code navigation queries.
79
+
80
+ See [docs/v2-blog-post-notes.md](docs/v2-blog-post-notes.md) for the full rewrite narrative.
81
+
82
+ ## Development
83
+
84
+ ### Prerequisites
85
+
86
+ - Node.js (v16+)
87
+ - tree-sitter CLI: `npm install -g tree-sitter-cli`
88
+
89
+ ### Building
90
+
91
+ ```bash
92
+ tree-sitter generate # Generate parser from grammar.js
93
+ tree-sitter test # Run test suite
94
+ ```
95
+
96
+ ### Validation
97
+
98
+ ```bash
99
+ ./validate-grammar.sh # Quick: generation, tests, orphan/duplicate detection
100
+ ./validate-grammar.sh --full # Full: includes production AL file parsing
101
+ ```
102
+
103
+ ### Parsing AL files
104
+
105
+ ```bash
106
+ tree-sitter parse path/to/file.al
107
+ tree-sitter parse path/to/file.al -d # Debug output
108
+ tree-sitter parse path/to/file.al -q # Quiet (errors only)
109
+ ```
110
+
111
+ ### Key files
112
+
113
+ | File | Purpose |
114
+ |------|---------|
115
+ | `grammar.js` | Main grammar definition |
116
+ | `src/scanner.c` | External scanner (property disambiguation, preprocessor) |
117
+ | `test/corpus/` | Test suite (1,404 tests) |
118
+ | `queries/` | Syntax highlighting, code navigation, folding, indentation |
119
+
120
+ ## Contributing
121
+
122
+ See [CLAUDE.md](CLAUDE.md) for detailed development guidelines including architecture, debugging, and conventions.
123
+
124
+ ---
125
+
126
+ **Author**: Torben Leth (sshadows@sshadows.dk)
127
+ **License**: MIT (see [LICENSE](LICENSE))
package/binding.gyp ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "targets": [
3
+ {
4
+ "target_name": "tree_sitter_al_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
+ "src/scanner.c"
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_al();
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, "al");
14
+ auto language = Napi::External<TSLanguage>::New(env, tree_sitter_al());
15
+ language.TypeTag(&LANGUAGE_TYPE_TAG);
16
+ exports["language"] = language;
17
+ return exports;
18
+ }
19
+
20
+ NODE_API_MODULE(tree_sitter_al_binding, Init)
@@ -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 (_) {}