Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 226 lines (165 sloc) 5.384 kb
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
1 use("core")
2 use("define")
3 use("control-flow")
4 use("dynamic")
5 use("particles")
6 use("comparison")
5c33b0b Alex Suraci add ast macro for defining nodes
authored
7 use("node")
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
8
9 require("kpeg")
e273725 Alex Suraci updates for kpeg 0.10.0
authored
10 require("kpeg/compiled_parser")
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
11 require("set")
12
66c9bfc Alex Suraci refactor pretty kernel to use module functions
authored
13
14 -- lazily load this module to prevent circular dependency
15 pretty() = @pretty ||= require("pretty")
16
17
15b61e4 Alex Suraci use locals for internal grammar kernel bits
authored
18 actions = dynamic
19 setups = dynamic
20 vars = dynamic
21 rule = dynamic
22 rule-vars = dynamic
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
23
416a17f Alex Suraci update kernel/ and lib/ for constant changes
authored
24 const-name(c: Atomy AST Compose) = const-name(c left) + "::" + const-name(c right)
25 const-name(c: Atomy AST Constant) = c name to-s
26 const-name(c: Atomy AST ScopedConstant) = const-name(c parent) + "::" + c name to-s
27 const-name(c: Atomy AST ToplevelConstant) = "::" + c name to-s
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
28
c5c21a5 Alex Suraci improve external invoke/assign in grammar dsl
authored
29 process-val(g, `(%(~grammar)(~name)(~*args))) = do:
a71a9ff Alex Suraci inflexible symbols
authored
30 `(~g foreign-invoke(
31 ~(grammar text)
c5c21a5 Alex Suraci improve external invoke/assign in grammar dsl
authored
32 ~(name text)
66c9bfc Alex Suraci refactor pretty kernel to use module functions
authored
33 ~(args collect .(pretty show(_)) join(", "))))
a71a9ff Alex Suraci inflexible symbols
authored
34
c5c21a5 Alex Suraci improve external invoke/assign in grammar dsl
authored
35 process-val(g, `(%(~grammar)(~name))) = do:
36 `(~g foreign-invoke(~(grammar text), ~(name text)))
a71a9ff Alex Suraci inflexible symbols
authored
37
c5c21a5 Alex Suraci improve external invoke/assign in grammar dsl
authored
38 process-val(g, `((~name)=(~v))) = do:
8d8653b Alex Suraci grammar kernel updates
authored
39 val = process-val(g, v)
309faee Alex Suraci style cleanup
authored
40
41 when(^rule-vars):
42 ^rule-vars << name text
43
8d8653b Alex Suraci grammar kernel updates
authored
44 `(~g t(~val, ~(name text to-s)))
45
00d271c Alex Suraci introduce functions
authored
46 process-val(g, `(~a ~b)) =
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
47 `(~g seq(~process-val(g, a), ~process-val(g, b)))
48
00d271c Alex Suraci introduce functions
authored
49 process-val(g, `(~a | ~b)) =
8d8653b Alex Suraci grammar kernel updates
authored
50 process-val(g, `[~a, ~b])
51
00d271c Alex Suraci introduce functions
authored
52 process-val(g, `[~*xs]) = do:
8840e0c Alex Suraci correct choice operator in grammar
authored
53 original = ^rule-vars
54 new = Set new
8d8653b Alex Suraci grammar kernel updates
authored
55 choices =
56 xs collect [x]:
8840e0c Alex Suraci correct choice operator in grammar
authored
57 let(rule-vars = original dup):
58 choice = process-val(g, x)
59 new merge(^rule-vars - original)
60 choice
61
62 ^rule-vars merge(new)
8d8653b Alex Suraci grammar kernel updates
authored
63
64 `(~g any(~*choices))
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
65
00d271c Alex Suraci introduce functions
authored
66 process-val(g, a: `{ ~*bs }) =
15b61e4 Alex Suraci use locals for internal grammar kernel bits
authored
67 if(^actions)
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
68 then:
15b61e4 Alex Suraci use locals for internal grammar kernel bits
authored
69 vars = ^rule-vars to-a
70 call = "actions[" + ^actions size to-s + "][" + vars collect .to-s join(", ") + "]"
71 ^actions << [a, vars]
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
72 `(~g action(~call))
73 else:
74 `(~g action({ ~*bs }))
75
00d271c Alex Suraci introduce functions
authored
76 process-val(g, `(<(~x)>)) = do:
8d8653b Alex Suraci grammar kernel updates
authored
77 val = process-val(g, x)
309faee Alex Suraci style cleanup
authored
78
79 when(^rule-vars):
80 ^rule-vars << .text
81
8d8653b Alex Suraci grammar kernel updates
authored
82 `(~g collect(~val))
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
83
00d271c Alex Suraci introduce functions
authored
84 process-val(g, `(@<(~x)>)) =
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
85 `(~g bounds(~process-val(g, x)))
86
69f185c Alex Suraci AST reorganization
authored
87 process-val(g, s: Atomy AST StringLiteral) =
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
88 `(~g str(~s))
89
69f185c Alex Suraci AST reorganization
authored
90 process-val(g, `(/(~(b: Atomy AST StringLiteral))/)) =
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
91 `(~g reg(~(b raw)))
92
416a17f Alex Suraci update kernel/ and lib/ for constant changes
authored
93 process-val(g, c: Atomy AST Call) = do:
66c9bfc Alex Suraci refactor pretty kernel to use module functions
authored
94 args = "(" + c arguments collect .(pretty show(_)) join(", ") + ")"
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
95
96 `(~g ref(~(c name text to-s), nil, ~args))
97
416a17f Alex Suraci update kernel/ and lib/ for constant changes
authored
98 process-val(g, `@~(c: Atomy AST Call)) = do:
66c9bfc Alex Suraci refactor pretty kernel to use module functions
authored
99 args = "(" + c arguments collect .(pretty show(_)) join(", ") + ")"
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
100
101 `(~g invoke(~(c name text to-s), ~args))
102
416a17f Alex Suraci update kernel/ and lib/ for constant changes
authored
103 process-val(g, `^~(c: Atomy AST Call)) = do:
66c9bfc Alex Suraci refactor pretty kernel to use module functions
authored
104 args = "(" + c arguments collect .(pretty show(_)) join(", ") + ")"
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
105
106 `(~g foreign-invoke("parent", ~(c name text to-s), ~args))
107
416a17f Alex Suraci update kernel/ and lib/ for constant changes
authored
108 process-val(g, w: Atomy AST Word) =
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
109 `(~g ref(~(w text to-s)))
110
416a17f Alex Suraci update kernel/ and lib/ for constant changes
authored
111 process-val(g, `@~(w: Atomy AST Word)) = do:
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
112 `(~g invoke(~(w text to-s)))
113
416a17f Alex Suraci update kernel/ and lib/ for constant changes
authored
114 process-val(g, `^~(w: Atomy AST Word)) = do:
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
115 `(~g foreign-invoke("parent", ~(w text to-s)))
116
00d271c Alex Suraci introduce functions
authored
117 process-val(g, `((~v)?)) =
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
118 `(~g maybe(~process-val(g, v)))
119
00d271c Alex Suraci introduce functions
authored
120 process-val(g, `((~v)+)) =
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
121 `(~g many(~process-val(g, v)))
122
00d271c Alex Suraci introduce functions
authored
123 process-val(g, `((~v)*)) =
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
124 `(~g kleene(~process-val(g, v)))
125
00d271c Alex Suraci introduce functions
authored
126 process-val(g, `&~v) =
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
127 `(~g andp(~process-val(g, v)))
128
00d271c Alex Suraci introduce functions
authored
129 process-val(g, `!~v) =
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
130 `(~g notp(~process-val(g, v)))
131
00d271c Alex Suraci introduce functions
authored
132 process-val(g, '_) =
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
133 `(~g dot)
134
00d271c Alex Suraci introduce functions
authored
135 process-val(_, s) =
3ea652d Alex Suraci use inspect rather than to-sexp for errors
authored
136 raise(ArgumentError, "unknown parsing operator:\n" + s inspect)
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
137
416a17f Alex Suraci update kernel/ and lib/ for constant changes
authored
138 Atomy AST open:
5c33b0b Alex Suraci add ast macro for defining nodes
authored
139 ast(ParserDefiner(@source))
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
140
2243afa Alex Suraci no more CodeLoader global state
authored
141 ParserDefiner bytecode(g, _) := do:
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
142 g push-rubinius
98d7af6 Alex Suraci use . rather than # for particles/symbols
authored
143 g push-literal(.__parser_init__)
416a17f Alex Suraci update kernel/ and lib/ for constant changes
authored
144 g push-literal(Rubinius Compiler compile-string(@source))
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
145 g push-scope
146 g push-self
98d7af6 Alex Suraci use . rather than # for particles/symbols
authored
147 g send(.attach-method, 4)
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
148 g pop
149
150 g push-self
98d7af6 Alex Suraci use . rather than # for particles/symbols
authored
151 g send(.__parser_init__, 0)
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
152
153
154 macro(grammar: ~*body): names [g]:
155 b = body collect [n]:
156 n match:
157 `%%{ ~*xs } -> do:
158 call =
15b61e4 Alex Suraci use locals for internal grammar kernel bits
authored
159 if(^setups)
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
160 then:
15b61e4 Alex Suraci use locals for internal grammar kernel bits
authored
161 ^setups << `{ ~*xs }
162 "setups[" + (^setups size - 1) to-s + "][]"
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
163 else:
164 `{ ~*xs }
165
166 `(~g add-setup(~g action(~call)))
167
168 `(%~name := ~const) ->
169 `(~g add-foreign-grammar(~(name text to-s), ~const-name(const)))
170
171 `(%~name = ~expr) -> do:
172 call =
15b61e4 Alex Suraci use locals for internal grammar kernel bits
authored
173 if(^vars)
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
174 then:
8e79e04 Alex Suraci fix %foo = bar in grammar kernel
authored
175 ^vars << expr
176 "vars[" + (^vars size - 1) to-s + "]"
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
177 else:
8e79e04 Alex Suraci fix %foo = bar in grammar kernel
authored
178 expr
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
179
180 `(~g set-variable(~(name text to-s), call))
181
c5c21a5 Alex Suraci improve external invoke/assign in grammar dsl
authored
182 `((~name)(~*args) := ~val) -> do:
15b61e4 Alex Suraci use locals for internal grammar kernel bits
authored
183 let(rule = name text,
f7a497d Alex Suraci use // to refer to toplevel, / for current module
authored
184 rule-vars = //Set new(args collect .text)):
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
185 `(~g set(~(name text to-s)
186 ~process-val(g, val)
187 ~(args collect [x]: x text to-s)))
188
189 `(~name := ~val) -> do:
15b61e4 Alex Suraci use locals for internal grammar kernel bits
authored
190 let(rule = name text,
f7a497d Alex Suraci use // to refer to toplevel, / for current module
authored
191 rule-vars = //Set new):
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
192 `(~g set(~(name text to-s), ~process-val(g, val)))
193
c5c21a5 Alex Suraci improve external invoke/assign in grammar dsl
authored
194 u ->
195 raise("unknown toplevel grammar form:\n" + u inspect)
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
196
f7a497d Alex Suraci use // to refer to toplevel, / for current module
authored
197 `(//KPeg Grammar new tap [~g]: ~*b)
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
198
199
200 macro(parser(~name): ~*grammar):
15b61e4 Alex Suraci use locals for internal grammar kernel bits
authored
201 let(actions = [],
202 setups = [],
203 vars = []):
2243afa Alex Suraci no more CodeLoader global state
authored
204 g = eval(`(grammar: ~*grammar))
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
205
19fc23c Alex Suraci remove $: cosmetics macro; just use better style
authored
206 acts =
15b61e4 Alex Suraci use locals for internal grammar kernel bits
authored
207 ^actions collect [action, as]:
416a17f Alex Suraci update kernel/ and lib/ for constant changes
authored
208 args = as collect [a]: Atomy AST Word new(0, a)
19fc23c Alex Suraci remove $: cosmetics macro; just use better style
authored
209 `([~*args] ~action)
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
210
19fc23c Alex Suraci remove $: cosmetics macro; just use better style
authored
211 def =
416a17f Alex Suraci update kernel/ and lib/ for constant changes
authored
212 Atomy AST ParserDefiner new(
19fc23c Alex Suraci remove $: cosmetics macro; just use better style
authored
213 node line
f7a497d Alex Suraci use // to refer to toplevel, / for current module
authored
214 //KPeg CodeGenerator new(const-name(name), g) output)
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
215
216 `(do:
f7a497d Alex Suraci use // to refer to toplevel, / for current module
authored
217 ~name = //KPeg CompiledParser class:
15b61e4 Alex Suraci use locals for internal grammar kernel bits
authored
218 { self } setups := @setups ||= ~^setups
219 { self } vars := @vars ||= ~^vars
d9c27b4 Alex Suraci update/rename kpeg kernel, use for interpolation
authored
220
221 actions := @actions ||= ~acts
222
223 ~def
224
225 ~name)
Something went wrong with that request. Please try again.