Last active
May 19, 2020 23:23
-
-
Save nmz787/4888cfadf707a575de0662f8a3914ce0 to your computer and use it in GitHub Desktop.
ANTLR not working for some reason that I can't figure out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parser grammar Parser; | |
options { | |
tokenVocab=Lexer; | |
} | |
whole_log: colon_attributes+; | |
colon_attributes: COLON_ATTRIBUTES COLON_ATTRIBUTES_INTERMEDIATE COLON_ATTRIBUTES_DATA COLON_ATTRIBUTES_END; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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