delorean_lang 0.3.1 → 0.3.2
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 +8 -8
- data/lib/delorean/base.rb +17 -2
- data/lib/delorean/delorean.rb +111 -82
- data/lib/delorean/delorean.treetop +5 -0
- data/lib/delorean/engine.rb +1 -1
- data/lib/delorean/nodes.rb +11 -0
- data/lib/delorean/version.rb +1 -1
- data/spec/eval_spec.rb +20 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZDc1Y2Y0NzUxOGNkMzlmNmE2Y2M5OGM3YjBiOGNiNjBjZTM5OTY1Ng==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZDgxMGNiOTM4NDhhM2Q3NDM0ZWQ4NjU3NDIzMDc4NzNlNDE4YzUxZg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MjM0ODI5NmY0NmU4MGE1YWQ5ZmVlMDFkNThmYzdiNGQxNjJmZmE3OGZiNzNl
|
10
|
+
NmRlYzQwZTcyZjNmOThlNTExNDNmZmRiZmQ1MTMyNWFlODMzOTk2NjQzYWNi
|
11
|
+
ZDNjNmNlYzlmNzk5ZTU0Nzk4Y2Q5YzYwNmEzOWFlOWQyYWI0ODk=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NGQwM2M5YWU2MTAwNTlhNGE0ZmU4YjI4NmU5OWE4OTkwNjk0MGRlYTVhZGUz
|
14
|
+
ZDQ3ZTFlNjQ1OTU1MWExZDM0YjI0ZDRiZmMzMzFmNzkwNGM1YThhMjg2MmEz
|
15
|
+
NjZjMjAxZmM1ZmIwNjBjMjY0ZDAyMTc5NTk3ZGFlMzE0MzZlZmU=
|
data/lib/delorean/base.rb
CHANGED
@@ -69,7 +69,9 @@ module Delorean
|
|
69
69
|
class NodeCall < Struct.new(:_e, :engine, :node, :params)
|
70
70
|
def evaluate(attr)
|
71
71
|
# FIXME: evaluate() modifies params! => need to clone it.
|
72
|
-
# This is pretty awful.
|
72
|
+
# This is pretty awful. NOTE: can't sanitize params as Marty
|
73
|
+
# patches NodeCall and modifies params to send _parent_id.
|
74
|
+
# This whole thing needs to be redone.
|
73
75
|
engine.evaluate(node, attr, params.clone)
|
74
76
|
end
|
75
77
|
|
@@ -77,7 +79,7 @@ module Delorean
|
|
77
79
|
raise "bad arg to %" unless args.is_a?(Array)
|
78
80
|
|
79
81
|
# FIXME: params.clone!!!!
|
80
|
-
engine.
|
82
|
+
engine.eval_to_hash(node, args, params.clone)
|
81
83
|
end
|
82
84
|
|
83
85
|
# add new arguments, results in a new NodeCall
|
@@ -86,6 +88,10 @@ module Delorean
|
|
86
88
|
|
87
89
|
NodeCall.new(_e, engine, node, params.merge(args))
|
88
90
|
end
|
91
|
+
|
92
|
+
def sanitized_params
|
93
|
+
BaseClass._sanitize_hash(params)
|
94
|
+
end
|
89
95
|
end
|
90
96
|
|
91
97
|
class BaseClass
|
@@ -147,6 +153,15 @@ module Delorean
|
|
147
153
|
|
148
154
|
######################################################################
|
149
155
|
|
156
|
+
def self._sanitize_hash(_e)
|
157
|
+
_e.each_with_object({}) do
|
158
|
+
|(k,v), h|
|
159
|
+
h[k] = v if k =~ /\A[a-z][A-Za-z0-9_]*\z/
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
######################################################################
|
164
|
+
|
150
165
|
def self._err(*args)
|
151
166
|
str = args.map(&:to_s).join(", ")
|
152
167
|
raise str
|
data/lib/delorean/delorean.rb
CHANGED
@@ -2308,112 +2308,117 @@ module Delorean
|
|
2308
2308
|
if r5
|
2309
2309
|
r0 = r5
|
2310
2310
|
else
|
2311
|
-
r6 =
|
2311
|
+
r6 = _nt_self
|
2312
2312
|
if r6
|
2313
2313
|
r0 = r6
|
2314
2314
|
else
|
2315
|
-
r7 =
|
2315
|
+
r7 = _nt_list_expr
|
2316
2316
|
if r7
|
2317
2317
|
r0 = r7
|
2318
2318
|
else
|
2319
|
-
r8 =
|
2319
|
+
r8 = _nt_set_expr
|
2320
2320
|
if r8
|
2321
2321
|
r0 = r8
|
2322
2322
|
else
|
2323
|
-
|
2324
|
-
i11, s11 = index, []
|
2325
|
-
r12 = _nt_class_name
|
2326
|
-
s11 << r12
|
2327
|
-
if r12
|
2328
|
-
if has_terminal?('::', false, index)
|
2329
|
-
r13 = instantiate_node(SyntaxNode,input, index...(index + 2))
|
2330
|
-
@index += 2
|
2331
|
-
else
|
2332
|
-
terminal_parse_failure('::')
|
2333
|
-
r13 = nil
|
2334
|
-
end
|
2335
|
-
s11 << r13
|
2336
|
-
end
|
2337
|
-
if s11.last
|
2338
|
-
r11 = instantiate_node(SyntaxNode,input, i11...index, s11)
|
2339
|
-
r11.extend(Value0)
|
2340
|
-
else
|
2341
|
-
@index = i11
|
2342
|
-
r11 = nil
|
2343
|
-
end
|
2344
|
-
if r11
|
2345
|
-
r10 = r11
|
2346
|
-
else
|
2347
|
-
r10 = instantiate_node(SyntaxNode,input, index...index)
|
2348
|
-
end
|
2349
|
-
s9 << r10
|
2350
|
-
if r10
|
2351
|
-
r14 = _nt_class_name
|
2352
|
-
s9 << r14
|
2353
|
-
end
|
2354
|
-
if s9.last
|
2355
|
-
r9 = instantiate_node(NodeAsValue,input, i9...index, s9)
|
2356
|
-
r9.extend(Value1)
|
2357
|
-
else
|
2358
|
-
@index = i9
|
2359
|
-
r9 = nil
|
2360
|
-
end
|
2323
|
+
r9 = _nt_hash_expr
|
2361
2324
|
if r9
|
2362
2325
|
r0 = r9
|
2363
2326
|
else
|
2364
|
-
|
2365
|
-
|
2366
|
-
|
2367
|
-
|
2327
|
+
i10, s10 = index, []
|
2328
|
+
i12, s12 = index, []
|
2329
|
+
r13 = _nt_class_name
|
2330
|
+
s12 << r13
|
2331
|
+
if r13
|
2332
|
+
if has_terminal?('::', false, index)
|
2333
|
+
r14 = instantiate_node(SyntaxNode,input, index...(index + 2))
|
2334
|
+
@index += 2
|
2335
|
+
else
|
2336
|
+
terminal_parse_failure('::')
|
2337
|
+
r14 = nil
|
2338
|
+
end
|
2339
|
+
s12 << r14
|
2340
|
+
end
|
2341
|
+
if s12.last
|
2342
|
+
r12 = instantiate_node(SyntaxNode,input, i12...index, s12)
|
2343
|
+
r12.extend(Value0)
|
2344
|
+
else
|
2345
|
+
@index = i12
|
2346
|
+
r12 = nil
|
2347
|
+
end
|
2348
|
+
if r12
|
2349
|
+
r11 = r12
|
2350
|
+
else
|
2351
|
+
r11 = instantiate_node(SyntaxNode,input, index...index)
|
2352
|
+
end
|
2353
|
+
s10 << r11
|
2354
|
+
if r11
|
2355
|
+
r15 = _nt_class_name
|
2356
|
+
s10 << r15
|
2357
|
+
end
|
2358
|
+
if s10.last
|
2359
|
+
r10 = instantiate_node(NodeAsValue,input, i10...index, s10)
|
2360
|
+
r10.extend(Value1)
|
2368
2361
|
else
|
2369
|
-
|
2370
|
-
|
2362
|
+
@index = i10
|
2363
|
+
r10 = nil
|
2371
2364
|
end
|
2372
|
-
|
2373
|
-
|
2374
|
-
|
2375
|
-
|
2376
|
-
|
2365
|
+
if r10
|
2366
|
+
r0 = r10
|
2367
|
+
else
|
2368
|
+
i16, s16 = index, []
|
2369
|
+
if has_terminal?('(', false, index)
|
2370
|
+
r17 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
2371
|
+
@index += 1
|
2377
2372
|
else
|
2378
|
-
|
2373
|
+
terminal_parse_failure('(')
|
2374
|
+
r17 = nil
|
2379
2375
|
end
|
2380
|
-
|
2376
|
+
s16 << r17
|
2381
2377
|
if r17
|
2382
|
-
r19 =
|
2383
|
-
s15 << r19
|
2378
|
+
r19 = _nt_sp
|
2384
2379
|
if r19
|
2385
|
-
|
2386
|
-
|
2387
|
-
|
2388
|
-
|
2389
|
-
|
2390
|
-
|
2391
|
-
|
2380
|
+
r18 = r19
|
2381
|
+
else
|
2382
|
+
r18 = instantiate_node(SyntaxNode,input, index...index)
|
2383
|
+
end
|
2384
|
+
s16 << r18
|
2385
|
+
if r18
|
2386
|
+
r20 = _nt_expression
|
2387
|
+
s16 << r20
|
2392
2388
|
if r20
|
2393
|
-
|
2394
|
-
|
2395
|
-
|
2389
|
+
r22 = _nt_sp
|
2390
|
+
if r22
|
2391
|
+
r21 = r22
|
2396
2392
|
else
|
2397
|
-
|
2398
|
-
|
2393
|
+
r21 = instantiate_node(SyntaxNode,input, index...index)
|
2394
|
+
end
|
2395
|
+
s16 << r21
|
2396
|
+
if r21
|
2397
|
+
if has_terminal?(')', false, index)
|
2398
|
+
r23 = instantiate_node(SyntaxNode,input, index...(index + 1))
|
2399
|
+
@index += 1
|
2400
|
+
else
|
2401
|
+
terminal_parse_failure(')')
|
2402
|
+
r23 = nil
|
2403
|
+
end
|
2404
|
+
s16 << r23
|
2399
2405
|
end
|
2400
|
-
s15 << r22
|
2401
2406
|
end
|
2402
2407
|
end
|
2403
2408
|
end
|
2404
|
-
|
2405
|
-
|
2406
|
-
|
2407
|
-
|
2408
|
-
|
2409
|
-
|
2410
|
-
|
2411
|
-
|
2412
|
-
|
2413
|
-
|
2414
|
-
|
2415
|
-
|
2416
|
-
|
2409
|
+
if s16.last
|
2410
|
+
r16 = instantiate_node(Expr,input, i16...index, s16)
|
2411
|
+
r16.extend(Value2)
|
2412
|
+
else
|
2413
|
+
@index = i16
|
2414
|
+
r16 = nil
|
2415
|
+
end
|
2416
|
+
if r16
|
2417
|
+
r0 = r16
|
2418
|
+
else
|
2419
|
+
@index = i0
|
2420
|
+
r0 = nil
|
2421
|
+
end
|
2417
2422
|
end
|
2418
2423
|
end
|
2419
2424
|
end
|
@@ -3049,6 +3054,30 @@ module Delorean
|
|
3049
3054
|
r0
|
3050
3055
|
end
|
3051
3056
|
|
3057
|
+
def _nt_self
|
3058
|
+
start_index = index
|
3059
|
+
if node_cache[:self].has_key?(index)
|
3060
|
+
cached = node_cache[:self][index]
|
3061
|
+
if cached
|
3062
|
+
cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
|
3063
|
+
@index = cached.interval.end
|
3064
|
+
end
|
3065
|
+
return cached
|
3066
|
+
end
|
3067
|
+
|
3068
|
+
if has_terminal?('_', false, index)
|
3069
|
+
r0 = instantiate_node(Self,input, index...(index + 1))
|
3070
|
+
@index += 1
|
3071
|
+
else
|
3072
|
+
terminal_parse_failure('_')
|
3073
|
+
r0 = nil
|
3074
|
+
end
|
3075
|
+
|
3076
|
+
node_cache[:self][start_index] = r0
|
3077
|
+
|
3078
|
+
r0
|
3079
|
+
end
|
3080
|
+
|
3052
3081
|
def _nt_nil_val
|
3053
3082
|
start_index = index
|
3054
3083
|
if node_cache[:nil_val].has_key?(index)
|
@@ -105,6 +105,7 @@ grammar Delorean
|
|
105
105
|
boolean /
|
106
106
|
nil_val /
|
107
107
|
identifier /
|
108
|
+
self /
|
108
109
|
list_expr /
|
109
110
|
set_expr /
|
110
111
|
hash_expr /
|
@@ -144,6 +145,10 @@ grammar Delorean
|
|
144
145
|
'true' <Literal> / 'false' <Literal>
|
145
146
|
end
|
146
147
|
|
148
|
+
rule self
|
149
|
+
'_' <Self>
|
150
|
+
end
|
151
|
+
|
147
152
|
rule nil_val
|
148
153
|
'nil' <Literal>
|
149
154
|
end
|
data/lib/delorean/engine.rb
CHANGED
@@ -343,7 +343,7 @@ module Delorean
|
|
343
343
|
evaluate_attrs(node, [attr], params)[0]
|
344
344
|
end
|
345
345
|
|
346
|
-
def
|
346
|
+
def eval_to_hash(node, attrs, params={})
|
347
347
|
res = evaluate_attrs(node, attrs, params)
|
348
348
|
Hash[* attrs.zip(res).flatten(1)]
|
349
349
|
end
|
data/lib/delorean/nodes.rb
CHANGED
@@ -202,6 +202,17 @@ eos
|
|
202
202
|
end
|
203
203
|
end
|
204
204
|
|
205
|
+
# _ is self -- a naive implementation of "self" for now.
|
206
|
+
class Self < SNode
|
207
|
+
def check(context, *)
|
208
|
+
[]
|
209
|
+
end
|
210
|
+
|
211
|
+
def rewrite(context)
|
212
|
+
"_sanitize_hash(_e)"
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
205
216
|
class DString < Literal
|
206
217
|
def rewrite(context)
|
207
218
|
# remove the quotes and requote. We don't want the likes of #{}
|
data/lib/delorean/version.rb
CHANGED
data/spec/eval_spec.rb
CHANGED
@@ -244,7 +244,7 @@ describe "Delorean" do
|
|
244
244
|
}.should raise_error(Delorean::InvalidGetAttribute)
|
245
245
|
end
|
246
246
|
|
247
|
-
it "should be able to get attr on
|
247
|
+
it "should be able to get attr on AR objs using Class.method().attr syntax" do
|
248
248
|
engine.parse defn("A:",
|
249
249
|
' b = Dummy.i_just_met_you("CRJ", 1.234).name',
|
250
250
|
)
|
@@ -252,7 +252,7 @@ describe "Delorean" do
|
|
252
252
|
r.should == "CRJ"
|
253
253
|
end
|
254
254
|
|
255
|
-
it "should be able to access ActiveRecord whitelisted fns using .
|
255
|
+
it "should be able to access ActiveRecord whitelisted fns using .x syntax" do
|
256
256
|
engine.parse defn("A:",
|
257
257
|
' b = Dummy.i_just_met_you("CRJ", 1.234).name2',
|
258
258
|
)
|
@@ -809,4 +809,22 @@ eof
|
|
809
809
|
engine.evaluate("B", "m").should == [5, 2]
|
810
810
|
end
|
811
811
|
|
812
|
+
it "implements simple version of self (_)" do
|
813
|
+
engine.parse defn("B:",
|
814
|
+
" a =?",
|
815
|
+
" b =?",
|
816
|
+
" x = a - b",
|
817
|
+
"A:",
|
818
|
+
" a =?",
|
819
|
+
" b =?",
|
820
|
+
" x = _.a * _.b",
|
821
|
+
" y = a && _",
|
822
|
+
" z = (B() + _).x",
|
823
|
+
)
|
824
|
+
|
825
|
+
engine.evaluate("A", "x", {"a"=>3, "b"=>5}).should == 15
|
826
|
+
h = {"a"=>1, "b"=>2, "c"=>3}
|
827
|
+
engine.evaluate("A", "y", {"a"=>1, "b"=>2, "c"=>3}).should == h
|
828
|
+
engine.evaluate("A", "z", {"a"=>1, "b"=>2, "c"=>3}).should == -1
|
829
|
+
end
|
812
830
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delorean_lang
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arman Bostani
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: treetop
|