Debian Codegraph @

src/yapps3/yapps2.py

raw symbols SCIP
1 #!/usr/bin/python2
2
3 #
4 # Yapps 2 - yet another python parser system
5 # Copyright 1999-2003 by Amit J. Patel <[email protected]>
6 #
7 # This version of Yapps 2 can be distributed under the
8 # terms of the MIT open source license, either found in the LICENSE file
9 # included with the Yapps distribution
10 # <http://theory.stanford.edu/~amitp/yapps/> or at
11 # <http://www.opensource.org/licenses/mit-license.php>
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' # This pattern separates the pre/post parsers
31 preparser, postparser = None, None # Code before and after the parser desc
32
33 # Read the entire file
34 s = open(inputfilename,'r').read()
35
36 # See if there's a separation between the pre-parser and parser
37 f = s.find(DIVIDER)
38 if f >= 0: preparser, s = s[:f]+'\n\n', s[f+len(DIVIDER):]
39
40 # See if there's a separation between the parser and post-parser
41 f = s.find(DIVIDER)
42 if f >= 0: s, postparser = s[:f], '\n\n'+s[f+len(DIVIDER):]
43
44 # Create the parser and scanner and parse the text
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 # Failure
50 if preparser is not None: t.preparser = preparser
51 if postparser is not None: t.postparser = postparser
52
53 # Check the options
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 # Add command line options to the set
60 for f in flags.keys(): t.options[f] = flags[f]
61
62 # Generate the output
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 # Someday I will use optparse, but Python 2.3 is too new at the moment.
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 # Read in the options and create a list of flags
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)