mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 23:58:20 +00:00
Fix bug with tab handling.
Commit 5804a9a
introduced a bug where having the foldtabs option disabled (normal) prevented optimizations of functions. Fix it for good (hopefully). While on it, rename the nofoldtabs option to warntabs, making it default, and use it to disable a warning when there are tabs in a string.
This commit is contained in:
parent
95bd3209be
commit
b7e6e6f7b1
4 changed files with 23 additions and 18 deletions
|
@ -945,14 +945,14 @@ class foldconst(object):
|
|||
else:
|
||||
value = fn(*tuple(arg['value'] for arg in child))
|
||||
if not self.foldtabs:
|
||||
if not self.nofoldtabs:
|
||||
generatesTabs = (
|
||||
isinstance(value, unicode) and '\t' in value
|
||||
or type(value) == list and any(isinstance(x, unicode) and '\t' in x for x in value)
|
||||
)
|
||||
if generatesTabs:
|
||||
warning("Can't optimize call to %s because it would generate a tab character (you can force the optimization with the foldtabs option, or disable this warning with the nofoldtabs option)." % node['name'])
|
||||
return
|
||||
generatesTabs = (
|
||||
isinstance(value, unicode) and '\t' in value
|
||||
or type(value) == list and any(isinstance(x, unicode) and '\t' in x for x in value)
|
||||
)
|
||||
if generatesTabs:
|
||||
if self.warntabs:
|
||||
warning("Can't optimize call to %s because it would generate a tab character (you can force the optimization with the 'foldtabs' option, or disable this warning by disabling the 'warntabs' option)." % node['name'])
|
||||
return
|
||||
parent[index] = {'nt':'CONST', 't':node['t'], 'value':value}
|
||||
except lslfuncs.ELSLCantCompute:
|
||||
# Don't transform the tree if function is not computable
|
||||
|
|
|
@ -57,7 +57,8 @@ class optimizer(foldconst, renamer, deadcode):
|
|||
ret['X'] = value['X']
|
||||
return ret
|
||||
|
||||
def optimize(self, treesymtab, options = ('optimize','constfold','dcr')):
|
||||
def optimize(self, treesymtab, options = ('optimize','constfold','dcr',
|
||||
'warntabs')):
|
||||
"""Optimize the symbolic table symtab in place. Requires a table of
|
||||
predefined functions for folding constants.
|
||||
"""
|
||||
|
@ -68,7 +69,7 @@ class optimizer(foldconst, renamer, deadcode):
|
|||
self.addstrings = 'addstrings' in options
|
||||
|
||||
self.foldtabs = 'foldtabs' in options
|
||||
self.nofoldtabs = 'nofoldtabs' in options
|
||||
self.warntabs = 'warntabs' in options
|
||||
|
||||
self.shrinknames = 'shrinknames' in options
|
||||
|
||||
|
|
|
@ -53,9 +53,11 @@ class outscript(object):
|
|||
else:
|
||||
pfx = '((key)'
|
||||
sfx = ')'
|
||||
if '\t' in value:
|
||||
warning('A string contains a tab. Tabs are expanded to four'
|
||||
' spaces by the viewer when copy-pasting the code.')
|
||||
if '\t' in value and self.warntabs:
|
||||
warning("A string contains a tab. Tabs are expanded to four"
|
||||
" spaces by the viewer when copy-pasting the code"
|
||||
" (disable this warning by disabling the 'warntabs'"
|
||||
" option).")
|
||||
return pfx + '"' + value.encode('utf8').replace('\\','\\\\') \
|
||||
.replace('"','\\"').replace('\n','\\n') + '"' + sfx
|
||||
if tvalue == int:
|
||||
|
@ -428,14 +430,15 @@ class outscript(object):
|
|||
|
||||
assert False, "Internal error: node type not handled: " + nt # pragma: no cover
|
||||
|
||||
def output(self, treesymtab, options = ('optsigns','optfloats')):
|
||||
def output(self, treesymtab, options = ('optsigns','optfloats','warntabs')):
|
||||
# Build a sorted list of dict entries
|
||||
self.tree, self.symtab = treesymtab
|
||||
|
||||
# Optimize signs
|
||||
# Grab options
|
||||
self.optsigns = 'optsigns' in options
|
||||
self.optfloats = 'optfloats' in options
|
||||
self.foldconst = 'constfold' in options
|
||||
self.warntabs = 'warntabs' in options
|
||||
|
||||
ret = ''
|
||||
self.indent = ' '
|
||||
|
|
7
main.py
7
main.py
|
@ -281,8 +281,9 @@ Optimizer options (+ means active by default, - means inactive by default):
|
|||
expansion of functions that produce strings with tabs.
|
||||
The resulting source isn't guaranteed to be
|
||||
copy-paste-able to the viewer.
|
||||
nofoldtabs - Suppress warning when a function can't be optimized
|
||||
because it generates a string or list with a tab.
|
||||
warntabs + Suppress warning when a function can't be optimized
|
||||
because it generates a string or list with a tab, or
|
||||
when a string contains a tab.
|
||||
skippreproc + Skip preprocessor directives in the source as if they
|
||||
were comments. Not useful unless the script is itself
|
||||
the output of a preprocessor like GNU cpp, which inserts
|
||||
|
@ -303,7 +304,7 @@ def main():
|
|||
|
||||
# Default options
|
||||
options = set(('extendedglobalexpr','extendedtypecast','extendedassignment',
|
||||
'allowkeyconcat','allowmultistrings','skippreproc','optimize',
|
||||
'allowkeyconcat','allowmultistrings','skippreproc','warntabs','optimize',
|
||||
'optsigns','optfloats','constfold','dcr','errmissingdefault',
|
||||
))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue