-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup_grammar.py
179 lines (130 loc) · 5.23 KB
/
setup_grammar.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
import datetime
import logging
import subprocess
import sys
import re
from enum import Enum
from pathlib import Path
import requests
def setup_logging():
logging.basicConfig(stream=sys.stderr, level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
logger = logging.getLogger(__name__)
class Antlr4Target(Enum):
js = 'JavaScript'
python = 'Python3'
build_options = {
'antlr4_compiled_target_output': {
Antlr4Target.js: 'cratedb_sqlparse_js',
Antlr4Target.python: 'cratedb_sqlparse_py'
},
'antlr4_compiled_target_subdir': 'cratedb_sqlparse/generated_parser',
# List of '.g4' files that will be built
'files': [
{
'url': 'https://github.com/crate/crate/raw/{version}/libs/sql-parser/src/main/antlr/io/crate/sql/parser/antlr/SqlBaseLexer.g4',
'filename': 'SqlBaseLexer.g4'
},
{
'url': 'https://github.com/crate/crate/raw/{version}/libs/sql-parser/src/main/antlr/io/crate/sql/parser/antlr/SqlBaseParser.g4',
'filename': 'SqlBaseParser.g4'
}
]
}
PARSER_COMPILE_PATH = Path(__file__).parent
def download_cratedb_grammar(version='master'):
"""
Downloads CrateDB's `version` grammar files.
Version should match a tag; for a list of tags run:
$ curl https://api.github.com/repos/crate/crate/tags | jq -r '.[] | .name'
"""
for file in build_options['files']:
url = file['url'].format(version=version)
logger.info(f"Downloading grammar: {url}")
response = requests.get(url)
# We annotate the CrateDB branch and date of download to the Grammar files for reference.
text = f'/* crate_branch={version}, at={datetime.datetime.now()}, annotatedby=cratedb_sqlparse */\n' + response.text
outfile = PARSER_COMPILE_PATH / file['filename']
logger.info(f"Writing downloaded grammar: {outfile}")
outfile.write_text(text)
def compile_grammar(target: Antlr4Target):
"""
Compiles antlr4 files into `target` code.
"""
base_dir = build_options['antlr4_compiled_target_output'][target]
sub_dir = build_options['antlr4_compiled_target_subdir']
for file in build_options['files']:
outfile = PARSER_COMPILE_PATH / base_dir / sub_dir / file['filename']
logger.info(f"Compiling grammar: {outfile}")
subprocess.check_call(
[
'antlr4',
f'-Dlanguage={target.value}',
'-visitor',
'-o',
str(PARSER_COMPILE_PATH / base_dir / sub_dir),
file['filename']
]
)
def patch_lexer(target: Antlr4Target):
"""
Patches the lexer file, removing bad syntax generated by Antlr4.
"""
logger.info(f"Patching lexer type: {target}")
REMOVE_LINES = [
'import io.crate.sql.AbstractSqlBaseLexer;',
]
# If more targets are added, this needs to be improved.
extension = 'py' if target == Antlr4Target.python else 'js'
base_dir = build_options['antlr4_compiled_target_output'][target]
sub_dir = build_options['antlr4_compiled_target_subdir']
file_name = build_options['files'][0]['filename'].replace('g4', extension)
lexer_file = Path(PARSER_COMPILE_PATH / base_dir / sub_dir / file_name)
logger.info(f"Patching lexer file: {lexer_file}")
text = lexer_file.read_text()
for text_to_remove in REMOVE_LINES:
text = text.replace(text_to_remove, '')
lexer_file.write_text(text)
def set_version(target: Antlr4Target, version: str):
"""
Specifies the compiled version to the target package,
depending on the package the strategy differs.
"""
base_dir = build_options['antlr4_compiled_target_output'][target]
sub_dir = build_options['antlr4_compiled_target_subdir']
target_path = (PARSER_COMPILE_PATH / base_dir / sub_dir).parent
version = f'"{version}"' # Escape quotes on echo command.
index_file = ''
variable = ''
if target == Antlr4Target.python:
index_file = '__init__.py'
variable = '__cratedb_version__'
if target == Antlr4Target.js:
index_file = 'index.js'
variable = 'export const __cratedb_version__'
with open(target_path / index_file, "r+") as f:
content = f.read()
# Removes the current content on disk.
f.seek(0)
f.truncate()
updated_content = re.sub(f'({variable} = )"(.*)"', r'\1' + version, content)
f.write(updated_content)
logger.info(f'Updated {variable} to {version} in {index_file}')
if __name__ == '__main__':
"""
Invoke the grammar compiler / generator.
TODO: Converge `version` into command-line argument?
TODO: Improve efficiency by generating runtime parser for all implemented languages at once.
"""
setup_logging()
input_target = sys.argv[1]
version = '5.8.3'
if input_target.startswith("py"):
target = Antlr4Target.python
elif input_target.startswith("js") or input_target.startswith("java"):
target = Antlr4Target.js
else:
raise NotImplementedError(f"Parser generator for target {input_target} not implemented")
download_cratedb_grammar(version)
compile_grammar(target)
patch_lexer(target)
set_version(target, version)