Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception parsing object with class as key #31

Closed
andrewbaxter opened this issue Feb 6, 2018 · 4 comments · Fixed by #33
Closed

Exception parsing object with class as key #31

andrewbaxter opened this issue Feb 6, 2018 · 4 comments · Fixed by #33

Comments

@andrewbaxter
Copy link

import js2xml
js2xml.parse('var x = {class: 4};')

outputs:

Traceback (most recent call last):
  File "a.py", line 2, in <module>
    js2xml.parse('var x = {class: 4};')
  File "/.../lib/python3.6/site-packages/js2xml/__init__.py", line 17, in parse
    tree = _parser.parse(text, debug=debug)
  File "/.../lib/python3.6/site-packages/js2xml/parser.py", line 36, in parse
    result = super(CustomParser, self).parse(text, debug=debug)
  File "/.../lib/python3.6/site-packages/slimit/parser.py", line 93, in parse
    return self.parser.parse(text, lexer=self.lexer, debug=debug)
  File "/.../lib/python3.6/site-packages/ply/yacc.py", line 331, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/.../lib/python3.6/site-packages/ply/yacc.py", line 1199, in parseopt_notrack
    tok = call_errorfunc(self.errorfunc, errtoken, self)
  File "/.../lib/python3.6/site-packages/ply/yacc.py", line 193, in call_errorfunc
    r = errorfunc(token)
  File "/.../lib/python3.6/site-packages/slimit/parser.py", line 116, in p_error
    self._raise_syntax_error(token)
  File "/.../lib/python3.6/site-packages/slimit/parser.py", line 89, in _raise_syntax_error
    self.lexer.prev_token, self.lexer.token())
SyntaxError: Unexpected token (CLASS, 'class') at 1:9 between LexToken(LBRACE,'{',1,8) and LexToken(COLON,':',1,14)
@metatoaster
Copy link

This is related to the widly reported issue in slimit (first in rspivak/slimit#52, then rspivak/slimit#59, rspivak/slimit#81, rspivak/slimit#90) where slimit fails to parse reserved keywords as bare keys occurring inside objects and as attributes. This was fixed in calmjs.parse

>>> from calmjs.parse import es5
>>> es5('var x = {class: 4};')
<ES5Program @1:1 ?children=[
  <VarStatement @1:1 ?children=[
    <VarDecl @1:5 identifier=<Identifier ...>, initializer=<Object ...>>
  ]>
]>

@Gallaecio
Copy link
Member

@redapple Would moving to calmjs.parse be the right move here?

@redapple
Copy link
Contributor

redapple commented Apr 2, 2019

@Gallaecio , I believe so. If someone wants to give a shot at it. I don't know how easy the XmlVisitor is to rewrite when using calmjs.parse though.

@metatoaster
Copy link

The "visitor" pattern can still be used, however certain class names for the nodes have changed (e.g. Program -> ES5Program), or that new nodes have been introduced. The best way to catch this is to have the generic_visit method in the xmlvisitor class to raise Exception(repr(node)) instead of returning a string and this will show the node that couldn't be handled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants