-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcombinator_grammars.py
More file actions
33 lines (27 loc) · 906 Bytes
/
combinator_grammars.py
File metadata and controls
33 lines (27 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
A convenience for defining recursive grammars in the combinator DSL.
The delay() combinator works for this, but code using it is maybe uglier.
"""
import parson as P
class Grammar(object): # XXX call it something else? name clash
def __init__(self):
object.__setattr__(self, '_rules', {})
object.__setattr__(self, '_stubs', {})
def __getattr__(self, name):
try: return self._rules[name]
except KeyError: pass
try: return self._stubs[name]
except KeyError: pass
self._stubs[name] = result = P.delay(lambda: self._rules[name], '<%s>', name)
return result
def __setattr__(self, name, value):
self._rules[name] = value
# Example:
## g = Grammar()
## g.a = 'A' + g.b
## g.b = 'B'
## g.a('AB')
#. ()
# TODO try fancier examples
# TODO investigate implementing via descriptors instead
# TODO nicer error when misused