From 71cdf89eb330cb402a57de598badb11a0dcf56c8 Mon Sep 17 00:00:00 2001 From: Eric Vergnaud Date: Sat, 3 Oct 2020 14:09:46 +0800 Subject: [PATCH] generate es6 classes --- .../codegen/JavaScript/JavaScript.stg | 211 +++++++++--------- 1 file changed, 100 insertions(+), 111 deletions(-) diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/JavaScript/JavaScript.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/JavaScript/JavaScript.stg index aed7a25ac..de26a9efc 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/JavaScript/JavaScript.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/JavaScript/JavaScript.stg @@ -48,12 +48,12 @@ javascriptTypeInitMap ::= [ ParserFile(file, parser, namedActions, contextSuperClass) ::= << -var antlr4 = require('antlr4/index'); +const antlr4 = require('antlr4'); -var Listener = require('./Listener').Listener; +const Listener = require('./Listener').default; -var Visitor = require('./Visitor').Visitor; +const Visitor = require('./Visitor').default; @@ -62,54 +62,43 @@ var Visitor = require('./Visitor'). -var antlr4 = require('antlr4/index'); +const antlr4 = require('antlr4'); // This class defines a complete listener for a parse tree produced by . -function Listener() { - return this; -} - -Listener.prototype = Object.create(antlr4.tree.ParseTreeListener.prototype); -Listener.prototype.constructor = Listener; +export default class Listener extends antlr4.tree.ParseTreeListener { #. -Listener.prototype.enter = function(ctx) { -\}; + // Enter a parse tree produced by #. + enter(ctx) { + \} -// Exit a parse tree produced by #. -Listener.prototype.exit = function(ctx) { -\}; + // Exit a parse tree produced by #. + exit(ctx) { + \} }; separator="\n"> -exports.Listener = Listener; +} >> VisitorFile(file, header, namedActions) ::= << -var antlr4 = require('antlr4/index'); +const antlr4 = require('antlr4'); // This class defines a complete generic visitor for a parse tree produced by . -function Visitor() { - antlr4.tree.ParseTreeVisitor.call(this); - return this; -} - -Visitor.prototype = Object.create(antlr4.tree.ParseTreeVisitor.prototype); -Visitor.prototype.constructor = Visitor; +export default class Visitor extends antlr4.tree.ParseTreeVisitor { #. -Visitor.prototype.visit = function(ctx) { - return this.visitChildren(ctx); -\}; + // Visit a parse tree produced by #. + visit = function(ctx) { + return this.visitChildren(ctx); + \} }; separator="\n"> -exports.Visitor = Visitor; +} >> @@ -120,26 +109,27 @@ fileHeader(grammarFileName, ANTLRVersion) ::= << Parser(parser, funcs, atn, sempredFuncs, superClass) ::= << -var = require('./').; +const = require('./.js').default; -var grammarFileName = ""; - -var atn = new antlr4.atn.ATNDeserializer().deserialize(serializedATN); +const atn = new antlr4.atn.ATNDeserializer().deserialize(serializedATN); -var decisionsToDFA = atn.decisionToState.map( function(ds, index) { return new antlr4.dfa.DFA(ds, index); }); +const decisionsToDFA = atn.decisionToState.map( (ds, index) => new antlr4.dfa.DFA(ds, index) ); -var sharedContextCache = new antlr4.PredictionContextCache(); +const sharedContextCache = new antlr4.PredictionContextCache(); -var literalNames = [ }; null="null", separator=", ", wrap, anchor> ]; +const literalNames = [ }; null="null", separator=", ", wrap, anchor> ]; -var symbolicNames = [ }; null="null", separator=", ", wrap, anchor> ]; +const symbolicNames = [ }; null="null", separator=", ", wrap, anchor> ]; -var ruleNames = [ "}; separator=", ", wrap, anchor> ]; +const ruleNames = [ "}; separator=", ", wrap, anchor> ]; + +export default class extends { + + static grammarFileName = ""; -class extends { constructor(input) { super(input); this._interp = new antlr4.atn.ParserATNSimulator(this, atn, decisionsToDFA, sharedContextCache); @@ -161,10 +151,13 @@ class extends { default: throw "No predicate with index:" + ruleIndex; } - }; + } + + + } .EOF = antlr4.Token.EOF; @@ -174,12 +167,19 @@ class extends { .RULE_ = ;}; separator="\n", wrap, anchor> - +}; separator="\n"> -module.exports. = ; + +. = ; }; separator="\n"> >> +ruleContexts(currentRule) ::= << + + +}; separator="\n"> +>> + dumpActions(recog, argFuncs, actionFuncs, sempredFuncs) ::= << .prototype.action = function(localctx, ruleIndex, actionIndex) { @@ -244,16 +244,9 @@ RuleSempredFunction(r, actions) ::= << RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,postamble,exceptions) ::= << - -}; separator="\n"> - - -. = ; - -.prototype. = function(}; separator=", ">) { - - var localctx = new (this, this._ctx, this.state}>); +(}; separator=", ">) { + let localctx = new (this, this._ctx, this.state}>); this.enterRule(localctx, , .RULE_); @@ -277,7 +270,7 @@ RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,fina this.exitRule(); } return localctx; -}; +} >> @@ -285,18 +278,15 @@ LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs, namedActions,finallyAction,postamble) ::= << - -}; separator="\n"> - -.prototype. = function(_p, }>) { +(_p, }>) { if(_p===undefined) { _p = 0; } - var _parentctx = this._ctx; - var _parentState = this.state; - var localctx = new (this, this._ctx, _parentState}>); - var _prevctx = localctx; - var _startState = ; + const _parentctx = this._ctx; + const _parentState = this.state; + let localctx = new (this, this._ctx, _parentState}>); + let _prevctx = localctx; + const _startState = ; this.enterRecursionRule(localctx, , .RULE_, _p); @@ -317,7 +307,7 @@ LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs, this.unrollRecursionContexts(_parentctx) } return localctx; -}; +} >> @@ -424,7 +414,7 @@ if(la_===+1) { StarBlock(choice, alts, sync, iteration) ::= << this.state = ; this._errHandler.sync(this); -var _alt = this._interp.adaptivePredict(this._input,,this._ctx) +let _alt = this._interp.adaptivePredict(this._input,,this._ctx) while(_alt!= && _alt!=antlr4.atn.ATN.INVALID_ALT_NUMBER) { if(_alt===1+1) { @@ -440,7 +430,7 @@ while(_alt!= && _alt!=antlr4.atn.ATN.INVALID_ALT_NUMBER) { PlusBlock(choice, alts, error) ::= << this.state = ; this._errHandler.sync(this); -var _alt = 1+1; +let _alt = 1+1; do { switch (_alt) { = function() { +() { return this.getToken(., 0); }; >> // should never be called ContextTokenListGetterDecl(t) ::= << -def _list(self): - return self.getTokens(.) +_list() { + return this.getTokens(.); +} >> ContextTokenListIndexedGetterDecl(t) ::= << @@ -630,16 +621,16 @@ ContextTokenListIndexedGetterDecl(t) ::= << >> ContextRuleGetterDecl(r) ::= << - = function() { +() { return this.getTypedRuleContext(,0); }; >> // should never be called ContextRuleListGetterDecl(r) ::= << -def _list(self): - return self.getTypedRuleContexts(.) - +_list() { + return this.getTypedRuleContexts(.); +} >> ContextRuleListIndexedGetterDecl(r) ::= << @@ -672,6 +663,7 @@ CaptureNextTokenType(d) ::= " = this._input.LA(1);" StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers) ::= << class extends antlr4.ParserRuleContext { + constructor(parser, parent, invokingState}>) { if(parent===undefined) { parent = null; @@ -685,47 +677,48 @@ class extends antl }; separator="\n"> = || null;}; separator="\n"> } + + }; separator="\n\n"> + + + copyFrom(ctx) { + super.copyFrom(ctx); + = ctx.;}; separator="\n"> + } + + + + } -.prototype.}; separator="\n\n"> - - -.prototype.copyFrom = function(ctx) { - antlr4.ParserRuleContext.prototype.copyFrom.call(this, ctx); - = ctx.;}; separator="\n"> -}; - - - - >> AltLabelStructDecl(struct,attrs,getters,dispatchMethods) ::= << -class extends Context { +class extends Context { + constructor(parser, ctx) { super(parser); ;}; separator="\n"> - Context.prototype.copyFrom.call(this, ctx); + super.copyFrom(ctx); } -} -.prototype = Object.create(Context.prototype); -.prototype.constructor = ; + }; separator="\n\n"> + + + +} . = ; -.prototype.}; separator="\n\n"> - - >> ListenerDispatchMethod(method) ::= << -.prototype.enterexitRule = function(listener) { +enterexitRule(listener) { if(listener instanceof Listener ) { listener.enterexit(this); } -}; +} >> @@ -787,7 +780,7 @@ _prevctx = localctx; LexerFile(lexerFile, lexer, namedActions) ::= << -var antlr4 = require('antlr4/index'); +const antlr4 = require('antlr4'); @@ -797,32 +790,28 @@ var antlr4 = require('antlr4/index'); Lexer(lexer, atn, actionFuncs, sempredFuncs, superClass) ::= << -var = require('./').; +const = require('./.js').default; -var atn = new antlr4.atn.ATNDeserializer().deserialize(serializedATN); +const atn = new antlr4.atn.ATNDeserializer().deserialize(serializedATN); -var decisionsToDFA = atn.decisionToState.map( function(ds, index) { return new antlr4.dfa.DFA(ds, index); }); +const decisionsToDFA = atn.decisionToState.map( (ds, index) => new antlr4.dfa.DFA(ds, index) ); + +export default class extends antlr4.Lexer { + + static grammarFileName = ""; + static channelNames = [ "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator=", ", wrap, anchor> ]; + static modeNames = [ "}; separator=", ", wrap, anchor> ]; + static literalNames = [ }; null="null", separator=", ", wrap, anchor> ]; + static symbolicNames = [ }; null="null", separator=", ", wrap, anchor> ]; + static ruleNames = [ "}; separator=", ", wrap, anchor> ]; -class extends antlr4.Lexer { constructor(input) { super(input) this._interp = new antlr4.atn.LexerATNSimulator(this, atn, decisionsToDFA, new antlr4.PredictionContextCache()); - - this.channelNames = [ "DEFAULT_TOKEN_CHANNEL", "HIDDEN", "}; separator=", ", wrap, anchor> ]; - - this.modeNames = [ "}; separator=", ", wrap, anchor> ]; - - this.literalNames = [ }; null="null", separator=", ", wrap, anchor> ]; - - this.symbolicNames = [ }; null="null", separator=", ", wrap, anchor> ]; - - this.ruleNames = [ "}; separator=", ", wrap, anchor> ]; - - this.grammarFileName = ""; } get atn() { @@ -851,7 +840,7 @@ module.exports. = ; SerializedATN(model) ::= << -var serializedATN = [" "}>"].join(""); +const serializedATN = [" "}>"].join(""); >>