Skip to content

Commit 66ccb79

Browse files
committed
☕ Output parse errors
1 parent b6b37ee commit 66ccb79

File tree

2 files changed

+60
-15
lines changed

2 files changed

+60
-15
lines changed

scripts/gen-function/parse.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,42 @@ export function parse(content: string): Definition[] {
2222
let last = -1;
2323
for (const match of content.matchAll(/\*(\w+?)\(\)\*/g)) {
2424
const fn = match[1];
25-
const i = match.index ?? 0;
26-
if (i < last) {
25+
const index = match.index!;
26+
if (index < last) {
2727
// It is contained previous block
2828
continue;
2929
}
30-
const s = content.lastIndexOf("\n", i);
31-
const ms = regexIndexOf(content, /\n[<>\s]|$/, i);
32-
const me = regexIndexOf(content, /\n[^<>\s]|$/, ms);
33-
const e = content.lastIndexOf("\n", me);
34-
const block = content
35-
.substring(s, e)
36-
.replace(/\n<?(?:\s+\*\S+?\*)+\s*$/, "") // Remove next block tag
37-
.trimEnd();
38-
last = s + block.length;
39-
definitions.push(parseBlock(fn, block));
30+
const { block, start, end } = extractBlock(content, index);
31+
const definition = parseBlock(fn, block);
32+
if (definition) {
33+
definitions.push(definition);
34+
last = end;
35+
} else {
36+
const line = content.substring(0, start + 1).split("\n").length;
37+
console.error(
38+
`Failed to parse function definition for ${fn} at line ${line}`,
39+
);
40+
}
4041
}
4142
return definitions;
4243
}
4344

45+
function extractBlock(content: string, index: number): {
46+
block: string;
47+
start: number;
48+
end: number;
49+
} {
50+
const s = content.lastIndexOf("\n", index);
51+
const ms = regexIndexOf(content, /\n[<>\s]|$/, index);
52+
const me = regexIndexOf(content, /\n[^<>\s]|$/, ms);
53+
const e = content.lastIndexOf("\n", me);
54+
const block = content
55+
.substring(s, e)
56+
.replace(/\n<?(?:\s+\*\S+?\*)+\s*$/, "") // Remove next block tag
57+
.trimEnd();
58+
return { block, start: s, end: s + block.length };
59+
}
60+
4461
/**
4562
* Parse function definition block.
4663
*

scripts/gen-option/parse.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,46 @@ export function parse(content: string) {
2525
content = content.replace(/\n vim:[^\n]*\s*$/, "");
2626

2727
const options: Option[] = [];
28+
const succeeds = new Set<number>();
29+
const errors: Array<{ name: string; start: number }> = [];
30+
let last = -1;
2831
for (const match of content.matchAll(/\*'(\w+)'\*/g)) {
2932
const name = match[1];
30-
const block = extractBlock(content, match.index ?? 0);
33+
const index = match.index!;
34+
if (index < last) {
35+
// It is contained previous block
36+
continue;
37+
}
38+
const { block, start, end } = extractBlock(content, index);
3139
const option = parseBlock(name, block);
3240
if (option) {
3341
options.push(option);
42+
succeeds.add(start);
43+
last = end;
44+
} else {
45+
errors.push({ name, start });
3446
}
3547
}
48+
49+
if (errors.length) {
50+
for (const { name, start } of errors) {
51+
if (!succeeds.has(start)) {
52+
const line = content.substring(0, start + 1).split("\n").length;
53+
console.error(
54+
`Failed to parse option definition for ${name} at line ${line}`,
55+
);
56+
}
57+
}
58+
}
59+
3660
return options;
3761
}
3862

39-
function extractBlock(content: string, index: number): string {
63+
function extractBlock(content: string, index: number): {
64+
block: string;
65+
start: number;
66+
end: number;
67+
} {
4068
const s = content.lastIndexOf("\n", index);
4169
const ms = regexIndexOf(content, /\n[^<>\s]|$/, s);
4270
const me = regexIndexOf(content, /\n[^<>\s]|$/, ms + 1);
@@ -45,7 +73,7 @@ function extractBlock(content: string, index: number): string {
4573
.substring(s, e)
4674
.replace(/(\n<?)(?:\s+\*\S+?\*)+\s*$/, "$1") // Remove next block tag
4775
.trimEnd();
48-
return block;
76+
return { block, start: s, end: s + block.length };
4977
}
5078

5179
function parseBlock(name: string, body: string): Option | undefined {

0 commit comments

Comments
 (0)