| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
from __future__ import print_function |
| 15 |
|
| 16 |
import sys, re |
| 17 |
|
| 18 |
import yappsrt, parsetree |
| 19 |
|
| 20 |
def generate(inputfilename, outputfilename='', dump=0, **flags): |
| 21 |
"""Generate a grammar, given an input filename (X.g) |
| 22 |
and an output filename (defaulting to X.py).""" |
| 23 |
|
| 24 |
if not outputfilename: |
| 25 |
if inputfilename.endswith('.g'): |
| 26 |
outputfilename = inputfilename[:-2] + '.py' |
| 27 |
else: |
| 28 |
raise Exception('Must specify output filename if input filename is not *.g') |
| 29 |
|
| 30 |
DIVIDER = '\n%%\n' |
| 31 |
preparser, postparser = None, None |
| 32 |
|
| 33 |
|
| 34 |
s = open(inputfilename,'r').read() |
| 35 |
|
| 36 |
|
| 37 |
f = s.find(DIVIDER) |
| 38 |
if f >= 0: preparser, s = s[:f]+'\n\n', s[f+len(DIVIDER):] |
| 39 |
|
| 40 |
|
| 41 |
f = s.find(DIVIDER) |
| 42 |
if f >= 0: s, postparser = s[:f], '\n\n'+s[f+len(DIVIDER):] |
| 43 |
|
| 44 |
|
| 45 |
scanner = grammar.ParserDescriptionScanner(s) |
| 46 |
if preparser: scanner.first_line_number = 1 + preparser.count('\n') |
| 47 |
parser = grammar.ParserDescription(scanner) |
| 48 |
t = yappsrt.wrap_error_reporter(parser, 'Parser') |
| 49 |
if t is None: return |
| 50 |
if preparser is not None: t.preparser = preparser |
| 51 |
if postparser is not None: t.postparser = postparser |
| 52 |
|
| 53 |
|
| 54 |
for f in t.options.keys(): |
| 55 |
for opt,_,_ in yapps_options: |
| 56 |
if f == opt: break |
| 57 |
else: |
| 58 |
print('Warning: unrecognized option', f, file=sys.stderr) |
| 59 |
|
| 60 |
for f in flags.keys(): t.options[f] = flags[f] |
| 61 |
|
| 62 |
|
| 63 |
if dump: |
| 64 |
t.dump_information() |
| 65 |
else: |
| 66 |
t.output = open(outputfilename, 'w') |
| 67 |
t.generate_output() |
| 68 |
|
| 69 |
if __name__ == '__main__': |
| 70 |
import doctest |
| 71 |
doctest.testmod(sys.modules['__main__']) |
| 72 |
doctest.testmod(parsetree) |
| 73 |
|
| 74 |
|
| 75 |
yapps_options = [ |
| 76 |
('context-insensitive-scanner', |
| 77 |
'context-insensitive-scanner', |
| 78 |
'Scan all tokens (see docs)'), |
| 79 |
] |
| 80 |
|
| 81 |
import getopt |
| 82 |
optlist, args = getopt.getopt(sys.argv[1:], 'f:', ['help', 'dump', 'use-devel-grammar']) |
| 83 |
if not args or len(args) > 2: |
| 84 |
print('Usage:', file=sys.stderr) |
| 85 |
print(' python', sys.argv[0], '[flags] input.g [output.py]', file=sys.stderr) |
| 86 |
print('Flags:', file=sys.stderr) |
| 87 |
print((' --dump' + ' '*40)[:35] + 'Dump out grammar information', file=sys.stderr) |
| 88 |
print((' --use-devel-grammar' + ' '*40)[:35] + 'Use the devel grammar parser from yapps_grammar.py instead of the stable grammar from grammar.py', file=sys.stderr) |
| 89 |
for flag, _, doc in yapps_options: |
| 90 |
print((' -f' + flag + ' '*40)[:35] + doc, file=sys.stderr) |
| 91 |
else: |
| 92 |
|
| 93 |
flags = {} |
| 94 |
use_devel_grammar = 0 |
| 95 |
for opt in optlist: |
| 96 |
for flag, name, _ in yapps_options: |
| 97 |
if opt == ('-f', flag): |
| 98 |
flags[name] = 1 |
| 99 |
break |
| 100 |
else: |
| 101 |
if opt == ('--dump', ''): |
| 102 |
flags['dump'] = 1 |
| 103 |
elif opt == ('--use-devel-grammar', ''): |
| 104 |
use_devel_grammar = 1 |
| 105 |
else: |
| 106 |
print('Warning: unrecognized option', opt[0], opt[1], file=sys.stderr) |
| 107 |
|
| 108 |
if use_devel_grammar: |
| 109 |
import yapps_grammar as grammar |
| 110 |
else: |
| 111 |
import grammar |
| 112 |
|
| 113 |
generate(*tuple(args), **flags) |