-
Notifications
You must be signed in to change notification settings - Fork 381
/
Copy pathupdate_sql_parsers.py
executable file
·190 lines (170 loc) · 5.81 KB
/
update_sql_parsers.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
# Copyright (C) 2020 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
import subprocess
import shutil
import sys
import tempfile
GRAMMAR_FOOTER = '''
%token SPACE ILLEGAL.
'''
KEYWORDHASH_HEADER = '''
#include "src/trace_processor/perfetto_sql/grammar/perfettosql_keywordhash_helper.h"
'''
KEYWORD_END = '''
{ "WITHOUT", "TK_WITHOUT", ALWAYS, 1 },
};'''
KEYWORD_END_REPLACE = '''
{ "WITHOUT", "TK_WITHOUT", ALWAYS, 1 },
{ "PERFETTO", "TK_PERFETTO", ALWAYS, 1 },
{ "MACRO", "TK_MACRO", ALWAYS, 1 },
{ "INCLUDE", "TK_INCLUDE", ALWAYS, 1 },
{ "MODULE", "TK_MODULE", ALWAYS, 1 },
{ "RETURNS", "TK_RETURNS", ALWAYS, 1 },
{ "FUNCTION", "TK_FUNCTION", ALWAYS, 1 },
};'''
def copy_tokenizer(args: argparse.Namespace):
shutil.copy(args.sqlite_tokenize, args.sqlite_tokenize_out)
with open(args.sqlite_tokenize_out, 'r+', encoding='utf-8') as fp:
res: str = fp.read()
idx = res.find('/*\n** Run the parser on the given SQL string.')
assert idx != -1
res = res[0:idx]
res = res.replace(
'#include "sqliteInt.h"',
'#include "src/trace_processor/perfetto_sql/tokenizer/tokenize_internal_helper.h"',
)
res = res.replace(
"static int analyzeWindowKeyword",
"int sqliteTokenizeInternalAnalyzeWindowKeyword",
)
res = res.replace(
"static int analyzeOverKeyword",
"int sqliteTokenizeInternalAnalyzeOverKeyword",
)
res = res.replace(
"static int analyzeFilterKeyword",
"int sqliteTokenizeInternalAnalyzeFilterKeyword",
)
res = res.replace('#include "keywordhash.h"\n', '')
fp.seek(0)
fp.write(res)
fp.truncate()
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--lemon', default=os.path.normpath('buildtools/sqlite_src/tool/lemon.c'))
parser.add_argument(
'--mkkeywordhash',
default=os.path.normpath('buildtools/sqlite_src/tool/mkkeywordhash.c'))
parser.add_argument(
'--lemon-template',
default=os.path.normpath('buildtools/sqlite_src/tool/lempar.c'))
parser.add_argument(
'--clang', default=os.path.normpath('buildtools/linux64/clang/bin/clang'))
parser.add_argument(
'--preprocessor-grammar',
default=os.path.normpath(
'src/trace_processor/perfetto_sql/preprocessor/preprocessor_grammar.y'
),
)
parser.add_argument(
'--sqlite-grammar',
default=os.path.normpath('buildtools/sqlite_src/src/parse.y'),
)
parser.add_argument(
'--perfettosql-grammar-include',
default=os.path.normpath(
'src/trace_processor/perfetto_sql/grammar/perfettosql_include.y'),
)
parser.add_argument(
'--grammar-out',
default=os.path.join(
os.path.normpath('src/trace_processor/perfetto_sql/grammar/')),
)
parser.add_argument(
'--sqlite-tokenize',
default=os.path.normpath('buildtools/sqlite_src/src/tokenize.c'),
)
parser.add_argument(
'--sqlite-tokenize-out',
default=os.path.join(
os.path.normpath(
'src/trace_processor/perfetto_sql/tokenizer/tokenize_internal.c')
),
)
args = parser.parse_args()
with tempfile.TemporaryDirectory() as tmp:
# Preprocessor grammar
subprocess.check_call([
args.clang,
os.path.join(args.lemon), '-o',
os.path.join(tmp, 'lemon')
])
shutil.copy(args.lemon_template, tmp)
subprocess.check_call([
os.path.join(tmp, 'lemon'),
args.preprocessor_grammar,
'-q',
'-l',
'-s',
])
# PerfettoSQL keywords
keywordhash_tmp = os.path.join(tmp, 'mkkeywordhash.c')
shutil.copy(args.mkkeywordhash, keywordhash_tmp)
with open(keywordhash_tmp, "r+") as fp:
keyword_source = fp.read()
assert keyword_source.find(KEYWORD_END) != -1
fp.seek(0)
fp.write(keyword_source.replace(KEYWORD_END, KEYWORD_END_REPLACE))
fp.truncate()
subprocess.check_call([
args.clang,
os.path.join(keywordhash_tmp), '-o',
os.path.join(tmp, 'mkkeywordhash')
])
keywordhash_res = subprocess.check_output(
[os.path.join(tmp, 'mkkeywordhash')]).decode()
with open(os.path.join(args.grammar_out, "perfettosql_keywordhash.h"),
"w") as g:
idx = keywordhash_res.find('#define SQLITE_N_KEYWORD')
assert idx != -1
keywordhash_res = keywordhash_res[0:idx]
g.write(KEYWORDHASH_HEADER)
g.write(keywordhash_res)
# PerfettoSQL grammar
sqlite_grammar = subprocess.check_output([
os.path.join(tmp, 'lemon'),
args.sqlite_grammar,
'-g',
]).decode()
with open(os.path.join(args.grammar_out, "perfettosql_grammar.y"),
"w") as g:
with open(args.perfettosql_grammar_include, 'r') as i:
g.write(i.read())
g.write(sqlite_grammar)
g.write(GRAMMAR_FOOTER)
subprocess.check_call([
os.path.join(tmp, 'lemon'),
os.path.join(args.grammar_out, "perfettosql_grammar.y"),
'-q',
'-l',
'-s',
])
copy_tokenizer(args)
return 0
if __name__ == '__main__':
sys.exit(main())