raabro 1.1.4 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6159e68d6f573fed52d1dd3245c57bc4714a12b3a2f7c5eb75dfac4d364a1041
4
+ data.tar.gz: 50c1cce039e12ae3e3463eb2add731a7bf4395b6a82c92057676c3901b4d3f7e
5
+ SHA512:
6
+ metadata.gz: e7e68e4de4f750d51194a1b963205ff7b30d0896389aabc5eeaaf8a0821903745dda173dc1325d3c0dbf20f32db5568f61f84d18f207c057c00b503f117aa2af
7
+ data.tar.gz: 1b48246a064ad905a77104bc49d582d0fdf3d879d5de13e45ecea4bf8fbd7f02cba3e7b94ac6bc043f643fd5e45d1856e86b461fb0659129d00562ecd4cfe71a
@@ -2,6 +2,33 @@
2
2
  # raabro CHANGELOG.md
3
3
 
4
4
 
5
+ ## raabro 1.3.2 released 2020-09-24
6
+
7
+ * Make a tiny rewrite_ optimization
8
+
9
+
10
+ ## raabro 1.3.1 released 2020-05-10
11
+
12
+ * Add '!' (not) seq quantifier
13
+
14
+
15
+ ## raabro 1.3.0 released 2020-05-10
16
+
17
+ * Add `nott` parser element
18
+ * Add Tree#strinp and #strim
19
+ * Skip 1.2.0 to align on http://github.com/jmettraux/jaabro
20
+
21
+
22
+ ## raabro 1.1.6 released 2018-06-22
23
+
24
+ * Remove unused `add` var, gh-2, thanks to https://github.com/utilum
25
+
26
+
27
+ ## raabro 1.1.5 released 2017-08-19
28
+
29
+ * Default name to nil for Tree#subgather, #gather, #sublookup, and #lookup
30
+
31
+
5
32
  ## raabro 1.1.4 released 2017-08-17
6
33
 
7
34
  * fail with ArgumentError if Raabro.pp input is not a Raabro::Tree
@@ -0,0 +1,14 @@
1
+
2
+ # raabro credits
3
+
4
+ * Utilum https://github.com/utilum removed unused var
5
+ * John Mettraux https://github.com/jmettraux author and maintainer
6
+
7
+
8
+ ## aabro and jaabro credits
9
+
10
+ Thanks to all the people who contributed to aabro and jaabro as well.
11
+
12
+ * https://github.com/flon-io/aabro
13
+ * https://github.com/jmettraux/jaabro
14
+
@@ -1,5 +1,5 @@
1
1
 
2
- Copyright (c) 2015-2017, John Mettraux, jmettraux@gmail.com
2
+ Copyright (c) 2015-2020, 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
@@ -19,3 +19,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
19
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
20
  THE SOFTWARE.
21
21
 
22
+
23
+ Made in Japan.
24
+
data/Makefile CHANGED
@@ -1,9 +1,15 @@
1
1
 
2
+ ## gem tasks ##
3
+
2
4
  NAME = \
3
5
  $(shell ruby -e "s = eval(File.read(Dir['*.gemspec'][0])); puts s.name")
4
6
  VERSION = \
5
7
  $(shell ruby -e "s = eval(File.read(Dir['*.gemspec'][0])); puts s.version")
6
8
 
9
+ count_lines:
10
+ find lib -name "*.rb" | xargs cat | ruby -e "p STDIN.readlines.count { |l| l = l.strip; l[0, 1] != '#' && l != '' }"
11
+ find spec -name "*_spec.rb" | xargs cat | ruby -e "p STDIN.readlines.count { |l| l = l.strip; l[0, 1] != '#' && l != '' }"
12
+ cl: count_lines
7
13
 
8
14
  gemspec_validate:
9
15
  @echo "---"
@@ -13,11 +19,21 @@ gemspec_validate:
13
19
  name: gemspec_validate
14
20
  @echo "$(NAME) $(VERSION)"
15
21
 
22
+ cw:
23
+ find lib -name "*.rb" -exec ruby -cw {} \;
24
+
16
25
  build: gemspec_validate
17
26
  gem build $(NAME).gemspec
18
27
  mkdir -p pkg
19
28
  mv $(NAME)-$(VERSION).gem pkg/
20
29
 
21
30
  push: build
22
- gem push pkg/$(NAME)-$(VERSION).gem
31
+ gem push --otp "$(OTP)" pkg/$(NAME)-$(VERSION).gem
32
+
33
+ spec:
34
+ bundle exec rspec
35
+ test: spec
36
+
37
+
38
+ .PHONY: count_lines gemspec_validate name cw build push spec
23
39
 
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  # raabro
3
3
 
