reg 0.4.8 → 0.5.0a0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -0
  2. data/COPYING +0 -0
  3. data/History.txt +14 -0
  4. data/Makefile +59 -0
  5. data/README +87 -40
  6. data/article.txt +838 -0
  7. data/{assert.rb → lib/assert.rb} +3 -3
  8. data/{reg.rb → lib/reg.rb} +11 -4
  9. data/lib/reg/version.rb +21 -0
  10. data/lib/regarray.rb +455 -0
  11. data/{regarrayold.rb → lib/regarrayold.rb} +33 -7
  12. data/lib/regbackref.rb +73 -0
  13. data/lib/regbind.rb +230 -0
  14. data/{regcase.rb → lib/regcase.rb} +15 -5
  15. data/lib/regcompiler.rb +2341 -0
  16. data/{regcore.rb → lib/regcore.rb} +196 -85
  17. data/{regdeferred.rb → lib/regdeferred.rb} +35 -4
  18. data/{regposition.rb → lib/regevent.rb} +36 -38
  19. data/lib/reggraphpoint.rb +28 -0
  20. data/lib/reghash.rb +631 -0
  21. data/lib/reginstrumentation.rb +36 -0
  22. data/{regitem_that.rb → lib/regitem_that.rb} +32 -11
  23. data/{regknows.rb → lib/regknows.rb} +4 -2
  24. data/{reglogic.rb → lib/reglogic.rb} +76 -59
  25. data/{reglookab.rb → lib/reglookab.rb} +31 -21
  26. data/lib/regmatchset.rb +323 -0
  27. data/{regold.rb → lib/regold.rb} +27 -27
  28. data/{regpath.rb → lib/regpath.rb} +91 -1
  29. data/lib/regposition.rb +79 -0
  30. data/lib/regprogress.rb +1522 -0
  31. data/lib/regrepeat.rb +307 -0
  32. data/lib/regreplace.rb +254 -0
  33. data/lib/regslicing.rb +581 -0
  34. data/lib/regsubseq.rb +72 -0
  35. data/lib/regsugar.rb +361 -0
  36. data/lib/regvar.rb +180 -0
  37. data/lib/regxform.rb +212 -0
  38. data/{trace.rb → lib/trace_during.rb} +6 -4
  39. data/lib/warning.rb +37 -0
  40. data/parser.txt +26 -8
  41. data/philosophy.txt +18 -0
  42. data/reg.gemspec +58 -25
  43. data/regguide.txt +18 -0
  44. data/test/andtest.rb +46 -0
  45. data/test/regcompiler_test.rb +346 -0
  46. data/test/regdemo.rb +20 -0
  47. data/{item_thattest.rb → test/regitem_thattest.rb} +2 -2
  48. data/test/regtest.rb +2125 -0
  49. data/test/test_all.rb +32 -0
  50. data/test/test_reg.rb +19 -0
  51. metadata +108 -73
  52. data/calc.reg +0 -73
  53. data/forward_to.rb +0 -49
  54. data/numberset.rb +0 -200
  55. data/regarray.rb +0 -675
  56. data/regbackref.rb +0 -126
  57. data/regbind.rb +0 -74
  58. data/reggrid.csv +1 -2
  59. data/reghash.rb +0 -318
  60. data/regprogress.rb +0 -1054
  61. data/regreplace.rb +0 -114
  62. data/regsugar.rb +0 -230
  63. data/regtest.rb +0 -1078
  64. data/regvar.rb +0 -76
@@ -1,6 +1,6 @@
1
1
  =begin copyright
2
2
  reg - the ruby extended grammar
3
- Copyright (C) 2005 Caleb Clausen
3
+ Copyright (C) 2005, 2016 Caleb Clausen
4
4
 
5
5
  This library is free software; you can redistribute it and/or
6
6
  modify it under the terms of the GNU Lesser General Public
@@ -18,6 +18,32 @@
18
18
  =end
19
19
 
20
20
  module Reg
21
+ module Reg
22
+ #--------------------------
23
+ #set a breakpoint when matches against a Reg
24
+ #are made. of dubious value to those unfamiliar
25
+ #with Reg::Array source code.
26
+ def bp #:nodoc: all
27
+ class <<self #:nodoc:
28
+ alias_method :unbroken___eee, :=== #:nodoc:
29
+ alias_method :unbroken___mmatch,
30
+ method_defined?(:mmatch) ? :mmatch : :fail #:nodoc:
31
+ def ===(other) #:nodoc:
32
+ (defined? DEBUGGER__ or defined? Debugger) and Process.kill("INT",0)
33
+ unbroken___eee other
34
+ rescue
35
+ false
36
+ end
37
+
38
+ def mmatch(*xx) #:nodoc:
39
+ (defined? DEBUGGER__ or defined? Debugger) and Process.kill("INT",0)
40
+ unbroken___mmatch(*xx)
41
+ end
42
+ end
43
+ self
44
+ end
45
+ end
46
+
21
47
  module Backtrace
22
48
  #--------------------------
23
49
  def Backtrace.clean_result(result,restype=RR)
@@ -142,7 +168,7 @@ module Reg
142
168
 
143
169
  #--------------------------
144
170
  def backtrace(arr,start,result,ri)
145
- assert ri != INFINITY
171
+ assert ri != Infinity
146
172
  assert(Backtrace.check_result result)
147
173
  mat,matlen,di=nil
148
174
  loop do #might have to bt multiple times if prev prelim set also fails
@@ -338,7 +364,7 @@ module Reg
338
364
  return SingleRepeatMatchSet.new(i,-1,@times.begin)
339
365
  end
340
366
 
341
- def mmatch_multiple(arr,start)
367
+ def mmatch_full(arr,start)
342
368
  assert start <= arr.size
343
369
  r=[RR[]]
344
370
 
@@ -348,7 +374,7 @@ module Reg
348
374
  ri=di=0
349
375
  else
350
376
  arr.size==start and return nil
351
- assert @times.begin<INFINITY
377
+ assert @times.begin<Infinity
352
378
  r,di,ri=bt_match(arr,start,0,0,r,@times.begin) #matches @reg @times.begin times
353
379
  r.nil? and return nil
354
380
  end
@@ -398,7 +424,7 @@ module Reg
398
424
  return [RR[arr[start,@regs.size]], @regs.size]
399
425
  end
400
426
 
401
- def mmatch_multiple(arr,start)
427
+ def mmatch_full(arr,start)
402
428
  #in this version, at least one of @regs is a multiple reg
403
429
  #start==arr.size and huh
404
430
  assert( (0..arr.size).include?( start))
@@ -452,7 +478,7 @@ module Reg
452
478
  return result
453
479
  end
454
480
 
455
- def subregs; @regs end
481
+ def subregs; @regs.dup end
456
482
  end
457
483
 
458
484
  #--------------------------
@@ -474,4 +500,4 @@ module Reg
474
500
  end
475
501
  end
476
502
 
