tree-sitter-razor-modern 0.0.0-stage → 0.1.2

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) 2023 Tristan Knight
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/NOTICE ADDED
@@ -0,0 +1,6 @@
1
+ tree-sitter-razor-modern is a fork of tree-sitter-razor
2
+ (https://github.com/tris203/tree-sitter-razor), copyright (c) 2023 Tristan Knight (tris203),
3
+ licensed under the MIT License. See LICENSE for the full license text.
4
+
5
+ The grammar, scanner, queries, and test corpus derive from that project, which extends the
6
+ tree-sitter-c-sharp grammar (https://github.com/tree-sitter/tree-sitter-c-sharp, MIT).
package/README.md CHANGED
@@ -1,3 +1,61 @@
1
- # Temporary Holding Version
1
+ # tree-sitter-razor-modern
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![Verify](https://github.com/alfpricot/tree-sitter-razor-modern/actions/workflows/verify.yml/badge.svg?branch=dev)](https://github.com/alfpricot/tree-sitter-razor-modern/actions/workflows/verify.yml)
4
+ [![crates.io](https://img.shields.io/crates/v/tree-sitter-razor-modern.svg)](https://crates.io/crates/tree-sitter-razor-modern)
5
+
6
+ A [tree-sitter](https://tree-sitter.github.io/tree-sitter/) grammar for ASP.NET Core Razor markup (`.cshtml` and `.razor`), with Rust and Node bindings. It is a maintained fork of [`tris203/tree-sitter-razor`](https://github.com/tris203/tree-sitter-razor), licensed under MIT. Embedded C# is parsed by the C# grammar's own rules, so node kinds inside code blocks match `tree-sitter-c-sharp`.
7
+
8
+ ## Installation
9
+
10
+ Rust:
11
+
12
+ ```sh
13
+ cargo add tree-sitter tree-sitter-razor-modern
14
+ ```
15
+
16
+ Node:
17
+
18
+ ```sh
19
+ npm install tree-sitter-razor-modern
20
+ ```
21
+
22
+ A GitHub Packages copy of the npm package is also published as `@alfpricot/tree-sitter-razor-modern`. Prebuilt native addons ship for Linux and Windows, so an install on those platforms needs no C toolchain. Other platforms compile the committed parser from source.
23
+
24
+ ### Requirements
25
+
26
+ - Node.js 24 for building and testing from a checkout (`.nvmrc`).
27
+ - Rust 1.85 or newer (`rust-version` in `Cargo.toml`).
28
+ - A C compiler. The Rust build script and `node-gyp` both compile `src/parser.c`.
29
+
30
+ ## Usage
31
+
32
+ Rust:
33
+
34
+ ```rust
35
+ let mut parser = tree_sitter::Parser::new();
36
+ parser
37
+ .set_language(&tree_sitter_razor_modern::LANGUAGE.into())
38
+ .expect("Error loading the Razor grammar");
39
+ let tree = parser.parse("<p>@DateTime.Now</p>", None).unwrap();
40
+ ```
41
+
42
+ Node:
43
+
44
+ ```js
45
+ import Parser from 'tree-sitter';
46
+ import Language from 'tree-sitter-razor-modern';
47
+
48
+ const parser = new Parser();
49
+ parser.setLanguage(Language);
50
+ const tree = parser.parse('<p>@DateTime.Now</p>');
51
+ ```
52
+
53
+ The Rust crate also exports `NODE_TYPES`, `HIGHLIGHTS_QUERY`, and `INJECTIONS_QUERY`. [docs/SYNTAX.md](docs/SYNTAX.md) lists the Razor constructs the grammar covers, and [ARCHITECTURE.md](ARCHITECTURE.md) describes how the repository is laid out.
54
+
55
+ ## How to contribute
56
+
57
+ Branch from `dev` and open pull requests against it. After any change to `grammar.js`, regenerate the parser and commit the result, then run the corpus tests and the checks CI runs. [CONTRIBUTING.md](CONTRIBUTING.md) has the exact commands.
58
+
59
+ ## License
60
+
61
+ MIT. See [LICENSE](LICENSE) and [NOTICE](NOTICE).
package/binding.gyp ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "targets": [
3
+ {
4
+ "target_name": "tree_sitter_razor_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_razor();
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, "razor");
14
+ auto language = Napi::External<TSLanguage>::New(env, tree_sitter_razor());
15
+ language.TypeTag(&LANGUAGE_TYPE_TAG);
16
+ exports["language"] = language;
17
+ return exports;
18
+ }
19
+
20
+ NODE_API_MODULE(tree_sitter_razor_binding, Init)
@@ -0,0 +1,11 @@
1
+ import assert from 'node:assert';
2
+ import { test } from 'node:test';
3
+ import Parser from 'tree-sitter';
4
+
5
+ test('can load grammar', async () => {
6
+ const parser = new Parser();
7
+ const { default: language } = await import('./index.js');
8
+ assert.doesNotThrow(() => {
9
+ parser.setLanguage(language);
10
+ });
11
+ });
@@ -0,0 +1,63 @@
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
+ /**
22
+ * The tree-sitter language object for this grammar.
23
+ *
24
+ * @see {@linkcode https://tree-sitter.github.io/node-tree-sitter/interfaces/Parser.Language.html Parser.Language}
25
+ *
26
+ * @example
27
+ * import Parser from "tree-sitter";
28
+ * import Razor from "tree-sitter-razor-modern";
29
+ *
30
+ * const parser = new Parser();
31
+ * parser.setLanguage(Razor);
32
+ */
33
+ declare const binding: {
34
+ /**
35
+ * The inner language object.
36
+ * @private
37
+ */
38
+ language: unknown;
39
+
40
+ /**
41
+ * The content of the `node-types.json` file for this grammar.
42
+ *
43
+ * @see {@linkplain https://tree-sitter.github.io/tree-sitter/using-parsers/6-static-node-types Static Node Types}
44
+ */
45
+ nodeTypeInfo: NodeInfo[];
46
+
47
+ /** The syntax highlighting query for this grammar. */
48
+ HIGHLIGHTS_QUERY?: string;
49
+
50
+ /** The indentation query for this grammar. */
51
+ INDENTS_QUERY?: string;
52
+
53
+ /** The language injection query for this grammar. */
54
+ INJECTIONS_QUERY?: string;
55
+
56
+ /** The local variable query for this grammar. */
57
+ LOCALS_QUERY?: string;
58
+
59
+ /** The symbol tagging query for this grammar. */
60
+ TAGS_QUERY?: string;
61
+ };
62
+
63
+ export default binding;
@@ -0,0 +1,42 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { fileURLToPath } from 'node:url';
3
+
4
+ const root = fileURLToPath(new URL('../..', import.meta.url));
5
+
6
+ const binding =
7
+ typeof process.versions.bun === 'string'
8
+ ? // Support `bun build --compile` by being statically analyzable enough to find the .node file at build-time
9
+ await import(
10
+ `${root}/prebuilds/${process.platform}-${process.arch}/tree-sitter-razor-modern.node`
11
+ )
12
+ : (await import('node-gyp-build')).default(root);
13
+
14
+ try {
15
+ const nodeTypes = await import(`${root}/src/node-types.json`, { with: { type: 'json' } });
16
+ binding.nodeTypeInfo = nodeTypes.default;
17
+ } catch {
18
+ // node-types.json is optional; nodeTypeInfo stays unset if it's missing.
19
+ }
20
+
21
+ const queries = [
22
+ ['HIGHLIGHTS_QUERY', `${root}/queries/highlights.scm`],
23
+ ['INJECTIONS_QUERY', `${root}/queries/injections.scm`],
24
+ ];
25
+
26
+ for (const [prop, path] of queries) {
27
+ Object.defineProperty(binding, prop, {
28
+ configurable: true,
29
+ enumerable: true,
30
+ get() {
31
+ delete binding[prop];
32
+ try {
33
+ binding[prop] = readFileSync(path, 'utf8');
34
+ } catch {
35
+ // The query file is optional; the property stays undefined if it's missing.
36
+ }
37
+ return binding[prop];
38
+ },
39
+ });
40
+ }
41
+
42
+ export default binding;
@@ -0,0 +1,25 @@
1
+ import assert from 'node:assert';
2
+ import { readdirSync, readFileSync } from 'node:fs';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { test } from 'node:test';
5
+ import Parser from 'tree-sitter';
6
+
7
+ const root = fileURLToPath(new URL('../..', import.meta.url));
8
+ const queriesDir = `${root}/queries`;
9
+
10
+ test('every queries/*.scm file compiles against the grammar', async () => {
11
+ const { default: language } = await import('./index.js');
12
+ const files = readdirSync(queriesDir).filter((name) => name.endsWith('.scm'));
13
+ assert.ok(files.length > 0, 'expected at least one query file under queries/');
14
+
15
+ for (const file of files) {
16
+ const source = readFileSync(`${queriesDir}/${file}`, 'utf8');
17
+ assert.doesNotThrow(
18
+ () => new Parser.Query(language, source),
19
+ (error) => {
20
+ error.message = `${file}: ${error.message}`;
21
+ return true;
22
+ },
23
+ );
24
+ }
25
+ });