Skip to content

Instantly share code, notes, and snippets.

@nmz787
Last active May 19, 2020 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmz787/4888cfadf707a575de0662f8a3914ce0 to your computer and use it in GitHub Desktop.
Save nmz787/4888cfadf707a575de0662f8a3914ce0 to your computer and use it in GitHub Desktop.
ANTLR not working for some reason that I can't figure out
lexer grammar Lexer;
fragment WS: [\t ]+;
fragment NL: WS? ('\r'* '\n')+;
COLON_ATTRIBUTES: ('KeyId' |
'first key' |
'key' |
'key (interesting)' |
'key(# of 1second)' |
'key.subkey')+ -> mode(COLON_ATTRIBUTES_MODE);
mode COLON_ATTRIBUTES_MODE; //key: value is whatever might come before the newline
COLON_ATTRIBUTES_INTERMEDIATE: (' '|'\t')* ':' (' '|'\t')* -> channel(HIDDEN);
COLON_ATTRIBUTES_DATA: ~[\r\n]+;
COLON_ATTRIBUTES_END: NL -> channel(HIDDEN), mode(DEFAULT_MODE);
from antlr4 import *
if __name__ is not None and "." in __name__:
from .Parser import Parser
else:
from Parser import Parser
# This class defines a complete listener for a parse tree produced by Parser.
class Listener(ParseTreeListener):
def __init__(self):
super().__init__()
self.colon_attributes = {}
# Enter a parse tree produced by Parser#whole_log.
def enterWhole_log(self, ctx:Parser.Whole_logContext):
pass
# Exit a parse tree produced by Parser#whole_log.
def exitWhole_log(self, ctx:Parser.Whole_logContext):
pass
# Enter a parse tree produced by Parser#colon_attributes.
def enterColon_attributes(self, ctx:Parser.Colon_attributesContext):
k, v = ctx.children[0].getText(), ctx.children[2].getText()
print(f'key ({k}) value ({v})')
self.colon_attributes[k] = v
# Exit a parse tree produced by Parser#colon_attributes.
def exitColon_attributes(self, ctx:Parser.Colon_attributesContext):
pass
del Parser
parser grammar Parser;
options {
tokenVocab=Lexer;
}
whole_log: colon_attributes+;
colon_attributes: COLON_ATTRIBUTES COLON_ATTRIBUTES_INTERMEDIATE COLON_ATTRIBUTES_DATA COLON_ATTRIBUTES_END;
r"""
"C:\Program Files\JetBrains\PyCharm Community Edition 2020.1\jbr\bin\java.exe" -Xmx500M -cp "C:\Users\nmz787\Downloads\antlr-4.8-complete.jar" org.antlr.v4.Tool Lexer.g4 -Dlanguage=Python3
"C:\Program Files\JetBrains\PyCharm Community Edition 2020.1\jbr\bin\java.exe" -Xmx500M -cp "C:\Users\nmz787\Downloads\antlr-4.8-complete.jar" org.antlr.v4.Tool Parser.g4 -Dlanguage=Python3
"""
from antlr4 import CommonTokenStream, ParseTreeWalker, InputStream
inp = InputStream("""key: value
""")
from Lexer import Lexer
from Parser import Parser
from Listener import Listener
lexer = Lexer(inp)
stream = CommonTokenStream(lexer)
parser = Parser(stream)
printer = Listener()
# parser.removeErrorListeners()
# lexer.removeErrorListeners()
tree = parser.whole_log()
walker = ParseTreeWalker()
walker.walk(printer, tree)
assert 'key' in printer.colon_attributes
assert 'value' == printer.colon_attributes['key'], f"actual value was ({printer.colon_attributes['key']})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment