-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_patterns.py
158 lines (118 loc) · 3.78 KB
/
model_patterns.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"Parser combinators for pattern-matching Hy model trees."
from collections import namedtuple
from functools import reduce
from itertools import repeat
from math import isinf
from operator import add
from funcparserlib.parser import (
NoParseError,
Parser,
State,
a,
finished,
many,
skip,
some,
)
from hy.models import (
Bytes,
Complex,
Dict,
Expression,
Float,
Integer,
Keyword,
List,
String,
Symbol,
Tuple,
)
FORM = some(lambda _: True)
SYM = some(lambda x: isinstance(x, Symbol))
KEYWORD = some(lambda x: isinstance(x, Keyword))
STR = some(lambda x: isinstance(x, String)) # matches literal strings only!
LITERAL = some(lambda x: isinstance(x, (String, Integer, Float, Complex, Bytes)))
def sym(wanted):
"Parse and skip the given symbol or keyword."
return _sym(wanted, skip)
def keepsym(wanted):
"Parse the given symbol or keyword."
return _sym(wanted)
def _sym(wanted, f=lambda x: x):
if wanted.startswith(":"):
return f(a(Keyword(wanted[1:])))
return f(some(lambda x: x == Symbol(wanted)))
def whole(parsers):
"""Parse the parsers in the given list one after another, then
expect the end of the input."""
if len(parsers) == 0:
return finished >> (lambda x: [])
if len(parsers) == 1:
return parsers[0] + finished >> (lambda x: x[:-1])
return reduce(add, parsers) + skip(finished)
def _grouped(group_type, parsers):
return some(lambda x: isinstance(x, group_type)) >> (
lambda x: group_type(whole(parsers).parse(x)).replace(x, recursive=False)
)
def brackets(*parsers):
"Parse the given parsers inside square brackets."
return _grouped(List, parsers)
def in_tuple(*parsers):
return _grouped(Tuple, parsers)
def braces(*parsers):
"Parse the given parsers inside curly braces"
return _grouped(Dict, parsers)
def pexpr(*parsers):
"Parse the given parsers inside a parenthesized expression."
return _grouped(Expression, parsers)
def dolike(head):
"Parse a `do`-like form."
return pexpr(sym(head), many(FORM))
def notpexpr(*disallowed_heads):
"""Parse any object other than a hy.models.Expression beginning with a
hy.models.Symbol equal to one of the disallowed_heads."""
disallowed_heads = list(map(Symbol, disallowed_heads))
return some(
lambda x: not (isinstance(x, Expression) and x and x[0] in disallowed_heads)
)
def unpack(kind):
"Parse an unpacking form, returning it unchanged."
return some(
lambda x: isinstance(x, Expression)
and len(x) > 0
and x[0] == Symbol("unpack-" + kind)
)
def times(lo, hi, parser):
"""Parse `parser` several times (`lo` to `hi`) in a row. `hi` can be
float('inf'). The result is a list no matter the number of instances."""
@Parser
def f(tokens, s):
result = []
for _ in range(lo):
(v, s) = parser.run(tokens, s)
result.append(v)
end = s.max
try:
for _ in repeat(1) if isinf(hi) else range(hi - lo):
(v, s) = parser.run(tokens, s)
result.append(v)
except NoParseError as e:
end = e.state.max
return result, State(s.pos, end)
return f
Tag = namedtuple("Tag", ["tag", "value"])
def tag(tag_name, parser):
"""Matches the given parser and produces a named tuple `(Tag tag value)`
with `tag` set to the given tag name and `value` set to the parser's
value."""
return parser >> (lambda x: Tag(tag_name, x))
def parse_if(pred, parser):
"""
Return a parser that parses a token with `parser` if it satisfies the
predicate `pred`.
"""
@Parser
def _parse_if(tokens, s):
some(pred).run(tokens, s)
return parser.run(tokens, s)
return _parse_if