@konomanoasa/tree-sitter-c 0.0.0-stage → 0.2.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/Cargo.toml ADDED
@@ -0,0 +1,38 @@
1
+ [package]
2
+ name = "konomanoasa-tree-sitter-c"
3
+ version = "0.2.0"
4
+ description = "Tree-sitter grammar for C."
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ repository = "https://github.com/konomanoasa/tree-sitter-c"
8
+ edition = "2024"
9
+ build = "bindings/rust/build.rs"
10
+ links = "tree-sitter-c"
11
+ include = [
12
+ "/LICENSE",
13
+ "/PARSING_POLICY.md",
14
+ "/README.md",
15
+ "/bindings/rust/*.rs",
16
+ "/queries/*.scm",
17
+ "/src/*.c",
18
+ "/src/*.h",
19
+ "/src/node-types.json",
20
+ "/src/tree_sitter/*.h",
21
+ "/test/bindings.test.rs",
22
+ ]
23
+
24
+ [lib]
25
+ path = "bindings/rust/lib.rs"
26
+
27
+ [[test]]
28
+ name = "bindings"
29
+ path = "test/bindings.test.rs"
30
+
31
+ [dependencies]
32
+ tree-sitter-language = "=0.1.9"
33
+
34
+ [dev-dependencies]
35
+ tree-sitter = "=0.27.2"
36
+
37
+ [build-dependencies]
38
+ cc = "=1.12.0"
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.
@@ -0,0 +1,76 @@
1
+ # Parsing Policy
2
+
3
+ The parser reads [C17](https://www.iso.org/standard/74528.html) source before
4
+ preprocessing. Macro definitions, conditional branches and identifiers retain
5
+ their source locations.
6
+
7
+ | Source | Effects not applied |
8
+ | ------------------ | ----------------------------------- |
9
+ | Macro definitions | Macro expansion. |
10
+ | Conditional groups | Branch selection. |
11
+ | Declarations | Name lookup and typedef resolution. |
12
+
13
+ ## Type-Dependent Syntax
14
+
15
+ When a name can denote either a type or a value, the tree retains the
16
+ readings allowed by the surrounding syntax.
17
+
18
+ | Source | Node |
19
+ | --------------------------- | ---------------------------------------- |
20
+ | `sizeof(T)`, `_Alignas(T)` | `type_or_expression` for `T` |
21
+ | `T *p;` in a block or `for` | `declaration_or_expression` |
22
+ | `(T) *p`, `(T)(x)` | `cast_or_expression` |
23
+ | `int f(T, U)` | `parameter_identifiers` for `T, U` |
24
+ | `int f(int (T))` | `named_or_abstract_declarator` for `(T)` |
25
+
26
+ A cast candidate retains the operand whose grouping depends on the meaning of
27
+ `T`. Where the surrounding syntax determines the reading, `T x;` is a
28
+ declaration, `(T)x` is a cast and `_Alignof(T)` takes a type name.
29
+
30
+ ## Macro Invocations
31
+
32
+ Direct calls to `va_arg`, `alignof` and `offsetof` accept a type name in the
33
+ argument position specified by C17.
34
+
35
+ ```c
36
+ va_arg(ap, int)
37
+ alignof(int)
38
+ offsetof(struct S, x)
39
+ ```
40
+
41
+ In those positions, an identifier such as `T` retains both readings as
42
+ `type_or_expression`. In `offsetof(T, a[i].x)`, the second argument retains
43
+ the member-designator and expression readings as `member_designator_or_expression`.
44
+
45
+ An ordinary call such as `F(x + y)` retains its expression tree. A direct name
46
+ can also introduce a `macro_invocation_candidate` in an expression or at file
47
+ scope. The argument list preserves empty arguments and complete expressions.
48
+ Only nested parentheses protect a comma. An unclosed invocation uses normal
49
+ error recovery.
50
+
51
+ ```c
52
+ F(,)
53
+ F(x + 1, int)
54
+ ```
55
+
56
+ ## Conditional Groups
57
+
58
+ When a conditional group supplies an operand, each branch retains its own
59
+ reading. A `sizeof` operand can be `int` in one branch and `n + 1` in another.
60
+
61
+ In a parameter list, a group belongs to the list when placing it among the
62
+ declaration specifiers would require the same assumptions about type names.
63
+ A group following a branch's `...` belongs to that branch.
64
+
65
+ ## Numeric Constants
66
+
67
+ An integer or floating constant spans its entire spelling. In the example
68
+ below, the floating constant retains the prefix `0x`, significand `1.f`,
69
+ exponent `p+2` and suffix `L`. The significand contains separate leaves for
70
+ `1`, `.` and `f`. The exponent retains its marker, sign and digits.
71
+ The macro replacement list retains one preprocessing number.
72
+
73
+ ```c
74
+ long double x = 0x1.fp+2L;
75
+ #define VALUE 0x1.fp+2L
76
+ ```
package/README.md CHANGED
@@ -1,3 +1,32 @@
1
- # Temporary Holding Version
1
+ # tree-sitter-c
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
+ [![CI](https://github.com/konomanoasa/tree-sitter-c/actions/workflows/ci.yaml/badge.svg)](https://github.com/konomanoasa/tree-sitter-c/actions/workflows/ci.yaml)
4
+ [![crates.io](https://img.shields.io/crates/v/konomanoasa-tree-sitter-c)](https://crates.io/crates/konomanoasa-tree-sitter-c)
5
+ [![npm](https://img.shields.io/npm/v/@konomanoasa/tree-sitter-c)](https://www.npmjs.com/package/@konomanoasa/tree-sitter-c)
6
+
7
+ [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) grammar for C17.
8
+
9
+ ## Installation
10
+
11
+ ```sh
12
+ npm install @konomanoasa/tree-sitter-c
13
+ ```
14
+
15
+ ## Parsing Policy
16
+
17
+ [Parsing Policy](https://github.com/konomanoasa/tree-sitter-c/blob/main/PARSING_POLICY.md)
18
+
19
+ ## Development
20
+
21
+ ```sh
22
+ npm ci
23
+ npm test
24
+ ```
25
+
26
+ ## Specification
27
+
28
+ [ISO/IEC 9899:2018 — Programming languages — C](https://www.iso.org/standard/74528.html)
29
+
30
+ ## License
31
+
32
+ [MIT](LICENSE)
package/binding.gyp ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "targets": [
3
+ {
4
+ "target_name": "tree_sitter_c_binding",
5
+ "sources": ["bindings/node/binding.cc", "src/parser.c", "src/scanner.c"],
6
+ "include_dirs": ["src"],
7
+ "dependencies": [
8
+ "<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except"
9
+ ],
10
+ "cflags_c": ["-std=c17"],
11
+ "cflags_cc": ["-std=c++17"],
12
+ "msvs_settings": {
13
+ "VCCLCompilerTool": {
14
+ "AdditionalOptions": ["/utf-8"],
15
+ "AdditionalOptions!": ["-std:c++20"],
16
+ "LanguageStandard": "stdcpp17",
17
+ "LanguageStandard_C": "stdc17"
18
+ }
19
+ },
20
+ "target_conditions": [
21
+ [
22
+ "OS==\"mac\"",
23
+ {
24
+ "xcode_settings": {
25
+ "CLANG_CXX_LANGUAGE_STANDARD": "c++17",
26
+ "GCC_C_LANGUAGE_STANDARD": "c17",
27
+ "MACOSX_DEPLOYMENT_TARGET": "14.0"
28
+ }
29
+ }
30
+ ]
31
+ ]
32
+ }
33
+ ]
34
+ }
@@ -0,0 +1,25 @@
1
+ #include <napi.h>
2
+
3
+ struct TSLanguage;
4
+ extern "C" const TSLanguage *tree_sitter_c();
5
+
6
+ namespace {
7
+
8
+ constexpr napi_type_tag language_type_tag = {
9
+ 0x8AF2E5212AD58ABF,
10
+ 0xD5006CAD83ABBA16
11
+ };
12
+
13
+ Napi::Object Init(Napi::Env env, Napi::Object exports) {
14
+ auto language = Napi::External<TSLanguage>::New(
15
+ env,
16
+ const_cast<TSLanguage *>(tree_sitter_c())
17
+ );
18
+ language.TypeTag(&language_type_tag);
19
+ exports["language"] = language;
20
+ return exports;
21
+ }
22
+
23
+ }
24
+
25
+ NODE_API_MODULE(tree_sitter_c_binding, Init)
@@ -0,0 +1,5 @@
1
+ import type { Language } from "tree-sitter";
2
+
3
+ declare const binding: Language & { HIGHLIGHTS_QUERY: string };
4
+
5
+ export default binding;
@@ -0,0 +1,15 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { fileURLToPath } from "node:url";
3
+ import load from "node-gyp-build";
4
+
5
+ const binding = load(fileURLToPath(new URL("../..", import.meta.url)));
6
+ binding.nodeTypeInfo = JSON.parse(
7
+ readFileSync(new URL("../../src/node-types.json", import.meta.url), "utf8"),
8
+ );
9
+
10
+ binding.HIGHLIGHTS_QUERY = readFileSync(
11
+ new URL("../../queries/highlights.scm", import.meta.url),
12
+ "utf8",
13
+ );
14
+
15
+ export default binding;
@@ -0,0 +1,9 @@
1
+ from importlib.resources import files
2
+
3
+ from .binding import language
4
+
5
+ HIGHLIGHTS_QUERY: str = (
6
+ files(__package__).joinpath("queries/highlights.scm").read_text("utf-8")
7
+ )
8
+
9
+ __all__ = ["HIGHLIGHTS_QUERY", "language"]
@@ -0,0 +1,27 @@
1
+ #include <Python.h>
2
+
3
+ #include "../../src/tree_sitter/parser.h"
4
+
5
+ const TSLanguage *tree_sitter_c(void);
6
+
7
+ static PyObject *language(PyObject *self, PyObject *args) {
8
+ (void)self;
9
+ (void)args;
10
+ return PyCapsule_New((void *)tree_sitter_c(), "tree_sitter.Language", NULL);
11
+ }
12
+
13
+ static PyMethodDef methods[] = {
14
+ {"language", language, METH_NOARGS, "Return the C17 language capsule."},
15
+ {NULL, NULL, 0, NULL},
16
+ };
17
+
18
+ static struct PyModuleDef module = {
19
+ PyModuleDef_HEAD_INIT,
20
+ .m_name = "binding",
21
+ .m_size = -1,
22
+ .m_methods = methods,
23
+ };
24
+
25
+ PyMODINIT_FUNC PyInit_binding(void) {
26
+ return PyModule_Create(&module);
27
+ }
@@ -0,0 +1,3 @@
1
+ from types import CapsuleType
2
+
3
+ def language() -> CapsuleType: ...
File without changes
@@ -0,0 +1,23 @@
1
+ use std::{env, path::Path};
2
+
3
+ fn main() {
4
+ let mut build = cc::Build::new();
5
+ build.std("c17");
6
+ if build.get_compiler().is_like_msvc() {
7
+ build.flag("-utf-8");
8
+ }
9
+ if env::var("TARGET").expect("Cargo must provide TARGET")
10
+ == "wasm32-unknown-unknown"
11
+ {
12
+ let headers = env::var_os("DEP_TREE_SITTER_LANGUAGE_WASM_HEADERS")
13
+ .expect("tree-sitter-language must provide WebAssembly headers");
14
+ build.include(&headers);
15
+ println!("cargo::rerun-if-changed={}", Path::new(&headers).display());
16
+ }
17
+
18
+ println!("cargo::rerun-if-changed=src");
19
+ build
20
+ .include("src")
21
+ .files(["src/parser.c", "src/scanner.c"])
22
+ .compile("tree-sitter-c");
23
+ }
@@ -0,0 +1,9 @@
1
+ use tree_sitter_language::LanguageFn;
2
+
3
+ unsafe extern "C" {
4
+ fn tree_sitter_c() -> *const ();
5
+ }
6
+
7
+ pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_c) };
8
+ pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
9
+ pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");