Reserved Keywords ? #105
Replies: 2 comments
-
Hi, def identifier (): return Not(reserved_keywords), _(r'...') Another approach is to implement your own parsing expression classes. First approach is probably what you want. Second is an interesting exercise in making custom parsing expressions. :) |
Beta Was this translation helpful? Give feedback.
-
Hi, Thank you for your quick reply ! I was actually looking for a solution to handle keywords of the grammar more dynamically. The following code will do the job for me: from arpeggio import StrMatch, Not
from arpeggio import RegExMatch as _
class Kwd (StrMatch):
_keyword_table = []
def __init__ (self, to_match):
super (Kwd, self).__init__ (to_match)
self.to_match = to_match
self.root = True
self.rule_name = 'keyword'
if to_match not in Kwd._keyword_table:
Kwd._keyword_table.append (to_match)
def reserved_keywords (): return _(r'(\b({})\b)'.format ('|'.join (Kwd._keyword_table)))
def identifier (): return Not (reserved_keywords), _(r'([a-zA-Z]\w*)')
# ... |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, first of all, thanks for this library !
Sorry if I'm missing something obvious, but I'm wondering how I can simply handle reserved keywords in my grammar with Arpeggio.
Suppose that I have some reserved keywords in my grammar, like
class
orfunction
, and I don't want them to be recognized as valididentifier
:The code above will parse the text
'class class { }'
without errors, because the second wordclass
match the ruleclass_name
:For now, I'm using the following workaround that excludes keywords from the
identifier
regex:It works as I expected:
But is there something more automatic in Arpeggio to achieve that same purpose ? I'm thinking of something like the Keyword class in PyPEG that internally maintains a list of keywords used in the grammar.
Thanks !
Beta Was this translation helpful? Give feedback.
All reactions