ceml 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,320 @@
1
+ # Autogenerated from a Treetop grammar. Edits may be lost.
2
+
3
+
4
+ module CEML
5
+ module Instructions
6
+ include Treetop::Runtime
7
+
8
+ def root
9
+ @root || :instructions
10
+ end
11
+
12
+ include Lexer
13
+
14
+ def _nt_instructions
15
+ start_index = index
16
+ if node_cache[:instructions].has_key?(index)
17
+ cached = node_cache[:instructions][index]
18
+ @index = cached.interval.end if cached
19
+ return cached
20
+ end
21
+
22
+ s0, i0 = [], index
23
+ loop do
24
+ r1 = _nt_instruction
25
+ if r1
26
+ s0 << r1
27
+ else
28
+ break
29
+ end
30
+ end
31
+ if s0.empty?
32
+ @index = i0
33
+ r0 = nil
34
+ else
35
+ r0 = instantiate_node(Instructions,input, i0...index, s0)
36
+ end
37
+
38
+ node_cache[:instructions][start_index] = r0
39
+
40
+ r0
41
+ end
42
+
43
+ module Instruction0
44
+
45
+ INTERPOLATE_REGEX = /\|(\w+)\.?(\w+)?\|/
46
+
47
+ def interpolate(env)
48
+ text =~ INTERPOLATE_REGEX or return text
49
+ text.gsub(INTERPOLATE_REGEX) do |m|
50
+ var, role = *[$2, $1].compact
51
+ env.expand(role, var) or return false
52
+ end
53
+ end
54
+
55
+ def role; id.text_value.to_sym; end
56
+ def text; super.text_value; end
57
+ def var; (!defined?(:about) || about.empty?) ? nil : about.varname.text_value; end
58
+ def key; var || text; end
59
+ def cmd;
60
+ text_value =~ /^ask/ and return :ask
61
+ text_value =~ /^tell/ and return :tell
62
+ end
63
+ end
64
+
65
+ def _nt_instruction
66
+ start_index = index
67
+ if node_cache[:instruction].has_key?(index)
68
+ cached = node_cache[:instruction][index]
69
+ @index = cached.interval.end if cached
70
+ return cached
71
+ end
72
+
73
+ i0 = index
74
+ r1 = _nt_ask_stmt
75
+ if r1
76
+ r0 = r1
77
+ r0.extend(Instruction0)
78
+ else
79
+ r2 = _nt_tell_stmt
80
+ if r2
81
+ r0 = r2
82
+ r0.extend(Instruction0)
83
+ else
84
+ @index = i0
85
+ r0 = nil
86
+ end
87
+ end
88
+
89
+ node_cache[:instruction][start_index] = r0
90
+
91
+ r0
92
+ end
93
+
94
+ module TellStmt0
95
+ def ws
96
+ elements[1]
97
+ end
98
+
99
+ def id
100
+ elements[2]
101
+ end
102
+
103
+ def text
104
+ elements[5]
105
+ end
106
+
107
+ def nl
108
+ elements[6]
109
+ end
110
+ end
111
+
112
+ def _nt_tell_stmt
113
+ start_index = index
114
+ if node_cache[:tell_stmt].has_key?(index)
115
+ cached = node_cache[:tell_stmt][index]
116
+ @index = cached.interval.end if cached
117
+ return cached
118
+ end
119
+
120
+ i0, s0 = index, []
121
+ if has_terminal?('tell', false, index)
122
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 4))
123
+ @index += 4
124
+ else
125
+ terminal_parse_failure('tell')
126
+ r1 = nil
127
+ end
128
+ s0 << r1
129
+ if r1
130
+ r2 = _nt_ws
131
+ s0 << r2
132
+ if r2
133
+ r3 = _nt_id
134
+ s0 << r3
135
+ if r3
136
+ if has_terminal?(':', false, index)
137
+ r4 = instantiate_node(SyntaxNode,input, index...(index + 1))
138
+ @index += 1
139
+ else
140
+ terminal_parse_failure(':')
141
+ r4 = nil
142
+ end
143
+ s0 << r4
144
+ if r4
145
+ r6 = _nt_ws
146
+ if r6
147
+ r5 = r6
148
+ else
149
+ r5 = instantiate_node(SyntaxNode,input, index...index)
150
+ end
151
+ s0 << r5
152
+ if r5
153
+ r7 = _nt_text
154
+ s0 << r7
155
+ if r7
156
+ r8 = _nt_nl
157
+ s0 << r8
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
164
+ if s0.last
165
+ r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
166
+ r0.extend(TellStmt0)
167
+ else
168
+ @index = i0
169
+ r0 = nil
170
+ end
171
+
172
+ node_cache[:tell_stmt][start_index] = r0
173
+
174
+ r0
175
+ end
176
+
177
+ module AskStmt0
178
+ def ws1
179
+ elements[0]
180
+ end
181
+
182
+ def ws2
183
+ elements[2]
184
+ end
185
+
186
+ def varname
187
+ elements[3]
188
+ end
189
+ end
190
+
191
+ module AskStmt1
192
+ def ws
193
+ elements[1]
194
+ end
195
+
196
+ def id
197
+ elements[2]
198
+ end
199
+
200
+ def about
201
+ elements[3]
202
+ end
203
+
204
+ def text
205
+ elements[6]
206
+ end
207
+
208
+ def nl
209
+ elements[7]
210
+ end
211
+ end
212
+
213
+ def _nt_ask_stmt
214
+ start_index = index
215
+ if node_cache[:ask_stmt].has_key?(index)
216
+ cached = node_cache[:ask_stmt][index]
217
+ @index = cached.interval.end if cached
218
+ return cached
219
+ end
220
+
221
+ i0, s0 = index, []
222
+ if has_terminal?('ask', false, index)
223
+ r1 = instantiate_node(SyntaxNode,input, index...(index + 3))
224
+ @index += 3
225
+ else
226
+ terminal_parse_failure('ask')
227
+ r1 = nil
228
+ end
229
+ s0 << r1
230
+ if r1
231
+ r2 = _nt_ws
232
+ s0 << r2
233
+ if r2
234
+ r3 = _nt_id
235
+ s0 << r3
236
+ if r3
237
+ i5, s5 = index, []
238
+ r6 = _nt_ws
239
+ s5 << r6
240
+ if r6
241
+ if has_terminal?('re', false, index)
242
+ r7 = instantiate_node(SyntaxNode,input, index...(index + 2))
243
+ @index += 2
244
+ else
245
+ terminal_parse_failure('re')
246
+ r7 = nil
247
+ end
248
+ s5 << r7
249
+ if r7
250
+ r8 = _nt_ws
251
+ s5 << r8
252
+ if r8
253
+ r9 = _nt_id
254
+ s5 << r9
255
+ end
256
+ end
257
+ end
258
+ if s5.last
259
+ r5 = instantiate_node(SyntaxNode,input, i5...index, s5)
260
+ r5.extend(AskStmt0)
261
+ else
262
+ @index = i5
263
+ r5 = nil
264
+ end
265
+ if r5
266
+ r4 = r5
267
+ else
268
+ r4 = instantiate_node(SyntaxNode,input, index...index)
269
+ end
270
+ s0 << r4
271
+ if r4
272
+ if has_terminal?(':', false, index)
273
+ r10 = instantiate_node(SyntaxNode,input, index...(index + 1))
274
+ @index += 1
275
+ else
276
+ terminal_parse_failure(':')
277
+ r10 = nil
278
+ end
279
+ s0 << r10
280
+ if r10
281
+ r12 = _nt_ws
282
+ if r12
283
+ r11 = r12
284
+ else
285
+ r11 = instantiate_node(SyntaxNode,input, index...index)
286
+ end
287
+ s0 << r11
288
+ if r11
289
+ r13 = _nt_text
290
+ s0 << r13
291
+ if r13
292
+ r14 = _nt_nl
293
+ s0 << r14
294
+ end
295
+ end
296
+ end
297
+ end
298
+ end
299
+ end
300
+ end
301
+ if s0.last
302
+ r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
303
+ r0.extend(AskStmt1)
304
+ else
305
+ @index = i0
306
+ r0 = nil
307
+ end
308
+
309
+ node_cache[:ask_stmt][start_index] = r0
310
+
311
+ r0
312
+ end
313
+
314
+ end
315
+
316
+ class InstructionsParser < Treetop::Runtime::CompiledParser
317
+ include Instructions
318
+ end
319
+
320
+ end
@@ -0,0 +1,42 @@
1
+ module CEML
2
+ grammar Instructions
3
+ include Lexer
4
+
5
+ rule instructions
6
+ instruction+ <Instructions>
7
+ end
8
+
9
+ rule instruction
10
+ (ask_stmt / tell_stmt) {
11
+
12
+ INTERPOLATE_REGEX = /\|(\w+)\.?(\w+)?\|/
13
+
14
+ def interpolate(env)
15
+ text =~ INTERPOLATE_REGEX or return text
16
+ text.gsub(INTERPOLATE_REGEX) do |m|
17
+ var, role = *[$2, $1].compact
18
+ env.expand(role, var) or return false
19
+ end
20
+ end
21
+
22
+ def role; id.text_value.to_sym; end
23
+ def text; super.text_value; end
24
+ def var; (!defined?(:about) || about.empty?) ? nil : about.varname.text_value; end
25
+ def key; var || text; end
26
+ def cmd;
27
+ text_value =~ /^ask/ and return :ask
28
+ text_value =~ /^tell/ and return :tell
29
+ end
30
+ }
31
+ end
32
+
33
+ rule tell_stmt
34
+ 'tell' ws id ':' ws? text nl
35
+ end
36
+
37
+ rule ask_stmt
38
+ 'ask' ws id about:(ws 're' ws varname:id)? ':' ws? text nl
39
+ end
40
+
41
+ end
42
+ end