Spaces:
Sleeping
Sleeping
File size: 6,163 Bytes
a17642c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
#!/Users/alaahussein/Code/Winemakers-Dilemma/env/bin/python3
__author__ = 'jszheng'
import optparse
import sys
import os
import importlib
from antlr4 import *
# this is a python version of TestRig
def beautify_lisp_string(in_string):
indent_size = 3
add_indent = ' '*indent_size
out_string = in_string[0] # no indent for 1st (
indent = ''
for i in range(1, len(in_string)):
if in_string[i] == '(' and in_string[i+1] != ' ':
indent += add_indent
out_string += "\n" + indent + '('
elif in_string[i] == ')':
out_string += ')'
if len(indent) > 0:
indent = indent.replace(add_indent, '', 1)
else:
out_string += in_string[i]
return out_string
if __name__ == '__main__':
#############################################################
# parse options
# not support -gui -encoding -ps
#############################################################
usage = "Usage: %prog [options] Grammar_Name Start_Rule"
parser = optparse.OptionParser(usage=usage)
# parser.add_option('-t', '--tree',
# dest="out_file",
# default="default.out",
# help='set output file name',
# )
parser.add_option('-t', '--tree',
default=False,
action='store_true',
help='Print AST tree'
)
parser.add_option('-k', '--tokens',
dest="token",
default=False,
action='store_true',
help='Show Tokens'
)
parser.add_option('-s', '--sll',
dest="sll",
default=False,
action='store_true',
help='Show SLL'
)
parser.add_option('-d', '--diagnostics',
dest="diagnostics",
default=False,
action='store_true',
help='Enable diagnostics error listener'
)
parser.add_option('-a', '--trace',
dest="trace",
default=False,
action='store_true',
help='Enable Trace'
)
options, remainder = parser.parse_args()
if len(remainder) < 2:
print('ERROR: You have to provide at least 2 arguments!')
parser.print_help()
exit(1)
else:
grammar = remainder.pop(0)
start_rule = remainder.pop(0)
file_list = remainder
#############################################################
# check and load antlr generated files
#############################################################
# dynamic load the module and class
lexerName = grammar + 'Lexer'
parserName = grammar + 'Parser'
# check if the generate file exist
lexer_file = lexerName + '.py'
parser_file = parserName + '.py'
if not os.path.exists(lexer_file):
print("[ERROR] Can't find lexer file {}!".format(lexer_file))
print(os.path.realpath('.'))
exit(1)
if not os.path.exists(parser_file):
print("[ERROR] Can't find parser file {}!".format(lexer_file))
print(os.path.realpath('.'))
exit(1)
# current directory is where the generated file loaded
# the script might be in different place.
sys.path.append('.')
# print(sys.path)
# print("Load Lexer {}".format(lexerName))
module_lexer = __import__(lexerName, globals(), locals(), lexerName)
class_lexer = getattr(module_lexer, lexerName)
# print(class_lexer)
# print("Load Parser {}".format(parserName))
module_parser = __import__(parserName, globals(), locals(), parserName)
class_parser = getattr(module_parser, parserName)
# print(class_parser)
#############################################################
# main process steps.
#############################################################
def process(input_stream, class_lexer, class_parser):
lexer = class_lexer(input_stream)
token_stream = CommonTokenStream(lexer)
token_stream.fill()
if options.token: # need to show token
for tok in token_stream.tokens:
print(tok)
if start_rule == 'tokens':
return
parser = class_parser(token_stream)
if options.diagnostics:
parser.addErrorListener(DiagnosticErrorListener())
parser._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION
if options.tree:
parser.buildParseTrees = True
if options.sll:
parser._interp.predictionMode = PredictionMode.SLL
#parser.setTokenStream(token_stream)
parser.setTrace(options.trace)
if hasattr(parser, start_rule):
func_start_rule = getattr(parser, start_rule)
parser_ret = func_start_rule()
if options.tree:
lisp_tree_str = parser_ret.toStringTree(recog=parser)
print(beautify_lisp_string(lisp_tree_str))
else:
print("[ERROR] Can't find start rule '{}' in parser '{}'".format(start_rule, parserName))
#############################################################
# use stdin if not provide file as input stream
#############################################################
if len(file_list) == 0:
input_stream = InputStream(sys.stdin.read())
process(input_stream, class_lexer, class_parser)
exit(0)
#############################################################
# iterate all input file
#############################################################
for file_name in file_list:
if os.path.exists(file_name) and os.path.isfile(file_name):
input_stream = FileStream(file_name)
process(input_stream, class_lexer, class_parser)
else:
print("[ERROR] file {} not exist".format(os.path.normpath(file_name)))
|