Add --prettify option, to reformat a script (losing comments).

Since our syntax extensions transform the source at parse time, all syntax extensions are disabled. The optimizations are disabled too, as it doesn't make sense to prettify and optimize at the same time (the optimizer would remove the constants that we're trying to keep).

Addresses #4 in a more user-friendly way.
This commit is contained in:
Sei Lisa 2017-11-19 19:48:35 +01:00
parent e42479756b
commit f492d3e291
2 changed files with 24 additions and 1 deletions

View file

@ -2628,6 +2628,9 @@ list lazy_list_set(list L, integer i, list v)
# # Allow referencing undefined functions inside function definitions. # # Allow referencing undefined functions inside function definitions.
# self.allowundeffn = 'allowundeffn' in options # self.allowundeffn = 'allowundeffn' in options
# Prettify a source file
self.prettify = 'prettify' in options
# Symbol table: # Symbol table:
# This is a list of all local and global symbol tables. # This is a list of all local and global symbol tables.
# The first element (0) is the global scope. Each symbol table is a # The first element (0) is the global scope. Each symbol table is a
@ -2650,6 +2653,16 @@ list lazy_list_set(list L, integer i, list v)
self.symtab[0][-1] = None self.symtab[0][-1] = None
self.scopeindex = 0 self.scopeindex = 0
if self.prettify:
# Add the constants as symbol table variables...
for i in self.constants:
self.symtab[0][i] = {'Kind':'v', 'Scope':0,
'Type':lslcommon.PythonType2LSL[type(self.constants[i])]}
# ... and remove them as constants.
self.constants = {}
# Remove TRUE and FALSE from keywords
self.keywords -= set(('TRUE', 'FALSE'))
# Last preprocessor __FILE__. <stdin> means the current file. # Last preprocessor __FILE__. <stdin> means the current file.
self.lastFILE = '<stdin>' self.lastFILE = '<stdin>'

12
main.py
View file

@ -217,6 +217,7 @@ Usage: {progname}
[--avname=<name>] * specify name of avatar saving the script [--avname=<name>] * specify name of avatar saving the script
[--assetid=<UUID>] * specify the asset UUID of the script [--assetid=<UUID>] * specify the asset UUID of the script
[--shortname=<name>] * specify the script's short file name [--shortname=<name>] * specify the script's short file name
[--prettify] Prettify source file. Disables all -O options.
filename input file filename input file
Options marked with * are used to define the preprocessor macros __AGENTID__, Options marked with * are used to define the preprocessor macros __AGENTID__,
@ -359,6 +360,7 @@ validoptions = frozenset(('extendedglobalexpr','breakcont','extendedtypecast',
# undocumented # undocumented
'lso','expr','rsrclimit', 'lso','expr','rsrclimit',
# 'clear' is handled as a special case # 'clear' is handled as a special case
# 'prettify' is internal, as it's a user flag
)) ))
def main(argv): def main(argv):
@ -382,7 +384,7 @@ def main(argv):
try: try:
opts, args = getopt.gnu_getopt(argv[1:], 'hO:o:p:P:HTyb:L:', opts, args = getopt.gnu_getopt(argv[1:], 'hO:o:p:P:HTyb:L:',
('optimizer-options=', 'help', 'version', 'output=', 'header', ('optimizer-options=', 'help', 'version', 'output=', 'header',
'timestamp','python-exceptions', 'timestamp', 'python-exceptions', 'prettify',
'preproc=', 'precmd=', 'prearg=', 'prenodef', 'preshow', 'preproc=', 'precmd=', 'prearg=', 'prenodef', 'preshow',
'avid=', 'avname=', 'assetid=', 'shortname=', 'builtins=' 'avid=', 'avname=', 'assetid=', 'shortname=', 'builtins='
'libdata=')) 'libdata='))
@ -404,6 +406,7 @@ def main(argv):
mcpp_mode = False mcpp_mode = False
preshow = False preshow = False
raise_exception = False raise_exception = False
prettify = False
builtins = None builtins = None
libdata = None libdata = None
@ -525,8 +528,15 @@ def main(argv):
elif opt == '--shortname': elif opt == '--shortname':
shortname = arg shortname = arg
elif opt == '--prettify':
prettify = True
del opts del opts
if prettify:
options &= set(('rsrclimit',))
options.add('prettify')
try: try:
if 'rsrclimit' in options: if 'rsrclimit' in options: