raabro 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/CHANGELOG.md +5 -0
  2. data/README.md +41 -0
  3. data/lib/raabro.rb +5 -5
  4. metadata +2 -2
data/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
  # raabro CHANGELOG.md
3
3
 
4
4
 
5
+ ## raabro 1.1.5 released 2017-08-19
6
+
7
+ * Default name to nil for Tree#subgather, #gather, #sublookup, and #lookup
8
+
9
+
5
10
  ## raabro 1.1.4 released 2017-08-17
6
11
 
7
12
  * fail with ArgumentError if Raabro.pp input is not a Raabro::Tree
data/README.md CHANGED
@@ -163,6 +163,47 @@ end
163
163
  (Yes, this sample parser parses string like "appletomatocabbage", it's not very useful, but I hope you get the point about `.seq`)
164
164
 
165
165
 
166
+ ## trees
167
+
168
+ An instance of `Raabro::Tree` is passed to `rewrite()` and `rewrite_{name}()` functions.
169
+
170
+ The most useful methods of this class are:
171
+ ```ruby
172
+ class Raabro::Tree
173
+
174
+ # Look for the first child or sub-child with the given name.
175
+ # If the given name is nil, looks for the first child with a name (not nil).
176
+ #
177
+ def sublookup(name=nil)
178
+
179
+ # Gathers all the children or sub-children with the given name.
180
+ # If the given name is nil, gathers all the children with a name (not nil).
181
+ # When a child matches, does not pursue gathering from the children of the
182
+ # matching child.
183
+ #
184
+ def subgather(name=nil)
185
+ end
186
+ ```
187
+
188
+ I'm using "child or sub-child" instead of "descendant" because once a child or sub-child matches, those methods do not consider the children or sub-children of that matching entity.
189
+
190
+ Here is a closeup on the rewrite functions of the sample parser at [doc/readme1.rb](doc/readme1.rb) (extracted from an early version of [floraison/dense](https://github.com/floraison/dense):
191
+ ```ruby
192
+ require 'raabro'
193
+
194
+ module PathParser include Raabro
195
+
196
+ # (...)
197
+
198
+ def rewrite_name(t); t.string; end
199
+ def rewrite_off(t); t.string.to_i; end
200
+ def rewrite_index(t); rewrite(t.sublookup); end
201
+ def rewrite_path(t); t.subgather(:index).collect { |tt| rewrite(tt) }; end
202
+ end
203
+ ```
204
+ Where `rewrite_index(t)` returns the result of the rewrite of the first of its children that has a name and `rewrite_path(t)` collects the result of the rewrite of all of its children that have the "index" name.
205
+
206
+
166
207
  ## errors
167
208
 
168
209
  By default, a parser will return nil when it cannot successfully parse the input.
data/lib/raabro.rb CHANGED
@@ -25,7 +25,7 @@
25
25
 
26
26
  module Raabro
27
27
 
28
- VERSION = '1.1.4'
28
+ VERSION = '1.1.5'
29
29
 
30
30
  class Input
31
31
 
@@ -112,7 +112,7 @@ module Raabro
112
112
  @input.string[@offset, l]
113
113
  end
114
114
 
115
- def lookup(name)
115
+ def lookup(name=nil)
116
116
 
117
117
  name = name ? name.to_s : nil
118
118
 
@@ -121,14 +121,14 @@ module Raabro
121
121
  sublookup(name)
122
122
  end
123
123
 
124
- def sublookup(name)
124
+ def sublookup(name=nil)
125
125
 
126
126
  @children.each { |c| if n = c.lookup(name); return n; end }
127
127
 
128
128
  nil
129
129
  end
130
130
 
131
- def gather(name, acc=[])
131
+ def gather(name=nil, acc=[])
132
132
 
133
133
  name = name ? name.to_s : nil
134
134
 
@@ -141,7 +141,7 @@ module Raabro
141
141
  acc
142
142
  end
143
143
 
144
- def subgather(name, acc=[])
144
+ def subgather(name=nil, acc=[])
145
145
 
146
146
  @children.each { |c| c.gather(name, acc) }
147
147
 
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.1.4
4
+ version: 1.1.5
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: 2017-08-17 00:00:00.000000000 Z
12
+ date: 2017-08-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec