Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ANTLR Python Lexer unit-testing
from antlr4 import CommonTokenStream, InputStream, Token
# from mytool.parsers.error_listener import MyErrorListener
def getTokensFromText(input_string, MyLexer):
input_stream = InputStream(input_string)
lexer = MyLexer(input_stream)
# lexer.addErrorListener(MyErrorListener)
stream = CommonTokenStream(lexer)
stream.fill()
return stream.tokens
def test_lexer_bad_input(self):
from mytool.parsers.Lexer import Lexer as MyToolsLexer
tokens = getTokensFromText("function ()", MyToolsLexer) # assumes MyLexer won't detect/parse "function ()"
self.assertEqual(1, len(tokens)) #// includes EOF
self.assertEqual(Token.EOF, tokens[0].type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment