Created
May 19, 2020 23:06
-
-
Save nmz787/0ad1f42fe6cb2838befdb6c13e28210e to your computer and use it in GitHub Desktop.
Run with:
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): | |
# 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): | |
print([c.getText() for c in ctx.getChildren()]) | |
pass | |
# 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
""" | |
"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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment