raabro 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.txt CHANGED
@@ -2,6 +2,12 @@
2
2
  = raabro CHANGELOG.txt
3
3
 
4
4
 
5
+ == raabro 1.1.0 released 2016-02-09
6
+
7
+ - many improvements
8
+ - unlock custom `rewrite(t)`
9
+
10
+
5
11
  == raabro 1.0.5 released 2015-09-25
6
12
 
7
13
  - allow for .parse(s, debug: 1 to 3)
data/LICENSE.txt CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- Copyright (c) 2015-2015, John Mettraux, jmettraux@gmail.com
2
+ Copyright (c) 2015-2016, John Mettraux, jmettraux@gmail.com
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -33,23 +33,25 @@ module Fun include Raabro
33
33
 
34
34
  def num(i); rex(:num, i, /-?[0-9]+\s*/); end
35
35
 
36
- def args(i); eseq(:args, i, :pa, :exp, :com, :pz); end
37
- def funame(i); rex(:funame, i, /[a-z][a-z0-9]*/); end
36
+ def args(i); eseq(nil, i, :pa, :exp, :com, :pz); end
37
+ def funame(i); rex(nil, i, /[a-z][a-z0-9]*/); end
38
38
  def fun(i); seq(:fun, i, :funame, :args); end
39
39
 
40
- def exp(i); alt(:exp, i, :fun, :num); end
40
+ def exp(i); alt(nil, i, :fun, :num); end
41
41
 
42
42
  # rewrite
43
43
  #
44
44
  # Names above (:num, :fun, ...) get a rewrite_xxx function.
45
45
  # "t" stands for "tree".
46
+ #
47
+ # The trees with a nil name are handled by rewrite_(tree) a default
48
+ # rewrite function
46
49
 
47
- def rewrite_exp(t); rewrite(t.children[0]); end
48
50
  def rewrite_num(t); t.string.to_i; end
49
51
 
50
52
  def rewrite_fun(t)
51
53
  [ t.children[0].string ] +
52
- t.children[1].children.inject([]) { |a, e| a << rewrite(e) if e.name; a }
54
+ t.children[1].odd_children.collect { |a| rewrite(a) }
53
55
  end
54
56
  end
55
57
 
@@ -66,10 +68,37 @@ p Fun.parse('mul (1, 2)')
66
68
 
67
69
  This sample is available at: [doc/readme0.rb](doc/readme0.rb).
68
70
 
71
+ ## custom rewrite()
72
+
73
+ By default, a parser gets a `rewrite(t)` that looks at the parse tree node names and calls the corresponding `rewrite_{node_name}()`.
74
+
75
+ It's OK to provide a custom `rewrite(t)` function.
76
+
77
+ ```ruby
78
+ module Hello include Raabro
79
+
80
+ def hello(i); str(:hello, i, 'hello'); end
81
+
82
+ def rewrite(t)
83
+ [ :ok, t.string ]
84
+ end
85
+ end
86
+ ```
87
+
69
88
 
70
89
  ## basic parsers
71
90
 
72
- The first parameter is the name used by rewrite rules.
91
+ One makes a parser by composing basic parsers, for example:
92
+ ```ruby
93
+ def args(i); eseq(:args, i, :pa, :exp, :com, :pz); end
94
+ def funame(i); rex(:funame, i, /[a-z][a-z0-9]*/); end
95
+ def fun(i); seq(:fun, i, :funame, :args); end
96
+ ```
97
+ where the `fun` parser is a sequence combining the `funame` parser then the `args` one. `:fun` (the first argument to the basic parser `seq`) will be the name of the resulting (local) parse tree.
98
+
99
+ Below is a list of the basic parsers provided by Raabro.
100
+
101
+ The first parameter to the basic parser is the name used by rewrite rules.
73
102
  The second parameter is a `Raabro::Input` instance, mostly a wrapped string.
74
103
 
