Skip to content

Commit 7ba6d0e

Browse files
Merge pull request #119 from misdocumeno/warn-missing-loc-option
Warning about capture characters
2 parents 91ff38f + 46d2537 commit 7ba6d0e

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

src/bin/syntax.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,10 @@ function getGrammar(grammarFile, mode) {
554554
return null;
555555
}
556556

557-
const grammarData = Grammar.dataFromGrammarFile(grammarFile, 'bnf');
557+
const grammarData = Grammar.dataFromGrammarFile(grammarFile, {
558+
grammarType: 'bnf',
559+
useLocation: options.loc,
560+
});
558561

559562
// If explicit lexical grammar file was passed, use it.
560563
const lexGrammarData = getLexGrammarData(options);
@@ -584,7 +587,7 @@ function getLexGrammarData(options) {
584587

585588
// If explicit lexical grammar file was passed, use it.
586589
if (options.lex) {
587-
data = Grammar.dataFromGrammarFile(options.lex, 'lex');
590+
data = Grammar.dataFromGrammarFile(options.lex, { grammarType: 'lex' });
588591
}
589592

590593
if (options['ignore-whitespaces'] && !data) {

src/grammar/grammar.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,41 @@ export default class Grammar {
168168
* for the specific options.
169169
*/
170170
static fromGrammarFile(grammarFile, options = {}, grammarType = 'bnf') {
171-
const grammarData = Grammar.dataFromGrammarFile(grammarFile, grammarType);
171+
const grammarData = Grammar.dataFromGrammarFile(grammarFile, { grammarType });
172172
return Grammar.fromData(grammarData, options);
173173
}
174174

175175
/**
176176
* Reads grammar file data. Supports reading `bnf`,
177177
* and `lex` grammars based on mode.
178178
*/
179-
static dataFromGrammarFile(grammarFile, grammarType = 'bnf') {
180-
return Grammar.dataFromString(
181-
fs.readFileSync(grammarFile, 'utf-8'),
182-
grammarType
183-
);
179+
static dataFromGrammarFile(grammarFile, { grammarType = 'bnf', useLocation = false }) {
180+
const grammar = fs.readFileSync(grammarFile, 'utf8');
181+
182+
// check if the bnf grammar contains location capture characters
183+
if (grammarType === 'bnf' && !useLocation) {
184+
const bnf = grammar
185+
// lexer rules
186+
.replace(/%lex[\n\s\S]*?\/lex/g, '')
187+
// comments
188+
.replace(/\/\*[\n\s\S]*?\*\//g, '')
189+
.replace(/\/\/.*?\n/g, '')
190+
//strings
191+
.replace(/'(\\.|[^'\\])*'/g, '')
192+
.replace(/"(\\.|[^"\\])*"/g, '')
193+
.replace(/`(\\.|[^`\\])*`/g, '')
194+
// module include
195+
.replace(/%{[\n\s\S]*?%}/g, '');
196+
197+
if (/@\w+/.test(bnf)) {
198+
console.info(colors.red(
199+
'The grammar file contains location capture characters (@), which require the ' +
200+
'"--loc" option, but it has not been provided. The generated parser will throw an error.'
201+
));
202+
}
203+
}
204+
205+
return Grammar.dataFromString(grammar, grammarType);
184206
}
185207

186208
/**

0 commit comments

Comments
 (0)