mirrored from https://chromium.googlesource.com/angle/angle
-
Notifications
You must be signed in to change notification settings - Fork 614
/
generate_parser_tools.py
165 lines (126 loc) · 5.37 KB
/
generate_parser_tools.py
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
#!/usr/bin/python3
# Copyright 2019 The ANGLE Project Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# generate_parser_tools.py:
# Common functionality to call flex and bison to generate lexer and parser of
# the translator and preprocessor.
import os
import platform
import subprocess
import sys
is_linux = platform.system() == 'Linux'
is_mac = platform.system() == 'Darwin'
is_windows = platform.system() == 'Windows'
def get_tool_path_platform(tool_name, platform):
exe_path = os.path.join(sys.path[0], '..', '..', '..', 'tools', 'flex-bison', platform)
return os.path.join(exe_path, tool_name)
def get_tool_path(tool_name):
if is_linux:
platform = 'linux'
ext = ''
elif is_mac:
platform = 'mac'
ext = ''
else:
assert (is_windows)
platform = 'windows'
ext = '.exe'
return get_tool_path_platform(tool_name + ext, platform)
def get_tool_file_sha1s():
files = [
get_tool_path_platform('flex', 'linux'),
get_tool_path_platform('bison', 'linux'),
get_tool_path_platform('flex.exe', 'windows'),
get_tool_path_platform('bison.exe', 'windows'),
get_tool_path_platform('m4.exe', 'windows'),
get_tool_path_platform('flex', 'mac'),
get_tool_path_platform('bison', 'mac'),
]
files += [
get_tool_path_platform(dll, 'windows')
for dll in ['msys-2.0.dll', 'msys-iconv-2.dll', 'msys-intl-8.dll']
]
return [f + '.sha1' for f in files]
def run_flex(basename):
flex = get_tool_path('flex')
input_file = basename + '.l'
output_source = basename + '_lex_autogen.cpp'
flex_args = [flex, '--noline', '--nounistd', '--outfile=' + output_source, input_file]
flex_env = os.environ.copy()
if is_windows:
flex_env['M4'] = get_tool_path_platform('m4.exe', 'windows')
process = subprocess.Popen(flex_args, env=flex_env, cwd=sys.path[0])
process.communicate()
if process.returncode != 0:
return process.returncode
# Patch flex output for 64-bit. The patch is simple enough that we could do a string
# replacement. More importantly, the location of the line of code that needs to be substituted
# can vary based on flex version, and the string substitution will find the correct place
# automatically.
patch_in = """\n\t\tYY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]),\n\t\t\tyyg->yy_n_chars, num_to_read );"""
patch_out = """
yy_size_t ret = 0;
YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]),
ret, num_to_read );
yyg->yy_n_chars = static_cast<int>(ret);"""
with open(output_source, 'r') as flex_output:
output = flex_output.read()
# If flex's output changes such that this line no longer exists, the patch needs to be
# updated, or possibly removed.
assert (output.find(patch_in) != -1)
patched = output.replace(patch_in, patch_out)
# Remove all tab characters from output. WebKit does not allow any tab characters in source
# files.
patched = patched.replace('\t', ' ')
with open(output_source, 'w') as flex_output_patched:
flex_output_patched.write(patched)
return 0
def run_bison(basename, generate_header):
bison = get_tool_path('bison')
input_file = basename + '.y'
output_header = basename + '_tab_autogen.h'
output_source = basename + '_tab_autogen.cpp'
bison_args = [bison, '--no-lines', '--skeleton=yacc.c']
if generate_header:
bison_args += ['--defines=' + output_header]
bison_args += ['--output=' + output_source, input_file]
bison_env = os.environ.copy()
bison_env['BISON_PKGDATADIR'] = get_tool_path_platform('', 'third_party')
if is_windows:
bison_env['M4'] = get_tool_path_platform('m4.exe', 'windows')
process = subprocess.Popen(bison_args, env=bison_env, cwd=sys.path[0])
process.communicate()
return process.returncode
def get_input_files(basename):
files = [basename + '.l', basename + '.y']
return [os.path.join(sys.path[0], f) for f in files]
def get_output_files(basename, generate_header):
optional_header = [basename + '_tab_autogen.h'] if generate_header else []
files = [basename + '_lex_autogen.cpp', basename + '_tab_autogen.cpp'] + optional_header
return [os.path.join(sys.path[0], f) for f in files]
def generate_parser(basename, generate_header):
# Handle inputs/outputs for run_code_generation.py's auto_script
if len(sys.argv) > 1:
if sys.argv[1] == 'inputs':
inputs = get_tool_file_sha1s()
inputs += get_input_files(basename)
current_file = __file__
if current_file.endswith('.pyc'):
current_file = current_file[:-1]
inputs += [current_file]
print(','.join(inputs))
if sys.argv[1] == 'outputs':
print(','.join(get_output_files(basename, generate_header)))
return 0
# Call flex and bison to generate the lexer and parser.
flex_result = run_flex(basename)
if flex_result != 0:
print('Failed to run flex. Error %s' % str(flex_result))
return 1
bison_result = run_bison(basename, generate_header)
if bison_result != 0:
print('Failed to run bison. Error %s' % str(bison_result))
return 2
return 0