PiotrDabkowski / Js2Py

JavaScript to Python Translator & JavaScript interpreter written in 100% pure Python🚀 Try it online:
http://piter.io/projects/js2py
MIT License
2.47k stars 261 forks source link

JavaScript starting with dictionary results in SyntaxError #278

Open LMuter opened 2 years ago

LMuter commented 2 years ago

I try to run a Zotero translator in python, which results in a syntax error:

>>> js2py.translate_file('BibTeX.js', 'BibTeX.py')
File "/home/.../python3.8/site-packages/pyjsparser/parser.py", line 1120, in consumeSemicolon
    self.throwUnexpectedToken(self.lookahead)
File "/home/.../python3.8/site-packages/pyjsparser/parser.py", line 1046, in throwUnexpectedToken 
    raise self.unexpectedTokenError(token, message) js2py.internals.simplex.JsException: SyntaxError: Line 3: Unexpected token :

Cause: js-file starts with a data dictionary:

{
    "translatorID": "9cb70025-a888-4a29-a210-93ec52da40d4",
    "label": "BibTeX",
        [...]
}

Workaround: read the js-file and conduct a re.sub:

import re
re_str = re.compile(r"^\s*\{([^()]|(R))*\}") 
with open("BibTeX.js") as f:
    js_str = f.read()
    re_sub = re.sub(re_str, "", js_str)
js2py.eval_js(re_sub) # All works perfectly!