adjustments for return\n/regex/ instances (where \n needs to stay)

This commit is contained in:
ndparker 2015-10-18 19:42:50 +02:00
parent 04fe02df90
commit 844bd6f24b
3 changed files with 108 additions and 95 deletions

View File

@ -142,24 +142,25 @@ which looks like this:
r'\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:'
r'[^/*][^*]*\*+)*/))*)+(?=[^\000-\040&)+,.:;=?\]|}-]))?|(?<=[\00'
r'0-#%-,./:-@\[-^`{-~-]return)(?:[\000-\011\013\014\016-\040]|(?'
r':/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*((?:/(?![\r\n/*])[^/\\\[\r\n'
r']*(?:(?:\\[^\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*'
r'\]))[^/\\\[\r\n]*)*/))((?:[\000-\011\013\014\016-\040]|(?:/\*['
r'^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:['
r':/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:((?:(?://[^\r\n]*)?[\r\n]'
r'))(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*'
r'\*+)*/))*)*((?:/(?![\r\n/*])[^/\\\[\r\n]*(?:(?:\\[^\r\n]|(?:\['
r'[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*\]))[^/\\\[\r\n]*)*/))(('
r'?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)'
r'*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\04'
r'0]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000-\040&)+,.:;'
r'=?\]|}-]))?|(?<=[^\000-!#%&(*,./:-@\[\\^`{|~])(?:[\000-\011\01'
r'3\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:((?:(?:'
r'//[^\r\n]*)?[\r\n]))(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]'
r'*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000-\040"#%-\047)*,./:-@\\-^'
r'`|-~])|(?<=[^\000-#%-,./:-@\[-^`{-~-])((?:[\000-\011\013\014\0'
r'16-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=[^\000-#%-,./'
r':-@\[-^`{-~-])|(?<=\+)((?:[\000-\011\013\014\016-\040]|(?:/\*['
r'^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=\+)|(?<=-)((?:[\000-\011\013'
r'\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=-)|(?:['
r'\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)'
r')*)+(?=[^\000-\040&)+,.:;=?\]|}-]))?|(?<=[^\000-!#%&(*,./:-@\['
r'\\^`{|~])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*'
r'][^*]*\*+)*/))*(?:((?:(?://[^\r\n]*)?[\r\n]))(?:[\000-\011\013'
r'\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000'
r'-\040"#%-\047)*,./:-@\\-^`|-~])|(?<=[^\000-#%-,./:-@\[-^`{-~-]'
r')((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*'
r'\*+)*/)))+(?=[^\000-#%-,./:-@\[-^`{-~-])|(?<=\+)((?:[\000-\011'
r'\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=\+)'
r'|(?<=-)((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*]'
r'[^*]*\*+)*/)))+(?=-)|(?:[\000-\011\013\014\016-\040]|(?:/\*[^*'
r']*\*+(?:[^/*][^*]*\*+)*/))+|(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\0'
r'00-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*'
r')+'
r')+|(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]'
r'|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+'
)

3
foo.js
View File

@ -1,2 +1,5 @@
x = /abc/ /*! lalala */
if (1) console.log(x)
return /*!lalala */
/blahr/

165
rjsmin.py
View File

