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

How to make an identifier NOT a keyword (dgrammar wrongly rejects keyword as prefix) #147

Open
timotheecour opened this issue Jan 26, 2015 · 5 comments

Comments

@timotheecour
Copy link

in pegged/examples/dgrammar.d there is:
Identifier <~ !Keyword [a-zA-Z_] [a-zA-Z0-9_]*
Keyword < "abstract" / "alias" ...

This rules out valid identifiers that use a keyword as a prefix (eg: int abstract_val=0), so this is incorrect.
Is there a better way?

Could the following be made to work? (where '\b' represents word break)

Identifier <~ !(Keyword '\b') [a-zA-Z_] [a-zA-Z0-9_]*
Keyword < "abstract" / "alias" ...

Could we generalize this to specify custom 'word breaks' using arbitrary regular expressions?

@timotheecour
Copy link
Author

Answering my own question:

Identifier <~ !(Keyword WordBreak) [a-zA-Z_] [a-zA-Z0-9_]*
WordBreak <- ![a-zA-Z0-9_]
Keyword < "abstract" / "alias" ...

seems to work.

perhaps dgrammar could be updated with this?

@PhilippeSigaud
Copy link
Collaborator

Or !(Keyword Spacing) maybe?

I admit not using Pegged for quite some time. Does dgrammar generate OK? At
some time it was far too big for the CTFE engine.

On Mon, Jan 26, 2015 at 7:23 AM, Timothee Cour [email protected]
wrote:

Answering my own question:

Identifier <~ !(Keyword WordBreak) [a-zA-Z_] [a-zA-Z0-9_]*
WordBreak <- ![a-zA-Z0-9_]
Keyword < "abstract" / "alias" ...

seems to work.

perhaps dgrammar could be updated with this?


Reply to this email directly or view it on GitHub
#147 (comment)
.

@timotheecour
Copy link
Author

haven't tried rencently, but it'd be useful to fix grammars for correctness
btw what's the difference bw 'foo' and "foo" ?

@PhilippeSigaud
Copy link
Collaborator

On Wed, Jan 28, 2015 at 10:57 PM, Timothee Cour [email protected]
wrote:

haven't tried rencently, but it'd be useful to fix grammars for
correctness
btw what's the difference bw 'foo' and "foo" ?

None at all. The creator of PEGs needed two ways to create terminals, to be
able to define both " and ' as possible terminals (that is, to define Rule
<- '"' and Rule <- "'").

@veelo
Copy link
Collaborator

veelo commented Mar 6, 2016

Is there a better way?

I think there is: You can use semantic actions to filter out identifiers that are identical to keywords as I describe in #190 (comment). The actions can of course be defined in their own module that is included through the header instead of being defined in the header string.

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