Js2Py icon indicating copy to clipboard operation
Js2Py copied to clipboard

JavaScript starting with dictionary results in SyntaxError

Open LMuter opened this issue 3 years ago • 0 comments

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!

LMuter avatar Feb 09 '22 11:02 LMuter