Problem parsing Javascript containing backtick with js2xml

62 views Asked by At

I'm using Python/js2xml to parse JavaScript from a webpage but I'm getting an error message due to the inclusion of backticks in the JavaScript for example...

htmlBlock = `<option disabled selected value="0">Select lesson...</option>`;

...is raising the following error...

Traceback (most recent call last):
  File "Z:\course_to_course_pkl.py", line 71, in <module>
    course_admin_parsed = js2xml.parse(course_admin_js) # js_course_structure.txt
  File "C:\Program Files\Python311\Lib\site-packages\js2xml\__init__.py", line 17, in parse
    tree = _parser.parse(text, debug=debug)
  File "C:\Program Files\Python311\Lib\site-packages\calmjs\parse\parsers\es5.py", line 115, in parse
    return self.parser.parse(
  File "C:\Program Files\Python311\Lib\site-packages\ply\yacc.py", line 331, in parse
    return self.parseopt(input, lexer, debug, tracking, tokenfunc)
  File "C:\Program Files\Python311\Lib\site-packages\ply\yacc.py", line 757, in parseopt
    lookahead = get_token()     # Get the next token
  File "C:\Program Files\Python311\Lib\site-packages\calmjs\parse\lexers\es5.py", line 290, in _token
    tok = self._get_update_token()
  File "C:\Program Files\Python311\Lib\site-packages\calmjs\parse\lexers\es5.py", line 355, in _get_update_token
    self._set_tokens(self.get_lexer_token())
  File "C:\Program Files\Python311\Lib\site-packages\calmjs\parse\lexers\es5.py", line 242, in get_lexer_token
    token = self.lexer.token()
  File "C:\Program Files\Python311\Lib\site-packages\ply\lex.py", line 386, in token
    newtok = self.lexerrorf(tok)
  File "C:\Program Files\Python311\Lib\site-packages\calmjs\parse\lexers\es5.py", line 687, in t_error
    raise ECMASyntaxError(
calmjs.parse.exceptions.ECMASyntaxError: Illegal character '`' at 1299:23 after '=' at 1299:21

I can't seem to find a solution - I've tried looking through the calmjs package to see if I can simply add the backtick in the list of recognised characters but it's beyond me.

1

There are 1 answers

4
BadPiggie On

As @InSync mentioned in the comment, The error comes from calmjs.parse method which only supports ES5. But Template Literals is an ES6 feature.

https://github.com/calmjs/calmjs.parse

You have to replace backticks with double or single quote.

htmlBlock = "<option disabled selected value=\"0\">Select lesson...</option>";