LexerATNSimulator: avoid repeatable import of Lexer

Importing module is quite expensive operation for using it
in a lexer during normal operations.

The patch avoids it by caching the required properties
in LexerATNSimulator object.

Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
This commit is contained in:
Alexey Khoroshilov 2019-01-01 14:13:17 +03:00
parent 6808f9c4b4
commit f73ae702f2
2 changed files with 8 additions and 7 deletions

View File

@ -210,4 +210,5 @@ YYYY/MM/DD, github id, Full name, email
2018/11/14, nxtstep, Adriaan (Arjan) Duz, codewithadriaan[et]gmail[dot]com
2018/11/15, amykyta3, Alex Mykyta, amykyta3@users.noreply.github.com
2018/11/29, hannemann-tamas, Ralf Hannemann-Tamas, ralf.ht@gmail.com
2018/12/20, WalterCouto, Walter Couto, WalterCouto@users.noreply.github.com
2018/12/20, WalterCouto, Walter Couto, WalterCouto@users.noreply.github.com
2019/01/01, khoroshilov, Alexey Khoroshilov, khoroshilov@ispras.ru

View File

@ -75,6 +75,9 @@ class LexerATNSimulator(ATNSimulator):
self.column = 0
from antlr4.Lexer import Lexer
self.mode = Lexer.DEFAULT_MODE
# Cache Lexer properties to avoid further imports
self.DEFAULT_MODE = Lexer.DEFAULT_MODE
self.MAX_CHAR_VALUE = Lexer.MAX_CHAR_VALUE
# Used during DFA/ATN exec to record the most recent accept configuration info
self.prevAccept = SimState()
@ -105,8 +108,7 @@ class LexerATNSimulator(ATNSimulator):
self.startIndex = -1
self.line = 1
self.column = 0
from antlr4.Lexer import Lexer
self.mode = Lexer.DEFAULT_MODE
self.mode = self.DEFAULT_MODE
def matchATN(self, input:InputStream):
startState = self.atn.modeToStartState[self.mode]
@ -291,8 +293,7 @@ class LexerATNSimulator(ATNSimulator):
lexerActionExecutor.execute(self.recog, input, startIndex)
def getReachableTarget(self, trans:Transition, t:int):
from antlr4.Lexer import Lexer
if trans.matches(t, 0, Lexer.MAX_CHAR_VALUE):
if trans.matches(t, 0, self.MAX_CHAR_VALUE):
return trans.target
else:
return None
@ -420,8 +421,7 @@ class LexerATNSimulator(ATNSimulator):
elif t.serializationType in [ Transition.ATOM, Transition.RANGE, Transition.SET ]:
if treatEofAsEpsilon:
from antlr4.Lexer import Lexer
if t.matches(Token.EOF, 0, Lexer.MAX_CHAR_VALUE):
if t.matches(Token.EOF, 0, self.MAX_CHAR_VALUE):
c = LexerATNConfig(state=t.target, config=config)
return c