00001 """ANTLR3 runtime package"""
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 import sys
00035 import optparse
00036
00037 import antlr3
00038
00039
00040 class _Main(object):
00041 def __init__(self):
00042 self.stdin = sys.stdin
00043 self.stdout = sys.stdout
00044 self.stderr = sys.stderr
00045
00046
00047 def parseOptions(self, argv):
00048 optParser = optparse.OptionParser()
00049 optParser.add_option(
00050 "--encoding",
00051 action="store",
00052 type="string",
00053 dest="encoding"
00054 )
00055 optParser.add_option(
00056 "--input",
00057 action="store",
00058 type="string",
00059 dest="input"
00060 )
00061 optParser.add_option(
00062 "--interactive", "-i",
00063 action="store_true",
00064 dest="interactive"
00065 )
00066 optParser.add_option(
00067 "--no-output",
00068 action="store_true",
00069 dest="no_output"
00070 )
00071 optParser.add_option(
00072 "--profile",
00073 action="store_true",
00074 dest="profile"
00075 )
00076 optParser.add_option(
00077 "--hotshot",
00078 action="store_true",
00079 dest="hotshot"
00080 )
00081
00082 self.setupOptions(optParser)
00083
00084 return optParser.parse_args(argv[1:])
00085
00086
00087 def setupOptions(self, optParser):
00088 pass
00089
00090
00091 def execute(self, argv):
00092 options, args = self.parseOptions(argv)
00093
00094 self.setUp(options)
00095
00096 if options.interactive:
00097 while True:
00098 try:
00099 input = raw_input(">>> ")
00100 except (EOFError, KeyboardInterrupt):
00101 self.stdout.write("\nBye.\n")
00102 break
00103
00104 inStream = antlr3.ANTLRStringStream(input)
00105 self.parseStream(options, inStream)
00106
00107 else:
00108 if options.input is not None:
00109 inStream = antlr3.ANTLRStringStream(options.input)
00110
00111 elif len(args) == 1 and args[0] != '-':
00112 inStream = antlr3.ANTLRFileStream(
00113 args[0], encoding=options.encoding
00114 )
00115
00116 else:
00117 inStream = antlr3.ANTLRInputStream(
00118 self.stdin, encoding=options.encoding
00119 )
00120
00121 if options.profile:
00122 try:
00123 import cProfile as profile
00124 except ImportError:
00125 import profile
00126
00127 profile.runctx(
00128 'self.parseStream(options, inStream)',
00129 globals(),
00130 locals(),
00131 'profile.dat'
00132 )
00133
00134 import pstats
00135 stats = pstats.Stats('profile.dat')
00136 stats.strip_dirs()
00137 stats.sort_stats('time')
00138 stats.print_stats(100)
00139
00140 elif options.hotshot:
00141 import hotshot
00142
00143 profiler = hotshot.Profile('hotshot.dat')
00144 profiler.runctx(
00145 'self.parseStream(options, inStream)',
00146 globals(),
00147 locals()
00148 )
00149
00150 else:
00151 self.parseStream(options, inStream)
00152
00153
00154 def setUp(self, options):
00155 pass
00156
00157
00158 def parseStream(self, options, inStream):
00159 raise NotImplementedError
00160
00161
00162 def write(self, options, text):
00163 if not options.no_output:
00164 self.stdout.write(text)
00165
00166
00167 def writeln(self, options, text):
00168 self.write(options, text + '\n')
00169
00170
00171 class LexerMain(_Main):
00172 def __init__(self, lexerClass):
00173 _Main.__init__(self)
00174
00175 self.lexerClass = lexerClass
00176
00177
00178 def parseStream(self, options, inStream):
00179 lexer = self.lexerClass(inStream)
00180 for token in lexer:
00181 self.writeln(options, str(token))
00182
00183
00184 class ParserMain(_Main):
00185 def __init__(self, lexerClassName, parserClass):
00186 _Main.__init__(self)
00187
00188 self.lexerClassName = lexerClassName
00189 self.lexerClass = None
00190 self.parserClass = parserClass
00191
00192
00193 def setupOptions(self, optParser):
00194 optParser.add_option(
00195 "--lexer",
00196 action="store",
00197 type="string",
00198 dest="lexerClass",
00199 default=self.lexerClassName
00200 )
00201 optParser.add_option(
00202 "--rule",
00203 action="store",
00204 type="string",
00205 dest="parserRule"
00206 )
00207
00208
00209 def setUp(self, options):
00210 lexerMod = __import__(options.lexerClass)
00211 self.lexerClass = getattr(lexerMod, options.lexerClass)
00212
00213
00214 def parseStream(self, options, inStream):
00215 lexer = self.lexerClass(inStream)
00216 tokenStream = antlr3.CommonTokenStream(lexer)
00217 parser = self.parserClass(tokenStream)
00218 result = getattr(parser, options.parserRule)()
00219 if result is not None:
00220 if hasattr(result, 'tree'):
00221 if result.tree is not None:
00222 self.writeln(options, result.tree.toStringTree())
00223 else:
00224 self.writeln(options, repr(result))
00225
00226
00227 class WalkerMain(_Main):
00228 def __init__(self, walkerClass):
00229 _Main.__init__(self)
00230
00231 self.lexerClass = None
00232 self.parserClass = None
00233 self.walkerClass = walkerClass
00234
00235
00236 def setupOptions(self, optParser):
00237 optParser.add_option(
00238 "--lexer",
00239 action="store",
00240 type="string",
00241 dest="lexerClass",
00242 default=None
00243 )
00244 optParser.add_option(
00245 "--parser",
00246 action="store",
00247 type="string",
00248 dest="parserClass",
00249 default=None
00250 )
00251 optParser.add_option(
00252 "--parser-rule",
00253 action="store",
00254 type="string",
00255 dest="parserRule",
00256 default=None
00257 )
00258 optParser.add_option(
00259 "--rule",
00260 action="store",
00261 type="string",
00262 dest="walkerRule"
00263 )
00264
00265
00266 def setUp(self, options):
00267 lexerMod = __import__(options.lexerClass)
00268 self.lexerClass = getattr(lexerMod, options.lexerClass)
00269 parserMod = __import__(options.parserClass)
00270 self.parserClass = getattr(parserMod, options.parserClass)
00271
00272
00273 def parseStream(self, options, inStream):
00274 lexer = self.lexerClass(inStream)
00275 tokenStream = antlr3.CommonTokenStream(lexer)
00276 parser = self.parserClass(tokenStream)
00277 result = getattr(parser, options.parserRule)()
00278 if result is not None:
00279 assert hasattr(result, 'tree'), "Parser did not return an AST"
00280 nodeStream = antlr3.tree.CommonTreeNodeStream(result.tree)
00281 nodeStream.setTokenStream(tokenStream)
00282 walker = self.walkerClass(nodeStream)
00283 result = getattr(walker, options.walkerRule)()
00284 if result is not None:
00285 if hasattr(result, 'tree'):
00286 self.writeln(options, result.tree.toStringTree())
00287 else:
00288 self.writeln(options, repr(result))
00289