477
- end
503
+ end
@@ -0,0 +1,73 @@
1
+ =begin copyright
2
+ reg - the ruby extended grammar
3
+ Copyright (C) 2005, 2016 Caleb Clausen
4
+
5
+ This library is free software; you can redistribute it and/or
6
+ modify it under the terms of the GNU Lesser General Public
7
+ License as published by the Free Software Foundation; either
8
+ version 2.1 of the License, or (at your option) any later version.
9
+
10
+ This library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public
16
+ License along with this library; if not, write to the Free Software
17
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ =end
19
+ require 'pp'
20
+ require 'regdeferred'
21
+
22
+ module Reg
23
+
24
+
25
+ #----------------------------------
26
+ module BackrefLike
27
+ def mmatch(pr)
28
+ huh #need info thats in Progress
29
+ end
30
+
31
+ def mixmod; BackrefLike end
32
+ end
33
+ #----------------------------------
34
+ class Backref
35
+ include BlankSlate
36
+ restore :inspect,:extend
37
+ restore :respond_to?
38
+ include Formula
39
+ include BackrefLike
40
+ def initialize(name,*path)
41
+ #complex paths not handled yet
42
+ raise ParameterError.new unless path.empty?
43
+ @name=normalize_bind_name name
44
+ #super
45
+ end
46
+
47
+ def formula_value(other,progress)
48
+ progress.lookup_var @name
49
+ end
50
+
51
+ class<<self
52
+ alias [] new
53
+ end
54
+ end
55
+
56
+
57
+ #----------------------------------
58
+ module BRLike
59
+ include BackrefLike
60
+ include Reg
61
+ def mixmod; BRLike end
62
+ end
63
+ class BR < Backref
64
+ include BRLike
65
+ restore :respond_to?
66
+ def inspect
67
+ 'BR['+@name.inspect+']'
68
+ end
69
+ end
70
+
71
+ def self.BR(*args) BR.new(*args) end
72
+ def self.BackRef(*args) BackRef.new(*args) end
73
+ end
@@ -0,0 +1,230 @@
1
+ =begin copyright
2
+ reg - the ruby extended grammar
3
+ Copyright (C) 2005, 2016 Caleb Clausen
4
+
5
+ This library is free software; you can redistribute it and/or
6
+ modify it under the terms of the GNU Lesser General Public
7
+ License as published by the Free Software Foundation; either
8
+ version 2.1 of the License, or (at your option) any later version.
9
+
10
+ This library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public
16
+ License along with this library; if not, write to the Free Software
17
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ =end
19
+ module Reg
20
+ module Reg
21
+ def bind(name=self)
22
+ name=name.name if BoundRef===name
23
+ Bound.new(name,self)
24
+ end
25
+ alias % bind
26
+
27
+ def side_effect(&block); SideEffect.new(self,&block) end
28
+ def undo(&block); Undo.new(self,&block) end
29
+ def trace(&block); Trace.new(self,&block) end
30
+
31
+ def normalize_bind_name(name)
32
+ case name
33
+ when Reg; name=name.inspect
34
+ when Symbol
35
+ else name=name.to_s.to_sym
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ #-------------------------------------
42
+ class Bound
43
+ include Reg,Undoable,Composite
44
+ def initialize(name,reg)
45
+ @name,@reg=(normalize_bind_name name),reg
46
+ super
47
+ end
48
+
49
+ def mmatch(progress)
50
+ progress.register_var(@name,progress.get_index)
51
+ result=@reg.mmatch(progress)
52
+ #the variable needs to be unbound if the match failed
53
+ result or progress.delete_variable @name
54
+ result
55
+ end
56
+
57
+
58
+ def mmatch_full(progress)
59
+ huh "what if result is a matchset?"
60
+ if result=@reg.mmatch(progress)
61
+ idx=progress.get_index
62
+ range= if 1==result.last then idx else idx...idx+result.last end
63
+ progress.register_var(@name,range)
64
+ end
65
+ return result
66
+ end
67
+
68
+ def inspect
69
+ @reg.inspect+"%"+@name.inspect
70
+ end
71
+ end
72
+
73
+ #-------------------------------------
74
+ class BoundRef
75
+ include Formula
76
+ def initialize(name)
77
+ @name=name
78
+ end
79
+ attr :name
80
+
81
+ def formula_value(other,session)
82
+ session[@name]
83
+ end
84
+
85
+ def == other
86
+ BoundRef===other and other.name==name
87
+ end
88
+
89
+ def hash
90
+ "BoundRef of ".<<(name.to_s).hash
91
+ end
92
+
93
+ def unparse o
94
+ inspect
95
+ end
96
+ end
97
+
98
+
99
+ #-------------------------------------
100
+ class WithBoundRefValues
101
+ include Formula
102
+ def initialize(br,values)
103
+ @br,@values=br,values
104
+ end
105
+ attr_reader :br,:values
106
+
107
+ def formula_value(other,session)
108
+ saved_keys=@values.keys
109
+ saved=saved_keys.map{|k| session[k] }
110
+ result=@br.formula_value(other,session.merge!(@values))
111
+ saved_keys.each_with_index{|k,i|
112
+ session[k]=saved[i]
113
+ }
114
+
115
+ if $Reg_rtrace and $Reg_rtrace===other
116
+ if values.size>1
117
+ vals=@values.dup
118
+ vals.delete :self
119
+ vals_str="values=#{vals.inspect}"
120
+ end
121
+ with=@br
122
+ with=with.repldata if Replace::Form===with
123
+ puts "replacing \n#{other.inspect} with \n#{with.inspect} #{vals_str}"
124
+ end
125
+
126
+ return result
127
+ end
128
+
129
+ def == other
130
+ other.is_a? WithBoundRefValues and
131
+ @br==other.br and @values==other.values
132
+ end
133
+
134
+ def hash
135
+ #@value is a Hash, and Hash#hash doesn't work in ruby 1.8 (fixed in 1.9)
136
+ #I thought I had a good implementation of Hash#hash somewhere....
137
+ @br.hash^@values.to_a.sort_by{|(k,v)| k }.hash
138
+ end
139
+
140
+ def chain_to inner
141
+ ::Ron::GraphWalk.graphcopy(self){|cntr,o,i,ty,useit|
142
+ if BoundRef===o and o.name==:self
143
+ useit[0]=true
144
+ inner
145
+ end
146
+ }
147
+ end
148
+ end
149
+
150
+ #-------------------------------------
151
+ class Trace
152
+ include Reg,Composite
153
+
154
+ def initialize(reg,&block)
155
+ block||=proc{|reg2,other,result|
156
+ print "not " unless result
157
+ print reg2.inspect, " === ", other.inspect, "\n"
158
+ }
159
+ @reg,@block=reg,block
160
+ super
161
+ # extend( HasCmatch===@reg ? HasCmatch : HasBmatch )
162
+ end
163
+
164
+ def === other
165
+ result= @reg===other
166
+ @block.call @reg,other,result
167
+ return result
168
+ end
169
+
170
+ def generate_bmatch
171
+ "
172
+ begin
173
+ item=progress.cursor.readahead1
174
+ result=@reg.bmatch progress
175
+ ensure
176
+ @block.call @reg,item,result
177
+ end
178
+ "
179
+ end
180
+ def generate_cmatch
181
+ "
182
+ begin
183
+ success=nil
184
+ item=progress.cursor.readahead1
185
+ @reg.cmatch(progress){
186
+ @block.call @reg,item,true
187
+ success=true
188
+ yield
189
+ }
190
+ ensure
191
+ @block.call @reg,item,false unless success
192
+ end
193
+ "
194
+ end
195
+ end
196
+
197
+ #-------------------------------------
198
+ class SideEffect
199
+ include Reg,Composite
200
+
201
+ def initialize(reg,&block)
202
+ @reg,@block=reg,block
203
+ super
204
+ end
205
+
206
+ def mmatch(progress)
207
+ huh "what if result is a matchset?"
208
+ result=@reg.mmatch(progress) and
209
+ @block.call(progress)
210
+ return result
211
+ end
212
+ end
213
+
214
+ #------------------------------------
215
+ class Undo
216
+ include Reg,Undoable,Composite
217
+
218
+ def initialize(reg,&block)
219
+ @reg,@block=reg,block
220
+ super
221
+ end
222
+
223
+ def mmatch(progress)
224
+ huh "what if result is a matchset?"
225
+ result=@reg.mmatch(progress) and
226
+ progress.register_undo(@block)
227
+ return result
228
+ end
229
+ end
230
+ end
@@ -1,6 +1,6 @@
1
1
  =begin copyright
