forked from andrewcooke/ParserCombinator.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsers.jl
221 lines (172 loc) · 5.82 KB
/
parsers.jl
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# the main loop for the trampoline is parameterised over the Config
# type. this lets us implement different semantics by simply changing
# the provided Config subtype (and implementing the appropriate
# dispatch() functions, etc).
# this works for most configs
parent(k::Config) = k.stack[end][1]
# evaluation without a cache (memoization)
# mutable struct NoCache{S,I}<:Config{S,I}
mutable struct NoCache{S, I}<:Config{S,I}
source::S
stack::Vector{Tuple{Matcher, State}}
NoCache{S, I}(source::S; kargs...) where {S, I} = new{S, I}(source, Vector{Tuple{Matcher,State}}())
end
function dispatch(k::NoCache, e::Execute)
push!(k.stack, (e.parent, e.parent_state))
try
execute(k, e.child, e.child_state, e.iter)
catch x
isa(x, FailureException) ? FAILURE : rethrow()
end
end
function dispatch(k::NoCache, s::Success)
(parent, parent_state) = pop!(k.stack)
try
return success(k, parent, parent_state, s.child_state, s.iter, s.result)
catch x
isa(x, FailureException) ? FAILURE : rethrow()
end
end
function dispatch(k::NoCache, f::Failure)
(parent, parent_state) = pop!(k.stack)
try
return failure(k, parent, parent_state)
catch x
isa(x, FailureException) ? FAILURE : rethrow()
end
end
# evaluation with a complete cache (all intermediate results memoized)
const Key{I} = Tuple{Matcher,State,I}
mutable struct Cache{S,I}<:Config{S,I}
source::S
stack::Vector{Tuple{Matcher,State,Key{I}}}
cache::Dict{Key{I},Message}
Cache{S, I}(source::S; kargs...) where {I, S} = new{S, I}(source, Vector{Tuple{Matcher,State,Key{I}}}(), Dict{Key{I},Message}())
end
function dispatch(k::Cache, e::Execute)
key = (e.child, e.child_state, e.iter)
push!(k.stack, (e.parent, e.parent_state, key))
if haskey(k.cache, key)
k.cache[key]
else
try
execute(k, e.child, e.child_state, e.iter)
catch x
isa(x, FailureException) ? FAILURE : rethrow()
end
end
end
function dispatch(k::Cache, s::Success)
parent, parent_state, key = pop!(k.stack)
try
k.cache[key] = s
catch x
isa(x, CacheException) ? nothing : rethrow()
end
try
success(k, parent, parent_state, s.child_state, s.iter, s.result)
catch x
isa(x, FailureException) ? FAILURE : rethrow()
end
end
function dispatch(k::Cache, f::Failure)
parent, parent_state, key = pop!(k.stack)
try
k.cache[key] = f
catch x
isa(x, CacheException) ? nothing : rethrow()
end
try
failure(k, parent, parent_state)
catch x
isa(x, FailureException) ? FAILURE : rethrow()
end
end
# TODO - some kind of MRU cache?
# a dummy matcher used by the parser
mutable struct Root<:Delegate
name::Symbol
Root() = new(:Root)
end
struct RootState<:DelegateState
state::State
end
success(k::Config, m::Root, s::State, t::State, i, r::Value) = Success(RootState(t), i, r)
failure(k::Config, m::Root, s::State) = FAILURE
# the core loop that drives the parser, calling the appropriate dispatch
# functions (above) depending on which Config was used.
# to modify the behaviour you can create a new Config subtype and then
# add your own dispatch functions.
function producer(c::Channel, k::Config, m::Matcher; debug=false)
root = Root()
msg::Message = Execute(root, CLEAN, m, CLEAN, firstindex(k.source))
try
while true
msg = dispatch(k, msg)
if isempty(k.stack)
if isa(msg, Execute)
error("Unexpected execute message")
elseif isa(msg, Success)
put!(c, msg.result)
# my head hurts
msg = Execute(root, CLEAN, m,
msg.child_state.state,
firstindex(k.source))
else
break
end
end
end
catch x
if (debug)
println("debug was set, so showing error from inside task")
println(x)
Base.show_backtrace(stdout, catch_backtrace())
end
rethrow(x)
end
end
# helper functions to generate the parsers from the above
# these assume that any config construct takes a single source argument
# plus optional keyword args
function make(config, source::S, matcher; debug=false, kargs...) where {S}
I = typeof(firstindex(source))
k = config{S,I}(source; debug=debug, kargs...)
(k, Channel(c -> producer(c, k, matcher; debug=debug)))
end
function make_all(config; kargs_make...)
function multiple_results(source, matcher::Matcher; kargs_parse...)
make(config, source, matcher; kargs_make..., kargs_parse...)[2]
end
end
function once(channel)
try
for result in channel
return result
end
catch ex
# unwrap exception
if VERSION >= v"1.1"
ex isa TaskFailedException ? throw(first(first(Base.catch_stack(ex.task)))) : rethrow()
else
rethrow()
end
end
throw(ParserException("cannot parse"))
end
function make_one(config; kargs_make...)
function single_result(source, matcher::Matcher; kargs_parse...)
once(make(config, source, matcher; kargs_make..., kargs_parse...)[2])
end
end
# the default parsers
# we use the cache only when matching multiple results but obviously motivated
# users are free to construct their own...
parse_all_cache = make_all(Cache)
parse_all_nocache = make_all(NoCache)
parse_all = parse_all_cache
parse_one_cache = make_one(Cache)
parse_one_nocache = make_one(NoCache)
parse_one = parse_one_nocache
parse_lines(source, matcher; kargs...) = parse_one(LineSource(source), matcher; kargs...)
parse_lines_cache(source, matcher; kargs...) = parse_one_cache(LineSource(source), matcher; kargs...)