mcchatman8009 / antlr4-tool

A useful Antlr4 tool with full TypeScript support
Other
36 stars 20 forks source link

package is a reserved word in strict mode #10

Closed krazyjakee closed 6 years ago

krazyjakee commented 6 years ago

I get an error when trying to import in babel.

Error: SyntaxError: LanguageLexer.js: package is a reserved word in strict mode

// Generated from Language.g4 by ANTLR 4.7
// jshint ignore: start
var antlr4 = require('antlr4/index');
package antlr; // <---
mcchatman8009 commented 6 years ago

@krazyjakee Hmm... that's interesting, is package a rule name?

krazyjakee commented 6 years ago

@mcchatman8009 it's classification is "reserved for the future" whatever the hell that means. In any case, since the syntax has actually mystified me, what is the course of action here?

mcchatman8009 commented 6 years ago

@krazyjakee I've never seen the package keyword used or emitted in any generated Lexer before. Could you send me your current steps in running the tool and the fundamental rules of your grammar? I would very much like to help you with your issue. I'm just not clear on the steps to reproduce this issue. Also, could you verify which version you are currently using?

If you send me the Grammar and the steps, I'm certain we can figure out your issue.

Thanks,

krazyjakee commented 6 years ago

@mcchatman8009 I hadn't seen it before (or bothered to look) but I think it's this...

In the first few lines of the g4 file:

grammar Language;

@header {
package antlr;
}

I'm guessing this gets put out the javascript.

mcchatman8009 commented 6 years ago

@krazyjakee it looks like your Antlr4 grammar is based on Java and not JavaScript. The header occurs before the lexer or parser class gets defined.

I believe it’s ideal if the grammar has no language specifics in it. Allowing for it to be portable. But I understand that in some cases it’s needed.

My solution would be to update the grammar, removing the Java package declaration. Making all the header sections and actions be JavaScript compliant. JavaScript doesn’t have a need for the package keyword. Your issue is that the grammar has Java assumptions baked into it, and is not fit for JavaScript based lexing and parsing.

mcchatman8009 commented 6 years ago

@krazyjakee so it looks like you found your problem. Thanks for reporting your issues.