reg 0.4.8 → 0.5.0a0
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 +4 -0
- data/COPYING +0 -0
- data/History.txt +14 -0
- data/Makefile +59 -0
- data/README +87 -40
- data/article.txt +838 -0
- data/{assert.rb → lib/assert.rb} +3 -3
- data/{reg.rb → lib/reg.rb} +11 -4
- data/lib/reg/version.rb +21 -0
- data/lib/regarray.rb +455 -0
- data/{regarrayold.rb → lib/regarrayold.rb} +33 -7
- data/lib/regbackref.rb +73 -0
- data/lib/regbind.rb +230 -0
- data/{regcase.rb → lib/regcase.rb} +15 -5
- data/lib/regcompiler.rb +2341 -0
- data/{regcore.rb → lib/regcore.rb} +196 -85
- data/{regdeferred.rb → lib/regdeferred.rb} +35 -4
- data/{regposition.rb → lib/regevent.rb} +36 -38
- data/lib/reggraphpoint.rb +28 -0
- data/lib/reghash.rb +631 -0
- data/lib/reginstrumentation.rb +36 -0
- data/{regitem_that.rb → lib/regitem_that.rb} +32 -11
- data/{regknows.rb → lib/regknows.rb} +4 -2
- data/{reglogic.rb → lib/reglogic.rb} +76 -59
- data/{reglookab.rb → lib/reglookab.rb} +31 -21
- data/lib/regmatchset.rb +323 -0
- data/{regold.rb → lib/regold.rb} +27 -27
- data/{regpath.rb → lib/regpath.rb} +91 -1
- data/lib/regposition.rb +79 -0
- data/lib/regprogress.rb +1522 -0
- data/lib/regrepeat.rb +307 -0
- data/lib/regreplace.rb +254 -0
- data/lib/regslicing.rb +581 -0
- data/lib/regsubseq.rb +72 -0
- data/lib/regsugar.rb +361 -0
- data/lib/regvar.rb +180 -0
- data/lib/regxform.rb +212 -0
- data/{trace.rb → lib/trace_during.rb} +6 -4
- data/lib/warning.rb +37 -0
- data/parser.txt +26 -8
- data/philosophy.txt +18 -0
- data/reg.gemspec +58 -25
- data/regguide.txt +18 -0
- data/test/andtest.rb +46 -0
- data/test/regcompiler_test.rb +346 -0
- data/test/regdemo.rb +20 -0
- data/{item_thattest.rb → test/regitem_thattest.rb} +2 -2
- data/test/regtest.rb +2125 -0
- data/test/test_all.rb +32 -0
- data/test/test_reg.rb +19 -0
- metadata +108 -73
- data/calc.reg +0 -73
- data/forward_to.rb +0 -49
- data/numberset.rb +0 -200
- data/regarray.rb +0 -675
- data/regbackref.rb +0 -126
- data/regbind.rb +0 -74
- data/reggrid.csv +1 -2
- data/reghash.rb +0 -318
- data/regprogress.rb +0 -1054
- data/regreplace.rb +0 -114
- data/regsugar.rb +0 -230
- data/regtest.rb +0 -1078
- data/regvar.rb +0 -76
@@ -0,0 +1,36 @@
|
|
1
|
+
=begin copyright
|
2
|
+
reg - the ruby extended grammar
|
3
|
+
Copyright (C) 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 'instrument'
|
20
|
+
require 'reg'
|
21
|
+
|
22
|
+
Instrument.on_entry_to(Reg::Progress, :bt_match, "args[0]||matcher")
|
23
|
+
Instrument.on_exit_from(Reg::Progress, :bt_match)
|
24
|
+
|
25
|
+
Instrument.on_entry_to(Reg::Progress, :last_next_match,
|
26
|
+
"args[0].matcher", "args[0].regsidx", "args[0].cursor.pos", "(args[0]||context).position_inc_stack")
|
27
|
+
Instrument.on_exit_from(Reg::Progress, :last_next_match, "(args[0] || context).position_inc_stack")
|
28
|
+
|
29
|
+
Instrument.on_entry_to(Reg::RepeatMatchSet, :next_match, :@consumed, "@context.matcher", "@context.position_inc_stack")
|
30
|
+
Instrument.on_exit_from(Reg::RepeatMatchSet, :next_match, "@context.position_inc_stack")
|
31
|
+
|
32
|
+
Instrument.on_entry_to(Reg::SubseqMatchSet, :next_match)
|
33
|
+
Instrument.on_exit_from(Reg::SubseqMatchSet, :next_match)
|
34
|
+
|
35
|
+
Instrument.on_entry_to(Reg::Progress, :backtrack, :position_stack, :position_inc_stack, "(args[0] || context).regsidx", "(args[0] || context).matcher")
|
36
|
+
Instrument.on_exit_from(Reg::Progress, :backtrack)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
=begin copyright
|
2
2
|
reg - the ruby extended grammar
|
3
|
-
Copyright (C) 2005,
|
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
|
@@ -25,12 +25,12 @@ module Reg
|
|
25
25
|
return ItemThat.new(klass) unless block
|
26
26
|
|
27
27
|
class <<block
|
28
|
-
#
|
28
|
+
# arguments to include appear to be processed in a strange order... this is
|
29
29
|
#significant if order is important to you
|
30
30
|
#the #included methods are perhaps not called when i think they should be....
|
31
31
|
#(DON'T simplify the next line; it'll break.)
|
32
|
-
[BlankSlate ,Formula, BlockLike].each{|mod| include mod}
|
33
|
-
restore :inspect,:extend,:call
|
32
|
+
[BlankSlate ,];[Formula, BlockLike].each{|mod| include mod}
|
33
|
+
# restore :inspect,:extend,:call
|
34
34
|
|
35
35
|
alias === eeee #workaround-- shouldn't be needed
|
36
36
|
alias formula_value call
|
@@ -57,6 +57,11 @@ module Reg
|
|
57
57
|
alias === eeee #does nothing in includers.... why?
|
58
58
|
def formula_value *a,&b; call( *a,&b) end
|
59
59
|
|
60
|
+
def % name
|
61
|
+
super unless Symbol===name
|
62
|
+
Bound.new(name,self)
|
63
|
+
end
|
64
|
+
|
60
65
|
|
61
66
|
def mixmod; ItemThatLike end
|
62
67
|
def reg; dup.extend Reg end
|
@@ -64,20 +69,22 @@ module Reg
|
|
64
69
|
|
65
70
|
#----------------------------------
|
66
71
|
module DeferredQuery
|
67
|
-
#just an ancestor for
|
72
|
+
#just an ancestor for ItemThatLike and RegThatLike
|
68
73
|
def eee(item)
|
69
|
-
|
70
|
-
rescue; false
|
71
|
-
end
|
74
|
+
formula_value item,{} rescue false
|
72
75
|
end
|
73
76
|
alias === eee #this doesn't work!!! WHY????
|
74
77
|
#there's no way to define the method === in this
|
75
78
|
#module and have it be defined in class ItemThat.
|
76
79
|
#mixmod and reg don't have this problem. this must
|
77
|
-
#be a bug. for now, I work around it with clever
|
80
|
+
#be a bug(??). for now, I work around it with clever
|
78
81
|
#alias/undefing in places that include/extend ItemThatLike
|
79
82
|
#(seemingly, this is only a problem when including, not extending... dunno why)
|
80
83
|
#this bug may be gone now; need to try to get rid of weird eee stuff.
|
84
|
+
|
85
|
+
# def mmatch(pr)
|
86
|
+
# !pr.cursor.eof? and self===pr.cursor.readahead1 and [true,1]
|
87
|
+
# end
|
81
88
|
|
82
89
|
end
|
83
90
|
|
@@ -87,6 +94,13 @@ module Reg
|
|
87
94
|
|
88
95
|
def mixmod; ItemThatLike end
|
89
96
|
def reg; dup.extend RegThatLike end
|
97
|
+
|
98
|
+
|
99
|
+
#I should really consider adding better definitions of to_str, to_ary, (maybe) to_proc here
|
100
|
+
#respond_to? would be a good one too.
|
101
|
+
#or even in Deferred, maybe
|
102
|
+
#as it is, all kinds of weird stuff happens if respond_to? and to_ary both return true values,
|
103
|
+
#(which they do currently)
|
90
104
|
end
|
91
105
|
|
92
106
|
#----------------------------------
|
@@ -118,10 +132,16 @@ module Reg
|
|
118
132
|
include Reg
|
119
133
|
|
120
134
|
def mixmod; RegThatLike end
|
135
|
+
def reg; self end
|
136
|
+
end
|
137
|
+
|
138
|
+
#----------------------------------
|
139
|
+
class RegThat < ItemThat
|
140
|
+
include RegThatLike
|
121
141
|
end
|
122
142
|
|
123
143
|
#----------------------------------
|
124
|
-
class RegThat
|
144
|
+
nil&&class RegThat
|
125
145
|
include BlankSlate
|
126
146
|
restore :inspect,:extend
|
127
147
|
include Formula
|
@@ -130,13 +150,14 @@ module Reg
|
|
130
150
|
undef eee
|
131
151
|
def initialize(klass=nil)
|
132
152
|
@klass=klass
|
153
|
+
super
|
133
154
|
end
|
134
155
|
|
135
156
|
def formula_value(val,*rest)
|
136
157
|
#the exception raised here should be (eventually) caught by
|
137
158
|
#the handler in RegThatLike#eee. calling RegThat#formula_value isn't
|
138
159
|
#really legitimate otherwise.
|
139
|
-
@klass and @klass===val || fail("
|
160
|
+
@klass and @klass===val || fail("reg_that constraint mismatch")
|
140
161
|
|
141
162
|
val
|
142
163
|
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
|
@@ -20,7 +20,9 @@ module Reg
|
|
20
20
|
class Knows
|
21
21
|
include Reg
|
22
22
|
attr :sym
|
23
|
-
def initialize(sym)
|
23
|
+
def initialize(sym)
|
24
|
+
@sym=sym
|
25
|
+
end
|
24
26
|
|
25
27
|
def === (obj)
|
26
28
|
obj.respond_to? @sym
|
@@ -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
|
@@ -17,7 +17,7 @@
|
|
17
17
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18
18
|
=end
|
19
19
|
|
20
|
-
|
20
|
+
require 'warning'
|
21
21
|
|
22
22
|
module Reg
|
23
23
|
|
@@ -81,10 +81,10 @@ module Reg
|
|
81
81
|
|
82
82
|
#--------------------------
|
83
83
|
class Not
|
84
|
-
include Reg
|
84
|
+
include Reg,Composite
|
85
85
|
def initialize(reg)
|
86
|
-
@reg=reg
|
87
|
-
|
86
|
+
@reg=Deferred.defang! reg
|
87
|
+
super
|
88
88
|
end
|
89
89
|
def ===(obj)
|
90
90
|
!(@reg===obj)
|
@@ -92,6 +92,7 @@ module Reg
|
|
92
92
|
def ~
|
93
93
|
@reg
|
94
94
|
end
|
95
|
+
|
95
96
|
|
96
97
|
#deMorgan's methods...
|
97
98
|
def &(reg)
|
@@ -108,8 +109,20 @@ module Reg
|
|
108
109
|
super reg
|
109
110
|
end
|
110
111
|
end
|
112
|
+
def ^(reg)
|
113
|
+
if Not===reg
|
114
|
+
~self ^ ~reg
|
115
|
+
else
|
116
|
+
super reg
|
117
|
+
end
|
118
|
+
end
|
119
|
+
#a|b|!a|!b|!a^!b|a^b
|
120
|
+
#0|0|1 |1 |0 |0
|
121
|
+
#0|1|1 |0 |1 |1
|
122
|
+
#1|0|0 |1 |1 |1
|
123
|
+
#1|1|1 |1 |0 |0
|
111
124
|
|
112
|
-
#
|
125
|
+
#mmatch_full implementation needed
|
113
126
|
|
114
127
|
def inspect
|
115
128
|
"~("+@reg.inspect+")"
|
@@ -117,79 +130,83 @@ module Reg
|
|
117
130
|
|
118
131
|
def subregs; [@reg] end
|
119
132
|
end
|
120
|
-
|
133
|
+
|
121
134
|
#--------------------------
|
122
|
-
|
123
|
-
|
135
|
+
#only subclasses should be created
|
136
|
+
class Logical
|
137
|
+
include Reg,Composite
|
124
138
|
attr :regs
|
139
|
+
class<<self
|
140
|
+
def new(*regs)
|
141
|
+
#"optimization of Logicals over a single sub-expression disabled"
|
142
|
+
#regs.size==1 and return regs.first
|
143
|
+
regs=regs.map{|r| Deferred.defang! r }
|
144
|
+
super
|
145
|
+
end
|
146
|
+
alias [] new
|
147
|
+
end
|
125
148
|
def initialize(*regs)
|
126
149
|
@regs=regs
|
150
|
+
(@regs.size-1).downto(0){|i|
|
151
|
+
@regs[i,1]=*@regs[i].subregs if self.class==@regs[i].class
|
152
|
+
}
|
127
153
|
|
128
|
-
|
129
|
-
end
|
130
|
-
def ===(obj)
|
131
|
-
@regs.each {|reg| reg===obj and return obj||true }
|
132
|
-
return false
|
154
|
+
super
|
133
155
|
end
|
134
|
-
|
135
|
-
|
136
|
-
|
156
|
+
def subregs; @regs.dup end
|
157
|
+
|
158
|
+
|
159
|
+
def op(reg)
|
160
|
+
newregs=@regs + ((self.class===reg )? reg.subregs : [reg])
|
137
161
|
self.class.new(*newregs)
|
138
162
|
end
|
139
163
|
|
140
|
-
|
141
|
-
|
142
|
-
|
164
|
+
def mmatch(progress)
|
165
|
+
progress.cursor.eof? and return
|
166
|
+
other=progress.cursor.readahead1
|
167
|
+
self.eee other and [true,1]
|
143
168
|
end
|
169
|
+
|
170
|
+
def max_matches; @regs.size end
|
144
171
|
|
145
|
-
def
|
172
|
+
def self.specialize_with_operator oper
|
173
|
+
%{
|
174
|
+
alias_method :#{oper}, :op
|
175
|
+
def inspect
|
176
|
+
@regs.size==0 and return "::#{name}[]"
|
177
|
+
@regs.size==1 and return "::#{name}[\#{@regs.first.inspect}]"
|
178
|
+
@regs.collect{|r| r.inspect}.join' #{oper} '
|
179
|
+
end
|
180
|
+
}
|
181
|
+
end
|
146
182
|
end
|
147
183
|
|
148
184
|
#--------------------------
|
149
|
-
class
|
150
|
-
include Reg
|
151
|
-
attr :regs
|
152
|
-
def initialize(*regs)
|
153
|
-
@regs=regs
|
154
|
-
maybe_multiples(*regs)
|
155
|
-
end
|
185
|
+
class Or < Logical
|
156
186
|
def ===(obj)
|
157
|
-
@regs.each {|reg| reg===obj
|
158
|
-
return
|
159
|
-
end
|
160
|
-
def &(reg)
|
161
|
-
newregs=@regs + ((self.class===reg)? reg.regs : [reg])
|
162
|
-
self.class.new(*newregs)
|
163
|
-
end
|
164
|
-
|
165
|
-
def inspect
|
166
|
-
@regs.collect{|r| r.inspect}.join' & '
|
187
|
+
@regs.each {|reg| reg===obj and return obj || true }
|
188
|
+
return false
|
167
189
|
end
|
168
|
-
|
169
|
-
|
190
|
+
alias eee ===
|
191
|
+
eval specialize_with_operator(:|)
|
170
192
|
end
|
171
|
-
|
172
193
|
#--------------------------
|
173
|
-
class
|
174
|
-
include Reg
|
175
|
-
attr :regs
|
176
|
-
def initialize(*regs)
|
177
|
-
@regs=regs
|
178
|
-
maybe_multiples(*regs)
|
179
|
-
end
|
194
|
+
class And < Logical
|
180
195
|
def ===(obj)
|
181
|
-
@regs.
|
196
|
+
@regs.each {|reg| reg===obj or return false }
|
197
|
+
return obj || true
|
182
198
|
end
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
199
|
+
alias eee ===
|
200
|
+
eval specialize_with_operator(:&)
|
201
|
+
end
|
202
|
+
#--------------------------
|
203
|
+
class Xor < Logical
|
204
|
+
def ===(obj)
|
205
|
+
@regs.find_all {|reg| reg===obj }.size==1 or return
|
206
|
+
return obj || true
|
191
207
|
end
|
192
|
-
|
193
|
-
|
208
|
+
alias eee ===
|
209
|
+
eval specialize_with_operator(:^)
|
194
210
|
end
|
211
|
+
|
195
212
|
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
|
@@ -26,73 +26,83 @@ module Reg
|
|
26
26
|
class LookAhead
|
27
27
|
include Reg
|
28
28
|
include Multiple
|
29
|
+
include Composite
|
29
30
|
def initialize(reg)
|
30
31
|
@reg=reg
|
32
|
+
super
|
31
33
|
end
|
32
34
|
|
33
35
|
def mmatch(pr)
|
34
|
-
@reg.mmatch(pr
|
36
|
+
@reg.mmatch(pr)
|
37
|
+
return [true,0]
|
38
|
+
end
|
39
|
+
|
40
|
+
def mmatch_full
|
41
|
+
huh
|
35
42
|
end
|
36
43
|
|
37
44
|
def itemrange; 0..0 end
|
38
45
|
|
39
|
-
def subregs; [@reg] end
|
40
|
-
|
41
46
|
#most methods should be forwarded to @reg... tbd
|
42
|
-
|
47
|
+
|
43
48
|
end
|
44
49
|
|
45
50
|
|
46
51
|
class LookBack
|
47
52
|
include Reg
|
48
53
|
include Multiple
|
54
|
+
include Composite
|
55
|
+
|
49
56
|
|
50
57
|
def initialize(reg)
|
51
58
|
|
52
59
|
r=reg.itemrange
|
53
60
|
@r=r.last-r.first
|
54
61
|
@reg=reg
|
55
|
-
|
62
|
+
super
|
56
63
|
end
|
57
64
|
|
58
65
|
def itemrange; 0..0 end
|
59
|
-
|
60
|
-
def
|
61
|
-
|
66
|
+
|
67
|
+
def mmatch(pr)
|
68
|
+
warn "look at prev item, not next_"
|
69
|
+
@reg.mmatch(pr)
|
70
|
+
return [true,0]
|
62
71
|
end
|
63
|
-
|
72
|
+
|
64
73
|
def regs_ary(pos,r=@r)
|
65
74
|
[ (OB-r).l, @reg, Position[pos] ] #require unimplemented lazy operator....
|
66
75
|
end
|
67
76
|
|
68
|
-
def
|
77
|
+
def mmatch_full(pr)
|
78
|
+
huh
|
79
|
+
|
69
80
|
cu=pr.cursor
|
70
81
|
ra=@reg.itemrange
|
71
82
|
pos=cu.pos
|
72
83
|
startpos=pos-ra.last
|
84
|
+
pos>=ra.first or return
|
73
85
|
r=if startpos<0
|
74
86
|
huh 'dunno what to do here'
|
75
|
-
|
87
|
+
newlast=ra.last+startpos
|
88
|
+
assert newlast>=ra.first #???
|
76
89
|
startpos=0
|
77
|
-
|
90
|
+
ra=ra.first..ra.last+startpos
|
91
|
+
ra.last-ra.first
|
78
92
|
else
|
79
93
|
@r
|
80
94
|
end
|
81
95
|
reg= +regs_ary(pos,r)
|
82
|
-
cu
|
83
|
-
cu.pos=startpos
|
84
|
-
reg.mmatch pr.subprogress(cu,reg)
|
96
|
+
reg.mmatch pr.subprogress(cu.position(startpos))
|
85
97
|
|
86
98
|
#original pr.cursor position should be unchanged
|
87
99
|
assert pos==cu.pos
|
88
100
|
|
89
|
-
huh #result should be fixed up to remove evidence of
|
101
|
+
#huh #result should be fixed up to remove evidence of
|
90
102
|
#the subsequence and all but it's middle element.
|
103
|
+
return [true,0]
|
91
104
|
end
|
92
|
-
|
93
|
-
def subregs; [@reg] end
|
94
|
-
|
105
|
+
|
95
106
|
#most methods should be forwarded to @reg... tbd
|
96
107
|
end
|
97
|
-
|
98
108
|
end
|