Initial Commit

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-01-14 12:49:49 -05:00
commit 08beba6cb1
24 changed files with 1816 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
#include <napi.h>
typedef struct TSLanguage TSLanguage;
extern "C" TSLanguage *tree_sitter_p4();
// "tree-sitter", "language" hashed with BLAKE2
const napi_type_tag LANGUAGE_TYPE_TAG = {
0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16
};
Napi::Object Init(Napi::Env env, Napi::Object exports) {
auto language = Napi::External<TSLanguage>::New(env, tree_sitter_p4());
language.TypeTag(&LANGUAGE_TYPE_TAG);
exports["language"] = language;
return exports;
}
NODE_API_MODULE(tree_sitter_p4_binding, Init)
+11
View File
@@ -0,0 +1,11 @@
import assert from "node:assert";
import { test } from "node:test";
import Parser from "tree-sitter";
test("can load grammar", () => {
const parser = new Parser();
assert.doesNotReject(async () => {
const { default: language } = await import("./index.js");
parser.setLanguage(language);
});
});
+60
View File
@@ -0,0 +1,60 @@
type BaseNode = {
type: string;
named: boolean;
};
type ChildNode = {
multiple: boolean;
required: boolean;
types: BaseNode[];
};
type NodeInfo =
| (BaseNode & {
subtypes: BaseNode[];
})
| (BaseNode & {
fields: { [name: string]: ChildNode };
children: ChildNode[];
});
/**
* The tree-sitter language object for this grammar.
*
* @see {@linkcode https://tree-sitter.github.io/node-tree-sitter/interfaces/Parser.Language.html Parser.Language}
*
* @example
* import Parser from "tree-sitter";
* import P4 from "tree-sitter-p4";
*
* const parser = new Parser();
* parser.setLanguage(P4);
*/
declare const binding: {
/**
* The inner language object.
* @private
*/
language: unknown;
/**
* The content of the `node-types.json` file for this grammar.
*
* @see {@linkplain https://tree-sitter.github.io/tree-sitter/using-parsers/6-static-node-types Static Node Types}
*/
nodeTypeInfo: NodeInfo[];
/** The syntax highlighting query for this grammar. */
HIGHLIGHTS_QUERY?: string;
/** The language injection query for this grammar. */
INJECTIONS_QUERY?: string;
/** The local variable query for this grammar. */
LOCALS_QUERY?: string;
/** The symbol tagging query for this grammar. */
TAGS_QUERY?: string;
};
export default binding;
+37
View File
@@ -0,0 +1,37 @@
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
const root = fileURLToPath(new URL("../..", import.meta.url));
const binding = typeof process.versions.bun === "string"
// Support `bun build --compile` by being statically analyzable enough to find the .node file at build-time
? await import(`${root}/prebuilds/${process.platform}-${process.arch}/tree-sitter-p4.node`)
: (await import("node-gyp-build")).default(root);
try {
const nodeTypes = await import(`${root}/src/node-types.json`, { with: { type: "json" } });
binding.nodeTypeInfo = nodeTypes.default;
} catch { }
const queries = [
["HIGHLIGHTS_QUERY", `${root}/queries/highlights.scm`],
["INJECTIONS_QUERY", `${root}/queries/injections.scm`],
["LOCALS_QUERY", `${root}/queries/locals.scm`],
["TAGS_QUERY", `${root}/queries/tags.scm`],
];
for (const [prop, path] of queries) {
Object.defineProperty(binding, prop, {
configurable: true,
enumerable: true,
get() {
delete binding[prop];
try {
binding[prop] = readFileSync(path, "utf8");
} catch { }
return binding[prop];
}
});
}
export default binding;
+16
View File
@@ -0,0 +1,16 @@
#ifndef TREE_SITTER_P4_H_
#define TREE_SITTER_P4_H_
typedef struct TSLanguage TSLanguage;
#ifdef __cplusplus
extern "C" {
#endif
const TSLanguage *tree_sitter_p4(void);
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_P4_H_
@@ -0,0 +1,12 @@
import XCTest
import SwiftTreeSitter
import TreeSitterP4
final class TreeSitterP4Tests: XCTestCase {
func testCanLoadGrammar() throws {
let parser = Parser()
let language = Language(language: tree_sitter_p4())
XCTAssertNoThrow(try parser.setLanguage(language),
"Error loading P4 grammar")
}
}