aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordirkf <fieldhouse@gmx.net>2024-06-20 02:36:54 +0100
committerdirkf <fieldhouse@gmx.net>2024-06-20 20:03:49 +0100
commitfd8242e3efd3c0e2ba9a45c662d6983c00b21d6d (patch)
treeb8366c0a7cc2bd79cdc00fcdf063ea99425fe99f
parentad01fa6ccadd1ecade8002e937492a141d3b8f25 (diff)
[jsinterp] Fix and improve expression parsing
* improve BODMAS (fixes https://github.com/ytdl-org/youtube-dl/issues/32815) * support more weird expressions with multiple unary ops
-rw-r--r--youtube_dl/jsinterp.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/youtube_dl/jsinterp.py b/youtube_dl/jsinterp.py
index e258ebd00..12b71ed6a 100644
--- a/youtube_dl/jsinterp.py
+++ b/youtube_dl/jsinterp.py
@@ -798,18 +798,28 @@ class JSInterpreter(object):
right_expr = separated.pop()
# handle operators that are both unary and binary, minimal BODMAS
if op in ('+', '-'):
+ # simplify/adjust consecutive instances of these operators
undone = 0
while len(separated) > 1 and not separated[-1].strip():
undone += 1
separated.pop()
if op == '-' and undone % 2 != 0:
right_expr = op + right_expr
+ elif op == '+':
+ while len(separated) > 1 and separated[-1].strip() in self.OP_CHARS:
+ right_expr = separated.pop() + right_expr
+ # hanging op at end of left => unary + (strip) or - (push right)
left_val = separated[-1]
for dm_op in ('*', '%', '/', '**'):
bodmas = tuple(self._separate(left_val, dm_op, skip_delims=skip_delim))
if len(bodmas) > 1 and not bodmas[-1].strip():
expr = op.join(separated) + op + right_expr
- right_expr = None
+ if len(separated) > 1:
+ separated.pop()
+ right_expr = op.join((left_val, right_expr))
+ else:
+ separated = [op.join((left_val, right_expr))]
+ right_expr = None
break
if right_expr is None:
continue