2
2
  reg - the ruby extended grammar
3
- Copyright (C) 2005 Caleb Clausen
3
+ Copyright (C) 2005, 2016 Caleb Clausen
4
4
 
5
5
  This library is free software; you can redistribute it and/or
6
6
  modify it under the terms of the GNU Lesser General Public
@@ -18,6 +18,8 @@
18
18
  =end
19
19
  module Reg
20
20
  class Case
21
+ include Reg
22
+ include Composite
21
23
  def initialize(*args)
22
24
  if args.size==1 and Hash===args.first
23
25
  scalars=[]; sets=[]
@@ -35,7 +37,8 @@ module Reg
35
37
  args=scalars+sets+matchers
36
38
  end
37
39
 
38
- @others=others_given=nil
40
+ @others=None
41
+ others_given=nil
39
42
  @pairlist=args.delete_if{|a|
40
43
  if !(Pair===a)
41
44
  warn "ignoring non-Reg::Pair in Reg::Case: #{a}"
@@ -43,16 +46,17 @@ module Reg
43
46
  elsif OB==a.left
44
47
  others_given and warn 'more than one default specified'
45
48
  others_given=true
46
- @others=v
49
+ @others=a.right
47
50
  end
48
51
  }
52
+ super
53
+ assert(!is_a? Multiple)
49
54
  end
50
55
 
51
56
  def ===(other)
52
57
  @pairlist.each{|pair|
53
58
  pair.left===other and pair.right===other || return
54
- }
55
- @others===other
59
+ } or @others===other
56
60
  end
57
61
 
58
62
  #hash-based optimization of scalars and sets is possible here
@@ -70,6 +74,12 @@ module Reg
70
74
  }
71
75
 
72
76
  end
77
+
78
+ def subregs
79
+ result=@pairlist.inject([]){|a,pair| a+pair.to_a}
80
+ result+=[OB,@others] if @others
81
+ return result
82
+ end
73
83
 
74
84
  end
75
85