mirror of
https://github.com/ytdl-org/youtube-dl
synced 2024-12-22 20:20:09 +09:00
[jsinterp] Strip /* comments */ when parsing
* NB: _separate() is looking creaky
This commit is contained in:
parent
7ab85d88d6
commit
067ad7439c
@ -160,7 +160,6 @@ class TestJSInterpreter(unittest.TestCase):
|
|||||||
self._test('function f(){var x = 20; x += 30 + 1; return x;}', 51)
|
self._test('function f(){var x = 20; x += 30 + 1; return x;}', 51)
|
||||||
self._test('function f(){var x = 20; x -= 30 + 1; return x;}', -11)
|
self._test('function f(){var x = 20; x -= 30 + 1; return x;}', -11)
|
||||||
|
|
||||||
@unittest.skip('Not yet fully implemented')
|
|
||||||
def test_comments(self):
|
def test_comments(self):
|
||||||
self._test('''
|
self._test('''
|
||||||
function f() {
|
function f() {
|
||||||
@ -179,6 +178,15 @@ class TestJSInterpreter(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
''', 3)
|
''', 3)
|
||||||
|
|
||||||
|
self._test('''
|
||||||
|
function f() {
|
||||||
|
var x = ( /* 1 + */ 2 +
|
||||||
|
/* 30 * 40 */
|
||||||
|
50);
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
''', 52)
|
||||||
|
|
||||||
def test_precedence(self):
|
def test_precedence(self):
|
||||||
self._test('''
|
self._test('''
|
||||||
function f() {
|
function f() {
|
||||||
|
@ -488,9 +488,18 @@ class JSInterpreter(object):
|
|||||||
skipping = 0
|
skipping = 0
|
||||||
if skip_delims:
|
if skip_delims:
|
||||||
skip_delims = variadic(skip_delims)
|
skip_delims = variadic(skip_delims)
|
||||||
|
skip_txt = None
|
||||||
for idx, char in enumerate(expr):
|
for idx, char in enumerate(expr):
|
||||||
|
if skip_txt and idx <= skip_txt[1]:
|
||||||
|
continue
|
||||||
paren_delta = 0
|
paren_delta = 0
|
||||||
if not in_quote:
|
if not in_quote:
|
||||||
|
if char == '/' and expr[idx:idx + 2] == '/*':
|
||||||
|
# skip a comment
|
||||||
|
skip_txt = expr[idx:].find('*/', 2)
|
||||||
|
skip_txt = [idx, idx + skip_txt + 1] if skip_txt >= 2 else None
|
||||||
|
if skip_txt:
|
||||||
|
continue
|
||||||
if char in _MATCHING_PARENS:
|
if char in _MATCHING_PARENS:
|
||||||
counters[_MATCHING_PARENS[char]] += 1
|
counters[_MATCHING_PARENS[char]] += 1
|
||||||
paren_delta = 1
|
paren_delta = 1
|
||||||
@ -523,12 +532,19 @@ class JSInterpreter(object):
|
|||||||
if pos < delim_len:
|
if pos < delim_len:
|
||||||
pos += 1
|
pos += 1
|
||||||
continue
|
continue
|
||||||
yield expr[start: idx - delim_len]
|
if skip_txt and skip_txt[0] >= start and skip_txt[1] <= idx - delim_len:
|
||||||
|
yield expr[start:skip_txt[0]] + expr[skip_txt[1] + 1: idx - delim_len]
|
||||||
|
else:
|
||||||
|
yield expr[start: idx - delim_len]
|
||||||
|
skip_txt = None
|
||||||
start, pos = idx + 1, 0
|
start, pos = idx + 1, 0
|
||||||
splits += 1
|
splits += 1
|
||||||
if max_split and splits >= max_split:
|
if max_split and splits >= max_split:
|
||||||
break
|
break
|
||||||
yield expr[start:]
|
if skip_txt and skip_txt[0] >= start:
|
||||||
|
yield expr[start:skip_txt[0]] + expr[skip_txt[1] + 1:]
|
||||||
|
else:
|
||||||
|
yield expr[start:]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _separate_at_paren(cls, expr, delim=None):
|
def _separate_at_paren(cls, expr, delim=None):
|
||||||
|
Loading…
Reference in New Issue
Block a user