2014-03-30 07:02:58 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
2016-12-04 19:15:35 +01:00
|
|
|
from ..utils import ExtractorError
|
|
|
|
from .tstream import TokenStream
|
2016-12-07 07:28:09 +01:00
|
|
|
from .jsgrammar import Token
|
2016-12-03 06:32:11 +01:00
|
|
|
|
2016-12-07 08:25:19 +01:00
|
|
|
_token_keys = set((Token.NULL, Token.BOOL, Token.ID, Token.STR, Token.INT, Token.FLOAT, Token.REGEX))
|
2016-11-30 07:49:47 +01:00
|
|
|
|
2016-12-06 18:42:59 +01:00
|
|
|
|
2016-12-09 23:38:48 +01:00
|
|
|
class Context(object):
|
2016-12-10 02:01:19 +01:00
|
|
|
def __init__(self, variables=None, ended=False):
|
2016-12-09 23:38:48 +01:00
|
|
|
self.ended = ended
|
2016-12-12 22:56:07 +01:00
|
|
|
self.no_in = True
|
2016-12-10 02:01:19 +01:00
|
|
|
self.local_vars = {}
|
|
|
|
if variables is not None:
|
|
|
|
for k, v in dict(variables).items():
|
2016-12-11 09:42:43 +01:00
|
|
|
# XXX validate identifiers
|
2016-12-10 02:01:19 +01:00
|
|
|
self.local_vars[k] = Reference(v, (self.local_vars, k))
|
2016-12-09 23:38:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Reference(object):
|
|
|
|
def __init__(self, value, parent=None):
|
2016-12-10 14:36:32 +01:00
|
|
|
self._value = value
|
|
|
|
self._parent = parent
|
|
|
|
|
|
|
|
def getvalue(self):
|
|
|
|
return self._value
|
|
|
|
|
|
|
|
def putvalue(self, value):
|
|
|
|
if self._parent is None:
|
|
|
|
raise ExtractorError('Trying to set a read-only reference')
|
|
|
|
parent, key = self._parent
|
|
|
|
if not hasattr(parent, '__setitem__'):
|
|
|
|
raise ExtractorError('Unknown reference')
|
|
|
|
parent.__setitem__(key, Reference(value, (parent, key)))
|
2016-12-09 23:38:48 +01:00
|
|
|
|
2016-12-10 02:01:19 +01:00
|
|
|
def __repr__(self):
|
2016-12-10 17:31:29 +01:00
|
|
|
if self._parent is not None:
|
|
|
|
parent, key = self._parent
|
|
|
|
return '<Reference value: %s, parent: %s@(0x%x), key: %s>' % (
|
|
|
|
str(self._value), parent.__class__.__name__, id(parent), key)
|
|
|
|
return '<Reference value: %s, parent: %s>' % (self._value, None)
|
2016-12-10 02:01:19 +01:00
|
|
|
|
2016-12-09 23:38:48 +01:00
|
|
|
|
2014-03-30 07:02:58 +02:00
|
|
|
class JSInterpreter(object):
|
2016-12-06 18:42:59 +01:00
|
|
|
# TODO support json
|
2016-12-03 06:32:11 +01:00
|
|
|
undefined = object()
|
|
|
|
|
2016-12-10 02:01:19 +01:00
|
|
|
def __init__(self, code, variables=None):
|
2015-02-18 10:47:40 +01:00
|
|
|
self.code = code
|
2016-12-10 00:52:04 +01:00
|
|
|
self.global_vars = {}
|
2016-12-10 02:01:19 +01:00
|
|
|
if variables is not None:
|
|
|
|
for k, v in dict(variables).items():
|
2016-12-11 09:42:43 +01:00
|
|
|
# XXX validate identifiers
|
2016-12-10 02:01:19 +01:00
|
|
|
self.global_vars[k] = Reference(v, (self.global_vars, k))
|
2016-12-12 22:56:07 +01:00
|
|
|
self._context = Context(self.global_vars)
|
2016-12-09 23:38:48 +01:00
|
|
|
self._context_stack = []
|
2015-02-01 22:38:26 +01:00
|
|
|
|
2016-12-12 18:00:50 +01:00
|
|
|
def statements(self, code=None, pos=0, stack_size=100):
|
|
|
|
if code is None:
|
|
|
|
code = self.code
|
|
|
|
ts = TokenStream(code, pos)
|
|
|
|
while not ts.ended:
|
2016-12-12 21:38:52 +01:00
|
|
|
yield self._statement(ts, stack_size)
|
2016-12-12 18:00:50 +01:00
|
|
|
raise StopIteration
|
|
|
|
|
2016-12-12 21:38:52 +01:00
|
|
|
def _statement(self, token_stream, stack_top):
|
2016-12-03 13:21:03 +01:00
|
|
|
if stack_top < 0:
|
|
|
|
raise ExtractorError('Recursion limit reached')
|
2016-12-03 06:32:11 +01:00
|
|
|
# ast
|
2016-12-04 12:49:30 +01:00
|
|
|
statement = None
|
|
|
|
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
2016-12-12 12:18:31 +01:00
|
|
|
if token_id is Token.END:
|
2016-12-04 12:49:30 +01:00
|
|
|
# empty statement goes straight here
|
2016-12-11 17:36:19 +01:00
|
|
|
token_stream.pop()
|
2016-12-04 12:49:30 +01:00
|
|
|
return statement
|
2016-12-11 09:40:43 +01:00
|
|
|
|
2016-12-12 12:18:31 +01:00
|
|
|
elif token_id is Token.ID and token_value == 'function':
|
2016-12-11 23:00:34 +01:00
|
|
|
# FIXME allowed only in program and function body
|
|
|
|
# main, function expr, object literal (set, get), function declaration
|
2016-12-11 21:05:09 +01:00
|
|
|
statement = self._function(token_stream, stack_top - 1)
|
2016-12-11 09:40:43 +01:00
|
|
|
|
|
|
|
# block
|
2016-12-07 07:28:09 +01:00
|
|
|
elif token_id is Token.COPEN:
|
2016-12-12 21:38:52 +01:00
|
|
|
# XXX refactor will deprecate some _statement calls
|
2016-12-10 22:57:02 +01:00
|
|
|
open_pos = token_pos
|
2016-12-03 06:32:11 +01:00
|
|
|
token_stream.pop()
|
2016-12-11 11:48:31 +01:00
|
|
|
block = []
|
2016-12-10 22:57:02 +01:00
|
|
|
while True:
|
2016-12-04 12:49:30 +01:00
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if token_id is Token.CCLOSE:
|
2016-12-11 17:36:19 +01:00
|
|
|
token_stream.pop()
|
2016-12-04 12:49:30 +01:00
|
|
|
break
|
2016-12-10 22:57:02 +01:00
|
|
|
elif token_id is Token.END and token_stream.ended:
|
|
|
|
raise ExtractorError('Unbalanced parentheses at %d' % open_pos)
|
2016-12-12 21:38:52 +01:00
|
|
|
block.append(self._statement(token_stream, stack_top - 1))
|
2016-12-11 11:48:31 +01:00
|
|
|
|
|
|
|
statement = (Token.BLOCK, block)
|
2016-12-11 09:40:43 +01:00
|
|
|
|
2016-12-07 07:28:09 +01:00
|
|
|
elif token_id is Token.ID:
|
2016-12-04 12:49:30 +01:00
|
|
|
if token_value == 'var':
|
2016-12-12 22:56:07 +01:00
|
|
|
# XXX refactor (create dedicated method for handling variable declaration list)
|
2016-12-06 18:42:59 +01:00
|
|
|
token_stream.pop()
|
2016-12-04 12:49:30 +01:00
|
|
|
variables = []
|
|
|
|
init = []
|
|
|
|
has_another = True
|
|
|
|
while has_another:
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
2016-12-07 07:28:09 +01:00
|
|
|
if token_id is not Token.ID:
|
2016-12-04 12:49:30 +01:00
|
|
|
raise ExtractorError('Missing variable name at %d' % token_pos)
|
2016-12-04 19:15:35 +01:00
|
|
|
token_stream.chk_id(last=True)
|
2016-12-04 12:49:30 +01:00
|
|
|
variables.append(token_value)
|
2016-12-03 06:32:11 +01:00
|
|
|
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.AOP:
|
2016-12-03 06:32:11 +01:00
|
|
|
token_stream.pop()
|
2016-12-04 12:49:30 +01:00
|
|
|
init.append(self._assign_expression(token_stream, stack_top - 1))
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
|
|
|
else:
|
|
|
|
init.append(JSInterpreter.undefined)
|
|
|
|
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.END:
|
2016-12-12 22:56:07 +01:00
|
|
|
if self._context.no_in:
|
|
|
|
token_stream.pop()
|
2016-12-04 12:49:30 +01:00
|
|
|
has_another = False
|
2016-12-07 07:28:09 +01:00
|
|
|
elif peek_id is Token.COMMA:
|
2016-12-04 12:49:30 +01:00
|
|
|
pass
|
2016-12-03 06:32:11 +01:00
|
|
|
else:
|
|
|
|
# FIXME automatic end insertion
|
2016-12-07 07:28:09 +01:00
|
|
|
# - token_id is Token.CCLOSE
|
2016-12-04 12:49:30 +01:00
|
|
|
# - check line terminator
|
|
|
|
# - restricted token
|
2016-12-12 18:00:50 +01:00
|
|
|
raise ExtractorError('Unexpected sequence at %d' % peek_pos)
|
2016-12-07 08:25:19 +01:00
|
|
|
statement = (Token.VAR, zip(variables, init))
|
2016-12-11 09:40:43 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
elif token_value == 'if':
|
2016-12-12 18:00:50 +01:00
|
|
|
statement = self._if_statement(token_stream, stack_top - 1)
|
2016-12-11 09:40:43 +01:00
|
|
|
|
2016-12-12 21:38:52 +01:00
|
|
|
elif token_value == 'for':
|
2016-12-12 18:00:50 +01:00
|
|
|
statement = self._for_loop(token_stream, stack_top - 1)
|
2016-12-12 12:18:31 +01:00
|
|
|
|
2016-12-12 21:38:52 +01:00
|
|
|
elif token_value == 'do':
|
2016-12-12 18:00:50 +01:00
|
|
|
statement = self._do_loop(token_stream, stack_top - 1)
|
2016-12-12 12:18:31 +01:00
|
|
|
|
2016-12-12 21:38:52 +01:00
|
|
|
elif token_value == 'while':
|
2016-12-12 18:00:50 +01:00
|
|
|
statement = self._while_loop(token_stream, stack_top - 1)
|
2016-12-11 09:40:43 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
elif token_value in ('break', 'continue'):
|
2016-12-11 09:40:43 +01:00
|
|
|
token_stream.pop()
|
|
|
|
token = {'break': Token.BREAK, 'continue': Token.CONTINUE}[token_value]
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-11 09:42:43 +01:00
|
|
|
# XXX no line break here
|
2016-12-11 21:05:09 +01:00
|
|
|
label_name = None
|
2016-12-11 09:40:43 +01:00
|
|
|
if peek_id is not Token.END:
|
|
|
|
token_stream.chk_id()
|
2016-12-11 21:05:09 +01:00
|
|
|
label_name = peek_value
|
2016-12-11 09:40:43 +01:00
|
|
|
token_stream.pop()
|
2016-12-11 21:05:09 +01:00
|
|
|
statement = (token, label_name)
|
2016-12-11 09:40:43 +01:00
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-12 13:16:08 +01:00
|
|
|
if peek_id is Token.END:
|
|
|
|
token_stream.pop()
|
|
|
|
else:
|
2016-12-11 09:40:43 +01:00
|
|
|
# FIXME automatic end insertion
|
2016-12-12 18:00:50 +01:00
|
|
|
raise ExtractorError('Unexpected sequence at %d' % peek_pos)
|
2016-12-11 09:40:43 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
elif token_value == 'return':
|
2016-12-12 18:00:50 +01:00
|
|
|
statement = self._return_statement(token_stream, stack_top - 1)
|
2016-12-04 12:49:30 +01:00
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-12 13:16:08 +01:00
|
|
|
if peek_id is Token.END:
|
|
|
|
token_stream.pop()
|
|
|
|
else:
|
2016-12-04 12:49:30 +01:00
|
|
|
# FIXME automatic end insertion
|
2016-12-12 18:00:50 +01:00
|
|
|
raise ExtractorError('Unexpected sequence at %d' % peek_pos)
|
2016-12-11 09:40:43 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
elif token_value == 'with':
|
2016-12-12 18:00:50 +01:00
|
|
|
statement = self._with_statement(token_stream, stack_top - 1)
|
2016-12-11 09:40:43 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
elif token_value == 'switch':
|
2016-12-12 18:00:50 +01:00
|
|
|
statement = self._switch_statement(token_stream, stack_top - 1)
|
2016-12-11 09:40:43 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
elif token_value == 'throw':
|
2016-12-11 19:04:17 +01:00
|
|
|
token_stream.pop()
|
|
|
|
# XXX no line break here
|
|
|
|
expr = self._expression(token_stream, stack_top - 1)
|
|
|
|
statement = (Token.RETURN, expr)
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-12 13:16:08 +01:00
|
|
|
if peek_id is Token.END:
|
|
|
|
token_stream.pop()
|
|
|
|
else:
|
2016-12-11 19:04:17 +01:00
|
|
|
# FIXME automatic end insertion
|
2016-12-12 18:00:50 +01:00
|
|
|
raise ExtractorError('Unexpected sequence at %d' % peek_pos)
|
2016-12-11 09:40:43 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
elif token_value == 'try':
|
2016-12-12 18:00:50 +01:00
|
|
|
statement = self._try_statement(token_stream, stack_top - 1)
|
2016-12-11 09:40:43 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
elif token_value == 'debugger':
|
2016-12-11 19:04:17 +01:00
|
|
|
token_stream.pop()
|
|
|
|
statement = (Token.DEBUG)
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-12 13:16:08 +01:00
|
|
|
if peek_id is Token.END:
|
|
|
|
token_stream.pop()
|
|
|
|
else:
|
2016-12-11 19:04:17 +01:00
|
|
|
# FIXME automatic end insertion
|
2016-12-12 18:00:50 +01:00
|
|
|
raise ExtractorError('Unexpected sequence at %d' % peek_pos)
|
|
|
|
else: # label
|
2016-12-12 12:18:31 +01:00
|
|
|
# XXX possible refactoring (this is the only branch not poping)
|
2016-12-11 23:30:03 +01:00
|
|
|
token_id, token_value, token_pos = token_stream.peek(2)
|
|
|
|
if token_id is Token.COLON:
|
|
|
|
token_id, label_name, token_pos = token_stream.pop(2)
|
|
|
|
token_stream.chk_id(last=True)
|
2016-12-12 21:38:52 +01:00
|
|
|
statement = (Token.LABEL, label_name, self._statement(token_stream, stack_top - 1))
|
2016-12-11 09:40:43 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
# expr
|
|
|
|
if statement is None:
|
2016-12-12 13:16:08 +01:00
|
|
|
statement = self._expression(token_stream, stack_top - 1)
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
|
|
|
if peek_id is Token.END:
|
|
|
|
token_stream.pop()
|
|
|
|
else:
|
|
|
|
# FIXME automatic end insertion
|
2016-12-12 18:00:50 +01:00
|
|
|
raise ExtractorError('Unexpected sequence at %d' % peek_pos)
|
2016-12-04 12:49:30 +01:00
|
|
|
|
|
|
|
return statement
|
2016-12-03 06:32:11 +01:00
|
|
|
|
2016-12-12 18:00:50 +01:00
|
|
|
def _if_statement(self, token_stream, stack_top):
|
|
|
|
token_stream.pop()
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.POPEN:
|
|
|
|
raise ExtractorError('Missing condition at %d' % token_pos)
|
|
|
|
cond_expr = self._expression(token_stream, stack_top - 1)
|
|
|
|
token_stream.pop() # Token.PCLOSE
|
2016-12-12 21:38:52 +01:00
|
|
|
true_expr = self._statement(token_stream, stack_top - 1)
|
2016-12-12 18:00:50 +01:00
|
|
|
false_expr = None
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
if token_id is Token.ID and token_value == 'else':
|
|
|
|
token_stream.pop()
|
2016-12-12 21:38:52 +01:00
|
|
|
false_expr = self._statement(token_stream, stack_top - 1)
|
2016-12-12 18:00:50 +01:00
|
|
|
return (Token.IF, cond_expr, true_expr, false_expr)
|
2016-12-03 06:32:11 +01:00
|
|
|
|
2016-12-12 18:00:50 +01:00
|
|
|
def _for_loop(self, token_stream, stack_top):
|
|
|
|
token_stream.pop()
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.POPEN:
|
|
|
|
raise ExtractorError('''Expected '(' at %d''' % token_pos)
|
|
|
|
|
|
|
|
# FIXME set infor True (checked by variable declaration and relation expression)
|
2016-12-12 22:56:07 +01:00
|
|
|
self._context.no_in = False
|
2016-12-12 18:00:50 +01:00
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
if token_id is Token.END:
|
|
|
|
init = None
|
2016-12-12 22:56:07 +01:00
|
|
|
elif token_id is Token.ID and token_value == 'var':
|
2016-12-12 21:38:52 +01:00
|
|
|
init = self._statement(token_stream, stack_top - 1)
|
2016-12-12 18:00:50 +01:00
|
|
|
else:
|
|
|
|
init = self._expression(token_stream, stack_top - 1)
|
2016-12-12 22:56:07 +01:00
|
|
|
self._context.no_in = True
|
|
|
|
|
2016-12-12 18:00:50 +01:00
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is Token.IN:
|
|
|
|
cond = self._expression(token_stream, stack_top - 1)
|
|
|
|
# FIXME further processing might be needed for interpretation
|
|
|
|
incr = None
|
|
|
|
# NOTE ES6 has of operator
|
|
|
|
elif token_id is Token.END:
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
cond = None if token_id is Token.END else self._expression(token_stream, stack_top - 1)
|
|
|
|
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
2016-12-12 22:56:07 +01:00
|
|
|
if token_id is not Token.END:
|
|
|
|
raise ExtractorError('''Expected ';' at %d''' % token_pos)
|
2016-12-12 18:00:50 +01:00
|
|
|
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
incr = None if token_id is Token.END else self._expression(token_stream, stack_top - 1)
|
|
|
|
else:
|
|
|
|
raise ExtractorError('Invalid condition in for loop initialization at %d' % token_pos)
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.PCLOSE:
|
|
|
|
raise ExtractorError('''Expected ')' at %d''' % token_pos)
|
2016-12-12 21:38:52 +01:00
|
|
|
body = self._statement(token_stream, stack_top - 1)
|
2016-12-12 18:00:50 +01:00
|
|
|
return (Token.FOR, init, cond, incr, body)
|
|
|
|
|
|
|
|
def _do_loop(self, token_stream, stack_top):
|
|
|
|
token_stream.pop()
|
2016-12-12 21:38:52 +01:00
|
|
|
body = self._statement(token_stream, stack_top - 1)
|
2016-12-12 18:00:50 +01:00
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.ID and token_value != 'while':
|
|
|
|
raise ExtractorError('''Expected 'while' at %d''' % token_pos)
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.POPEN:
|
|
|
|
raise ExtractorError('''Expected '(' at %d''' % token_pos)
|
|
|
|
expr = self._expression(token_stream, stack_top - 1)
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.PCLOSE:
|
|
|
|
raise ExtractorError('''Expected ')' at %d''' % token_pos)
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
|
|
|
if peek_id is Token.END:
|
|
|
|
token_stream.pop()
|
|
|
|
else:
|
|
|
|
# FIXME automatic end insertion
|
|
|
|
raise ExtractorError('''Expected ';' at %d''' % peek_pos)
|
|
|
|
return (Token.DO, expr, body)
|
|
|
|
|
|
|
|
def _while_loop(self, token_stream, stack_top):
|
|
|
|
token_stream.pop()
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.POPEN:
|
|
|
|
raise ExtractorError('''Expected '(' at %d''' % token_pos)
|
|
|
|
expr = self._expression(token_stream, stack_top - 1)
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.PCLOSE:
|
|
|
|
raise ExtractorError('''Expected ')' at %d''' % token_pos)
|
2016-12-12 21:38:52 +01:00
|
|
|
body = self._statement(token_stream, stack_top)
|
2016-12-12 21:45:08 +01:00
|
|
|
return (Token.WHILE, expr, body)
|
2016-12-12 18:00:50 +01:00
|
|
|
|
|
|
|
def _return_statement(self, token_stream, stack_top):
|
|
|
|
token_stream.pop()
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
|
|
|
# XXX no line break here
|
|
|
|
expr = self._expression(token_stream, stack_top - 1) if peek_id is not Token.END else None
|
|
|
|
return (Token.RETURN, expr)
|
|
|
|
|
|
|
|
def _with_statement(self, token_stream, stack_top):
|
|
|
|
token_stream.pop()
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.POPEN:
|
|
|
|
raise ExtractorError('Missing expression at %d' % token_pos)
|
|
|
|
expr = self._expression(token_stream, stack_top - 1)
|
|
|
|
token_stream.pop() # Token.PCLOSE
|
2016-12-12 21:38:52 +01:00
|
|
|
return (Token.WITH, expr, self._statement(token_stream, stack_top - 1))
|
2016-12-12 18:00:50 +01:00
|
|
|
|
|
|
|
def _switch_statement(self, token_stream, stack_top):
|
|
|
|
token_stream.pop()
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.POPEN:
|
|
|
|
raise ExtractorError('Missing expression at %d' % token_pos)
|
|
|
|
discriminant = self._expression(token_stream, stack_top - 1)
|
|
|
|
token_stream.pop() # Token.PCLOSE
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.COPEN:
|
|
|
|
raise ExtractorError('Missing case block at %d' % token_pos)
|
|
|
|
open_pos = token_pos
|
|
|
|
has_default = False
|
|
|
|
block = []
|
|
|
|
while True:
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
if token_id is Token.CCLOSE:
|
|
|
|
break
|
|
|
|
elif token_id is Token.ID and token_value == 'case':
|
|
|
|
token_stream.pop()
|
|
|
|
expr = self._expression(token_stream, stack_top - 1)
|
|
|
|
|
|
|
|
elif token_id is Token.ID and token_value == 'default':
|
|
|
|
if has_default:
|
|
|
|
raise ExtractorError('Multiple default clause')
|
|
|
|
token_stream.pop()
|
|
|
|
has_default = True
|
|
|
|
expr = None
|
|
|
|
|
|
|
|
elif token_id is Token.END and token_stream.ended:
|
|
|
|
raise ExtractorError('Unbalanced parentheses at %d' % open_pos)
|
|
|
|
else:
|
|
|
|
raise ExtractorError('Unexpected sequence at %d, default or case clause is expected' %
|
|
|
|
token_pos)
|
|
|
|
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.COLON:
|
|
|
|
raise ExtractorError('''Unexpected sequence at %d, ':' is expected''' % token_pos)
|
|
|
|
|
|
|
|
statement_list = []
|
|
|
|
while True:
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
if token_id == Token.CCLOSE or (token_id is Token.ID and (token_value in ('default', 'case'))):
|
|
|
|
break
|
|
|
|
elif token_id is Token.END and token_stream.ended:
|
|
|
|
raise ExtractorError('Unbalanced parentheses at %d' % open_pos)
|
2016-12-12 21:38:52 +01:00
|
|
|
statement_list.append(self._statement(token_stream, stack_top - 1))
|
2016-12-12 18:00:50 +01:00
|
|
|
|
|
|
|
block.append((expr, statement_list))
|
|
|
|
token_stream.pop()
|
|
|
|
return (Token.SWITCH, discriminant, block)
|
|
|
|
|
|
|
|
def _try_statement(self, token_stream, stack_top):
|
|
|
|
token_stream.pop()
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
if token_id is not Token.COPEN:
|
|
|
|
raise ExtractorError('Block is expected at %d' % token_pos)
|
2016-12-12 21:38:52 +01:00
|
|
|
try_block = self._statement(token_stream, stack_top - 1)
|
2016-12-12 18:00:50 +01:00
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
catch_block = None
|
|
|
|
if token_id is Token.ID and token_value == 'catch':
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
if token_id is not Token.POPEN:
|
|
|
|
raise ExtractorError('Catch clause is missing an identifier at %d' % token_pos)
|
|
|
|
token_stream.pop()
|
|
|
|
token_stream.chk_id()
|
|
|
|
token_id, error_name, token_pos = token_stream.pop()
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.PCLOSE:
|
|
|
|
raise ExtractorError('Catch clause expects a single identifier at %d' % token_pos)
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
if token_id is not Token.COPEN:
|
|
|
|
raise ExtractorError('Block is expected at %d' % token_pos)
|
2016-12-12 21:38:52 +01:00
|
|
|
catch_block = (error_name, self._statement(token_stream, stack_top - 1))
|
2016-12-12 18:00:50 +01:00
|
|
|
finally_block = None
|
|
|
|
if token_id is Token.ID and token_value == 'finally':
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
if token_id is not Token.COPEN:
|
|
|
|
raise ExtractorError('Block is expected at %d' % token_pos)
|
2016-12-12 21:38:52 +01:00
|
|
|
finally_block = self._statement(token_stream, stack_top - 1)
|
2016-12-12 18:00:50 +01:00
|
|
|
if catch_block is None and finally_block is None:
|
|
|
|
raise ExtractorError('Try statement is expecting catch or finally at %d' % token_pos)
|
|
|
|
return (Token.TRY, try_block, catch_block, finally_block)
|
2016-12-03 06:32:11 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
def _expression(self, token_stream, stack_top):
|
2016-12-11 09:40:43 +01:00
|
|
|
expr_list = []
|
2016-12-04 12:49:30 +01:00
|
|
|
has_another = True
|
|
|
|
while has_another:
|
2016-12-11 09:40:43 +01:00
|
|
|
expr_list.append(self._assign_expression(token_stream, stack_top - 1))
|
2016-12-04 12:49:30 +01:00
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.COMMA:
|
2016-12-04 12:49:30 +01:00
|
|
|
token_stream.pop()
|
2016-12-07 07:28:09 +01:00
|
|
|
elif peek_id is Token.ID and peek_value == 'yield':
|
2016-12-06 18:42:59 +01:00
|
|
|
# TODO parse yield
|
2016-12-04 12:49:30 +01:00
|
|
|
raise ExtractorError('Yield statement is not yet supported at %d' % peek_pos)
|
|
|
|
else:
|
|
|
|
has_another = False
|
2016-12-11 09:40:43 +01:00
|
|
|
return (Token.EXPR, expr_list)
|
2016-12-03 06:32:11 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
def _assign_expression(self, token_stream, stack_top):
|
|
|
|
if stack_top < 0:
|
|
|
|
raise ExtractorError('Recursion limit reached')
|
|
|
|
|
|
|
|
left = self._conditional_expression(token_stream, stack_top - 1)
|
2016-12-03 06:32:11 +01:00
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.AOP:
|
2016-12-03 13:21:03 +01:00
|
|
|
token_stream.pop()
|
2016-12-04 19:15:35 +01:00
|
|
|
_, op = peek_value
|
2016-12-04 12:49:30 +01:00
|
|
|
right = self._assign_expression(token_stream, stack_top - 1)
|
2016-12-03 06:32:11 +01:00
|
|
|
else:
|
2016-12-04 19:15:35 +01:00
|
|
|
op = None
|
2016-12-03 13:21:03 +01:00
|
|
|
right = None
|
2016-12-07 08:25:19 +01:00
|
|
|
return (Token.ASSIGN, op, left, right)
|
2016-11-30 07:49:47 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
def _member_expression(self, token_stream, stack_top):
|
2016-12-03 13:21:03 +01:00
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.ID and peek_value == 'new':
|
2016-12-04 12:49:30 +01:00
|
|
|
token_stream.pop()
|
|
|
|
target = self._member_expression(token_stream, stack_top - 1)
|
|
|
|
args = self._arguments(token_stream, stack_top - 1)
|
|
|
|
# Rhino has check for args length
|
|
|
|
# Rhino has experimental syntax allowing an object literal to follow a new expression
|
|
|
|
else:
|
|
|
|
target = self._primary_expression(token_stream, stack_top)
|
|
|
|
args = None
|
|
|
|
|
2016-12-07 08:25:19 +01:00
|
|
|
return (Token.MEMBER, target, args, self._member_tail(token_stream, stack_top - 1))
|
2016-12-03 13:21:03 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
def _member_tail(self, token_stream, stack_top):
|
2016-12-06 18:42:59 +01:00
|
|
|
if stack_top < 0:
|
|
|
|
raise ExtractorError('Recursion limit reached')
|
|
|
|
|
2016-12-03 13:21:03 +01:00
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.DOT:
|
2016-12-03 13:21:03 +01:00
|
|
|
token_stream.pop()
|
2016-12-04 12:49:30 +01:00
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.DOT:
|
2016-12-04 12:49:30 +01:00
|
|
|
token_stream.pop()
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
elif peek_id is Token.POPEN:
|
2016-12-11 21:05:09 +01:00
|
|
|
# TODO parse field query
|
2016-12-10 02:59:32 +01:00
|
|
|
raise ExtractorError('Field query is not yet supported at %d' % peek_pos)
|
2016-12-04 12:49:30 +01:00
|
|
|
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.ID:
|
2016-12-04 12:49:30 +01:00
|
|
|
token_stream.pop()
|
2016-12-07 08:25:19 +01:00
|
|
|
return (Token.FIELD, peek_value, self._member_tail(token_stream, stack_top - 1))
|
2016-12-04 12:49:30 +01:00
|
|
|
else:
|
|
|
|
raise ExtractorError('Identifier name expected at %d' % peek_pos)
|
2016-12-07 08:25:19 +01:00
|
|
|
elif peek_id is Token.SOPEN:
|
2016-12-04 12:49:30 +01:00
|
|
|
token_stream.pop()
|
|
|
|
index = self._expression(token_stream, stack_top - 1)
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
2016-12-07 07:28:09 +01:00
|
|
|
if token_id is Token.SCLOSE:
|
2016-12-07 08:25:19 +01:00
|
|
|
return (Token.ELEM, index, self._member_tail(token_stream, stack_top - 1))
|
2016-12-04 12:49:30 +01:00
|
|
|
else:
|
|
|
|
raise ExtractorError('Unexpected sequence at %d' % token_pos)
|
2016-12-07 07:28:09 +01:00
|
|
|
elif peek_id is Token.POPEN:
|
2016-12-04 12:49:30 +01:00
|
|
|
args = self._arguments(token_stream, stack_top - 1)
|
2016-12-07 08:25:19 +01:00
|
|
|
return (Token.CALL, args, self._member_tail(token_stream, stack_top - 1))
|
2016-12-04 12:49:30 +01:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _primary_expression(self, token_stream, stack_top):
|
2016-12-06 18:42:59 +01:00
|
|
|
if stack_top < 0:
|
|
|
|
raise ExtractorError('Recursion limit reached')
|
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
# TODO support let
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
|
|
|
if peek_id in _token_keys:
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.ID:
|
2016-12-04 12:49:30 +01:00
|
|
|
# this
|
|
|
|
if peek_value == 'this':
|
2016-12-12 20:05:31 +01:00
|
|
|
token_stream.pop()
|
2016-12-07 08:25:19 +01:00
|
|
|
return (Token.RSV, 'this')
|
2016-12-04 12:49:30 +01:00
|
|
|
# function expr
|
|
|
|
elif peek_value == 'function':
|
2016-12-11 21:05:09 +01:00
|
|
|
return self._function(token_stream, stack_top - 1, True)
|
2016-12-04 12:49:30 +01:00
|
|
|
# id
|
|
|
|
else:
|
2016-12-12 20:05:31 +01:00
|
|
|
token_stream.chk_id()
|
|
|
|
token_stream.pop()
|
2016-12-07 07:28:09 +01:00
|
|
|
return (Token.ID, peek_value)
|
2016-12-04 12:49:30 +01:00
|
|
|
# literals
|
|
|
|
else:
|
2016-12-12 20:05:31 +01:00
|
|
|
token_stream.pop()
|
2016-12-08 03:55:23 +01:00
|
|
|
return (peek_id, peek_value)
|
2016-12-04 12:49:30 +01:00
|
|
|
# array
|
2016-12-07 07:28:09 +01:00
|
|
|
elif peek_id is Token.SOPEN:
|
2016-12-04 12:49:30 +01:00
|
|
|
return self._array_literal(token_stream, stack_top - 1)
|
|
|
|
# object
|
2016-12-12 12:18:31 +01:00
|
|
|
elif peek_id is Token.COPEN:
|
2016-12-12 20:32:05 +01:00
|
|
|
return self._object_literal(token_stream, stack_top)
|
2016-12-04 12:49:30 +01:00
|
|
|
# expr
|
2016-12-07 07:28:09 +01:00
|
|
|
elif peek_id is Token.POPEN:
|
2016-12-04 12:49:30 +01:00
|
|
|
token_stream.pop()
|
|
|
|
open_pos = peek_pos
|
|
|
|
expr = self._expression(token_stream, stack_top - 1)
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is not Token.PCLOSE:
|
2016-12-04 12:49:30 +01:00
|
|
|
raise ExtractorError('Unbalanced parentheses at %d' % open_pos)
|
|
|
|
token_stream.pop()
|
2016-12-08 08:29:12 +01:00
|
|
|
return expr
|
2016-12-04 12:49:30 +01:00
|
|
|
else:
|
2016-12-12 12:18:31 +01:00
|
|
|
raise ExtractorError('Syntax error at %d' % peek_pos)
|
2016-12-04 12:49:30 +01:00
|
|
|
|
2016-12-11 21:05:09 +01:00
|
|
|
def _function(self, token_stream, stack_top, is_expr=False):
|
|
|
|
token_stream.pop()
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
name = None
|
|
|
|
if token_id is Token.ID:
|
|
|
|
token_stream.chk_id()
|
|
|
|
token_id, name, token_pos = token_stream.pop()
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
elif not is_expr:
|
|
|
|
raise ExtractorError('Function declaration at %d is missing identifier' % token_pos)
|
|
|
|
|
2016-12-11 23:30:03 +01:00
|
|
|
if token_id is not Token.POPEN:
|
2016-12-11 21:05:09 +01:00
|
|
|
raise ExtractorError('Expected argument list at %d' % token_pos)
|
|
|
|
|
2016-12-11 23:30:03 +01:00
|
|
|
token_stream.pop()
|
|
|
|
open_pos = token_pos
|
|
|
|
|
2016-12-11 21:05:09 +01:00
|
|
|
args = []
|
|
|
|
while True:
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
if token_id is Token.PCLOSE:
|
|
|
|
token_stream.pop()
|
|
|
|
break
|
|
|
|
token_stream.chk_id()
|
|
|
|
token_stream.pop()
|
|
|
|
args.append(token_value)
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
if token_id is Token.COMMA:
|
|
|
|
token_stream.pop()
|
|
|
|
elif token_id is Token.PCLOSE:
|
|
|
|
pass
|
|
|
|
elif token_id is Token.END and token_stream.ended:
|
|
|
|
raise ExtractorError('Unbalanced parentheses at %d' % open_pos)
|
|
|
|
else:
|
|
|
|
raise ExtractorError('Expected , separator at %d' % token_pos)
|
|
|
|
|
|
|
|
token_id, token_value, token_pos = token_stream.peek()
|
|
|
|
if token_id is not Token.COPEN:
|
|
|
|
raise ExtractorError('Expected function body at %d' % token_pos)
|
|
|
|
|
2016-12-12 21:38:52 +01:00
|
|
|
return (Token.FUNC, name, args, self._statement(token_stream, stack_top - 1))
|
2016-12-11 21:05:09 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
def _arguments(self, token_stream, stack_top):
|
2016-12-06 18:42:59 +01:00
|
|
|
if stack_top < 0:
|
|
|
|
raise ExtractorError('Recursion limit reached')
|
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.POPEN:
|
2016-12-04 12:49:30 +01:00
|
|
|
token_stream.pop()
|
|
|
|
open_pos = peek_pos
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
args = []
|
|
|
|
while True:
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.PCLOSE:
|
2016-12-04 12:49:30 +01:00
|
|
|
token_stream.pop()
|
|
|
|
return args
|
|
|
|
# FIXME handle infor
|
|
|
|
args.append(self._assign_expression(token_stream, stack_top - 1))
|
2016-12-06 18:42:59 +01:00
|
|
|
# TODO parse generator expression
|
2016-12-04 12:49:30 +01:00
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
|
|
|
|
2016-12-10 22:57:02 +01:00
|
|
|
if peek_id is Token.COMMA:
|
|
|
|
token_stream.pop()
|
|
|
|
elif peek_id is Token.PCLOSE:
|
|
|
|
pass
|
|
|
|
elif peek_id is Token.END and token_stream.ended:
|
2016-12-04 12:49:30 +01:00
|
|
|
raise ExtractorError('Unbalanced parentheses at %d' % open_pos)
|
2016-12-10 22:57:02 +01:00
|
|
|
else:
|
2016-12-12 21:38:52 +01:00
|
|
|
raise ExtractorError('''Expected ',' separator at %d''' % peek_pos)
|
2016-12-04 12:49:30 +01:00
|
|
|
|
|
|
|
def _array_literal(self, token_stream, stack_top):
|
2016-12-06 18:42:59 +01:00
|
|
|
if stack_top < 0:
|
|
|
|
raise ExtractorError('Recursion limit reached')
|
|
|
|
|
2016-12-11 09:42:43 +01:00
|
|
|
# XXX check no linebreak here
|
2016-12-04 12:49:30 +01:00
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is not Token.SOPEN:
|
2016-12-04 12:49:30 +01:00
|
|
|
raise ExtractorError('Array expected at %d' % peek_pos)
|
|
|
|
token_stream.pop()
|
|
|
|
elements = []
|
|
|
|
|
|
|
|
has_another = True
|
|
|
|
while has_another:
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.COMMA:
|
2016-12-04 12:49:30 +01:00
|
|
|
token_stream.pop()
|
|
|
|
elements.append(None)
|
2016-12-07 07:28:09 +01:00
|
|
|
elif peek_id is Token.SCLOSE:
|
2016-12-04 12:49:30 +01:00
|
|
|
token_stream.pop()
|
|
|
|
has_another = False
|
2016-12-07 07:28:09 +01:00
|
|
|
elif peek_id is Token.ID and peek_value == 'for':
|
2016-12-11 21:05:09 +01:00
|
|
|
# TODO parse array comprehension
|
2016-12-04 12:49:30 +01:00
|
|
|
raise ExtractorError('Array comprehension is not yet supported at %d' % peek_pos)
|
|
|
|
else:
|
|
|
|
elements.append(self._assign_expression(token_stream, stack_top - 1))
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.pop()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.SCLOSE:
|
2016-12-06 18:42:59 +01:00
|
|
|
has_another = False
|
2016-12-07 07:28:09 +01:00
|
|
|
elif peek_id is not Token.COMMA:
|
2016-12-12 21:38:52 +01:00
|
|
|
raise ExtractorError('''Expected ',' after element at %d''' % peek_pos)
|
2016-12-06 18:42:59 +01:00
|
|
|
|
2016-12-07 08:25:19 +01:00
|
|
|
return (Token.ARRAY, elements)
|
2016-12-04 12:49:30 +01:00
|
|
|
|
2016-12-12 20:32:05 +01:00
|
|
|
def _object_literal(self, token_stream, stack_top):
|
|
|
|
token_id, token_value, open_pos = token_stream.pop()
|
|
|
|
property_list = []
|
|
|
|
while True:
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is Token.CCLOSE:
|
|
|
|
break
|
|
|
|
elif token_id is Token.COMMA:
|
|
|
|
continue
|
|
|
|
elif token_id is Token.ID and token_value in ('get', 'set'):
|
|
|
|
is_set = token_id is Token.ID and token_value == 'set'
|
|
|
|
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id not in (Token.ID, Token.STR, Token.INT, Token.FLOAT):
|
|
|
|
raise ExtractorError('Property name is expected at %d' % token_pos)
|
|
|
|
property_name = token_value
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.POPEN:
|
|
|
|
raise ExtractorError('''Expected '(' at %d''' % token_pos)
|
|
|
|
|
|
|
|
if is_set:
|
|
|
|
token_stream.chk_id()
|
|
|
|
token_id, arg, token_pos = token_stream.pop()
|
|
|
|
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.PCLOSE:
|
|
|
|
raise ExtractorError('''Expected ')' at %d''' % token_pos)
|
|
|
|
|
|
|
|
if is_set:
|
2016-12-12 21:38:52 +01:00
|
|
|
desc = (Token.PROPSET, arg, self._statement(token_stream, stack_top - 1))
|
2016-12-12 20:32:05 +01:00
|
|
|
else:
|
2016-12-12 21:38:52 +01:00
|
|
|
desc = (Token.PROPGET, self._statement(token_stream, stack_top - 1))
|
2016-12-12 20:32:05 +01:00
|
|
|
|
|
|
|
elif token_id in (Token.ID, Token.STR, Token.INT, Token.FLOAT):
|
|
|
|
property_name = token_value
|
|
|
|
token_id, token_value, token_pos = token_stream.pop()
|
|
|
|
if token_id is not Token.COLON:
|
|
|
|
raise ExtractorError('Property name is expected at %d' % token_pos)
|
|
|
|
|
|
|
|
desc = (Token.PROPVALUE, self._assign_expression(token_stream, stack_top - 1))
|
|
|
|
|
|
|
|
elif token_stream.ended:
|
2016-12-12 21:38:52 +01:00
|
|
|
raise ExtractorError('Unmatched parentheses at %d' % open_pos)
|
2016-12-12 20:32:05 +01:00
|
|
|
else:
|
|
|
|
raise ExtractorError('Property assignment is expected at %d' % token_pos)
|
|
|
|
|
|
|
|
property_list.append((property_name, desc))
|
|
|
|
|
|
|
|
return (Token.OBJECT, property_list)
|
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
def _conditional_expression(self, token_stream, stack_top):
|
2016-12-06 18:42:59 +01:00
|
|
|
if stack_top < 0:
|
|
|
|
raise ExtractorError('Recursion limit reached')
|
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
expr = self._operator_expression(token_stream, stack_top - 1)
|
2016-12-03 13:21:03 +01:00
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.HOOK:
|
2016-12-03 13:21:03 +01:00
|
|
|
hook_pos = peek_pos
|
2016-12-04 12:49:30 +01:00
|
|
|
true_expr = self._assign_expression(token_stream, stack_top - 1)
|
2016-12-03 13:21:03 +01:00
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.COLON:
|
2016-12-04 12:49:30 +01:00
|
|
|
false_expr = self._assign_expression(token_stream, stack_top - 1)
|
2016-12-03 13:21:03 +01:00
|
|
|
else:
|
|
|
|
raise ExtractorError('Missing : in conditional expression at %d' % hook_pos)
|
2016-12-07 08:25:19 +01:00
|
|
|
return (Token.COND, expr, true_expr, false_expr)
|
2016-12-05 11:44:32 +01:00
|
|
|
return expr
|
2016-12-03 13:21:03 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
def _operator_expression(self, token_stream, stack_top):
|
2016-12-06 18:42:59 +01:00
|
|
|
if stack_top < 0:
|
|
|
|
raise ExtractorError('Recursion limit reached')
|
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
# --<---------------------------------<-- op --<--------------------------<----
|
|
|
|
# | |
|
|
|
|
# | --<-- prefix --<-- -->-- postfix -->-- |
|
|
|
|
# | | ^ ^ | ^
|
|
|
|
# v v | | v |
|
|
|
|
# ->------------>----------->-- lefthand-side expression -->----------->------------>---|
|
|
|
|
#
|
2016-12-03 13:21:03 +01:00
|
|
|
# 20 grouping
|
|
|
|
# ... # handled by lefthandside_expression
|
|
|
|
# 17 postfix
|
|
|
|
# 16 unary
|
|
|
|
# 15 exponentiation # not yet found in grammar
|
|
|
|
# 14 mul
|
|
|
|
# 13 add
|
|
|
|
# 12 shift
|
|
|
|
# 11 rel
|
|
|
|
# 10 eq
|
|
|
|
# 9 band
|
|
|
|
# 8 bxor
|
|
|
|
# 7 bor
|
|
|
|
# 6 land
|
|
|
|
# 5 lor
|
|
|
|
# 4 cond # handled by conditional_expression
|
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
out = []
|
|
|
|
stack = []
|
|
|
|
|
2016-12-03 13:21:03 +01:00
|
|
|
has_another = True
|
|
|
|
while has_another:
|
2016-12-04 12:49:30 +01:00
|
|
|
had_inc = False
|
|
|
|
has_prefix = True
|
|
|
|
while has_prefix:
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.UOP:
|
2016-12-05 11:44:32 +01:00
|
|
|
name, op = peek_value
|
2016-12-07 07:28:09 +01:00
|
|
|
had_inc = name in (Token.INC, Token.DEC)
|
2016-12-06 18:42:59 +01:00
|
|
|
while stack and stack[-1][0] > 16:
|
2016-12-05 11:44:32 +01:00
|
|
|
_, stack_id, stack_op = stack.pop()
|
|
|
|
out.append((stack_id, stack_op))
|
|
|
|
stack.append((16, peek_id, op))
|
2016-12-04 12:49:30 +01:00
|
|
|
token_stream.pop()
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-07 07:28:09 +01:00
|
|
|
if had_inc and peek_id is not Token.ID:
|
2016-12-04 12:49:30 +01:00
|
|
|
raise ExtractorError('Prefix operator has to be followed by an identifier at %d' % peek_pos)
|
2016-12-07 07:28:09 +01:00
|
|
|
has_prefix = peek_id is Token.UOP
|
2016-12-04 12:49:30 +01:00
|
|
|
else:
|
|
|
|
has_prefix = False
|
2016-12-03 13:21:03 +01:00
|
|
|
|
2016-12-04 12:49:30 +01:00
|
|
|
left = self._member_expression(token_stream, stack_top - 1)
|
2016-12-03 13:21:03 +01:00
|
|
|
out.append(left)
|
|
|
|
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
2016-12-04 12:49:30 +01:00
|
|
|
# postfix
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.UOP:
|
2016-12-04 12:49:30 +01:00
|
|
|
if had_inc:
|
|
|
|
raise ExtractorError('''Can't have prefix and postfix operator at the same time at %d''' % peek_pos)
|
2016-12-03 13:21:03 +01:00
|
|
|
name, op = peek_value
|
2016-12-07 07:28:09 +01:00
|
|
|
if name in (Token.INC, Token.DEC):
|
2016-12-04 12:49:30 +01:00
|
|
|
prec = 17
|
2016-12-03 13:21:03 +01:00
|
|
|
else:
|
|
|
|
raise ExtractorError('Unexpected operator at %d' % peek_pos)
|
2016-12-06 18:42:59 +01:00
|
|
|
while stack and stack[-1][0] >= 17:
|
2016-12-05 11:44:32 +01:00
|
|
|
_, stack_id, stack_op = stack.pop()
|
|
|
|
out.append((stack_id, stack_op))
|
|
|
|
stack.append((prec, peek_id, op))
|
2016-12-04 12:49:30 +01:00
|
|
|
token_stream.pop()
|
|
|
|
peek_id, peek_value, peek_pos = token_stream.peek()
|
|
|
|
|
2016-12-07 07:28:09 +01:00
|
|
|
if peek_id is Token.REL:
|
2016-12-03 13:21:03 +01:00
|
|
|
name, op = peek_value
|
2016-12-11 17:36:19 +01:00
|
|
|
prec = 11
|
2016-12-07 07:28:09 +01:00
|
|
|
elif peek_id is Token.OP:
|
2016-12-03 13:21:03 +01:00
|
|
|
name, op = peek_value
|
2016-12-07 07:28:09 +01:00
|
|
|
if name in (Token.MUL, Token.DIV, Token.MOD):
|
2016-12-03 13:21:03 +01:00
|
|
|
prec = 14
|
2016-12-07 07:28:09 +01:00
|
|
|
elif name in (Token.ADD, Token.SUB):
|
2016-12-03 13:21:03 +01:00
|
|
|
prec = 13
|
2016-12-07 07:28:09 +01:00
|
|
|
elif name in (Token.RSHIFT, Token.LSHIFT, Token.URSHIFT):
|
2016-12-03 13:21:03 +01:00
|
|
|
prec = 12
|
2016-12-07 07:28:09 +01:00
|
|
|
elif name is Token.BAND:
|
2016-12-03 13:21:03 +01:00
|
|
|
prec = 9
|
2016-12-07 07:28:09 +01:00
|
|
|
elif name is Token.BXOR:
|
2016-12-03 13:21:03 +01:00
|
|
|
prec = 8
|
2016-12-07 07:28:09 +01:00
|
|
|
elif name is Token.BOR:
|
2016-12-03 13:21:03 +01:00
|
|
|
prec = 7
|
|
|
|
else:
|
|
|
|
raise ExtractorError('Unexpected operator at %d' % peek_pos)
|
2016-12-07 07:28:09 +01:00
|
|
|
elif peek_id is Token.LOP:
|
2016-12-03 13:21:03 +01:00
|
|
|
name, op = peek_value
|
2016-12-07 07:28:09 +01:00
|
|
|
prec = {Token.OR: 5, Token.AND: 6}[name]
|
2016-12-03 13:21:03 +01:00
|
|
|
else:
|
|
|
|
has_another = False
|
2016-12-06 18:42:59 +01:00
|
|
|
prec = 4 # empties stack
|
2016-12-03 13:21:03 +01:00
|
|
|
|
2016-12-06 18:42:59 +01:00
|
|
|
while stack and stack[-1][0] >= prec:
|
2016-12-05 11:44:32 +01:00
|
|
|
_, stack_id, stack_op = stack.pop()
|
|
|
|
out.append((stack_id, stack_op))
|
2016-12-03 13:21:03 +01:00
|
|
|
if has_another:
|
2016-12-05 11:44:32 +01:00
|
|
|
stack.append((prec, peek_id, op))
|
2016-12-03 13:21:03 +01:00
|
|
|
token_stream.pop()
|
|
|
|
|
2016-12-07 08:25:19 +01:00
|
|
|
return (Token.OPEXPR, out)
|
2016-12-03 13:21:03 +01:00
|
|
|
|
2016-12-10 02:01:19 +01:00
|
|
|
def interpret_statement(self, stmt):
|
2016-12-06 18:42:59 +01:00
|
|
|
if stmt is None:
|
2016-12-10 02:01:19 +01:00
|
|
|
return None
|
2016-12-06 18:42:59 +01:00
|
|
|
|
|
|
|
name = stmt[0]
|
|
|
|
ref = None
|
|
|
|
if name == 'funcdecl':
|
|
|
|
# TODO interpret funcdecl
|
|
|
|
raise ExtractorError('''Can't interpret statement called %s''' % name)
|
2016-12-07 08:25:19 +01:00
|
|
|
elif name is Token.BLOCK:
|
2016-12-06 18:42:59 +01:00
|
|
|
block = stmt[1]
|
|
|
|
for stmt in block:
|
2016-12-10 02:01:19 +01:00
|
|
|
s = self.interpret_statement(stmt)
|
2016-12-06 18:42:59 +01:00
|
|
|
if s is not None:
|
2016-12-10 14:36:32 +01:00
|
|
|
ref = s.getvalue()
|
2016-12-07 08:25:19 +01:00
|
|
|
elif name is Token.VAR:
|
2016-12-06 18:42:59 +01:00
|
|
|
for name, value in stmt[1]:
|
2016-12-12 22:56:07 +01:00
|
|
|
self._context.local_vars[name] = Reference(self.interpret_expression(value).getvalue(),
|
|
|
|
(self._context.local_vars, name))
|
2016-12-07 08:25:19 +01:00
|
|
|
elif name is Token.EXPR:
|
2016-12-06 18:42:59 +01:00
|
|
|
for expr in stmt[1]:
|
2016-12-10 02:01:19 +01:00
|
|
|
ref = self.interpret_expression(expr)
|
2016-12-06 18:42:59 +01:00
|
|
|
# if
|
|
|
|
# continue, break
|
2016-12-07 08:25:19 +01:00
|
|
|
elif name is Token.RETURN:
|
2016-12-10 02:01:19 +01:00
|
|
|
ref = self.interpret_statement(stmt[1])
|
2016-12-10 14:36:32 +01:00
|
|
|
ref = None if ref is None else ref.getvalue()
|
2016-12-07 19:41:06 +01:00
|
|
|
if isinstance(ref, list):
|
2016-12-09 23:38:48 +01:00
|
|
|
# TODO test nested arrays
|
2016-12-10 14:36:32 +01:00
|
|
|
ref = [elem.getvalue() for elem in ref]
|
2016-12-07 19:41:06 +01:00
|
|
|
|
2016-12-12 22:56:07 +01:00
|
|
|
self._context.ended = True
|
2016-12-06 18:42:59 +01:00
|
|
|
# with
|
|
|
|
# label
|
|
|
|
# switch
|
|
|
|
# throw
|
|
|
|
# try
|
|
|
|
# debugger
|
|
|
|
else:
|
|
|
|
raise ExtractorError('''Can't interpret statement called %s''' % name)
|
2016-12-10 02:01:19 +01:00
|
|
|
return ref
|
2016-12-06 18:42:59 +01:00
|
|
|
|
2016-12-10 02:01:19 +01:00
|
|
|
def interpret_expression(self, expr):
|
2016-12-09 23:38:48 +01:00
|
|
|
if expr is None:
|
|
|
|
return
|
2016-12-06 18:42:59 +01:00
|
|
|
name = expr[0]
|
2016-12-09 23:38:48 +01:00
|
|
|
|
2016-12-07 08:25:19 +01:00
|
|
|
if name is Token.ASSIGN:
|
2016-12-06 18:42:59 +01:00
|
|
|
op, left, right = expr[1:]
|
|
|
|
if op is None:
|
2016-12-10 02:01:19 +01:00
|
|
|
ref = self.interpret_expression(left)
|
2016-12-06 18:42:59 +01:00
|
|
|
else:
|
|
|
|
# TODO handle undeclared variables (create propery)
|
2016-12-10 02:01:19 +01:00
|
|
|
leftref = self.interpret_expression(left)
|
2016-12-10 14:36:32 +01:00
|
|
|
leftvalue = leftref.getvalue()
|
|
|
|
rightvalue = self.interpret_expression(right).getvalue()
|
|
|
|
leftref.putvalue(op(leftvalue, rightvalue))
|
2016-12-11 09:42:43 +01:00
|
|
|
# XXX check specs what to return
|
2016-12-09 23:38:48 +01:00
|
|
|
ref = leftref
|
|
|
|
|
2016-12-07 19:41:06 +01:00
|
|
|
elif name is Token.EXPR:
|
2016-12-10 02:01:19 +01:00
|
|
|
ref = self.interpret_statement(expr)
|
2016-12-09 23:38:48 +01:00
|
|
|
|
2016-12-07 08:25:19 +01:00
|
|
|
elif name is Token.OPEXPR:
|
2016-12-06 18:42:59 +01:00
|
|
|
stack = []
|
2016-12-07 19:41:06 +01:00
|
|
|
rpn = expr[1][:]
|
2016-12-06 18:42:59 +01:00
|
|
|
while rpn:
|
|
|
|
token = rpn.pop(0)
|
2016-12-07 07:28:09 +01:00
|
|
|
if token[0] in (Token.OP, Token.AOP, Token.UOP, Token.LOP, Token.REL):
|
2016-12-06 18:42:59 +01:00
|
|
|
right = stack.pop()
|
|
|
|
left = stack.pop()
|
2016-12-10 14:36:32 +01:00
|
|
|
stack.append(Reference(token[1](left.getvalue(), right.getvalue())))
|
2016-12-07 07:28:09 +01:00
|
|
|
elif token[0] is Token.UOP:
|
2016-12-06 18:42:59 +01:00
|
|
|
right = stack.pop()
|
2016-12-10 14:36:32 +01:00
|
|
|
stack.append(token[1](right.getvalue()))
|
2016-12-06 18:42:59 +01:00
|
|
|
else:
|
2016-12-10 02:01:19 +01:00
|
|
|
stack.append(self.interpret_expression(token))
|
2016-12-06 18:42:59 +01:00
|
|
|
result = stack.pop()
|
|
|
|
if not stack:
|
2016-12-07 21:03:57 +01:00
|
|
|
ref = result
|
2016-12-06 18:42:59 +01:00
|
|
|
else:
|
|
|
|
raise ExtractorError('Expression has too many values')
|
|
|
|
|
2016-12-07 08:25:19 +01:00
|
|
|
elif name is Token.MEMBER:
|
2016-12-06 18:42:59 +01:00
|
|
|
# TODO interpret member
|
|
|
|
target, args, tail = expr[1:]
|
2016-12-10 02:01:19 +01:00
|
|
|
target = self.interpret_expression(target)
|
2016-12-11 09:40:43 +01:00
|
|
|
if args is not None:
|
|
|
|
# TODO interpret NewExpression
|
|
|
|
pass
|
2016-12-06 18:42:59 +01:00
|
|
|
while tail is not None:
|
|
|
|
tail_name, tail_value, tail = tail
|
2016-12-07 08:25:19 +01:00
|
|
|
if tail_name is Token.FIELD:
|
2016-12-06 18:42:59 +01:00
|
|
|
# TODO interpret field
|
|
|
|
raise ExtractorError('''Can't interpret expression called %s''' % tail_name)
|
2016-12-07 08:25:19 +01:00
|
|
|
elif tail_name is Token.ELEM:
|
2016-12-10 14:36:32 +01:00
|
|
|
index = self.interpret_statement(tail_value).getvalue()
|
|
|
|
target = target.getvalue()[index]
|
2016-12-07 08:25:19 +01:00
|
|
|
elif tail_name is Token.CALL:
|
2016-12-06 18:42:59 +01:00
|
|
|
# TODO interpret call
|
|
|
|
raise ExtractorError('''Can't interpret expression called %s''' % tail_name)
|
2016-12-09 23:38:48 +01:00
|
|
|
ref = target
|
2016-12-06 18:42:59 +01:00
|
|
|
|
2016-12-09 23:38:48 +01:00
|
|
|
elif name is Token.ID:
|
2016-12-11 09:42:43 +01:00
|
|
|
# XXX error handling (unknown id)
|
2016-12-12 22:56:07 +01:00
|
|
|
ref = self._context.local_vars[expr[1]] if expr[1] in self._context.local_vars else self.global_vars[expr[1]]
|
2016-12-10 14:36:32 +01:00
|
|
|
|
2016-12-09 23:38:48 +01:00
|
|
|
# literal
|
2016-12-10 14:36:32 +01:00
|
|
|
elif name in _token_keys:
|
|
|
|
ref = Reference(expr[1])
|
|
|
|
|
|
|
|
elif name is Token.ARRAY:
|
|
|
|
array = []
|
|
|
|
for key, elem in enumerate(expr[1]):
|
|
|
|
value = self.interpret_expression(elem)
|
|
|
|
value._parent = array, key
|
|
|
|
array.append(value)
|
|
|
|
ref = Reference(array)
|
|
|
|
|
2016-12-06 18:42:59 +01:00
|
|
|
else:
|
|
|
|
raise ExtractorError('''Can't interpret expression called %s''' % name)
|
2014-03-30 07:02:58 +02:00
|
|
|
|
2016-12-07 21:03:57 +01:00
|
|
|
return ref
|
|
|
|
|
2014-07-15 22:46:39 +02:00
|
|
|
def extract_object(self, objname):
|
|
|
|
obj = {}
|
|
|
|
obj_m = re.search(
|
|
|
|
(r'(?:var\s+)?%s\s*=\s*\{' % re.escape(objname)) +
|
2015-11-24 07:45:02 +01:00
|
|
|
r'\s*(?P<fields>([a-zA-Z$0-9]+\s*:\s*function\(.*?\)\s*\{.*?\}(?:,\s*)?)*)' +
|
2014-07-15 22:46:39 +02:00
|
|
|
r'\}\s*;',
|
|
|
|
self.code)
|
|
|
|
fields = obj_m.group('fields')
|
|
|
|
# Currently, it only supports function definitions
|
|
|
|
fields_m = re.finditer(
|
2014-07-23 02:13:48 +02:00
|
|
|
r'(?P<key>[a-zA-Z$0-9]+)\s*:\s*function'
|
2014-07-15 22:46:39 +02:00
|
|
|
r'\((?P<args>[a-z,]+)\){(?P<code>[^}]+)}',
|
|
|
|
fields)
|
|
|
|
for f in fields_m:
|
|
|
|
argnames = f.group('args').split(',')
|
|
|
|
obj[f.group('key')] = self.build_function(argnames, f.group('code'))
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
2014-03-30 07:02:58 +02:00
|
|
|
def extract_function(self, funcname):
|
|
|
|
func_m = re.search(
|
2015-02-01 22:38:26 +01:00
|
|
|
r'''(?x)
|
2016-06-23 09:41:34 +07:00
|
|
|
(?:function\s+%s|[{;,]\s*%s\s*=\s*function|var\s+%s\s*=\s*function)\s*
|
2015-02-01 22:38:26 +01:00
|
|
|
\((?P<args>[^)]*)\)\s*
|
|
|
|
\{(?P<code>[^}]+)\}''' % (
|
2015-11-10 12:54:02 +08:00
|
|
|
re.escape(funcname), re.escape(funcname), re.escape(funcname)),
|
2014-03-30 07:02:58 +02:00
|
|
|
self.code)
|
2014-03-30 07:15:14 +02:00
|
|
|
if func_m is None:
|
|
|
|
raise ExtractorError('Could not find JS function %r' % funcname)
|
2014-03-30 07:02:58 +02:00
|
|
|
argnames = func_m.group('args').split(',')
|
|
|
|
|
2014-07-15 22:46:39 +02:00
|
|
|
return self.build_function(argnames, func_m.group('code'))
|
|
|
|
|
2016-12-10 02:01:19 +01:00
|
|
|
def push_context(self, cx):
|
2016-12-12 22:56:07 +01:00
|
|
|
self._context_stack.append(self._context)
|
|
|
|
self._context = cx
|
2016-12-10 02:01:19 +01:00
|
|
|
|
|
|
|
def pop_context(self):
|
2016-12-11 09:42:43 +01:00
|
|
|
# XXX check underflow
|
2016-12-12 22:56:07 +01:00
|
|
|
self._context = self._context_stack.pop()
|
2016-12-10 02:01:19 +01:00
|
|
|
|
2015-02-01 22:38:26 +01:00
|
|
|
def call_function(self, funcname, *args):
|
|
|
|
f = self.extract_function(funcname)
|
|
|
|
return f(args)
|
|
|
|
|
2014-07-15 22:46:39 +02:00
|
|
|
def build_function(self, argnames, code):
|
2014-03-30 07:02:58 +02:00
|
|
|
def resf(args):
|
2016-12-10 02:01:19 +01:00
|
|
|
self.push_context(Context(dict(zip(argnames, args))))
|
2016-12-03 06:32:11 +01:00
|
|
|
for stmt in self.statements(code):
|
2016-12-10 02:01:19 +01:00
|
|
|
res = self.interpret_statement(stmt)
|
2016-12-12 22:56:07 +01:00
|
|
|
if self._context.ended:
|
2016-12-10 02:01:19 +01:00
|
|
|
self.pop_context()
|
2016-12-06 18:42:59 +01:00
|
|
|
break
|
2016-12-09 23:38:48 +01:00
|
|
|
return res
|
2016-12-03 13:21:03 +01:00
|
|
|
return resf
|