emfrp 0.1.2 → 0.1.3
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.
- checksums.yaml +4 -4
- data/README.md +45 -12
- data/bin/emfrp +4 -1
- data/examples/LCDClock/LCDClock.mfrp +93 -93
- data/examples/LCDClock/LCDClock_LPC1768.bin +0 -0
- data/examples/LCDClock/README.md +24 -24
- data/examples/LCDPositioner/LCDPositioner.mfrp +30 -30
- data/examples/LCDPositioner/LCDPositionerMain.c +15 -15
- data/examples/MostDistantPoint/MostDistantPoint.mfrp +25 -25
- data/examples/MostDistantPoint/MostDistantPointMain.c +14 -14
- data/lib/emfrp/compile/c/alloc.rb +200 -200
- data/lib/emfrp/compile/c/codegen.rb +18 -18
- data/lib/emfrp/compile/c/codegen_context.rb +218 -218
- data/lib/emfrp/compile/c/monofy.rb +185 -185
- data/lib/emfrp/compile/c/syntax_codegen.rb +364 -364
- data/lib/emfrp/compile/c/syntax_exp_codegen.rb +119 -119
- data/lib/emfrp/compile/graphviz/graphviz.rb +53 -53
- data/lib/emfrp/compile_error.rb +95 -95
- data/lib/emfrp/interpreter/command_manager.rb +367 -367
- data/lib/emfrp/interpreter/evaluater.rb +146 -146
- data/lib/emfrp/interpreter/file_loader.rb +52 -52
- data/lib/emfrp/interpreter/interpreter.rb +200 -195
- data/lib/emfrp/parser/expression.rb +386 -386
- data/lib/emfrp/parser/misc.rb +184 -184
- data/lib/emfrp/parser/newnode_convert.rb +72 -72
- data/lib/emfrp/parser/operator.rb +25 -25
- data/lib/emfrp/parser/parser.rb +150 -150
- data/lib/emfrp/parser/parsing_error.rb +49 -49
- data/lib/emfrp/parser/toplevel.rb +555 -555
- data/lib/emfrp/pre_convert/pre_convert.rb +32 -32
- data/lib/emfrp/syntax.rb +171 -171
- data/lib/emfrp/typing/typing_error.rb +47 -47
- data/lib/emfrp/typing/union_type.rb +197 -197
- data/lib/emfrp/version.rb +1 -1
- data/mfrp_include/Std.mfrp +122 -122
- data/tests/Rakefile +8 -8
- data/tests/Rakefile.common +27 -27
- data/tests/command/Rakefile +2 -2
- data/tests/command/ReplaceNode.mfrp +39 -39
- data/tests/compiler/ComplexDataType/ComplexDataType.mfrp +14 -14
- data/tests/compiler/ComplexDataType/ComplexDataTypeMain.c +15 -15
- data/tests/compiler/ComplexDataType/Rakefile +2 -2
- data/tests/compiler/ComplexDataType/expected_out.txt +0 -0
- data/tests/compiler/ComplexDataType/in.txt +5 -5
- data/tests/compiler/LCDClock/LCDClock.mfrp +90 -90
- data/tests/compiler/LCDClock/LCDClockMain.c +0 -0
- data/tests/compiler/LCDClock/Rakefile +2 -2
- data/tests/compiler/LCDClock/expected_out.txt +0 -0
- data/tests/compiler/LCDClock/in.txt +0 -0
- data/tests/compiler/LCDPositioner/LCDPositioner.mfrp +30 -30
- data/tests/compiler/LCDPositioner/LCDPositionerMain.c +15 -15
- data/tests/compiler/LCDPositioner/Rakefile +2 -2
- data/tests/compiler/LCDPositioner/graph.dot +0 -0
- data/tests/compiler/LCDPositioner/graph.png +0 -0
- data/tests/compiler/Rakefile +8 -8
- data/tests/compiler/Rakefile.common +23 -23
- data/tests/compiler/UseData/Rakefile +2 -2
- data/tests/compiler/UseData/UseData.mfrp +8 -8
- data/tests/compiler/UseSubModule/Rakefile +2 -2
- data/tests/compiler/UseSubModule/SubModule.mfrp +8 -8
- data/tests/compiler/UseSubModule/SubModule2.mfrp +5 -5
- data/tests/compiler/UseSubModule/UseSubModule.mfrp +11 -11
- data/tests/core/FromAnnotation.mfrp +18 -18
- data/tests/core/Last.mfrp +10 -10
- data/tests/core/Rakefile +2 -2
- data/tests/core/TypingTest.mfrp +11 -11
- data/tests/core/WithoutInputs.mfrp +19 -19
- data/tests/load_time_error/Rakefile +32 -32
- data/tests/load_time_error/TypeMismatch.mfrp +4 -4
- metadata +3 -3
@@ -1,555 +1,555 @@
|
|
1
|
-
require 'parser_combinator/string_parser'
|
2
|
-
|
3
|
-
module Emfrp
|
4
|
-
class Parser < ParserCombinator::StringParser
|
5
|
-
|
6
|
-
# Top Level Definition Statements
|
7
|
-
# --------------------
|
8
|
-
|
9
|
-
parser :module_top_def do
|
10
|
-
defs = [
|
11
|
-
data_def,
|
12
|
-
func_def,
|
13
|
-
node_def,
|
14
|
-
type_def,
|
15
|
-
infix_def,
|
16
|
-
primtype_def,
|
17
|
-
primfunc_def,
|
18
|
-
command_def,
|
19
|
-
newnode_def,
|
20
|
-
]
|
21
|
-
defs.inject(&:^).map do |x|
|
22
|
-
[x].flatten
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
parser :material_top_def do
|
27
|
-
(data_def ^ func_def ^ type_def ^ infix_def ^ primtype_def ^ primfunc_def ^ command_def).map do |x|
|
28
|
-
[x].flatten
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
parser :appendable_top_def do
|
33
|
-
(data_def ^ func_def ^ type_def).map do |x|
|
34
|
-
[x].flatten
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
parser :module_or_material_file do
|
39
|
-
(module_file ^ material_file).err("file", "module-file or material_file")
|
40
|
-
end
|
41
|
-
|
42
|
-
parser :module_file do
|
43
|
-
place = "module-file"
|
44
|
-
seq(
|
45
|
-
many(ws),
|
46
|
-
symbol("module").name(:keyword1),
|
47
|
-
many1(ws).err(place, "space after `module' keyword"),
|
48
|
-
ident_begin_upper.name(:name),
|
49
|
-
opt_fail(
|
50
|
-
many1(ws) > symbol("in") > many1(ws) >
|
51
|
-
many1_fail(input_def, comma_separator).err(place, "definitions of inputs")
|
52
|
-
).map(&:flatten).name(:inputs),
|
53
|
-
many1(ws).err(place, "spaec after definitions of input"),
|
54
|
-
symbol("out").err(place, "keyword `out'"),
|
55
|
-
many1(ws).err(place, "space after keyword `out'"),
|
56
|
-
many1_fail(output_def, comma_separator).err(place, "definitions of outputs").name(:outputs),
|
57
|
-
opt_fail(
|
58
|
-
many1(ws) >
|
59
|
-
symbol("use") >
|
60
|
-
many1(ws).err(place, "space after keyword `use'") >
|
61
|
-
many1_fail(load_path, comma_separator).err(place, "definitions of include-files")
|
62
|
-
).map{|x| x == [] ? [] : x[0]}.name(:uses),
|
63
|
-
many1(ws).err(place, "space before top-definitions"),
|
64
|
-
many_fail(module_top_def, many(ws)).map(&:flatten).err(place, "top definitions").name(:defs),
|
65
|
-
many(ws),
|
66
|
-
end_of_input.err("module-file", "valid end of file")
|
67
|
-
).map do |x|
|
68
|
-
t = Top.new(:inputs => x[:inputs], :outputs => x[:outputs], :uses => x[:uses], :module_name => x[:name])
|
69
|
-
x[:defs].each do |d|
|
70
|
-
k = case d
|
71
|
-
when DataDef then :datas
|
72
|
-
when FuncDef then :funcs
|
73
|
-
when NodeDef then :nodes
|
74
|
-
when TypeDef then :types
|
75
|
-
when InfixDef then :infixes
|
76
|
-
when PrimTypeDef then :ptypes
|
77
|
-
when PrimFuncDef then :pfuncs
|
78
|
-
when CommandDef then :commands
|
79
|
-
when NewNodeDef then :newnodes
|
80
|
-
end
|
81
|
-
t[k] << d
|
82
|
-
end
|
83
|
-
t
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
parser :material_file do
|
88
|
-
place = "material-file"
|
89
|
-
seq(
|
90
|
-
many(ws),
|
91
|
-
symbol("material").name(:keyword1),
|
92
|
-
many1(ws),
|
93
|
-
ident_begin_upper.name(:name),
|
94
|
-
opt_fail(
|
95
|
-
many1(ws) >
|
96
|
-
symbol("use") >
|
97
|
-
many1(ws) >
|
98
|
-
many1_fail(load_path, comma_separator)
|
99
|
-
).to_nil.name(:uses),
|
100
|
-
many1(ws),
|
101
|
-
many_fail(material_top_def, many(ws)).map(&:flatten).name(:defs),
|
102
|
-
many(ws),
|
103
|
-
end_of_input.err("module", "valid end of file")
|
104
|
-
).map do |x|
|
105
|
-
t = Top.new(:uses => x[:uses], :module_name => nil)
|
106
|
-
x[:defs].each do |d|
|
107
|
-
k = case d
|
108
|
-
when DataDef then :datas
|
109
|
-
when FuncDef then :funcs
|
110
|
-
when TypeDef then :types
|
111
|
-
when InfixDef then :infixes
|
112
|
-
when PrimTypeDef then :ptypes
|
113
|
-
when PrimFuncDef then :pfuncs
|
114
|
-
when CommandDef then :commands
|
115
|
-
end
|
116
|
-
t[k] << d
|
117
|
-
end
|
118
|
-
t
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
parser :oneline_file do
|
123
|
-
many(ws) > appendable_top_def < many(ws) < end_of_input
|
124
|
-
end
|
125
|
-
|
126
|
-
parser :load_path do
|
127
|
-
many1(ident_begin_upper, str("."))
|
128
|
-
end
|
129
|
-
|
130
|
-
parser :input_def do
|
131
|
-
seq(
|
132
|
-
var_name.name(:name),
|
133
|
-
opt_fail(
|
134
|
-
symbol("(") > exp < symbol(")")
|
135
|
-
).to_nil.name(:init_exp),
|
136
|
-
many(ws),
|
137
|
-
str(":"),
|
138
|
-
many(ws),
|
139
|
-
type.err("param-def", "type").name(:type)
|
140
|
-
).map do |x|
|
141
|
-
InputDef.new(x.to_h)
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
parser :output_def do
|
146
|
-
seq(
|
147
|
-
var_name.name(:name),
|
148
|
-
opt_fail(
|
149
|
-
many(ws) > str(":") > many(ws) >
|
150
|
-
type.err("param-def", "type")
|
151
|
-
).to_nil.name(:type)
|
152
|
-
).map do |x|
|
153
|
-
OutputDef.new(x.to_h)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
parser :data_def do # -> DataDef
|
158
|
-
seq(
|
159
|
-
symbol("data").name(:keyword),
|
160
|
-
many1(ws),
|
161
|
-
data_name.err("data-def", "name of data").name(:name),
|
162
|
-
opt_fail(
|
163
|
-
many(ws) >
|
164
|
-
str(":") >
|
165
|
-
many(ws) >
|
166
|
-
type.err("data-def", "type")
|
167
|
-
).to_nil.name(:type),
|
168
|
-
many(ws),
|
169
|
-
str("="),
|
170
|
-
many(ws),
|
171
|
-
exp.err("data-def", "valid expression").name(:exp),
|
172
|
-
end_of_def.err("data-def", "valid end of data-def")
|
173
|
-
).map do |x|
|
174
|
-
DataDef.new(x.to_h)
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
parser :func_def do # -> FuncDef
|
179
|
-
seq(
|
180
|
-
symbol("func").name(:keyword),
|
181
|
-
many1(ws),
|
182
|
-
(func_name | operator).err("func-def", "name of func").name(:name),
|
183
|
-
many(ws),
|
184
|
-
str("("),
|
185
|
-
many1_fail(func_param_def, comma_separator).err("func-def", "list of param for function").name(:params),
|
186
|
-
str(")"),
|
187
|
-
opt_fail(
|
188
|
-
many(ws) >
|
189
|
-
str(":") >
|
190
|
-
many(ws) >
|
191
|
-
type_with_var.err("func-def", "type of return value")
|
192
|
-
).to_nil.name(:type),
|
193
|
-
many(ws),
|
194
|
-
str("="),
|
195
|
-
many(ws),
|
196
|
-
exp.err("func-def", "valid expression").name(:exp),
|
197
|
-
end_of_def.err("func-def", "valid end of func-def")
|
198
|
-
).map do |x|
|
199
|
-
FuncDef.new(x.to_h)
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
parser :node_def do # -> [NodeDef]
|
204
|
-
seq(
|
205
|
-
symbol("node").name(:keyword),
|
206
|
-
opt_fail(many1(ws) > init_def).to_nil.name(:init_exp),
|
207
|
-
opt_fail(many1(ws) > from_def).to_nil.name(:params),
|
208
|
-
many1(ws),
|
209
|
-
pattern.name(:pattern),
|
210
|
-
many(ws),
|
211
|
-
str("="),
|
212
|
-
many(ws),
|
213
|
-
exp.err("node-def", "body").name(:exp),
|
214
|
-
end_of_def.err("node-def", "valid end of node-def")
|
215
|
-
).map do |x|
|
216
|
-
refs = x[:pattern].find_refs
|
217
|
-
if x[:pattern][:ref]
|
218
|
-
whole_name = x[:pattern][:ref]
|
219
|
-
else
|
220
|
-
whole_name = SSymbol.new(
|
221
|
-
:desc => "anonymous" + x[:pattern].object_id.to_s,
|
222
|
-
:keyword => x[:pattern].deep_copy
|
223
|
-
)
|
224
|
-
end
|
225
|
-
whole_node = NodeDef.new(
|
226
|
-
:keyword => x[:keyword],
|
227
|
-
:init_exp => x[:init_exp],
|
228
|
-
:params => x[:params],
|
229
|
-
:exp => x[:exp],
|
230
|
-
:type => x[:pattern][:type],
|
231
|
-
:name => whole_name
|
232
|
-
)
|
233
|
-
if refs.size == 0
|
234
|
-
[]
|
235
|
-
else
|
236
|
-
gen = proc do |left_exp, ref|
|
237
|
-
c = Case.new(:pattern => x[:pattern].deep_copy, :exp => VarRef.new(:name => ref))
|
238
|
-
MatchExp.new(:exp => left_exp, :cases => [c])
|
239
|
-
end
|
240
|
-
child_nodes = refs.reject{|x| x == whole_name}.map do |ref|
|
241
|
-
init_exp = x[:init_exp] ? gen.call(x[:init_exp].deep_copy, ref) : nil
|
242
|
-
params = [NodeRef.new(:name => whole_name, :as => whole_name, :last => false)]
|
243
|
-
exp = gen.call(VarRef.new(:name => whole_name), ref)
|
244
|
-
NodeDef.new(
|
245
|
-
:keyword => x[:keyword],
|
246
|
-
:init_exp => init_exp,
|
247
|
-
:params => params,
|
248
|
-
:name => ref,
|
249
|
-
:type => nil,
|
250
|
-
:exp => exp
|
251
|
-
)
|
252
|
-
end
|
253
|
-
[whole_node] + child_nodes
|
254
|
-
end
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
parser :type_def do # -> TypeDef
|
259
|
-
seq(
|
260
|
-
symbol("type").name(:keyword),
|
261
|
-
opt(many1(ws) > str("static")).map{|x| x == [] ? false : true}.name(:static),
|
262
|
-
many1(ws),
|
263
|
-
type_with_param.err("type-def", "type with param").name(:type),
|
264
|
-
many(ws),
|
265
|
-
opt_fail(
|
266
|
-
seq(
|
267
|
-
str("["),
|
268
|
-
many1_fail(type_var, comma_separator).err("type-def", "valid params"),
|
269
|
-
str("]").err("type-def", "']' after params")
|
270
|
-
).map{|x| x[1]}
|
271
|
-
).map{|x| x.flatten}.name(:params),
|
272
|
-
many(ws),
|
273
|
-
str("=").err("type-def", "'='"),
|
274
|
-
many(ws),
|
275
|
-
many1_fail(tvalue_def, or_separator).err("type-def", "value constructors").name(:tvalues),
|
276
|
-
end_of_def.err("type-def", "valid end of type-def")
|
277
|
-
).map do |x|
|
278
|
-
TypeDef.new(x.to_h, :name => x[:type][:name])
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
|
-
parser :infix_def do # -> InfixDef
|
283
|
-
seq(
|
284
|
-
(symbol("infixl") | symbol("infixr") | symbol("infix")).name(:type),
|
285
|
-
opt(many1(ws) > digit_symbol).to_nil.name(:priority),
|
286
|
-
many1(ws),
|
287
|
-
operator_general.err("infix-def", "operator").name(:op),
|
288
|
-
end_of_def
|
289
|
-
). map do |x|
|
290
|
-
InfixDef.new(x.to_h)
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
parser :primtype_def do
|
295
|
-
seq(
|
296
|
-
symbol("primtype").name(:keyword),
|
297
|
-
many1(ws),
|
298
|
-
type_symbol.err("primtype-def", "type symbol to be define").name(:name),
|
299
|
-
many(ws),
|
300
|
-
str("=").err("primtype-def", "'='"),
|
301
|
-
many(ws),
|
302
|
-
many1(foreign_exp, comma_separator).err("primtype-def", "foreign-definitions").name(:foreigns),
|
303
|
-
end_of_def.err("primtype-def", "valid end of primtype-def")
|
304
|
-
).map do |x|
|
305
|
-
PrimTypeDef.new(x.to_h)
|
306
|
-
end
|
307
|
-
end
|
308
|
-
|
309
|
-
parser :primfunc_def do
|
310
|
-
seq(
|
311
|
-
symbol("primfunc").name(:keyword),
|
312
|
-
many1(ws),
|
313
|
-
(func_name | operator).err("primfunc-def", "name of primfunc").name(:name),
|
314
|
-
many(ws),
|
315
|
-
str("("),
|
316
|
-
many1_fail(primfunc_param_def, comma_separator).err("primfunc-def", "list of param for function").name(:params),
|
317
|
-
str(")"),
|
318
|
-
many(ws),
|
319
|
-
str(":"),
|
320
|
-
many(ws),
|
321
|
-
type_with_var.err("func-def", "type of return value").name(:type),
|
322
|
-
many(ws),
|
323
|
-
str("=").err("primfunc-def", "'='"),
|
324
|
-
many(ws),
|
325
|
-
many1(foreign_exp, comma_separator).err("primfunc-def", "foreign-definitions").name(:foreigns),
|
326
|
-
end_of_def.err("primfunc-def", "valid end of primfunc-def")
|
327
|
-
).map do |x|
|
328
|
-
PrimFuncDef.new(x.to_h)
|
329
|
-
end
|
330
|
-
end
|
331
|
-
|
332
|
-
parser :command_def do
|
333
|
-
seq(
|
334
|
-
symbol("#@").name(:keyword1),
|
335
|
-
many(non_newline).name(:command),
|
336
|
-
char("\n"),
|
337
|
-
many(str("#-") > many(non_newline) < char("\n")).name(:following_lines)
|
338
|
-
).map do |x|
|
339
|
-
lines = [x[:command]] + x[:following_lines]
|
340
|
-
CommandDef.new(
|
341
|
-
:line_number => x[:keyword1][:start_pos][:line_number],
|
342
|
-
:file_name => x[:keyword1][:start_pos][:document_name],
|
343
|
-
:command_str => lines.map{|line| line.map(&:item).join}.join(" ")
|
344
|
-
)
|
345
|
-
end
|
346
|
-
end
|
347
|
-
|
348
|
-
parser :newnode_def do
|
349
|
-
seq(
|
350
|
-
symbol("newnode").name(:keyword1),
|
351
|
-
many1(ws),
|
352
|
-
many1_fail(ident_begin_lower, comma_separator).name(:names),
|
353
|
-
many(ws),
|
354
|
-
char("="),
|
355
|
-
many(ws),
|
356
|
-
load_path.name(:module_path),
|
357
|
-
many(ws),
|
358
|
-
char("("),
|
359
|
-
many(ws),
|
360
|
-
many_fail(exp, comma_separator).name(:args),
|
361
|
-
many(ws),
|
362
|
-
symbol(")").name(:keyword2)
|
363
|
-
).map do |x|
|
364
|
-
NewNodeDef.new(x.to_h)
|
365
|
-
end
|
366
|
-
end
|
367
|
-
|
368
|
-
# Func associated
|
369
|
-
# --------------------
|
370
|
-
|
371
|
-
parser :func_param_def do # -> ParamDef
|
372
|
-
seq(
|
373
|
-
var_name.name(:name),
|
374
|
-
opt_fail(
|
375
|
-
many(ws) >
|
376
|
-
str(":") >
|
377
|
-
many(ws) >
|
378
|
-
type_with_var.err("param-def", "type with type-var")
|
379
|
-
).to_nil.name(:type)
|
380
|
-
).map do |x|
|
381
|
-
ParamDef.new(x.to_h)
|
382
|
-
end
|
383
|
-
end
|
384
|
-
|
385
|
-
parser :primfunc_param_def do # -> ParamDef
|
386
|
-
seq(
|
387
|
-
var_name.name(:name),
|
388
|
-
many(ws),
|
389
|
-
str(":"),
|
390
|
-
many(ws),
|
391
|
-
type_with_var.err("param-def", "type with type-var").name(:type)
|
392
|
-
).map do |x|
|
393
|
-
ParamDef.new(x.to_h)
|
394
|
-
end
|
395
|
-
end
|
396
|
-
|
397
|
-
|
398
|
-
# Body associated
|
399
|
-
# --------------------
|
400
|
-
|
401
|
-
parser :foreign_exp do
|
402
|
-
seq(
|
403
|
-
ident_begin_lower.name(:language),
|
404
|
-
symbol("{").name(:keyword1),
|
405
|
-
many(ws),
|
406
|
-
many1(notchar("}")).name(:items),
|
407
|
-
symbol("}").err("body-def", "'}' after c-expression").name(:keyword2)
|
408
|
-
).map do |x|
|
409
|
-
ForeignExp.new(
|
410
|
-
:language => x[:language],
|
411
|
-
:keyword1 => x[:keyword1],
|
412
|
-
:keyword2 => x[:keyword2],
|
413
|
-
:desc => x[:items].map(&:item).join.strip,
|
414
|
-
)
|
415
|
-
end
|
416
|
-
end
|
417
|
-
|
418
|
-
# Node associated
|
419
|
-
# --------------------
|
420
|
-
|
421
|
-
parser :init_def do # -> InitDef
|
422
|
-
seq(
|
423
|
-
symbol("init").name(:keyword1),
|
424
|
-
many(ws),
|
425
|
-
str("["),
|
426
|
-
many(ws),
|
427
|
-
exp.name(:exp),
|
428
|
-
many(ws),
|
429
|
-
symbol("]").name(:keyword)
|
430
|
-
).map do |x|
|
431
|
-
x[:exp]
|
432
|
-
end
|
433
|
-
end
|
434
|
-
|
435
|
-
parser :from_def do
|
436
|
-
seq(
|
437
|
-
symbol("from"),
|
438
|
-
str("[").err("from-def", "["),
|
439
|
-
many_fail(node_ref, comma_separator).err("from-def", "list of depended nodes").name(:params),
|
440
|
-
str("]").err("from-def", "]"),
|
441
|
-
).map do |x|
|
442
|
-
x[:params]
|
443
|
-
end
|
444
|
-
end
|
445
|
-
|
446
|
-
parser :node_ref do
|
447
|
-
node_name_last | node_name_current
|
448
|
-
end
|
449
|
-
|
450
|
-
parser :node_name_last do
|
451
|
-
seq(
|
452
|
-
node_instance_name.name(:name),
|
453
|
-
symbol("@last").name(:keyword),
|
454
|
-
opt_fail(many(ws) > str("as") > many(ws) > var_name.err("node-parameter", "name as exposed")).to_nil.name(:as)
|
455
|
-
).map do |x|
|
456
|
-
as = x[:as] || SSymbol.new(
|
457
|
-
:desc => x[:name][:desc] + "@last",
|
458
|
-
:start_pos => x[:name][:start_pos],
|
459
|
-
:end_pos => x[:keyword][:end_pos]
|
460
|
-
)
|
461
|
-
NodeRef.new(:last => true, :as => as, :name => x[:name])
|
462
|
-
end
|
463
|
-
end
|
464
|
-
|
465
|
-
parser :node_name_current do
|
466
|
-
seq(
|
467
|
-
node_instance_name.name(:name),
|
468
|
-
opt_fail(many(ws) > str("as") > many(ws) > var_name.err("node-parameter", "name as exposed")).to_nil.name(:as)
|
469
|
-
).map do |x|
|
470
|
-
NodeRef.new(:last => false, :as => x[:as] || x[:name], :name => x[:name])
|
471
|
-
end
|
472
|
-
end
|
473
|
-
|
474
|
-
# Type associated
|
475
|
-
# --------------------
|
476
|
-
|
477
|
-
parser :type_with_args do |inner|
|
478
|
-
seq(
|
479
|
-
type_symbol.name(:name),
|
480
|
-
symbol("[").name(:keyword1),
|
481
|
-
many1_fail(inner, comma_separator).err("type", "list of type").name(:args),
|
482
|
-
symbol("]").err("type", "']'").name(:keyword2)
|
483
|
-
).map do |x|
|
484
|
-
Type.new(x.to_h)
|
485
|
-
end
|
486
|
-
end
|
487
|
-
|
488
|
-
parser :type_without_args do
|
489
|
-
type_symbol.map do |x|
|
490
|
-
Type.new(:name => x, :args => [])
|
491
|
-
end
|
492
|
-
end
|
493
|
-
|
494
|
-
parser :type_tuple do |inner|
|
495
|
-
seq(
|
496
|
-
symbol("(").name(:keyword1),
|
497
|
-
many1_fail(inner, comma_separator).err("type", "list of type").name(:args),
|
498
|
-
symbol(")").err("type", "')'").name(:keyword2)
|
499
|
-
).map do |x|
|
500
|
-
type_name = SSymbol.new(:desc => "Tuple" + x[:args].size.to_s)
|
501
|
-
Type.new(x.to_h, :name => type_name)
|
502
|
-
end
|
503
|
-
end
|
504
|
-
|
505
|
-
parser :type_symbol do
|
506
|
-
ident_begin_upper
|
507
|
-
end
|
508
|
-
|
509
|
-
parser :tvalue_symbol do
|
510
|
-
ident_begin_upper
|
511
|
-
end
|
512
|
-
|
513
|
-
parser :type_var do # => TypeVar
|
514
|
-
ident_begin_lower.map do |s|
|
515
|
-
TypeVar.new(:name => s)
|
516
|
-
end
|
517
|
-
end
|
518
|
-
|
519
|
-
parser :type do
|
520
|
-
type_with_args(type) ^ type_tuple(type) ^ type_without_args
|
521
|
-
end
|
522
|
-
|
523
|
-
parser :type_with_var do
|
524
|
-
type_with_args(type_with_var) ^ type_tuple(type_with_var) ^ type_without_args ^ type_var
|
525
|
-
end
|
526
|
-
|
527
|
-
parser :type_with_param do
|
528
|
-
type_with_args(type_var) ^ type_tuple(type_var) ^ type_without_args
|
529
|
-
end
|
530
|
-
|
531
|
-
parser :tvalue_def do # -> TValue
|
532
|
-
seq(
|
533
|
-
tvalue_symbol.name(:name),
|
534
|
-
opt_fail(
|
535
|
-
many(ws) >
|
536
|
-
str("(") >
|
537
|
-
many1_fail(tvalue_def_type, comma_separator) <
|
538
|
-
str(")").err("value-constructor-def", "')'")
|
539
|
-
).map{|x| x.flatten}.name(:params),
|
540
|
-
).map do |x|
|
541
|
-
TValue.new(x.to_h)
|
542
|
-
end
|
543
|
-
end
|
544
|
-
|
545
|
-
parser :tvalue_def_type do # -> TValueParam
|
546
|
-
colon = (many(ws) < str(":") < many(ws))
|
547
|
-
seq(
|
548
|
-
opt_fail(func_name < colon).to_nil.name(:name),
|
549
|
-
type_with_var.name(:type)
|
550
|
-
).map do |x|
|
551
|
-
TValueParam.new(x.to_h)
|
552
|
-
end
|
553
|
-
end
|
554
|
-
end
|
555
|
-
end
|
1
|
+
require 'parser_combinator/string_parser'
|
2
|
+
|
3
|
+
module Emfrp
|
4
|
+
class Parser < ParserCombinator::StringParser
|
5
|
+
|
6
|
+
# Top Level Definition Statements
|
7
|
+
# --------------------
|
8
|
+
|
9
|
+
parser :module_top_def do
|
10
|
+
defs = [
|
11
|
+
data_def,
|
12
|
+
func_def,
|
13
|
+
node_def,
|
14
|
+
type_def,
|
15
|
+
infix_def,
|
16
|
+
primtype_def,
|
17
|
+
primfunc_def,
|
18
|
+
command_def,
|
19
|
+
newnode_def,
|
20
|
+
]
|
21
|
+
defs.inject(&:^).map do |x|
|
22
|
+
[x].flatten
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
parser :material_top_def do
|
27
|
+
(data_def ^ func_def ^ type_def ^ infix_def ^ primtype_def ^ primfunc_def ^ command_def).map do |x|
|
28
|
+
[x].flatten
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
parser :appendable_top_def do
|
33
|
+
(data_def ^ func_def ^ type_def).map do |x|
|
34
|
+
[x].flatten
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
parser :module_or_material_file do
|
39
|
+
(module_file ^ material_file).err("file", "module-file or material_file")
|
40
|
+
end
|
41
|
+
|
42
|
+
parser :module_file do
|
43
|
+
place = "module-file"
|
44
|
+
seq(
|
45
|
+
many(ws),
|
46
|
+
symbol("module").name(:keyword1),
|
47
|
+
many1(ws).err(place, "space after `module' keyword"),
|
48
|
+
ident_begin_upper.name(:name),
|
49
|
+
opt_fail(
|
50
|
+
many1(ws) > symbol("in") > many1(ws) >
|
51
|
+
many1_fail(input_def, comma_separator).err(place, "definitions of inputs")
|
52
|
+
).map(&:flatten).name(:inputs),
|
53
|
+
many1(ws).err(place, "spaec after definitions of input"),
|
54
|
+
symbol("out").err(place, "keyword `out'"),
|
55
|
+
many1(ws).err(place, "space after keyword `out'"),
|
56
|
+
many1_fail(output_def, comma_separator).err(place, "definitions of outputs").name(:outputs),
|
57
|
+
opt_fail(
|
58
|
+
many1(ws) >
|
59
|
+
symbol("use") >
|
60
|
+
many1(ws).err(place, "space after keyword `use'") >
|
61
|
+
many1_fail(load_path, comma_separator).err(place, "definitions of include-files")
|
62
|
+
).map{|x| x == [] ? [] : x[0]}.name(:uses),
|
63
|
+
many1(ws).err(place, "space before top-definitions"),
|
64
|
+
many_fail(module_top_def, many(ws)).map(&:flatten).err(place, "top definitions").name(:defs),
|
65
|
+
many(ws),
|
66
|
+
end_of_input.err("module-file", "valid end of file")
|
67
|
+
).map do |x|
|
68
|
+
t = Top.new(:inputs => x[:inputs], :outputs => x[:outputs], :uses => x[:uses], :module_name => x[:name])
|
69
|
+
x[:defs].each do |d|
|
70
|
+
k = case d
|
71
|
+
when DataDef then :datas
|
72
|
+
when FuncDef then :funcs
|
73
|
+
when NodeDef then :nodes
|
74
|
+
when TypeDef then :types
|
75
|
+
when InfixDef then :infixes
|
76
|
+
when PrimTypeDef then :ptypes
|
77
|
+
when PrimFuncDef then :pfuncs
|
78
|
+
when CommandDef then :commands
|
79
|
+
when NewNodeDef then :newnodes
|
80
|
+
end
|
81
|
+
t[k] << d
|
82
|
+
end
|
83
|
+
t
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
parser :material_file do
|
88
|
+
place = "material-file"
|
89
|
+
seq(
|
90
|
+
many(ws),
|
91
|
+
symbol("material").name(:keyword1),
|
92
|
+
many1(ws),
|
93
|
+
ident_begin_upper.name(:name),
|
94
|
+
opt_fail(
|
95
|
+
many1(ws) >
|
96
|
+
symbol("use") >
|
97
|
+
many1(ws) >
|
98
|
+
many1_fail(load_path, comma_separator)
|
99
|
+
).to_nil.name(:uses),
|
100
|
+
many1(ws),
|
101
|
+
many_fail(material_top_def, many(ws)).map(&:flatten).name(:defs),
|
102
|
+
many(ws),
|
103
|
+
end_of_input.err("module", "valid end of file")
|
104
|
+
).map do |x|
|
105
|
+
t = Top.new(:uses => x[:uses], :module_name => nil)
|
106
|
+
x[:defs].each do |d|
|
107
|
+
k = case d
|
108
|
+
when DataDef then :datas
|
109
|
+
when FuncDef then :funcs
|
110
|
+
when TypeDef then :types
|
111
|
+
when InfixDef then :infixes
|
112
|
+
when PrimTypeDef then :ptypes
|
113
|
+
when PrimFuncDef then :pfuncs
|
114
|
+
when CommandDef then :commands
|
115
|
+
end
|
116
|
+
t[k] << d
|
117
|
+
end
|
118
|
+
t
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
parser :oneline_file do
|
123
|
+
many(ws) > appendable_top_def < many(ws) < end_of_input
|
124
|
+
end
|
125
|
+
|
126
|
+
parser :load_path do
|
127
|
+
many1(ident_begin_upper, str("."))
|
128
|
+
end
|
129
|
+
|
130
|
+
parser :input_def do
|
131
|
+
seq(
|
132
|
+
var_name.name(:name),
|
133
|
+
opt_fail(
|
134
|
+
symbol("(") > exp < symbol(")")
|
135
|
+
).to_nil.name(:init_exp),
|
136
|
+
many(ws),
|
137
|
+
str(":"),
|
138
|
+
many(ws),
|
139
|
+
type.err("param-def", "type").name(:type)
|
140
|
+
).map do |x|
|
141
|
+
InputDef.new(x.to_h)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
parser :output_def do
|
146
|
+
seq(
|
147
|
+
var_name.name(:name),
|
148
|
+
opt_fail(
|
149
|
+
many(ws) > str(":") > many(ws) >
|
150
|
+
type.err("param-def", "type")
|
151
|
+
).to_nil.name(:type)
|
152
|
+
).map do |x|
|
153
|
+
OutputDef.new(x.to_h)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
parser :data_def do # -> DataDef
|
158
|
+
seq(
|
159
|
+
symbol("data").name(:keyword),
|
160
|
+
many1(ws),
|
161
|
+
data_name.err("data-def", "name of data").name(:name),
|
162
|
+
opt_fail(
|
163
|
+
many(ws) >
|
164
|
+
str(":") >
|
165
|
+
many(ws) >
|
166
|
+
type.err("data-def", "type")
|
167
|
+
).to_nil.name(:type),
|
168
|
+
many(ws),
|
169
|
+
str("="),
|
170
|
+
many(ws),
|
171
|
+
exp.err("data-def", "valid expression").name(:exp),
|
172
|
+
end_of_def.err("data-def", "valid end of data-def")
|
173
|
+
).map do |x|
|
174
|
+
DataDef.new(x.to_h)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
parser :func_def do # -> FuncDef
|
179
|
+
seq(
|
180
|
+
symbol("func").name(:keyword),
|
181
|
+
many1(ws),
|
182
|
+
(func_name | operator).err("func-def", "name of func").name(:name),
|
183
|
+
many(ws),
|
184
|
+
str("("),
|
185
|
+
many1_fail(func_param_def, comma_separator).err("func-def", "list of param for function").name(:params),
|
186
|
+
str(")"),
|
187
|
+
opt_fail(
|
188
|
+
many(ws) >
|
189
|
+
str(":") >
|
190
|
+
many(ws) >
|
191
|
+
type_with_var.err("func-def", "type of return value")
|
192
|
+
).to_nil.name(:type),
|
193
|
+
many(ws),
|
194
|
+
str("="),
|
195
|
+
many(ws),
|
196
|
+
exp.err("func-def", "valid expression").name(:exp),
|
197
|
+
end_of_def.err("func-def", "valid end of func-def")
|
198
|
+
).map do |x|
|
199
|
+
FuncDef.new(x.to_h)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
parser :node_def do # -> [NodeDef]
|
204
|
+
seq(
|
205
|
+
symbol("node").name(:keyword),
|
206
|
+
opt_fail(many1(ws) > init_def).to_nil.name(:init_exp),
|
207
|
+
opt_fail(many1(ws) > from_def).to_nil.name(:params),
|
208
|
+
many1(ws),
|
209
|
+
pattern.name(:pattern),
|
210
|
+
many(ws),
|
211
|
+
str("="),
|
212
|
+
many(ws),
|
213
|
+
exp.err("node-def", "body").name(:exp),
|
214
|
+
end_of_def.err("node-def", "valid end of node-def")
|
215
|
+
).map do |x|
|
216
|
+
refs = x[:pattern].find_refs
|
217
|
+
if x[:pattern][:ref]
|
218
|
+
whole_name = x[:pattern][:ref]
|
219
|
+
else
|
220
|
+
whole_name = SSymbol.new(
|
221
|
+
:desc => "anonymous" + x[:pattern].object_id.abs.to_s,
|
222
|
+
:keyword => x[:pattern].deep_copy
|
223
|
+
)
|
224
|
+
end
|
225
|
+
whole_node = NodeDef.new(
|
226
|
+
:keyword => x[:keyword],
|
227
|
+
:init_exp => x[:init_exp],
|
228
|
+
:params => x[:params],
|
229
|
+
:exp => x[:exp],
|
230
|
+
:type => x[:pattern][:type],
|
231
|
+
:name => whole_name
|
232
|
+
)
|
233
|
+
if refs.size == 0
|
234
|
+
[]
|
235
|
+
else
|
236
|
+
gen = proc do |left_exp, ref|
|
237
|
+
c = Case.new(:pattern => x[:pattern].deep_copy, :exp => VarRef.new(:name => ref))
|
238
|
+
MatchExp.new(:exp => left_exp, :cases => [c])
|
239
|
+
end
|
240
|
+
child_nodes = refs.reject{|x| x == whole_name}.map do |ref|
|
241
|
+
init_exp = x[:init_exp] ? gen.call(x[:init_exp].deep_copy, ref) : nil
|
242
|
+
params = [NodeRef.new(:name => whole_name, :as => whole_name, :last => false)]
|
243
|
+
exp = gen.call(VarRef.new(:name => whole_name), ref)
|
244
|
+
NodeDef.new(
|
245
|
+
:keyword => x[:keyword],
|
246
|
+
:init_exp => init_exp,
|
247
|
+
:params => params,
|
248
|
+
:name => ref,
|
249
|
+
:type => nil,
|
250
|
+
:exp => exp
|
251
|
+
)
|
252
|
+
end
|
253
|
+
[whole_node] + child_nodes
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
parser :type_def do # -> TypeDef
|
259
|
+
seq(
|
260
|
+
symbol("type").name(:keyword),
|
261
|
+
opt(many1(ws) > str("static")).map{|x| x == [] ? false : true}.name(:static),
|
262
|
+
many1(ws),
|
263
|
+
type_with_param.err("type-def", "type with param").name(:type),
|
264
|
+
many(ws),
|
265
|
+
opt_fail(
|
266
|
+
seq(
|
267
|
+
str("["),
|
268
|
+
many1_fail(type_var, comma_separator).err("type-def", "valid params"),
|
269
|
+
str("]").err("type-def", "']' after params")
|
270
|
+
).map{|x| x[1]}
|
271
|
+
).map{|x| x.flatten}.name(:params),
|
272
|
+
many(ws),
|
273
|
+
str("=").err("type-def", "'='"),
|
274
|
+
many(ws),
|
275
|
+
many1_fail(tvalue_def, or_separator).err("type-def", "value constructors").name(:tvalues),
|
276
|
+
end_of_def.err("type-def", "valid end of type-def")
|
277
|
+
).map do |x|
|
278
|
+
TypeDef.new(x.to_h, :name => x[:type][:name])
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
parser :infix_def do # -> InfixDef
|
283
|
+
seq(
|
284
|
+
(symbol("infixl") | symbol("infixr") | symbol("infix")).name(:type),
|
285
|
+
opt(many1(ws) > digit_symbol).to_nil.name(:priority),
|
286
|
+
many1(ws),
|
287
|
+
operator_general.err("infix-def", "operator").name(:op),
|
288
|
+
end_of_def
|
289
|
+
). map do |x|
|
290
|
+
InfixDef.new(x.to_h)
|
291
|
+
end
|
292
|
+
end
|
293
|
+
|
294
|
+
parser :primtype_def do
|
295
|
+
seq(
|
296
|
+
symbol("primtype").name(:keyword),
|
297
|
+
many1(ws),
|
298
|
+
type_symbol.err("primtype-def", "type symbol to be define").name(:name),
|
299
|
+
many(ws),
|
300
|
+
str("=").err("primtype-def", "'='"),
|
301
|
+
many(ws),
|
302
|
+
many1(foreign_exp, comma_separator).err("primtype-def", "foreign-definitions").name(:foreigns),
|
303
|
+
end_of_def.err("primtype-def", "valid end of primtype-def")
|
304
|
+
).map do |x|
|
305
|
+
PrimTypeDef.new(x.to_h)
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
parser :primfunc_def do
|
310
|
+
seq(
|
311
|
+
symbol("primfunc").name(:keyword),
|
312
|
+
many1(ws),
|
313
|
+
(func_name | operator).err("primfunc-def", "name of primfunc").name(:name),
|
314
|
+
many(ws),
|
315
|
+
str("("),
|
316
|
+
many1_fail(primfunc_param_def, comma_separator).err("primfunc-def", "list of param for function").name(:params),
|
317
|
+
str(")"),
|
318
|
+
many(ws),
|
319
|
+
str(":"),
|
320
|
+
many(ws),
|
321
|
+
type_with_var.err("func-def", "type of return value").name(:type),
|
322
|
+
many(ws),
|
323
|
+
str("=").err("primfunc-def", "'='"),
|
324
|
+
many(ws),
|
325
|
+
many1(foreign_exp, comma_separator).err("primfunc-def", "foreign-definitions").name(:foreigns),
|
326
|
+
end_of_def.err("primfunc-def", "valid end of primfunc-def")
|
327
|
+
).map do |x|
|
328
|
+
PrimFuncDef.new(x.to_h)
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
332
|
+
parser :command_def do
|
333
|
+
seq(
|
334
|
+
symbol("#@").name(:keyword1),
|
335
|
+
many(non_newline).name(:command),
|
336
|
+
char("\n"),
|
337
|
+
many(str("#-") > many(non_newline) < char("\n")).name(:following_lines)
|
338
|
+
).map do |x|
|
339
|
+
lines = [x[:command]] + x[:following_lines]
|
340
|
+
CommandDef.new(
|
341
|
+
:line_number => x[:keyword1][:start_pos][:line_number],
|
342
|
+
:file_name => x[:keyword1][:start_pos][:document_name],
|
343
|
+
:command_str => lines.map{|line| line.map(&:item).join}.join(" ")
|
344
|
+
)
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
parser :newnode_def do
|
349
|
+
seq(
|
350
|
+
symbol("newnode").name(:keyword1),
|
351
|
+
many1(ws),
|
352
|
+
many1_fail(ident_begin_lower, comma_separator).name(:names),
|
353
|
+
many(ws),
|
354
|
+
char("="),
|
355
|
+
many(ws),
|
356
|
+
load_path.name(:module_path),
|
357
|
+
many(ws),
|
358
|
+
char("("),
|
359
|
+
many(ws),
|
360
|
+
many_fail(exp, comma_separator).name(:args),
|
361
|
+
many(ws),
|
362
|
+
symbol(")").name(:keyword2)
|
363
|
+
).map do |x|
|
364
|
+
NewNodeDef.new(x.to_h)
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
# Func associated
|
369
|
+
# --------------------
|
370
|
+
|
371
|
+
parser :func_param_def do # -> ParamDef
|
372
|
+
seq(
|
373
|
+
var_name.name(:name),
|
374
|
+
opt_fail(
|
375
|
+
many(ws) >
|
376
|
+
str(":") >
|
377
|
+
many(ws) >
|
378
|
+
type_with_var.err("param-def", "type with type-var")
|
379
|
+
).to_nil.name(:type)
|
380
|
+
).map do |x|
|
381
|
+
ParamDef.new(x.to_h)
|
382
|
+
end
|
383
|
+
end
|
384
|
+
|
385
|
+
parser :primfunc_param_def do # -> ParamDef
|
386
|
+
seq(
|
387
|
+
var_name.name(:name),
|
388
|
+
many(ws),
|
389
|
+
str(":"),
|
390
|
+
many(ws),
|
391
|
+
type_with_var.err("param-def", "type with type-var").name(:type)
|
392
|
+
).map do |x|
|
393
|
+
ParamDef.new(x.to_h)
|
394
|
+
end
|
395
|
+
end
|
396
|
+
|
397
|
+
|
398
|
+
# Body associated
|
399
|
+
# --------------------
|
400
|
+
|
401
|
+
parser :foreign_exp do
|
402
|
+
seq(
|
403
|
+
ident_begin_lower.name(:language),
|
404
|
+
symbol("{").name(:keyword1),
|
405
|
+
many(ws),
|
406
|
+
many1(notchar("}")).name(:items),
|
407
|
+
symbol("}").err("body-def", "'}' after c-expression").name(:keyword2)
|
408
|
+
).map do |x|
|
409
|
+
ForeignExp.new(
|
410
|
+
:language => x[:language],
|
411
|
+
:keyword1 => x[:keyword1],
|
412
|
+
:keyword2 => x[:keyword2],
|
413
|
+
:desc => x[:items].map(&:item).join.strip,
|
414
|
+
)
|
415
|
+
end
|
416
|
+
end
|
417
|
+
|
418
|
+
# Node associated
|
419
|
+
# --------------------
|
420
|
+
|
421
|
+
parser :init_def do # -> InitDef
|
422
|
+
seq(
|
423
|
+
symbol("init").name(:keyword1),
|
424
|
+
many(ws),
|
425
|
+
str("["),
|
426
|
+
many(ws),
|
427
|
+
exp.name(:exp),
|
428
|
+
many(ws),
|
429
|
+
symbol("]").name(:keyword)
|
430
|
+
).map do |x|
|
431
|
+
x[:exp]
|
432
|
+
end
|
433
|
+
end
|
434
|
+
|
435
|
+
parser :from_def do
|
436
|
+
seq(
|
437
|
+
symbol("from"),
|
438
|
+
str("[").err("from-def", "["),
|
439
|
+
many_fail(node_ref, comma_separator).err("from-def", "list of depended nodes").name(:params),
|
440
|
+
str("]").err("from-def", "]"),
|
441
|
+
).map do |x|
|
442
|
+
x[:params]
|
443
|
+
end
|
444
|
+
end
|
445
|
+
|
446
|
+
parser :node_ref do
|
447
|
+
node_name_last | node_name_current
|
448
|
+
end
|
449
|
+
|
450
|
+
parser :node_name_last do
|
451
|
+
seq(
|
452
|
+
node_instance_name.name(:name),
|
453
|
+
symbol("@last").name(:keyword),
|
454
|
+
opt_fail(many(ws) > str("as") > many(ws) > var_name.err("node-parameter", "name as exposed")).to_nil.name(:as)
|
455
|
+
).map do |x|
|
456
|
+
as = x[:as] || SSymbol.new(
|
457
|
+
:desc => x[:name][:desc] + "@last",
|
458
|
+
:start_pos => x[:name][:start_pos],
|
459
|
+
:end_pos => x[:keyword][:end_pos]
|
460
|
+
)
|
461
|
+
NodeRef.new(:last => true, :as => as, :name => x[:name])
|
462
|
+
end
|
463
|
+
end
|
464
|
+
|
465
|
+
parser :node_name_current do
|
466
|
+
seq(
|
467
|
+
node_instance_name.name(:name),
|
468
|
+
opt_fail(many(ws) > str("as") > many(ws) > var_name.err("node-parameter", "name as exposed")).to_nil.name(:as)
|
469
|
+
).map do |x|
|
470
|
+
NodeRef.new(:last => false, :as => x[:as] || x[:name], :name => x[:name])
|
471
|
+
end
|
472
|
+
end
|
473
|
+
|
474
|
+
# Type associated
|
475
|
+
# --------------------
|
476
|
+
|
477
|
+
parser :type_with_args do |inner|
|
478
|
+
seq(
|
479
|
+
type_symbol.name(:name),
|
480
|
+
symbol("[").name(:keyword1),
|
481
|
+
many1_fail(inner, comma_separator).err("type", "list of type").name(:args),
|
482
|
+
symbol("]").err("type", "']'").name(:keyword2)
|
483
|
+
).map do |x|
|
484
|
+
Type.new(x.to_h)
|
485
|
+
end
|
486
|
+
end
|
487
|
+
|
488
|
+
parser :type_without_args do
|
489
|
+
type_symbol.map do |x|
|
490
|
+
Type.new(:name => x, :args => [])
|
491
|
+
end
|
492
|
+
end
|
493
|
+
|
494
|
+
parser :type_tuple do |inner|
|
495
|
+
seq(
|
496
|
+
symbol("(").name(:keyword1),
|
497
|
+
many1_fail(inner, comma_separator).err("type", "list of type").name(:args),
|
498
|
+
symbol(")").err("type", "')'").name(:keyword2)
|
499
|
+
).map do |x|
|
500
|
+
type_name = SSymbol.new(:desc => "Tuple" + x[:args].size.to_s)
|
501
|
+
Type.new(x.to_h, :name => type_name)
|
502
|
+
end
|
503
|
+
end
|
504
|
+
|
505
|
+
parser :type_symbol do
|
506
|
+
ident_begin_upper
|
507
|
+
end
|
508
|
+
|
509
|
+
parser :tvalue_symbol do
|
510
|
+
ident_begin_upper
|
511
|
+
end
|
512
|
+
|
513
|
+
parser :type_var do # => TypeVar
|
514
|
+
ident_begin_lower.map do |s|
|
515
|
+
TypeVar.new(:name => s)
|
516
|
+
end
|
517
|
+
end
|
518
|
+
|
519
|
+
parser :type do
|
520
|
+
type_with_args(type) ^ type_tuple(type) ^ type_without_args
|
521
|
+
end
|
522
|
+
|
523
|
+
parser :type_with_var do
|
524
|
+
type_with_args(type_with_var) ^ type_tuple(type_with_var) ^ type_without_args ^ type_var
|
525
|
+
end
|
526
|
+
|
527
|
+
parser :type_with_param do
|
528
|
+
type_with_args(type_var) ^ type_tuple(type_var) ^ type_without_args
|
529
|
+
end
|
530
|
+
|
531
|
+
parser :tvalue_def do # -> TValue
|
532
|
+
seq(
|
533
|
+
tvalue_symbol.name(:name),
|
534
|
+
opt_fail(
|
535
|
+
many(ws) >
|
536
|
+
str("(") >
|
537
|
+
many1_fail(tvalue_def_type, comma_separator) <
|
538
|
+
str(")").err("value-constructor-def", "')'")
|
539
|
+
).map{|x| x.flatten}.name(:params),
|
540
|
+
).map do |x|
|
541
|
+
TValue.new(x.to_h)
|
542
|
+
end
|
543
|
+
end
|
544
|
+
|
545
|
+
parser :tvalue_def_type do # -> TValueParam
|
546
|
+
colon = (many(ws) < str(":") < many(ws))
|
547
|
+
seq(
|
548
|
+
opt_fail(func_name < colon).to_nil.name(:name),
|
549
|
+
type_with_var.name(:type)
|
550
|
+
).map do |x|
|
551
|
+
TValueParam.new(x.to_h)
|
552
|
+
end
|
553
|
+
end
|
554
|
+
end
|
555
|
+
end
|