Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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