4
- [![Build Status](https://secure.travis-ci.org/jmettraux/raabro.svg)](http://travis-ci.org/jmettraux/raabro)
4
+ [![Build Status](https://secure.travis-ci.org/floraison/raabro.svg)](http://travis-ci.org/floraison/raabro)
5
5
  [![Gem Version](https://badge.fury.io/rb/raabro.svg)](http://badge.fury.io/rb/raabro)
6
6
 
7
7
  A very dumb PEG parser library.
@@ -27,31 +27,44 @@ module Fun include Raabro
27
27
  #
28
28
  # Last function is the root, "i" stands for "input".
29
29
 
30
- def pa(i); rex(nil, i, /\(\s*/); end
31
- def pz(i); rex(nil, i, /\)\s*/); end
32
- def com(i); rex(nil, i, /,\s*/); end
30
+ def pstart(i); rex(nil, i, /\(\s*/); end
31
+ def pend(i); rex(nil, i, /\)\s*/); end
32
+ # parenthese start and end, including trailing white space
33
+
34
+ def comma(i); rex(nil, i, /,\s*/); end
35
+ # a comma, including trailing white space
33
36
 
34
37
  def num(i); rex(:num, i, /-?[0-9]+\s*/); end
38
+ # name is :num, a positive or negative integer
39
+
40
+ def args(i); eseq(nil, i, :pstart, :exp, :comma, :pend); end
41
+ # a set of :exp, beginning with a (, punctuated by commas and ending with )
35
42
 
36
- def args(i); eseq(nil, i, :pa, :exp, :com, :pz); end
37
43
  def funame(i); rex(nil, i, /[a-z][a-z0-9]*/); end
38
44
  def fun(i); seq(:fun, i, :funame, :args); end
45
+ # name is :fun, a function composed of a function name
46
+ # followed by arguments
39
47
 
40
48
  def exp(i); alt(nil, i, :fun, :num); end
49
+ # an expression is either (alt) a function or a number
41
50
 
42
51
  # rewrite
43
52
  #
44
53
  # Names above (:num, :fun, ...) get a rewrite_xxx function.
45
54
  # "t" stands for "tree".
46
- #
47
- # The trees with a nil name are handled by rewrite_(tree) a default
48
- # rewrite function
49
55
 
56
+ def rewrite_exp(t); rewrite(t.children[0]); end
50
57
  def rewrite_num(t); t.string.to_i; end
51
58
 
52
59
  def rewrite_fun(t)
53
- [ t.children[0].string ] +
54
- t.children[1].odd_children.collect { |a| rewrite(a) }
60
+
61
+ funame, args = t.children
62
+
63
+ [ funame.string ] +
64
+ args.gather.collect { |e| rewrite(e) }
65
+ #
66
+ # #gather collect all the children in a tree that have
67
+ # a name, in this example, names can be :exp, :num, :fun
55
68
  end
56
69
  end
57
70
 
@@ -121,6 +134,9 @@ def altg(name, input, *parsers)
121
134
  def rep(name, input, parser, min, max=0)
122
135
  # repeats the the wrapped parser
123
136
 
137
+ def nott(name, input, parser)
138
+ # succeeds if the wrapped parser fails, fails if it succeeds
139
+
124
140
  def ren(name, input, parser)
125
141
  # renames the output of the wrapped parser
126
142
 
@@ -143,6 +159,8 @@ def eseq(name, input, startpa, eltpa, seppa, endpa)
143
159
 
144
160
  `seq` is special, it understands "quantifiers": `'?'`, `'+'` or `'*'`. They make behave `seq` a bit like a classical regex.
145
161
 
162
+ The `'!'` (bang, not) quantifier is explained at the end of this section.
163
+
146
164
  ```ruby
147
165
  module CartParser include Raabro
148
166
 
@@ -162,6 +180,57 @@ end
162
180
 
163
181
  (Yes, this sample parser parses string like "appletomatocabbage", it's not very useful, but I hope you get the point about `.seq`)
164
182
 
183
+ The `'!'` (bang, not) quantifier is a kind of "negative lookahead".
184
+
185
+ ```ruby
186
+ def menu(i)
187
+ seq(:menu, i, :mise_en_bouche, :main, :main, '!', :dessert)
188
+ end
189
+ ```
190
+
191
+ Lousy example, but here a main cannot follow a main.
192
+
193
+
194
+ ## trees
195
+
196
+ An instance of `Raabro::Tree` is passed to `rewrite()` and `rewrite_{name}()` functions.
197
+
198
+ The most useful methods of this class are:
199
+ ```ruby
200
+ class Raabro::Tree
201
+
202
+ # Look for the first child or sub-child with the given name.
203
+ # If the given name is nil, looks for the first child with a name (not nil).
204
+ #
205
+ def sublookup(name=nil)
206
+
207
+ # Gathers all the children or sub-children with the given name.
208
+ # If the given name is nil, gathers all the children with a name (not nil).
209
+ # When a child matches, does not pursue gathering from the children of the
210
+ # matching child.
211
+ #
212
+ def subgather(name=nil)
213
+ end
214
+ ```
215
+
216
+ 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.
217
+
218
+ 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)):
219
+ ```ruby
220
+ require 'raabro'
221
+
222
+ module PathParser include Raabro
223
+
224
+ # (...)
225
+
226
+ def rewrite_name(t); t.string; end
227
+ def rewrite_off(t); t.string.to_i; end
228
+ def rewrite_index(t); rewrite(t.sublookup); end
229
+ def rewrite_path(t); t.subgather(:index).collect { |tt| rewrite(tt) }; end
230
+ end
231
+ ```
232
+ 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.
233
+
165
234
 
166
235
  ## errors
167
236
 
@@ -1,31 +1,7 @@
1
- #--
2
- # Copyright (c) 2015-2017, John Mettraux, jmettraux@gmail.com
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining a copy
5
- # of this software and associated documentation files (the "Software"), to deal
6
- # in the Software without restriction, including without limitation the rights
7
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- # copies of the Software, and to permit persons to whom the Software is
9
- # furnished to do so, subject to the following conditions:
10
- #
11
- # The above copyright notice and this permission notice shall be included in
12
- # all copies or substantial portions of the Software.
13
- #
14
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
- # THE SOFTWARE.
21
- #
22
- # Made in Japan.
23
- #++
24
-
25
1
 
26
2
  module Raabro
27
3
 
28
- VERSION = '1.1.4'
4
+ VERSION = '1.3.2'
29
5
 
30
6
  class Input
31
7
 
@@ -102,17 +78,12 @@ module Raabro
102
78
  @children = successful_children
103
79
  end
104
80
 
105
- def string
106
-
107
- @input.string[@offset, @length]
108
- end
109
-
110
- def nonstring(l=7)
111
-
112
- @input.string[@offset, l]
113
- end
81
+ def string; @input.string[@offset, @length]; end
82
+ def strinp; @input.string[@offset, @length].strip; end
83
+ alias strim strinp
84
+ def nonstring(l=7); @input.string[@offset, l]; end
114
85
 
115
- def lookup(name)
86
+ def lookup(name=nil)
116
87
 
117
88
  name = name ? name.to_s : nil
118
89
 
@@ -121,14 +92,14 @@ module Raabro
121
92
  sublookup(name)
122
93
  end
123
94
 
124
- def sublookup(name)
95
+ def sublookup(name=nil)
125
96
 
126
97
  @children.each { |c| if n = c.lookup(name); return n; end }
127
98
 
128
99
  nil
129
100
  end
130
101
 
131
- def gather(name, acc=[])
102
+ def gather(name=nil, acc=[])
132
103
 
133
104
  name = name ? name.to_s : nil
134
105
 
@@ -141,7 +112,7 @@ module Raabro
141
112
  acc
142
113
  end
143
114
 
144
- def subgather(name, acc=[])
115
+ def subgather(name=nil, acc=[])
145
116
 
146
117
  @children.each { |c| c.gather(name, acc) }
147
118
 
@@ -287,10 +258,11 @@ module Raabro
287
258
  # so that :plus and co can be overriden
288
259
 
289
260
  case parser
290
- when '?', :q, :qmark then [ 0, 1 ]
291
- when '*', :s, :star then [ 0, 0 ]
292
- when '+', :p, :plus then [ 1, 0 ]
293
- else nil
261
+ when '?', :q, :qmark then [ 0, 1 ]
262
+ when '*', :s, :star then [ 0, 0 ]
263
+ when '+', :p, :plus then [ 1, 0 ]
264
+ when '!' then :bang
265
+ else nil
294
266
  end
295
267
  end
296
268
 
@@ -322,7 +294,11 @@ module Raabro
322
294
  pa = parsers.shift
323
295
  break unless pa
324
296
 
325
- if q = _quantify(parsers.first)
297
+ if parsers.first == '!'
298
+ parsers.shift
299
+ c = nott(nil, input, pa)
300
+ r.children << c
301
+ elsif q = _quantify(parsers.first)
326
302
  parsers.shift
327
303
  c = rep(nil, input, pa, *q)
328
304
  r.children.concat(c.children)
@@ -433,6 +409,22 @@ module Raabro
433
409
  end
434
410
  alias rename ren
435
411
 
412
+ def nott(name, input, parser)
413
+
414
+ start = input.offset
415
+
416
+ r = ::Raabro::Tree.new(name, :nott, input)
417
+ c = _parse(parser, input)
418
+ r.children << c
419
+
420
+ r.length = 0
421
+ r.result = c.result == 1 ? 0 : 1
422
+
423
+ input.offset = start
424
+
425
+ r
426
+ end
427
+
436
428
  def all(name, input, parser)
437
429
 
438
430
  start = input.offset
@@ -478,8 +470,6 @@ module Raabro
478
470
 
479
471
  loop do
480
472
 
481
- add = true
482
-
483
473
  st = i > 0 ? _parse(seppa, input) : nil
484
474
  et = st == nil || st.result == 1 ? _parse(eltpa, input) : nil
485
475
 
@@ -575,7 +565,7 @@ module Raabro
575
565
 
576
566
  def rewrite(tree)
577
567
 
578
- return !! methods.find { |m| m.to_s.match(/^rewrite_/) } if tree == 0
568
+ return !! methods.find { |m| m.to_s.match?(/^rewrite_/) } if tree == 0
579
569
  # return true when "rewrite_xxx" methods seem to have been provided
580
570
 
581
571
  send("rewrite_#{tree.name}", tree)
@@ -9,9 +9,8 @@ Gem::Specification.new do |s|
9
9
 
10
10
  s.platform = Gem::Platform::RUBY
11
11
  s.authors = [ 'John Mettraux' ]
12
- s.email = [ 'jmettraux@gmail.com' ]
13
- s.homepage = 'http://github.com/jmettraux/raabro'
14
- #s.rubyforge_project = 'rufus'
12
+ s.email = [ 'jmettraux+flor@gmail.com' ]
13
+ s.homepage = 'http://github.com/floraison/raabro'
15
14
  s.license = 'MIT'
16
15
  s.summary = 'a very dumb PEG parser library'
17
16
 
@@ -30,7 +29,7 @@ A very dumb PEG parser library, with a horrible interface.
30
29
 
31
30
  #s.add_runtime_dependency 'tzinfo'
32
31
 
33
- s.add_development_dependency 'rspec', '~> 3.4.0'
32
+ s.add_development_dependency 'rspec', '~> 3.7'
34
33
 
35
34
  s.require_path = 'lib'
36
35
  end
metadata CHANGED
@@ -1,68 +1,64 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raabro
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
5
- prerelease:
4
+ version: 1.3.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - John Mettraux
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2017-08-17 00:00:00.000000000 Z
11
+ date: 2020-09-24 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: 3.4.0
19
+ version: '3.7'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: 3.4.0
26
+ version: '3.7'
30
27
  description: A very dumb PEG parser library, with a horrible interface.
31
28
  email:
32
- - jmettraux@gmail.com
29
+ - jmettraux+flor@gmail.com
33
30
  executables: []
34
31
  extensions: []
35
32
  extra_rdoc_files: []
36
33
  files:
37
- - README.md
38
34
  - CHANGELOG.md
35
+ - CREDITS.md
39
36
  - LICENSE.txt
40
37
  - Makefile
38
+ - README.md
41
39
  - lib/raabro.rb
42
40
  - raabro.gemspec
43
- homepage: http://github.com/jmettraux/raabro
41
+ homepage: http://github.com/floraison/raabro
44
42
  licenses:
45
43
  - MIT
44
+ metadata: {}
46
45
  post_install_message:
47
46
  rdoc_options: []
48
47
  require_paths:
49
48
  - lib
50
49
  required_ruby_version: !ruby/object:Gem::Requirement
51
- none: false
52
50
  requirements:
53
- - - ! '>='
51
+ - - ">="
54
52
  - !ruby/object:Gem::Version
55
53
  version: '0'
56
54
  required_rubygems_version: !ruby/object:Gem::Requirement
57
- none: false
58
55
  requirements:
59
- - - ! '>='
56
+ - - ">="
60
57
  - !ruby/object:Gem::Version
61
58
  version: '0'
62
59
  requirements: []
63
- rubyforge_project:
64
- rubygems_version: 1.8.23.2
60
+ rubygems_version: 3.0.3
65
61
  signing_key:
66
- specification_version: 3
62
+ specification_version: 4
67
63
  summary: a very dumb PEG parser library
68
64
  test_files: []