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

"AttributeError: Name token redefined" when use loop to set NAME[token] = A #78

Open
alingse opened this issue Sep 4, 2021 · 2 comments

Comments

@alingse
Copy link

alingse commented Sep 4, 2021

i want use sly to parse "thrift" file (see https://github.com/alingse/thrift-parser/blob/master/simple.py )

and want to set several base_type to IDENTIFIER, but got AttributeError

    types = r'bool|byte|i8|i16|i32|i64|double|string|binary|slist'.split('|')
    for token in types:
        IDENTIFIER[token] = BASE_TYPE

see this simplify code https://github.com/alingse/thrift-parser/blob/master/demo3.py#L14-L16

i know this can be rewritten to

IDENTIFIER["bool"] = BASE_TYPE
IDENTIFIER["byte"] = BASE_TYPE
IDENTIFIER["i8"] = BASE_TYPE
IDENTIFIER["i16"] = BASE_TYPE
...

i want to known that is there any simple way to do this ?

like ?

IDENTIFIER[tuple("bool", "byte")] = BASE_TYPE

or is there some way to statement the reversed keywords for all identifier ( IDENTIFIER = r'[a-zA-Z_][a-zA-Z0-9._]*' )

@alingse alingse changed the title "AttributeError: Name token redefined" when use loop to set "AttributeError: Name token redefined" when use loop to set NAME[tokenA] = A Sep 4, 2021
@alingse alingse changed the title "AttributeError: Name token redefined" when use loop to set NAME[tokenA] = A "AttributeError: Name token redefined" when use loop to set NAME[token] = A Sep 4, 2021
@alingse
Copy link
Author

alingse commented Sep 4, 2021

😂 final, i use python script to generate the code

In [3]: for token in REVERSED_KEYWORD_LIST:
   ...:     print("IDENTIFIER['{}'] = REVERSED_KEYWORD".format(token))

🙏

@dabeaz
Copy link
Owner

dabeaz commented Sep 4, 2021

I'm looking at the SLY source and an "easy" workaround doesn't seem possible right now. However, I've been doing a bit of recent work on SLY. I'll look into this further.

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

No branches or pull requests

2 participants