Skip to content

Commit 1dd3348

Browse files
committed
Improve ##groups
1 parent 415879f commit 1dd3348

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

grammars/c23/preprocessing.bnf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ if_group = "#" (
1515
| ("ifdef\b" identifier group_part*)
1616
| ("ifndef\b" identifier group_part*)
1717
);
18-
##groups if_group if ifdef ifndef
18+
##groups if_group %#if %#ifdef %#ifndef
1919

2020
elif_group = "#" (
2121
| ("elif\b" constant_expression group_part*)
2222
| ("elifdef\b" identifier group_part*)
2323
| ("elifndef\b" identifier group_part*)
2424
);
25-
##groups elif_group elif elifdef elifndef
25+
##groups elif_group %#elif %#elifdef %#elifndef
2626

2727
else_group = "#" "else\b" group_part*;
2828

scripts/bnf.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
parser: { short: 'P', type: 'string' },
1919
verbose: { short: 'V', type: 'boolean', multiple: true },
2020
help: { short: 'h', type: 'boolean', default: false },
21+
compress: { type: 'boolean', default: false },
2122
},
2223
allowPositionals: true,
2324
});
@@ -29,6 +30,7 @@ Output options:
2930
--output,-o <path> Path to an output file
3031
--colors,-c Colorize output messages
3132
--help,-h Display this help message
33+
--compress Compress the output config
3234
Debugging options:
3335
--tokens,-T [only] Show tokenizer output. If 'only', only the tokenizer output will be shown.
3436
--parser,-P [only] Show parser output. If 'only', only the parser output will be shown.
@@ -112,7 +114,9 @@ function include(path) {
112114
}
113115
}
114116

115-
const config = compressConfig(bnf.ast_to_config(ast, logger(verbose), include));
117+
let config = bnf.ast_to_config(ast, logger(verbose), include);
118+
119+
if (options.compress) config = compressConfig(config);
116120

117121
const write = data => (options.output ? writeFileSync(options.output, data) : console.log);
118122

src/bnf.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function ast_to_config(ast: Node[], log: Logger = () => {}, include?: (na
8181
}
8282
// ##groups <rule> <name 0> <name 1> ... <name n>
8383
case 'groups': {
84-
const [, name, _names] = contents.match(/(\w+)\s+([\s\w]+)/) || [];
84+
const [, name, _names] = contents.match(/(\w+)\s+(.+)/) || [];
8585
const groupNames = _names.split(/[\s,]+/);
8686
const rule = definitions.find(d => d.name == name);
8787
if (!rule) {
@@ -96,14 +96,16 @@ export function ast_to_config(ast: Node[], log: Logger = () => {}, include?: (na
9696
break;
9797
}
9898

99-
for (const part of rule.pattern) {
99+
const new_name = groupNames[i].replaceAll('%', name);
100+
101+
for (const part of definitions.flatMap(d => d.pattern)) {
100102
if (part.kind == group.name) {
101-
part.kind = groupNames[i];
103+
part.kind = new_name;
102104
}
103105
}
104106

105-
_log(1, `Renaming group: ${group.name} -> ${groupNames[i]}`);
106-
group.name = groupNames[i];
107+
_log(1, `Renaming group: ${group.name} -> ${new_name}`);
108+
group.name = new_name;
107109
}
108110
break;
109111
}

0 commit comments

Comments
 (0)