Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ jobs:
node-version: 22 # https://github.com/tree-sitter/node-tree-sitter/issues/268
test-python: true
test-go: true

- name: Run test wild
if: runner.os != 'Windows'
continue-on-error: true
run: node scripts/test_wild.mjs
44 changes: 34 additions & 10 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export default grammar({
[$._non_function_inline_type, $.generic_type],
[$._type_identifier, $.polymorphic_type],
[$.type_declaration],
[$.variant_type],
],

rules: {
Expand Down Expand Up @@ -339,10 +340,11 @@ export default grammar({
tuple_type: ($) => prec.dynamic(-1, seq("(", commaSep1t($._type), ")")),

variant_type: ($) =>
prec.left(
seq(
optional("|"),
barSep1(choice($.variant_declaration, $.variant_type_spread)),
seq(
optional("|"),
sep1(
seq(repeat($._newline), "|"),
choice($.variant_declaration, $.variant_type_spread),
),
),

Expand Down Expand Up @@ -745,8 +747,20 @@ export default grammar({
"~",
$.value_identifier,
optional($.as_aliasing),
optional($.type_annotation),
optional(field("default_value", $._labeled_parameter_default_value)),
optional(
choice(
seq(
$.type_annotation,
optional(
field("default_value", $._labeled_parameter_default_value),
),
),
seq(
field("default_value", $._labeled_parameter_default_value),
optional($.type_annotation),
),
),
),
),

abstract_type: ($) => seq("type", repeat1($.type_identifier)),
Expand Down Expand Up @@ -1080,10 +1094,20 @@ export default grammar({
coercion_expression: ($) =>
prec.left(
"coercion_relation",
seq(
field("left", $.expression),
field("operator", ":>"),
field("right", $._type),
choice(
seq(
field("left", $.expression),
field("operator", ":>"),
field("right", $._type),
),
seq(
"(",
field("left", $.expression),
field("source_type", $.type_annotation),
field("operator", ":>"),
field("right", $._type),
")",
),
),
),

Expand Down
170 changes: 170 additions & 0 deletions scripts/test_wild.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import child_process from "node:child_process";
import fs from "node:fs";
import path from "node:path";

const repositories = [
{
url: "https://github.com/rescript-lang/experimental-rescript-webapi",
commit: null,
},
Comment thread
aspeddro marked this conversation as resolved.
{ url: "https://github.com/rescript-lang/rescript-lang.org", commit: null },
{ url: "https://github.com/rescript-lang/rescript-react", commit: null },
{ url: "https://github.com/enviodev/hyperindex", commit: null },
{ url: "https://github.com/frontman-ai/frontman", commit: null },
{ url: "https://github.com/zth/rescript-relay", commit: null },
{ url: "https://github.com/glennsl/rescript-jest", commit: null },
{ url: "https://github.com/cca-io/rescript-mui", commit: null },
{ url: "https://github.com/DZakh/sury", commit: null },
{ url: "https://github.com/DZakh/rescript-envsafe", commit: null },
{ url: "https://github.com/DZakh/rescript-rest", commit: null }
];

const TEST_WILD_DIR = path.join(process.cwd(), "test_wild");

if (!fs.existsSync(TEST_WILD_DIR)) {
fs.mkdirSync(TEST_WILD_DIR);
}

const signals = {
SIGINT: 2,
SIGQUIT: 3,
SIGKILL: 9,
SIGTERM: 15,
};
async function exec(command, args = [], cwd = process.cwd(), options = {}) {
const { throwOnFail = options.stdio === "inherit" } = options;

const stdoutChunks = [];
const stderrChunks = [];

const subprocess = child_process.spawn(command, args, {
cwd,
shell: process.platform === "win32",
stdio: ["ignore", "pipe", "pipe"],
...options,
});

subprocess.stdout?.on("data", (chunk) => {
stdoutChunks.push(chunk);
});

subprocess.stderr?.on("data", (chunk) => {
stderrChunks.push(chunk);
});

return await new Promise((resolve, reject) => {
subprocess.once("error", (err) => {
reject(err);
});

subprocess.once("close", (exitCode, signal) => {
const stdout = Buffer.concat(stdoutChunks).toString("utf8");
const stderr = Buffer.concat(stderrChunks).toString("utf8");

let code = exitCode ?? 1;
if (signals[signal]) {
// + 128 is standard POSIX practice, see also https://nodejs.org/api/process.html#exit-codes
code = signals[signal] + 128;
}

if (throwOnFail && code !== 0) {
reject(
new Error(`Command ${command} exited with non-zero status: ${code}`),
);
} else {
resolve({ status: code, stdout, stderr });
}
});
});
}

async function cloneRepo({ url, commit }) {
const baseName = url.split("/").pop();
const dirName = commit ? `${baseName}-${commit}` : baseName;
const repoDir = path.join(TEST_WILD_DIR, dirName);
const label = commit ? `${baseName}@${commit}` : baseName;

if (fs.existsSync(repoDir)) {
console.log(`[${label}] already cloned, skipping`);
return { repoDir, dirName, label };
}

if (commit) {
console.log(`[${label}] cloning at commit ${commit}...`);
await exec("git", ["clone", url, repoDir]);
await exec("git", ["checkout", commit], repoDir);
} else {
console.log(`[${label}] cloning...`);
await exec("git", ["clone", "--depth=1", url, repoDir]);
}
console.log(`[${label}] cloned`);
return { repoDir, dirName, label };
}

async function parseRepo(dirName, label) {
const globPatternRes = `test_wild/${dirName}/**/*.res`;
console.log(`[${label}] running tree-sitter parse...`);

const { stdout, stderr } = await exec("npx", [
"tree-sitter",
"parse",
"--json-summary",
globPatternRes,
]);

const validJson = stdout
.split("\n")
.filter((line) => !line.trim().startsWith("test_wild"))
.join("\n");

try {
const result = JSON.parse(validJson);
return result.parse_summaries ?? [];
} catch {
console.error({
stdout: stdout,
stderr: stderr,
});
throw new Error(`Failed to parse json`);
}
}

async function processRepo(repo) {
const { dirName, label } = await cloneRepo(repo);

const summaries = await parseRepo(dirName, label);
const errors = summaries.filter((s) => !s.successful);

console.log(
`[${label}] ${summaries.length} files parsed, ${errors.length} errors`,
);
return { label, summaries, errors };
}

async function main() {
const results = await Promise.all(repositories.map(processRepo));

console.log("\n=== Parse Error Summary ===\n");
let totalErrors = 0;

for (const { label, summaries, errors } of results) {
console.log(
`${label}: ${errors.length} / ${summaries.length} files with errors`,
);
for (const entry of errors) {
console.log(` ${entry.file}`);
}
totalErrors += errors.length;
}

console.log(`\nTotal files with errors: ${totalErrors}`);

if (totalErrors > 0) {
throw new Error("Test wild failed");
}
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
Loading
Loading