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,126 +0,0 @@
1
- =begin copyright
2
- reg - the ruby extended grammar
3
- Copyright (C) 2005 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
- =begin
25
- #----------------------------------
26
- module Block
27
- # include Formula
28
-
29
- attr_writer :klass #for internal use only
30
-
31
- def eeee(val)
32
- @klass and @klass===val || return
33
- begin call(val)
34
- rescue: false
35
- end
36
- end
37
- alias === eeee #does nothing in includers.... why?
38
- alias formula_value eeee
39
-
40
-
41
- def mixmod; ItemThatLike end
42
- def reg; extend Reg end
43
- end
44
- #----------------------------------
45
- module ItemThatLike
46
- def eee(item)
47
- begin formula_value item
48
- rescue: false
49
- end
50
- end
51
- alias === eee #this doesn't work!!! WHY????
52
- #there's no way to define the method === in this
53
- #module and have it be defined in class ItemThat.
54
- #mixmod and reg don't have this problem. this must
55
- #be a bug. for now, I work around it with clever
56
- #alias/undefing in places that include/extend ItemThatLike
57
- #(seemingly, this is only a problem when including, not extending... dunno why)
58
- #this bug may be gone now; need to try to get rid of weird eee stuff.
59
-
60
- def mixmod; ItemThatLike end
61
- def reg; extend Reg end
62
- end
63
- #----------------------------------
64
- class ItemThat
65
- include BlankSlate
66
- restore :inspect,:extend
67
- include Formula
68
- include ItemThatLike
69
- alias === eee
70
- undef eee
71
- def initialize(klass=nil)
72
- @klass=klass
73
- end
74
-
75
- def formula_value(val,*rest)
76
- #the exception raised here should be (eventually) caught by
77
- #the handler in ItemThatLike#eee. calling ItemThat#formula_value isn't
78
- #really legitimate otherwise.
79
- @klass and @klass===val || fail("item_that constraint mismatch")
80
-
81
- val
82
- end
83
-
84
- end
85
- =end
86
- #----------------------------------
87
- module BackrefLike
88
- def mmatch(ary,idx)
89
- huh #need info thats not_in ary or idx
90
- end
91
-
92
- def mixmod; BackrefLike end
93
- end
94
- #----------------------------------
95
- class Backref
96
- include BlankSlate
97
- restore :inspect,:extend
98
- include Formula
99
- include BackrefLike
100
- def initialize(name,*path)
101
- #complex paths not handled yet
102
- raise ParameterError.new unless path.empty? and Reg|Symbol===name
103
- @name=name
104
- end
105
-
106
- def formula_value(*ctx)
107
- progress=ctx[1]
108
- progress.variables[@name]
109
- end
110
-
111
- class<<self
112
- alias [] new
113
- end
114
- end
115
-
116
-
117
- #----------------------------------
118
- module BRLike
119
- include BackrefLike
120
- include Reg
121
- def mixmod; BRLike end
122
- end
123
- class BR < Backref
124
- include BRLike
125
- end
126
- end
data/regbind.rb DELETED
@@ -1,74 +0,0 @@
1
- =begin copyright
2
- reg - the ruby extended grammar
3
- Copyright (C) 2005 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
- Bound.new(name,self)
23
- end
24
- alias << bind
25
-
26
- def side_effect(&block); SideEffect.new(self,&block) end
27
- def undo(&block); Undo.new(self,&block) end
28
- end
29
-
30
- #-------------------------------------
31
- class Bound
32
- include Reg
33
- def initialize(name,reg)
34
- @name,@reg=name,reg
35
- end
36
-
37
-
38
- def mmatch(progress)
39
- result=@reg.mmatch(progress) and
40
- progress.register_var(@name,@reg)
41
- return result
42
- end
43
- end
44
-
45
- #-------------------------------------
46
- class SideEffect
47
- include Reg
48
-
49
- def initialize(reg,&block)
50
- @reg,@block=reg,block
51
- end
52
-
53
- def mmatch(progress)
54
- result=@reg.mmatch(progress) and
55
- @block.call(progress)
56
- return result
57
- end
58
- end
59
-
60
- #------------------------------------
61
- class Undo
62
- include Reg
63
-
64
- def initialize(reg,&block)
65
- @reg,@block=reg,block
66
- end
67
-
68
- def mmatch(progress)
69
- result=@reg.mmatch(progress) and
70
- progress.register_undo(@block)
71
- return result
72
- end
73
- end
74
- end
@@ -1 +0,0 @@
1
- Methods/classes Reg Or And Xor Not Hash OrderedHash Object OrderedObject Array Fixed Equals Literal Multiple Interpreter Subseq Repeat LookAhead LookBack Regexp Module Set Range Pair Position
+
2
-
3
1
 
