annotate ply-3.8/build/lib.linux-x86_64-2.7/ply/ygen.py @ 12254:616be78bd12e draft

<oerjan> revert
author HackEso <hackeso@esolangs.org>
date Fri, 06 Dec 2019 07:54:58 +0000
parents a1845676eaa0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7268
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
1 # ply: ygen.py
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
2 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
3 # This is a support program that auto-generates different versions of the YACC parsing
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
4 # function with different features removed for the purposes of performance.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
5 #
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
6 # Users should edit the method LParser.parsedebug() in yacc.py. The source code
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
7 # for that method is then used to create the other methods. See the comments in
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
8 # yacc.py for further details.
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
9
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
10 import os.path
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
11 import shutil
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
12
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
13 def get_source_range(lines, tag):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
14 srclines = enumerate(lines)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
15 start_tag = '#--! %s-start' % tag
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
16 end_tag = '#--! %s-end' % tag
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
17
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
18 for start_index, line in srclines:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
19 if line.strip().startswith(start_tag):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
20 break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
21
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
22 for end_index, line in srclines:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
23 if line.strip().endswith(end_tag):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
24 break
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
25
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
26 return (start_index + 1, end_index)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
27
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
28 def filter_section(lines, tag):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
29 filtered_lines = []
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
30 include = True
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
31 tag_text = '#--! %s' % tag
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
32 for line in lines:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
33 if line.strip().startswith(tag_text):
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
34 include = not include
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
35 elif include:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
36 filtered_lines.append(line)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
37 return filtered_lines
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
38
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
39 def main():
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
40 dirname = os.path.dirname(__file__)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
41 shutil.copy2(os.path.join(dirname, 'yacc.py'), os.path.join(dirname, 'yacc.py.bak'))
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
42 with open(os.path.join(dirname, 'yacc.py'), 'r') as f:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
43 lines = f.readlines()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
44
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
45 parse_start, parse_end = get_source_range(lines, 'parsedebug')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
46 parseopt_start, parseopt_end = get_source_range(lines, 'parseopt')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
47 parseopt_notrack_start, parseopt_notrack_end = get_source_range(lines, 'parseopt-notrack')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
48
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
49 # Get the original source
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
50 orig_lines = lines[parse_start:parse_end]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
51
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
52 # Filter the DEBUG sections out
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
53 parseopt_lines = filter_section(orig_lines, 'DEBUG')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
54
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
55 # Filter the TRACKING sections out
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
56 parseopt_notrack_lines = filter_section(parseopt_lines, 'TRACKING')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
57
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
58 # Replace the parser source sections with updated versions
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
59 lines[parseopt_notrack_start:parseopt_notrack_end] = parseopt_notrack_lines
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
60 lines[parseopt_start:parseopt_end] = parseopt_lines
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
61
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
62 lines = [line.rstrip()+'\n' for line in lines]
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
63 with open(os.path.join(dirname, 'yacc.py'), 'w') as f:
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
64 f.writelines(lines)
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
65
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
66 print('Updated yacc.py')
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
67
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
68 if __name__ == '__main__':
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
69 main()
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
70
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
71
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
72
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
73
a1845676eaa0 <ais523> ` (cd ply-3.8; python setup.py build)
HackBot
parents:
diff changeset
74