-
Notifications
You must be signed in to change notification settings - Fork 91
Warning about capture characters #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
f83dfa3
d0b13e4
e9c090a
46d2537
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -176,11 +176,33 @@ export default class Grammar { | |
| * Reads grammar file data. Supports reading `bnf`, | ||
| * and `lex` grammars based on mode. | ||
| */ | ||
| static dataFromGrammarFile(grammarFile, grammarType = 'bnf') { | ||
| return Grammar.dataFromString( | ||
| fs.readFileSync(grammarFile, 'utf-8'), | ||
| grammarType | ||
| ); | ||
| static dataFromGrammarFile(grammarFile, { grammarType = 'bnf', useLocation = false }) { | ||
| const grammar = fs.readFileSync(grammarFile, 'utf8'); | ||
|
|
||
| // check if the bnf grammar contains location capture characters | ||
| if (grammarType === 'bnf' && !useLocation) { | ||
| const bnf = grammar | ||
| // lexer rules | ||
| .replace(/%lex[\n\s\S]*?\/lex/g, '') | ||
| // comments | ||
| .replace(/\/\*[\n\s\S]*?\*\//g, '') | ||
| .replace(/\/\/.*?\n/g, '') | ||
| //strings | ||
| .replace(/'(\\.|[^'\\])*'/g, '') | ||
| .replace(/"(\\.|[^"\\])*"/g, '') | ||
| .replace(/`(\\.|[^`\\])*`/g, '') | ||
| // module include | ||
| .replace(/%{[\n\s\S]*?%}/g, ''); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we need to use all these if (/@\d+/.test(grammar) {
// Warning
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. without those replaces, the warning would be displayed even when finding @ inside strings, comments, lex rules, etc, which isn't good, for example, css uses that character a lot, for media queries and stuff, so it could be part of a lexer rule if someone tries to make a parser for that language. im sure there are more languages that use that character. let me know if you want me to make that change anyway tho, after all its just a warning, not an error
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, sounds good - the only thing we need to make sure those replaces don't slow down much the handling.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i tested it and it doesn't take even 0.015ms in a file of 10 millon characters, at least on my pc. also thats executed only once, and when generating the parser, it won't affect the parser itself at all, so i don't think it's a problem |
||
|
|
||
| if (/@\w+/.test(bnf)) { | ||
| console.info(colors.red( | ||
| 'The grammar file contains location capture characters (@), which require the ' + | ||
| '"--loc" option, but it has not been provided. The generated parser will throw an error.' | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| return Grammar.dataFromString(grammar, grammarType); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.