[jsinterp] Quick regex fixes (thx to yan12125)

This commit is contained in:
sulyi 2016-11-25 22:31:58 +01:00
parent 2076b0bb3c
commit da73cd90ec
2 changed files with 10 additions and 5 deletions

View File

@ -111,8 +111,12 @@ class TestJSInterpreter(unittest.TestCase):
function z() { return y(3); }
''')
self.assertEqual(jsi.call_function('z'), 5)
jsi = JSInterpreter('function w(a) { return a.split(""); }', objects={'a': 'abc'})
self.assertEqual(jsi.call_function('w'), ["a", "b", "c"])
jsi = JSInterpreter('function x(a) { return a.split(""); }', objects={'a': 'abc'})
self.assertEqual(jsi.call_function('x'), ["a", "b", "c"])
def test_getfield(self):
jsi = JSInterpreter('function c() { return a.var; }', objects={'a': {'var': 3}})
self.assertEqual(jsi.call_function('c'), 3)
if __name__ == '__main__':
unittest.main()

View File

@ -39,12 +39,14 @@ _DOUBLE_QUOTED = r'''"(?:[^"\\\\]*(?:\\\\\\\\|\\\\['"nurtbfx/\\n]))*[^"\\\\]*"''
_STRING_RE = r'%s|%s' % (_SINGLE_QUOTED, _DOUBLE_QUOTED)
_INTEGER_RE = r'%(hex)s|%(dec)s|%(oct)s' % {'hex': __HEXADECIMAL_RE, 'dec': __DECIMAL_RE, 'oct': __OCTAL_RE}
_FLOAT_RE = r'%(dec)s\.%(dec)s' % {'dec': __DECIMAL_RE}
_FLOAT_RE = r'(%(dec)s)?\.%(dec)s' % {'dec': __DECIMAL_RE}
_BOOL_RE = r'true|false'
# XXX: it seams group cannot be refed this way
# r'/(?=[^*])[^/\n]*/(?![gimy]*(?P<reflag>[gimy])[gimy]*\g<reflag>)[gimy]{0,4}'
_REGEX_RE = r'/(?=[^*])[^/\n]*/[gimy]{0,4}'
_REGEX_RE = r'''/(?=[^*])
((\\([tnvfr0.\\+*?^$\[\]{}()|/]|[0-7]{3}|x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|c[A-Z]|))|
[^/\n])*/[gimy]{0,4}'''
_LITERAL_RE = r'((?P<int>%(int)s)|(?P<float>%(float)s)|(?P<str>%(str)s)|(?P<bool>%(bool)s)|(?P<regex>%(regex)s))' % {
'int': _INTEGER_RE,
@ -88,7 +90,6 @@ class JSInterpreter(object):
@staticmethod
def _next_statement(code, pos=0):
def parse_expression(_pos, allowrecursion=100):
expr = ''
while _pos < len(code):