@ -200,16 +200,16 @@ def _make_jsmin(python_only=False):
r'(%(space)s*(?:%(newline)s%(space)s*)+' # 3
r'(?=%(post_regex_off)s))?'
r'|(?<=%(preregex2)s)'
r'%(space)s*'
r'(%(regex)s)' # 4
r'(%(space)s*(?:%(newline)s%(space)s*)+' # 5
r'%(space)s*(?:(%(newline)s)%(space)s*)*' # 4
r'(%(regex)s)' # 5
r'(%(space)s*(?:%(newline)s%(space)s*)+' # 6
r'(?=%(post_regex_off)s))?'
r'|(?<=%(id_literal_close)s)'
r'%(space)s*(?:(%(newline)s)%(space)s*)+' # 6
r'%(space)s*(?:(%(newline)s)%(space)s*)+' # 7
r'(?=%(id_literal_open)s)'
r'|(?<=%(id_literal)s)(%(space)s)+(?=%(id_literal)s)' # 7
r'|(?<=\+)(%(space)s)+(?=\+)' # 8
r'|(?<=-)(%(space)s)+(?=-)' # 9
r'|(?<=%(id_literal)s)(%(space)s)+(?=%(id_literal)s)' # 8
r'|(?<=\+)(%(space)s)+(?=\+)' # 9
r'|(?<=-)(%(space)s)+(?=-)' # 10
r'|%(space)s+'
r'|(?:%(newline)s%(space)s*)+'
) % locals()).sub
@ -229,13 +229,15 @@ def _make_jsmin(python_only=False):
if groups[3]:
return groups[2] + '\n'
return groups[2]
elif groups[4]:
if groups[5]:
return groups[4] + '\n'
return groups[4]
elif groups[6]:
elif groups[5]:
return "%s%s%s" % (
groups[4] and '\n' or '',
groups[5],
groups[6] and '\n' or '',
)
elif groups[7]:
return '\n'
elif groups[7] or groups[8] or groups[9]:
elif groups[8] or groups[9] or groups[10]:
return ' '
else:
return ''
@ -251,18 +253,18 @@ def _make_jsmin(python_only=False):
r'(%(space)s*(?:%(newline)s%(space)s*)+' # 4
r'(?=%(post_regex_off)s))?'
r'|(?<=%(preregex2)s)'
r'(%(space)s*)' # 5
r'(%(regex)s)' # 6
r'(%(space)s*(?:%(newline)s%(space)s*)+' # 7
r'(%(space)s*(?:(%(newline)s)%(space)s*)*)' # 5, 6
r'(%(regex)s)' # 7
r'(%(space)s*(?:%(newline)s%(space)s*)+' # 8
r'(?=%(post_regex_off)s))?'
r'|(?<=%(id_literal_close)s)'
r'(%(space)s*(?:%(newline)s%(space)s*)+)' # 8
r'(%(space)s*(?:%(newline)s%(space)s*)+)' # 9
r'(?=%(id_literal_open)s)'
r'|(?<=%(id_literal)s)(%(space)s+)(?=%(id_literal)s)' # 9
r'|(?<=\+)(%(space)s+)(?=\+)' # 10
r'|(?<=-)(%(space)s+)(?=-)' # 11
r'|(%(space)s+)' # 12
r'|((?:%(newline)s%(space)s*)+)' # 13
r'|(?<=%(id_literal)s)(%(space)s+)(?=%(id_literal)s)' # 10
r'|(?<=\+)(%(space)s+)(?=\+)' # 11
r'|(?<=-)(%(space)s+)(?=-)' # 12
r'|(%(space)s+)' # 13
r'|((?:%(newline)s%(space)s*)+)' # 14
) % locals()).sub
# print space_sub_banged.__self__.pattern
@ -291,19 +293,20 @@ def _make_jsmin(python_only=False):
keep(keeper, groups[4] or ''),
groups[4] and '\n' or '',
)
elif groups[6]:
return "%s%s%s%s" % (
elif groups[7]:
return "%s%s%s%s%s" % (
keep(keeper, groups[5]),
groups[6],
keep(keeper, groups[7] or ''),
groups[7] and '\n' or '',
groups[6] and '\n' or '',
groups[7],
keep(keeper, groups[8] or ''),
groups[8] and '\n' or '',
)
elif groups[8]:
return keep(keeper, groups[8]) + '\n'
elif groups[9] or groups[10] or groups[11]:
return keep(keeper, groups[9] or groups[10] or groups[11]) or ' '
elif groups[9]:
return keep(keeper, groups[9]) + '\n'
elif groups[10] or groups[11] or groups[12]:
return keep(keeper, groups[10] or groups[11] or groups[12]) or ' '
else:
return keep(keeper, groups[12] or groups[13])
return keep(keeper, groups[13] or groups[14])
def jsmin(script, keep_bang_comments=False):
r"""
@ -381,24 +384,25 @@ def jsmin_for_posers(script, keep_bang_comments=False):
r'\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:'
r'[^/*][^*]*\*+)*/))*)+(?=[^\000-\040&)+,.:;=?\]|}-]))?|(?<=[\00'
r'0-#%-,./:-@\[-^`{-~-]return)(?:[\000-\011\013\014\016-\040]|(?'
r':/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*((?:/(?![\r\n/*])[^/\\\[\r\n'
r']*(?:(?:\\[^\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*'
r'\]))[^/\\\[\r\n]*)*/))((?:[\000-\011\013\014\016-\040]|(?:/\*['
r'^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:['
r':/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:((?:(?://[^\r\n]*)?[\r\n]'
r'))(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*'
r'\*+)*/))*)*((?:/(?![\r\n/*])[^/\\\[\r\n]*(?:(?:\\[^\r\n]|(?:\['
r'[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*\]))[^/\\\[\r\n]*)*/))(('
r'?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)'
r'*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\04'
r'0]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000-\040&)+,.:;'
r'=?\]|}-]))?|(?<=[^\000-!#%&(*,./:-@\[\\^`{|~])(?:[\000-\011\01'
r'3\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:((?:(?:'
r'//[^\r\n]*)?[\r\n]))(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]'
r'*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000-\040"#%-\047)*,./:-@\\-^'
r'`|-~])|(?<=[^\000-#%-,./:-@\[-^`{-~-])((?:[\000-\011\013\014\0'
r'16-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=[^\000-#%-,./'
r':-@\[-^`{-~-])|(?<=\+)((?:[\000-\011\013\014\016-\040]|(?:/\*['
r'^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=\+)|(?<=-)((?:[\000-\011\013'
r'\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=-)|(?:['
r'\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)'
r')*)+(?=[^\000-\040&)+,.:;=?\]|}-]))?|(?<=[^\000-!#%&(*,./:-@\['
r'\\^`{|~])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*'
r'][^*]*\*+)*/))*(?:((?:(?://[^\r\n]*)?[\r\n]))(?:[\000-\011\013'
r'\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000'
r'-\040"#%-\047)*,./:-@\\-^`|-~])|(?<=[^\000-#%-,./:-@\[-^`{-~-]'
r')((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*'
r'\*+)*/)))+(?=[^\000-#%-,./:-@\[-^`{-~-])|(?<=\+)((?:[\000-\011'
r'\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=\+)'
r'|(?<=-)((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*]'
r'[^*]*\*+)*/)))+(?=-)|(?:[\000-\011\013\014\016-\040]|(?:/\*[^*'
r']*\*+(?:[^/*][^*]*\*+)*/))+|(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\0'
r'00-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*'
r')+'
r')+|(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]'
r'|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+'
)
def subber(match):
@ -409,12 +413,15 @@ def jsmin_for_posers(script, keep_bang_comments=False):
groups[1] or
(groups[3] and (groups[2] + '\n')) or
groups[2] or
(groups[5] and (groups[4] + '\n')) or
groups[4] or
(groups[6] and '\n') or
(groups[7] and ' ') or
(groups[5] and "%s%s%s" % (
groups[4] and '\n' or '',
groups[5],
groups[6] and '\n' or '',
)) or
(groups[7] and '\n') or
(groups[8] and ' ') or
(groups[9] and ' ') or
(groups[10] and ' ') or
''
)
else:
@ -431,24 +438,25 @@ def jsmin_for_posers(script, keep_bang_comments=False):
r'\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+('
r'?:[^/*][^*]*\*+)*/))*)+(?=[^\000-\040&)+,.:;=?\]|}-]))?|(?<=['
r'\000-#%-,./:-@\[-^`{-~-]return)((?:[\000-\011\013\014\016-\040'
r']|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)((?:/(?![\r\n/*])[^/'
r'\\\[\r\n]*(?:(?:\\[^\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]'
r'\r\n]*)*\]))[^/\\\[\r\n]*)*/))((?:[\000-\011\013\014\016-\040]'
r'|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r'
r'\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]'
r'*\*+)*/))*)+(?=[^\000-\040&)+,.:;=?\]|}-]))?|(?<=[^\000-!#%&(*'
r',./:-@\[\\^`{|~])((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*'
r'\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-'
r'\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+)'
r'(?=[^\000-\040"#%-\047)*,./:-@\\-^`|-~])|(?<=[^\000-#%-,./:-@'
r'\[-^`{-~-])((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:['
r'^/*][^*]*\*+)*/))+)(?=[^\000-#%-,./:-@\[-^`{-~-])|(?<=\+)((?:['
r'\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)'
r')+)(?=\+)|(?<=-)((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*'
r'+(?:[^/*][^*]*\*+)*/))+)(?=-)|((?:[\000-\011\013\014\016-\040]'
r'|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+)|((?:(?:(?://[^\r\n]*)?['
r'\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^'
r'*]*\*+)*/))*)+)'
r']|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:((?:(?://[^\r\n]*)?['
r'\r\n]))(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*]['
r'^*]*\*+)*/))*)*)((?:/(?![\r\n/*])[^/\\\[\r\n]*(?:(?:\\[^\r\n]|'
r'(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*\]))[^/\\\[\r\n]*)*'
r'/))((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]'
r'*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\01'
r'6-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000-\040&)'
r'+,.:;=?\]|}-]))?|(?<=[^\000-!#%&(*,./:-@\[\\^`{|~])((?:[\000-'
r'\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:'
r'(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/'
r'\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+)(?=[^\000-\040"#%-\047)*,./'
r':-@\\-^`|-~])|(?<=[^\000-#%-,./:-@\[-^`{-~-])((?:[\000-\011\01'
r'3\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+)(?=[^\000'
r'-#%-,./:-@\[-^`{-~-])|(?<=\+)((?:[\000-\011\013\014\016-\040]|'
r'(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+)(?=\+)|(?<=-)((?:[\000-\0'
r'11\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+)(?=-'
r')|((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*'
r'\*+)*/))+)|((?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014'
r'\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+)'
)
keep = _re.compile((
@ -470,17 +478,18 @@ def jsmin_for_posers(script, keep_bang_comments=False):
keep(keeper, groups[4] or ''),
groups[4] and '\n' or '',
)) or
(groups[6] and "%s%s%s%s" % (
(groups[7] and "%s%s%s%s%s" % (
keep(keeper, groups[5]),
groups[6],
keep(keeper, groups[7] or ''),
groups[7] and '\n' or '',
groups[6] and '\n' or '',
groups[7],
keep(keeper, groups[8] or ''),
groups[8] and '\n' or '',
)) or
(groups[8] and keep(keeper, groups[8] + '\n')) or
(groups[9] and keep(keeper, groups[9]) or ' ') or
(groups[9] and keep(keeper, groups[9] + '\n')) or
(groups[10] and keep(keeper, groups[10]) or ' ') or
(groups[11] and keep(keeper, groups[11]) or ' ') or
keep(keeper, groups[12] or groups[13])
(groups[12] and keep(keeper, groups[12]) or ' ') or
keep(keeper, groups[13] or groups[14])
)
return _re.sub(rex, subber, '\n%s\n' % script).strip()