Reputation:
I am playing around with ANTLR and building a DSL like jsx.
But in jsx you have javasciprt expressions inside {}, how can I define those ECMAScript rules in my lexer grammar? Should I rewrite the whole ECMAScript lexer from scratch or there are some ways to import those rules in my lexer grammar?
<div class={javasciprt expression in here}></div>
Upvotes: 0
Views: 257
Reputation: 170308
You can use lexical modes for that:
lexer grammar YourCurrentLexer;
EXISTING_TOKEN
: '...'
;
// other tokens
ECMA_START
: '{' -> pushMode(ECMA_MODE)
;
mode ECMA_MODE;
ECMA_TOKEN
: '...'
;
// other ECMA tokens
// Support nested { ... }
OPEN_BRACE
: '{' -> pushMode(ECMA_MODE)
;
CLOSE_BRACE
: '}' -> popMode
;
Should I rewrite the whole ECMAScript lexer from scratch or there are some ways to import those rules in my lexer grammar?
You cannot just import an existing grammar inside a lexical mode. Just copy-paste the rules you want: https://github.com/antlr/grammars-v4/blob/master/javascript/ecmascript/JavaScript/ECMAScript.g4
Upvotes: 1