X Tutup
Skip to content

Commit fdb2d4a

Browse files
author
Troy Melhase
committed
Added option to check syntax of generated source. Closes natural#8.
1 parent d74b09e commit fdb2d4a

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

bin/java_to_python

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
import compiler
34
import optparse
45
import os.path
56
import sys
@@ -52,8 +53,18 @@ def transform(options):
5253

5354
set_config(options.configs, options.includedefaults)
5455
M = Module(filein, fileout)
55-
W.walk(ast, M)
56-
print >> output, M
56+
W.walk(ast, M)
57+
source = str(M)
58+
print >> output, source
59+
60+
if options.syntaxcheck:
61+
try:
62+
compiler.parse(source)
63+
except (SyntaxError, ):
64+
msg = '## ERR: invalid syntax in generated source.'
65+
else:
66+
msg = '## INFO: generated source has valid syntax.'
67+
print >> sys.stderr, msg
5768
return 0
5869

5970

@@ -72,6 +83,9 @@ def cli_options(argv):
7283
parser.add_option('-n', '--nodefaults', dest='includedefaults',
7384
help='ignore default configuration',
7485
default=True, action='store_false')
86+
parser.add_option('-s', '--syntaxcheck', dest='syntaxcheck',
87+
help='Check source syntax after generation',
88+
default=False, action='store_true')
7589
options, args = parser.parse_args(argv)
7690
return options, args
7791

0 commit comments

Comments
 (0)
X Tutup