data/reghash.rb DELETED
@@ -1,318 +0,0 @@
1
-
2
- =begin copyright
3
- reg - the ruby extended grammar
4
- Copyright (C) 2005 Caleb Clausen
5
-
6
- This library is free software; you can redistribute it and/or
7
- modify it under the terms of the GNU Lesser General Public
8
- License as published by the Free Software Foundation; either
9
- version 2.1 of the License, or (at your option) any later version.
10
-
11
- This library is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
- Lesser General Public License for more details.
15
-
16
- You should have received a copy of the GNU Lesser General Public
17
- License along with this library; if not, write to the Free Software
18
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
- =end
20
-
21
-
22
- require 'forward_to'
23
-
24
-
25
-
26
- module Reg
27
-
28
- class Hash
29
- include Reg
30
- attr :others
31
-
32
- def initialize(hashdat=nil)
33
- @matchers={}
34
- @literals={}
35
- @others=nil
36
- hashdat or return
37
- hashdat.key?(OB) and @others=hashdat.delete(OB)
38
- hashdat.each {|key,val|
39
- if Reg.interesting_matcher? key
40
- Fixed===key and key=key.unwrap
41
- @matchers[key]=val
42
- else
43
- Equals===key and key=key.unwrap
44
- @literals[key]=val
45
- end
46
- }
47
- end
48
-
49
- def Hash.[](*args); new(*args); end
50
-
51
- def matches_class; ::Hash end
52
-
53
- def ordered
54
- pairs=[]
55
- @literals.each{|k,v| pairs<< Pair[k,v] }
56
- @matchers.each{|k,v| pairs<< Pair[k,v] }
57
- pairs<<Pair[OB,@others] if @others
58
- return +pairs
59
- end
60
-
61
- def subregs;
62
- @literals.keys + @literals.values +
63
- @matchers.keys + @matchers.values +
64
- (@others==nil ? [OB,@others] : [])
65
- end
66
-
67
- def inspect
68
- result=[]
69
- result<<@literals.inspect.sub(/.(.*)./, "\\1") unless @literals.empty?
70
- result<<@matchers.inspect.sub(/.(.*)./, "\\1") unless @matchers.empty?
71
- result<<"OB=>#{@others.inspect}" if defined? @others and @others!=nil
72
- return "+{#{result.join(", ")}}"
73
- end
74
-
75
- def -@ #make object matcher
76
- Rob(@literals.merge(@matchers).merge(OB=>@others))
77
- end
78
-
79
- def ===(other)
80
- matchedliterals={}
81
- matchedmatchers={}
82
- other.each_pair{|key,val|
83
- #literals get a chance first
84
- @literals.key? key and
85
- if (@literals[key]===val)
86
- matchedliterals[key]=true
87
- next
88
- else
89
- return
90
- end
91
-
92
- #now try more general matchers
93
- saw_matcher=nil
94
- @matchers.each_pair{|mkey,mval|
95
- if mkey===key
96
- return unless (mval===val)
97
- saw_matcher=matchedmatchers[mkey]=true
98
- break
99
- end
100
- }
101
-
102
-
103
- #last of all, try the catchall
104
- saw_matcher or @others===val or return
105
- }
106
- @literals.each_pair{|k,v| matchedliterals[k] or v==(other.default k) or return }
107
- @matchers.each_pair{|k,v| matchedmatchers[k] or v===other.default or return }
108
- other.empty? and @literals.empty? and @matchers.empty? and return @others===other.default
109
- return true
110
- end
111
-
112
- #tla of +{}
113
- assign_TLAs :Rah=>:Hash
114
-
115
- end
116
-
117
- #--------------------------
118
- class OrderedHash
119
- include Reg
120
- def initialize(*args)
121
- @keys=[]
122
- @vals=[]
123
- @others=nil
124
- args.each{|a|
125
- if Pair===a
126
- l,r=a.left,a.right
127
- Fixed===l and l=l.unwrap
128
- if l==OB
129
- @others=r
130
- else
131
- @keys<<l
132
- @vals<<r
133
- end
134
- else
135
- @keys<<a
136
- @vals<<OB
137
- end
138
- }
139
- end
140
-
141
- def ===(other)
142
- matched=0
143
- saw1=nil
144
- other.each_pair do |ko,vo|
145
- saw1=nil
146
- @vals.each_index do|i|
147
- kr,vr=@keys[i],@vals[i]
148
- if kr===ko
149
- vr===vo or return
150
- saw1=matched |= 1<<i
151
- break
152
- end
153
- end
154
- saw1 or (@others===vo or return)
155
- end
156
- @vals.each_index {|i|
157
- unless (matched&(1<<i)).nonzero?
158
- @vals[i]===other.default((@keys[i] unless Reg::interesting_matcher? @keys[i])) or return
159
- end
160
- }
161
- other.empty? and @vals.empty? and return @others===other.default
162
- # @subhashes.each do|subhash|
163
- # subhash===other or return false
164
- # end
165
- return other||true #huh need more complex result?
166
- end
167
-
168
- def self.[](*args); new(*args); end
169
-
170
- def matches_class; ::Hash end
171
-
172
- def &; huh end
173
- def inspect;
174
- "+[#{
175
- map{|k,v| k.inspect+ ((Reg===k)? "" : ".reg") +
176
- "**"+v.inspect unless OB==k && nil==v
177
- }.compact.join ", "
178
- }]"
179
- end
180
- def subregs; huh end
181
-
182
- def each
183
- @keys.each_index{|i|
184
- yield @keys[i],@vals[i]
185
- }
186
- yield OB,@others
187
- end
188
- include Enumerable
189
-
190
-
191
- def to_ruby
192
- result= "def self.===(a_hash)\n"
193
- result<<" a_hash.each_pair{|k,v|\n"
194
- result<<" case k\n"
195
- @keys.each_index{|i|
196
- result<<" when #{@keys[i]}: #{vals[i]}\n"
197
- }
198
- result<<" else #{@other}\n" +
199
- " end===v or break\n" +
200
- " }\n" +
201
- "end\n"
202
-
203
- return result
204
- end
205
- end
206
-
207
- #--------------------------
208
- class Object < Hash
209
- #decending from Hash isn't particularly useful here
210
- #it looks like everything (but &) is overridden, anyway
211
-
212
- def initialize(*args)
213
- hash= (::Hash===args.last ? args.pop : {})
214
- @vars={}; @meths={}; @meth_matchers={}
215
- hash.each_pair{|item,val|
216
- if ::String===item or ::Symbol===item
217
- item=item.to_s
218
- (/^@@?/===item ? @vars : @meths)[item.to_sym]=val
219
- else
220
- @meth_matchers[item]=val
221
- end
222
- }
223
- @meths[:class]=args.shift if (Module===args.first)
224
- end
225
- def self.[](*args) new(*args); end
226
-
227
-
228
- def inspect
229
- "-{"+@vars.inspect.sub( /^.(.*).$/, '\\1') +
230
- @meths.inspect.sub(/^.(.*).$/, '\\1') +
231
- @meth_matchers.inspect.sub(/^.(.*).$/, '\\1') + "}"
232
-
233
- end
234
-
235
- def matches_class
236
- @meths[:class] or Object
237
- end
238
-
239
- def -@ #make object matcher
240
- self
241
- end
242
-
243
- def ===(other)
244
- seenmeths=[];seenvars=[];seenmats=[]
245
- @meths.each_pair{|name,val|
246
- val===other.send(name) or return
247
- seenmeths<<name
248
- }
249
- @vars.each_pair{|name,val|
250
- val===other.instance_eval(name.to_s) or return
251
- seenvars<<name
252
- }
253
- @meth_matchers.empty? or other.public_methods.each {|meth|
254
- @meth_matchers.each_pair{|name, val|
255
- if name===meth
256
- val===other.send(meth) || return
257
- seenmats<< name
258
- end
259
- }
260
- }
261
- @meths.keys.-(seenmeths).empty? or return
262
- @vars.keys.-(seenvars).empty? or return
263
- @meth_matchers.keys.-(seenmats).empty? or return
264
-
265
-
266
- return other || true
267
-
268
- rescue
269
- return false
270
- end
271
-
272
- #tla of -{}
273
- assign_TLAs :Rob=>:Object
274
-
275
-
276
- end
277
-
278
-
279
- #OrderedObject not even attempted yet
280
-
281
- module Reg
282
- def **(other)
283
- Pair[self,other]
284
- end
285
- alias has **
286
- end
287
-
288
- class Pair
289
- include Reg
290
- class<<self; alias [] new; end
291
-
292
- attr_reader :left,:right
293
-
294
- def initialize(l,r)
295
- @left,@right=l,r
296
- end
297
-
298
- if false #not sure if i know what i want here...
299
- def hash_cmp(hash,k,v)
300
- @left
301
- end
302
-
303
- def obj_cmp
304
- end
305
- end
306
- #tla of **
307
- assign_TLAs :Rap=>:Pair
308
-
309
-
310
-
311
- end
312
-
313
-
314
-
315
- end
316
-
317
-
318
-