forked from congo-cc/congo-parser-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsharp_tests.py
336 lines (305 loc) · 12 KB
/
csharp_tests.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2022 Vinay Sajip ([email protected])
#
import argparse
import glob
import os
import re
import shutil
import subprocess
import sys
import tempfile
import time
from types import SimpleNamespace as Namespace
DEBUGGING = 'PY_DEBUG' in os.environ
JYTHON_PATH = None
VERSION_PATTERN = re.compile(r'\((\d+), (\d+), (\d+).*\)')
def check_jython(options):
# First check for Java and Java compiler being available
try:
p = subprocess.run(['java', '-version'], capture_output=True)
except Exception:
print('Unable to locate Java interpreter.', file=sys.stderr)
return 1
try:
p = subprocess.run(['javac', '-version'], capture_output=True)
except Exception:
print('Unable to locate Java compiler.', file=sys.stderr)
return 1
jd = options.jython_dir
if not os.path.isdir(jd):
print('Specified Jython directory '
'doesn\'t exist: %s' % jd, file=sys.stderr)
return 1
global JYTHON_PATH
JYTHON_PATH = os.path.join(jd, 'jython.jar')
if not os.path.isfile(JYTHON_PATH):
print('No jython.jar found in specified Jython directory: %s'
% jd, file=sys.stderr)
return 1
p = subprocess.run(['java', '-jar', JYTHON_PATH, '-c',
'import sys; print(sys.version_info[:3])'],
capture_output=True)
v = p.stdout.decode('utf-8').strip()
m = VERSION_PATTERN.match(v)
if not m:
print('Unable to interpret Jython version: %s' % v,
file=sys.stderr)
return 1
parts = tuple([int(n) for n in m.groups()])
if parts < (2, 7, 2):
print('Unsupported Jython version: %s' % v, file=sys.stderr)
return 1
return 0
def get_ipy_command(params):
result = ['ipy']
if 'GITHUB_WORKFLOW' in os.environ:
if sys.platform == 'darwin':
result = ['mono', '/Library/Frameworks/IronPython.framework/Versions/2.7.11/bin/ipy.exe']
elif os.name == 'nt':
loc = os.path.expanduser('~/bin/IronPython-2.7.11/netcoreapp3.1')
result = ['dotnet', os.path.join(loc, 'ipy.dll')]
result.extend(params)
return result
def check_ironpython(options):
p = subprocess.run(get_ipy_command(['-V']), capture_output=True)
if p.returncode:
print('IronPython not found', file=sys.stderr)
return 1
p = subprocess.run(['dotnet', '--version'], capture_output=True)
if p.returncode:
print('dotnet not found', file=sys.stderr)
return 1
return 0
def ensure_dir(p):
d = os.path.dirname(p)
if not os.path.exists(d):
os.makedirs(d)
def copy_files(srcdir, destdir, patterns):
for pattern in patterns:
p = os.path.join(srcdir, pattern)
for fn in glob.glob(p):
rp = os.path.relpath(fn, srcdir)
dp = os.path.join(destdir, rp)
if os.path.isfile(fn):
ensure_dir(dp)
shutil.copy(fn, dp)
else:
shutil.copytree(fn, dp)
# print('%s -> %s' % (p, dp))
def run_command(cmd, **kwargs):
# print(' '.join(cmd))
return subprocess.run(cmd, **kwargs)
def test_grammar(gdata, options):
lang = gdata.dir # Perhaps not intuitive, hence this comment
s = 'Testing with %s grammar' % gdata.name
line = '-' * 70
print(line)
print(s)
print(line)
# Create a temp directory and copy files into it.
workdir = tempfile.mkdtemp(prefix='congocc-csharp-test-')
gdata.workdir = workdir
print('Working directory: %s' % workdir)
if hasattr(gdata, 'srcdir'):
sd = gdata.srcdir
else:
sd = os.path.join('examples', gdata.dir)
dd = os.path.join(workdir, gdata.dir)
copy_files(sd, dd, gdata.files)
shutil.copy('ptest.py', dd)
print('Test files copied to working directory.')
# Run congocc to create the Java lexer and parser
if lang == 'csharp':
pf = os.path.join(dd, 'PPDirectiveLine.ccc')
cmd = ['java', '-jar', 'congocc.jar', '-n', '-q', pf]
p = run_command(cmd)
if p.returncode:
raise ValueError('Preprocessor generation in Java failed')
gf = os.path.join(dd, gdata.grammar)
cmd = ['java', '-jar', 'congocc.jar', '-n', '-q', gf]
p = run_command(cmd)
if p.returncode:
raise ValueError('Parser generation in Java failed')
print('Java version of lexer and parser created.')
# Run javac to compile the Java files created. Assuming doing
# the parser will sort everything else out
jparser = gdata.jparser
pkg, cls = jparser.rsplit('.', 1)
fn = os.path.join(pkg.replace('.', os.sep), '%s.java' % cls)
cmd = ['javac', fn]
p = run_command(cmd, cwd=dd)
if p.returncode:
raise ValueError('Java compilation failed')
print('Java lexer and parser compiled.')
# Run Jython to create the Java test result files
# For C#, you can't run the lexer standalone, because the parser switches lexical
# states during e.g. string parsing
if lang != 'csharp':
# First the lexer
cmd = ['java', '-jar', JYTHON_PATH, 'ptest.py', '-q',
gdata.jlexer, gdata.ext]
start = time.time()
p = run_command(cmd, cwd=dd)
if p.returncode:
raise ValueError('Java lexer test run failed')
elapsed = time.time() - start
print('Java lexer run completed (%.2f secs).' % elapsed)
# Then the parser
cmd = ['java', '-jar', JYTHON_PATH, 'ptest.py', '-q',
'--parser', gdata.production, gdata.jparser, gdata.ext]
start = time.time()
p = run_command(cmd, cwd=dd)
if p.returncode:
raise ValueError('Java parser test run failed')
elapsed = time.time() - start
print('Java parser run completed (%.2f secs).' % elapsed)
# Run congocc to create the C# lexer and parser
if lang == 'csharp':
cmd = ['java', '-jar', 'congocc.jar', '-n', '-q', '-lang', 'csharp', '-d', 'cs-csharpparser/ppline', pf]
p = run_command(cmd)
if p.returncode:
raise ValueError('Preprocessor generation in Python failed')
cmd = ['java', '-jar', 'congocc.jar', '-n', '-q', '-lang', 'csharp', gf]
p = run_command(cmd)
if p.returncode:
raise ValueError('Parser generation in C# failed')
print('C# version of lexer and parser created.')
# Run dotnet to build the C# code
csdir = os.path.join(dd, gdata.csdir)
cmd = ['dotnet', 'build', '--nologo']
p = run_command(cmd, cwd=csdir)
if p.returncode:
raise ValueError('Failed to build generated C# code')
# Run IronPython to create the C# test result files
# For C#, you can't run the lexer standalone, because the parser switches lexical
# states during e.g. string parsing
if lang != 'csharp':
# First the lexer
cmd = get_ipy_command(['-X:FullFrames', '-X:Debug', 'ptest.py', '-q', gdata.cspackage, gdata.ext])
start = time.time()
p = run_command(cmd, cwd=dd)
if p.returncode:
raise ValueError('C# lexer test run failed')
elapsed = time.time() - start
print('C# lexer run completed (%.2f secs).' % elapsed)
# Then the parser
cmd = get_ipy_command(['-X:FullFrames', '-X:Debug', 'ptest.py', '-q',
'--parser', gdata.production, gdata.cspackage, gdata.ext])
start = time.time()
p = run_command(cmd, cwd=dd)
if p.returncode:
raise ValueError('C# parser test run failed')
elapsed = time.time() - start
print('C# parser run completed (%.2f secs).' % elapsed)
# Compare differences between the directories
cmd = ['diff', os.path.join('testfiles', 'results', 'java'),
os.path.join('testfiles', 'results', 'csharp')]
if os.name == 'nt':
cmd.insert(1, '-b')
p = run_command(cmd, cwd=dd)
if p.returncode:
raise ValueError('Test results differ - '
'should be identical')
print('Results for C# & Java '
'lexers & parsers are identical - yay!')
def main():
# if sys.version_info[:2] < (3, 8):
# print('Unsupported Python version %s: '
# 'must be at least 3.8' % sys.version.split(' ', 1)[0])
# return 1
adhf = argparse.ArgumentDefaultsHelpFormatter
ap = argparse.ArgumentParser(formatter_class=adhf)
aa = ap.add_argument
jd = os.environ.get('JYTHONDIR', os.path.expanduser('~/bin'))
aa('--jython-dir', default=jd, help='Location of jython.jar')
aa('--no-delete', default=False, action='store_true',
help='Don\'t delete working directory')
aa('--langs', default='all', metavar='LANG1,LANG2...', help='Languages to test')
options = ap.parse_args()
# Check that jython is available
check = 'JYTHONDIR' in os.environ
if not check:
global JYTHON_PATH
JYTHON_PATH = os.path.expanduser('~/bin/jython.jar')
else:
rc = check_jython(options)
if rc:
return rc
rc = check_ironpython(options)
if rc:
return rc
workdirs = []
failed = False
# This can all be read from a data source in due course
languages = {
'json': Namespace(name='JSON', dir='json',
grammar='JSON.ccc',
files=['JSON.ccc', 'testfiles'],
jlexer='org.parsers.json.JSONLexer',
jparser='org.parsers.json.JSONParser',
csdir='cs-jsonparser',
cspackage='org.parsers.json', ext='.json',
production='Root'),
'java': Namespace(name='Java', dir='java',
grammar='Java.ccc',
files=['*.ccc', 'testfiles'],
jlexer='org.parsers.java.JavaLexer',
jparser='org.parsers.java.JavaParser',
csdir='cs-javaparser',
cspackage='org.parsers.java', ext='.java',
production='CompilationUnit'),
'csharp': Namespace(name='CSharp', dir='csharp',
grammar='CSharp.ccc',
files=['*.ccc', 'testfiles'],
jlexer='org.parsers.csharp.CSharpLexer',
jparser='org.parsers.csharp.CSharpParser',
csdir='cs-csharpparser',
cspackage='org.parsers.csharp', ext='.cs',
production='CompilationUnit'),
'python': Namespace(name='Python', dir='python',
grammar='Python.ccc',
files=['*.ccc', 'testfiles'],
jlexer='org.parsers.python.PythonLexer',
jparser='org.parsers.python.PythonParser',
cspackage='org.parsers.python', ext='.py',
csdir='cs-pythonparser',
production='Module'),
}
try:
langs = options.langs.split(',')
for lang, gdata in languages.items():
if options.langs == 'all' or lang in langs:
# if lang == 'csharp':
# print('Skipping csharp, because grammar is incomplete')
# continue
test_grammar(gdata, options)
workdirs.append(gdata.workdir)
except Exception as e:
print('Failed: %s.' % e)
failed = True
return 1
finally:
if failed or options.no_delete:
for workdir in workdirs:
print('TODO rm -rf %s' % workdir)
else:
for workdir in workdirs:
shutil.rmtree(workdir)
print('Working directories deleted.')
if __name__ == '__main__':
try:
rc = main()
except KeyboardInterrupt:
rc = 2
except Exception as e:
if DEBUGGING:
s = ' %s:' % type(e).__name__
else:
s = ''
sys.stderr.write('Failed:%s %s\n' % (s, e))
if DEBUGGING: import traceback; traceback.print_exc()
rc = 1
sys.exit(rc)