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

Literals should be valid token types #32

Closed
Naddiseo opened this issue May 3, 2013 · 3 comments
Closed

Literals should be valid token types #32

Naddiseo opened this issue May 3, 2013 · 3 comments

Comments

@Naddiseo
Copy link

Naddiseo commented May 3, 2013

I have a ply.lex class in which I've defined "{" and "}" as literals via the "literals" attribute, but I also want to attach an action to these tokens to match nesting.

I thought I would be able to do it as follows, but it complains about an unknown token type. Am I doing this correctly? I'd also like to avoid having a named token for something that really is a literal.

class Lexer(object):
    def __init__(self):
        self.lexer = ply.lex.lex(module=self)
        self.nesting = 0

    literals = [ '{', '}' ]

    def t_LBRACE(self, t):
        r'\{'
        self.nesting += 1
        t.type = '{'
        return t

    def t_RBRACE(self, t):
        r'\}'
        self.nesting += 1
        t.type = '}'
        return t

I'm not sure if this is intended design, or an oversight, so I thought I'd report it.

@dabeaz
Copy link
Owner

dabeaz commented May 3, 2013

Honestly, this use case has never come up before, but it's interesting. To support it, there would have to be some way to map the rule names LBRACE and RBRACE back to the literals set. Let me think about it.

@blastrock
Copy link

+1
I would have liked that feature

@dabeaz
Copy link
Owner

dabeaz commented Apr 20, 2015

Fixed in PLY-3.5.

@dabeaz dabeaz closed this as completed Apr 20, 2015
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

3 participants