75
104
  ```ruby
data/lib/raabro.rb CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  #--
3
- # Copyright (c) 2015-2015, John Mettraux, jmettraux@gmail.com
3
+ # Copyright (c) 2015-2016, John Mettraux, jmettraux@gmail.com
4
4
  #
5
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  # of this software and associated documentation files (the "Software"), to deal
@@ -26,7 +26,7 @@
26
26
 
27
27
  module Raabro
28
28
 
29
- VERSION = '1.0.5'
29
+ VERSION = '1.1.0'
30
30
 
31
31
  class Input
32
32
 
@@ -51,6 +51,11 @@ module Raabro
51
51
  @string[@offset, l] == s ? l : false
52
52
  end
53
53
  end
54
+
55
+ def tring(l=-1)
56
+
57
+ l < 0 ? @string[@offset..l] : @string[@offset, l]
58
+ end
54
59
  end
55
60
 
56
61
  class Tree
@@ -71,6 +76,17 @@ module Raabro
71
76
  @children = []
72
77
  end
73
78
 
79
+ def c0; @children[0]; end
80
+ def c1; @children[1]; end
81
+ def c2; @children[2]; end
82
+ def c3; @children[3]; end
83
+ def c4; @children[4]; end
84
+
85
+ def empty?
86
+
87
+ @result == 1 && @length == 0
88
+ end
89
+
74
90
  def successful_children
75
91
 
76
92
  @children.select { |c| c.result == 1 }
@@ -88,35 +104,53 @@ module Raabro
88
104
 
89
105
  def lookup(name)
90
106
 
91
- name = name.to_s
107
+ name = name ? name.to_s : nil
92
108
 
109
+ return self if @name && name == nil
93
110
  return self if @name.to_s == name
111
+ sublookup(name)
112
+ end
113
+
114
+ def sublookup(name)
115
+
94
116
  @children.each { |c| if n = c.lookup(name); return n; end }
117
+
95
118
  nil
96
119
  end
97
120
 
98
121
  def gather(name, acc=[])
99
122
 
100
- name = name.to_s
123
+ name = name ? name.to_s : nil
101
124
 
102
- if @name.to_s == name
125
+ if (@name && name == nil) || (@name.to_s == name)
103
126
  acc << self
104
127
  else
105
- @children.each { |c| c.gather(name, acc) }
128
+ subgather(name, acc)
106
129
  end
107
130
 
108
131
  acc
109
132
  end
110
133
 
134
+ def subgather(name, acc=[])
135
+
136
+ @children.each { |c| c.gather(name, acc) }
137
+
138
+ acc
139
+ end
140
+
111
141
  def to_a(opts={})
112
142
 
113
143
  opts = Array(opts).inject({}) { |h, e| h[e] = true; h } \
114
144
  unless opts.is_a?(Hash)
115
145
 
116
146
  cn =
117
- opts[:leaves] && (@result == 1) && @children.empty? ?
118
- string :
119
- @children.collect { |e| e.to_a(opts) }
147
+ if opts[:leaves] && (@result == 1) && @children.empty?
148
+ string
149
+ elsif opts[:children] != false
150
+ @children.collect { |e| e.to_a(opts) }
151
+ else
152
+ @children.length
153
+ end
120
154
 
121
155
  [ @name, @result, @offset, @length, @note, @parter, cn ]
122
156
  end
@@ -132,6 +166,16 @@ module Raabro
132
166
 
133
167
  depth == 0 ? io.string : nil
134
168
  end
169
+
170
+ def odd_children
171
+
172
+ cs = []; @children.each_with_index { |c, i| cs << c if i.odd? }; cs
173
+ end
174
+
175
+ def even_children
176
+
177
+ cs = []; @children.each_with_index { |c, i| cs << c if i.even? }; cs
178
+ end
135
179
  end
136
180
 
137
181
  module ModuleMethods
@@ -181,6 +225,10 @@ module Raabro
181
225
 
182
226
  def _parse(parser, input)
183
227
 
228
+ #p [ caller.length, parser, input.tring ]
229
+ #r = _narrow(parser).call(input)
230
+ #p [ caller.length, parser, input.tring, r.to_a(children: false) ]
231
+ #r
184
232
  _narrow(parser).call(input)
185
233
  end
186
234
 
@@ -240,9 +288,11 @@ module Raabro
240
288
  input.offset = start
241
289
 
242
290
  if greedy
243
- if cc.result == 1 && cc.length > (c ? c.length : -1)
291
+ if cc.result == 1 && cc.length >= (c ? c.length : -1)
244
292
  c.result = 0 if c
245
293
  c = cc
294
+ else
295
+ cc.result = 0
246
296
  end
247
297
  else
248
298
  c = cc
@@ -280,6 +330,7 @@ module Raabro
280
330
  r.children << c
281
331
  break if c.result != 1
282
332
  count += 1
333
+ break if c.length < 1
283
334
  break if max && count == max
284
335
  end
285
336
 
@@ -345,23 +396,28 @@ module Raabro
345
396
 
346
397
  if r.result == 1
347
398
 
348
- i = 1
349
- count = 0
399
+ i = 0
350
400
 
351
401
  loop do
352
402
 
353
- i = (i + 1) % 2
354
- pa = i == 0 ? eltpa : seppa
403
+ add = true
355
404
 
356
- c = _parse(pa, input)
357
- r.children << c
405
+ st = i > 0 ? _parse(seppa, input) : nil
406
+ et = st == nil || st.result == 1 ? _parse(eltpa, input) : nil
407
+
408
+ break if st && et && st.empty? && et.result == 0
409
+ break if st && et && st.empty? && et.empty?
410
+
411
+ r.children << st if st
412
+ r.children << et if et
358
413
 
359
- break if c.result != 1
414
+ break if et == nil
415
+ break if et.result != 1
360
416
 
361
- count += 1
417
+ i = i + 1
362
418
  end
363
419
 
364
- r.result = 0 if jseq && count < 1
420
+ r.result = 0 if jseq && i == 0
365
421
  end
366
422
 
367
423
  if r.result == 1 && endpa
@@ -415,11 +471,18 @@ module Raabro
415
471
 
416
472
  t = t.children.first if t.parter == :all
417
473
 
418
- return rewrite(t) if opts[:rewrite] != false && rewrite(0) == true
474
+ return rewrite(t) if opts[:rewrite] != false
419
475
 
420
476
  t
421
477
  end
422
478
 
479
+ def rewrite_(tree)
480
+
481
+ t = tree.lookup(nil)
482
+
483
+ t ? rewrite(t) : nil
484
+ end
485
+
423
486
  def rewrite(tree)
424
487
 
425
488
  return !! methods.find { |m| m.to_s.match(/^rewrite_/) } if tree == 0
@@ -427,15 +490,20 @@ module Raabro
427
490
 
428
491
  send("rewrite_#{tree.name}", tree)
429
492
  end
430
- end
431
- extend ModuleMethods
432
493
 
433
- def self.included(target)
494
+ def make_includable
495
+
496
+ def self.included(target)
434
497
 
435
- target.instance_eval do
436
- extend ::Raabro::ModuleMethods
437
- extend self
498
+ target.instance_eval do
499
+ extend ::Raabro::ModuleMethods
500
+ extend self
501
+ end
502
+ end
438
503
  end
439
504
  end
505
+ extend ModuleMethods
506
+
507
+ make_includable
440
508
  end
441
509
 
data/spec/alt_spec.rb CHANGED
@@ -136,6 +136,34 @@ describe Raabro do
136
136
  )
137
137
  expect(i.offset).to eq(2)
138
138
  end
139
+
140
+ it 'declares a single winner' do
141
+
142
+ i = Raabro::Input.new('xx', :prune => true)
143
+
144
+ t = Raabro.altg(nil, i, :twox, :onex)
145
+
146
+ expect(t.to_a(:leaves => true)).to eq(
147
+ [ nil, 1, 0, 2, nil, :altg, [
148
+ [ :twox, 1, 0, 2, nil, :str, 'xx' ]
149
+ ] ]
150
+ )
151
+ expect(i.offset).to eq(2)
152
+ end
153
+
154
+ it 'takes the longest and latest winner' do
155
+
156
+ i = Raabro::Input.new('xx', :prune => true)
157
+
158
+ t = Raabro.altg(nil, i, :twox, :onex, :deux)
159
+
160
+ expect(t.to_a(:leaves => true)).to eq(
161
+ [ nil, 1, 0, 2, nil, :altg, [
162
+ [ :deux, 1, 0, 2, nil, :str, 'xx' ]
163
+ ] ]
164
+ )
165
+ expect(i.offset).to eq(2)
166
+ end
139
167
  end
140
168
  end
141
169
 
data/spec/eseq_spec.rb CHANGED
@@ -49,11 +49,26 @@ describe Raabro do
49
49
  expect(i.offset).to eq(5)
50
50
  end
51
51
 
52
+ it 'parses <>' do
53
+
54
+ i = Raabro::Input.new('<>', :prune => true)
55
+
56
+ t = Raabro.eseq(:list, i, :lt, :cha, :com, :gt)
57
+
58
+ expect(t.to_a(:leaves => true)).to eq(
59
+ [ :list, 1, 0, 2, nil, :eseq, [
60
+ [ nil, 1, 0, 1, nil, :str, '<' ],
61
+ [ nil, 1, 1, 1, nil, :str, '>' ]
62
+ ] ]
63
+ )
64
+ expect(i.offset).to eq(2)
65
+ end
66
+
52
67
  context 'no start parser' do
53
68
 
54
69
  it 'parses successfully' do
55
70
 
56
- i = Raabro::Input.new('a,b>')
71
+ i = Raabro::Input.new('a,b>', prune: false)
57
72
 
58
73
  t = Raabro.eseq(:list, i, nil, :cha, :com, :gt)
59
74
 
@@ -84,7 +99,7 @@ describe Raabro do
84
99
  [ nil, 1, 1, 1, nil, :rex, 'a' ],
85
100
  [ nil, 1, 2, 1, nil, :str, ',' ],
86
101
  [ nil, 1, 3, 1, nil, :rex, 'b' ],
87
- [ nil, 0, 4, 0, nil, :str, [] ],
102
+ [ nil, 0, 4, 0, nil, :str, [] ]
88
103
  ] ]
89
104
  )
90
105
  expect(i.offset).to eq(4)
@@ -107,6 +122,45 @@ describe Raabro do
107
122
  expect(i.offset).to eq(4)
108
123
  end
109
124
  end
125
+
126
+ context 'no progress' do
127
+
128
+ it 'parses <>' do
129
+
130
+ i = Raabro::Input.new('<>', :prune => true)
131
+
132
+ t = arr(i)
133
+
134
+ expect(t.to_a(:leaves => true)).to eq(
135
+ [ nil, 1, 0, 2, nil, :eseq, [
136
+ [ nil, 1, 0, 1, nil, :str, '<' ],
137
+ [ nil, 1, 1, 0, nil, :rex, '' ],
138
+ [ nil, 1, 1, 1, nil, :str, '>' ]
139
+ ] ]
140
+ )
141
+ expect(i.offset).to eq(2)
142
+ end
143
+
144
+ it 'parses <a,,a>' do
145
+
146
+ i = Raabro::Input.new('<a,,a>', :prune => true)
147
+
148
+ t = arr(i)
149
+
150
+ expect(t.to_a(:leaves => true)).to eq(
151
+ [ nil, 1, 0, 6, nil, :eseq, [
152
+ [ nil, 1, 0, 1, nil, :str, '<' ],
153
+ [ nil, 1, 1, 1, nil, :rex, 'a' ],
154
+ [ nil, 1, 2, 1, nil, :rex, ',' ],
155
+ [ nil, 1, 3, 0, nil, :rex, '' ],
156
+ [ nil, 1, 3, 1, nil, :rex, ',' ],
157
+ [ nil, 1, 4, 1, nil, :rex, 'a' ],
158
+ [ nil, 1, 5, 1, nil, :str, '>' ]
159
+ ] ]
160
+ )
161
+ expect(i.offset).to eq(6)
162
+ end
163
+ end
110
164
  end
111
165
  end
112
166
 
@@ -0,0 +1,43 @@
1
+
2
+ #
3
+ # specifying raabro
4
+ #
5
+ # Sun Oct 11 04:24:32 SGT 2015
6
+ #
7
+
8
+ require 'spec_helper'
9
+
10
+
11
+ module Sample::OwnRewrite include Raabro
12
+
13
+ # parse
14
+
15
+ def hello(i); str(:hello, i, 'hello'); end
16
+
17
+ #alias root exp
18
+ # not necessary since Raabro takes the last defined parser as the root
19
+
20
+ # rewrite
21
+
22
+ def rewrite(t)
23
+
24
+ [ :ok, t.string ]
25
+ end
26
+ end
27
+
28
+
29
+ describe Raabro do
30
+
31
+ describe Sample::OwnRewrite do
32
+
33
+ it 'uses its own rewrite' do
34
+
35
+ expect(
36
+ Sample::OwnRewrite.parse('hello')
37
+ ).to eq(
38
+ [ :ok, 'hello' ]
39
+ )
40
+ end
41
+ end
42
+ end
43
+
@@ -33,8 +33,10 @@ module Sample::Xel include Raabro
33
33
  def rewrite_num(t); t.string.to_i; end
34
34
 
35
35
  def rewrite_fun(t)
36
- [ t.children[0].string ] +
37
- t.children[1].children.inject([]) { |a, e| a << rewrite(e) if e.name; a }
36
+
37
+ #[ t.children[0].string ] +
38
+ #t.children[1].children.inject([]) { |a, e| a << rewrite(e) if e.name; a }
39
+ [ t.children[0].string ] + t.children[1].odd_children.map { |c| rewrite(c) }
38
40
  end
39
41
  end
40
42
 
@@ -0,0 +1,92 @@
1
+
2
+ #
3
+ # specifying raabro
4
+ #
5
+ # Sun Dec 13 06:10:00 JST 2015
6
+ #
7
+
8
+ require 'spec_helper'
9
+
10
+
11
+ module Sample::Xell include Raabro
12
+
13
+ # parse
14
+
15
+ def pa(i); str(nil, i, '('); end
16
+ def pz(i); str(nil, i, ')'); end
17
+ def com(i); str(nil, i, ','); end
18
+
19
+ def num(i); rex(:num, i, /-?[0-9]+/); end
20
+
21
+ def args(i); eseq(nil, i, :pa, :exp, :com, :pz); end
22
+ def funame(i); rex(nil, i, /[A-Z][A-Z0-9]*/); end
23
+ def fun(i); seq(:fun, i, :funame, :args); end
24
+
25
+ def exp(i); alt(nil, i, :fun, :num); end
26
+
27
+ # rewrite
28
+
29
+ #def rewrite_(t)
30
+ #
31
+ # c = t.children.find { |c| c.length > 0 || c.name }
32
+ # c ? rewrite(c) : nil
33
+ #end
34
+ #
35
+ # part of aabro now
36
+
37
+ def rewrite_num(t); t.string.to_i; end
38
+
39
+ def rewrite_fun(t)
40
+
41
+ #as = []
42
+ #t.children[1].children.each_with_index { |e, i| as << e if i.odd? }
43
+ #[ t.children[0].string ] + as.collect { |a| rewrite(a) }
44
+ [ t.children[0].string ] +
45
+ t.children[1].odd_children.collect { |a| rewrite(a) }
46
+ end
47
+ end
48
+
49
+
50
+ describe Raabro do
51
+
52
+ describe Sample::Xell do
53
+
54
+ describe '.parse' do
55
+
56
+ it 'parses (success)' do
57
+
58
+ expect(
59
+ Sample::Xell.parse('MUL(7,-3)')
60
+ ).to eq(
61
+ [ 'MUL', 7, -3 ]
62
+ )
63
+ end
64
+
65
+ it 'parses (rewrite: false, success)' do
66
+
67
+ expect(
68
+ Sample::Xell.parse('MUL(7,-3)', rewrite: false).to_s
69
+ ).to eq(%{
70
+ 1 nil 0,9
71
+ 1 :fun 0,9
72
+ 1 nil 0,3 "MUL"
73
+ 1 nil 3,6
74
+ 1 nil 3,1 "("
75
+ 1 nil 4,1
76
+ 1 :num 4,1 "7"
77
+ 1 nil 5,1 ","
78
+ 1 nil 6,2
79
+ 1 :num 6,2 "-3"
80
+ 1 nil 8,1 ")"
81
+ }.strip)
82
+ end
83
+
84
+ it 'parses (miss)' do
85
+
86
+ expect(Sample::Xell.parse('MUL(7,3) ')).to eq(nil)
87
+ expect(Sample::Xell.parse('MUL(7,3')).to eq(nil)
88
+ end
89
+ end
90
+ end
91
+ end
92
+
data/spec/seq_spec.rb CHANGED
@@ -226,6 +226,21 @@ describe Raabro do
226
226
  )
227
227
  expect(i.offset).to eq(8)
228
228
  end
229
+
230
+ it 'stops when there is no progress' do
231
+
232
+ i = Raabro::Input.new('abc')
233
+
234
+ t = Raabro.seq(nil, i, :to_star, '*');
235
+
236
+ expect(t.to_a(:leaves => true)).to eq(
237
+ [ nil, 1, 0, 0, nil, :seq, [
238
+ [ nil, 1, 0, 0, nil, :rep, [
239
+ [ nil, 0, 0, 0, nil, :str, [] ]
240
+ ] ]
241
+ ] ]
242
+ )
243
+ end
229
244
  end
230
245
 
231
246
  describe 'the plus quantifier' do
@@ -262,31 +277,22 @@ describe Raabro do
262
277
  )
263
278
  expect(i.offset).to eq(0)
264
279
  end
280
+
281
+ it 'stops when there is no progress' do
282
+
283
+ i = Raabro::Input.new('abc')
284
+
285
+ t = Raabro.seq(nil, i, :to_star, '+');
286
+
287
+ expect(t.to_a(:leaves => true)).to eq(
288
+ [ nil, 1, 0, 0, nil, :seq, [
289
+ [ nil, 1, 0, 0, nil, :rep, [
290
+ [ nil, 0, 0, 0, nil, :str, [] ]
291
+ ] ]
292
+ ] ]
293
+ )
294
+ end
265
295
  end
266
296
  end
267
297
  end
268
298
 
269
- #describe "fabr_seq()"
270
- #
271
- # context "quantifiers"
272
- # {
273
- # describe "fabr_qmark"
274
- # {
275
- # it "prunes when input->flags & FABR_F_PRUNE"
276
- # {
277
- # i.string = "tatota";
278
- # i.flags = FABR_F_PRUNE;
279
- #
280
- # t = fabr_seq("z", &i, _ta, _to, fabr_qmark, _ta, NULL);
281
- #
282
- # expect(fabr_tree_to_string(t, i.string, 0) ===f ""
283
- # "[ \"z\", 1, 0, 6, null, \"seq\", 0, [\n"
284
- # " [ null, 1, 0, 2, null, \"str\", 2, \"ta\" ],\n"
285
- # " [ null, 1, 2, 2, null, \"str\", 2, \"to\" ],\n"
286
- # " [ null, 1, 4, 2, null, \"str\", 2, \"ta\" ]\n"
287
- # "] ]");
288
- # }
289
- # }
290
- # }
291
- #}
292
-
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  #
3
- # Specifying rufus-scheduler
3
+ # Specifying raabro
4
4
  #
5
5
  # Sat Sep 19 21:12:35 JST 2015
6
6
  #
@@ -17,6 +17,8 @@ def to(i); Raabro.str(nil, i, 'to'); end
17
17
  def tu(i); Raabro.str(nil, i, 'tu'); end
18
18
 
19
19
  def to_plus(input); Raabro.rep(:tos, input, :to, 1); end
20
+ def to_star(input); Raabro.rep(nil, input, :to, 0); end
21
+ def to_qmark(input); Raabro.rep(nil, input, :to, 0, 1); end
20
22
 
21
23
  def nta(i); Raabro.str('the-ta', i, 'ta'); end
22
24
 
@@ -28,6 +30,12 @@ def gt(i); Raabro.str(nil, i, '>'); end
28
30
 
29
31
  def onex(i); Raabro.str(:onex, i, 'x'); end
30
32
  def twox(i); Raabro.str(:twox, i, 'xx'); end
33
+ def deux(i); Raabro.str(:deux, i, 'xx'); end
34
+
35
+ # testing eseq...
36
+ def acom(i); Raabro.rex(nil, i, /,?/); end
37
+ def aval(i); Raabro.rex(nil, i, /[a-z]?/); end
38
+ def arr(i); Raabro.eseq(nil, i, :lt, :aval, :acom, :gt); end
31
39
 
32
40
 
33
41
  #
data/spec/tree_spec.rb CHANGED
@@ -14,7 +14,7 @@ describe Raabro::Tree do
14
14
 
15
15
  it 'returns the first node with the given name' do
16
16
 
17
- t = Sample::Cal.parse('4 5 6 + 1 2 3 * +')
17
+ t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false)
18
18
 
19
19
  expect(
20
20
  t.lookup('item').to_a(:leaves)
@@ -24,13 +24,41 @@ describe Raabro::Tree do
24
24
  ] ]
25
25
  )
26
26
  end
27
+
28
+ it 'returns the first named node if the given name is nil' do
29
+
30
+ t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false)
31
+
32
+ expect(
33
+ t.lookup(nil).to_a(:leaves)
34
+ ).to eq(
35
+ [ :item, 1, 0, 1, nil, :alt, [
36
+ [ :num, 1, 0, 1, nil, :rex, '4' ]
37
+ ] ]
38
+ )
39
+ end
40
+ end
41
+
42
+ describe '.sublookup' do
43
+
44
+ it 'skips the callee node' do
45
+
46
+ t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false)
47
+ t = t.children[0]
48
+
49
+ expect(
50
+ t.sublookup(nil).to_a(:leaves)
51
+ ).to eq(
52
+ [ :num, 1, 0, 1, nil, :rex, '4' ]
53
+ )
54
+ end
27
55
  end
28
56
 
29
57
  describe '.gather' do
30
58
 
31
59
  it 'returns all the nodes with a given name' do
32
60
 
33
- t = Sample::Cal.parse('4 5 6 + 1 2 3 * +')
61
+ t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false)
34
62
 
35
63
  expect(
36
64
  t.gather('op').collect { |n| n.to_a(:leaves) }
@@ -42,6 +70,57 @@ describe Raabro::Tree do
42
70
  ]
43
71
  )
44
72
  end
73
+
74
+ it 'returns all the nodes with a name if the given name is nil' do
75
+
76
+ t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false)
77
+
78
+ expect(
79
+ t.gather(nil).collect { |n| n.to_a(:leaves) }
80
+ ).to eq([
81
+ [ :item, 1, 0, 1, nil, :alt, [
82
+ [ :num, 1, 0, 1, nil, :rex, '4' ]
83
+ ] ],
84
+ [ :item, 1, 2, 1, nil, :alt, [
85
+ [ :num, 1, 2, 1, nil, :rex, '5' ]
86
+ ] ],
87
+ [ :item, 1, 4, 1, nil, :alt, [
88
+ [ :num, 1, 4, 1, nil, :rex, '6' ]
89
+ ] ],
90
+ [ :item, 1, 6, 1, nil, :alt, [
91
+ [ :op, 1, 6, 1, nil, :rex, '+' ]
92
+ ] ],
93
+ [ :item, 1, 8, 1, nil, :alt, [
94
+ [ :num, 1, 8, 1, nil, :rex, '1' ]
95
+ ] ],
96
+ [ :item, 1, 10, 1, nil, :alt, [
97
+ [ :num, 1, 10, 1, nil, :rex, '2' ]
98
+ ] ],
99
+ [ :item, 1, 12, 1, nil, :alt, [
100
+ [ :num, 1, 12, 1, nil, :rex, '3' ]
101
+ ] ],
102
+ [ :item, 1, 14, 1, nil, :alt, [
103
+ [ :op, 1, 14, 1, nil, :rex, '*' ]
104
+ ] ],
105
+ [ :item, 1, 16, 1, nil, :alt, [
106
+ [ :op, 1, 16, 1, nil, :rex, '+' ]
107
+ ] ]
108
+ ])
109
+ end
110
+ end
111
+
112
+ describe '.subgather' do
113
+
114
+ it 'skips the callee node' do
115
+
116
+ t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false)
117
+
118
+ expect(
119
+ t.children[0].subgather(nil).collect { |n| n.to_a(:leaves) }
120
+ ).to eq([
121
+ [ :num, 1, 0, 1, nil, :rex, '4' ]
122
+ ])
123
+ end
45
124
  end
46
125
  end
47
126
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raabro
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-09-25 00:00:00.000000000 Z
12
+ date: 2016-02-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -50,7 +50,6 @@ executables: []
50
50
  extensions: []
51
51
  extra_rdoc_files: []
52
52
  files:
53
- - Rakefile
54
53
  - lib/raabro.rb
55
54
  - spec/all_spec.rb
56
55
  - spec/alt_spec.rb
@@ -59,7 +58,9 @@ files:
59
58
  - spec/ren_spec.rb
60
59
  - spec/rep_spec.rb
61
60
  - spec/rex_spec.rb
61
+ - spec/sample_spec.rb
62
62
  - spec/sample_xel_spec.rb
63
+ - spec/sample_xell_spec.rb
63
64
  - spec/seq_spec.rb
64
65
  - spec/spec_helper.rb
65
66
  - spec/str_spec.rb
data/Rakefile DELETED
@@ -1,68 +0,0 @@
1
-
2
- require 'rubygems'
3
-
4
- require 'rake'
5
- require 'rake/clean'
6
- require 'rdoc/task'
7
-
8
-
9
- #
10
- # clean
11
-
12
- CLEAN.include('pkg', 'rdoc')
13
-
14
-
15
- #
16
- # test / spec
17
-
18
- #task :spec => :check_dependencies do
19
- task :spec do
20
- exec 'rspec spec/'
21
- end
22
- task :test => :spec
23
-
24
- task :default => :spec
25
-
26
-
27
- #
28
- # gem
29
-
30
- GEMSPEC_FILE = Dir['*.gemspec'].first
31
- GEMSPEC = eval(File.read(GEMSPEC_FILE))
32
- GEMSPEC.validate
33
-
34
-
35
- desc %{
36
- builds the gem and places it in pkg/
37
- }
38
- task :build do
39
-
40
- sh "gem build #{GEMSPEC_FILE}"
41
- sh "mkdir -p pkg"
42
- sh "mv #{GEMSPEC.name}-#{GEMSPEC.version}.gem pkg/"
43
- end
44
-
45
- desc %{
46
- builds the gem and pushes it to rubygems.org
47
- }
48
- task :push => :build do
49
-
50
- sh "gem push pkg/#{GEMSPEC.name}-#{GEMSPEC.version}.gem"
51
- end
52
-
53
-
54
- #
55
- # rdoc
56
- #
57
- # make sure to have rdoc 2.5.x to run that
58
-
59
- Rake::RDocTask.new do |rd|
60
-
61
- rd.main = 'README.txt'
62
- rd.rdoc_dir = "rdoc/#{GEMSPEC.name}"
63
-
64
- rd.rdoc_files.include('README.rdoc', 'CHANGELOG.txt', 'lib/**/*.rb')
65
-
66
- rd.title = "#{GEMSPEC.name} #{GEMSPEC.version}"
67
- end
68
-