reg 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,63 @@
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
+ class Knows
21
+ include Reg
22
+ attr :sym
23
+ def initialize(sym) @sym=sym end
24
+
25
+ def === (obj)
26
+ obj.respond_to? @sym
27
+ end
28
+
29
+ def [] *args
30
+ Args.new(sym,*args)
31
+ end
32
+
33
+ def -@; self end
34
+
35
+ def inspect
36
+ "-:#{sym}"
37
+ end
38
+
39
+ #(also, the usual [] => new alias)
40
+ class<< self
41
+ alias[]new
42
+ end
43
+
44
+ class Args < Knows
45
+ attr :argsreg
46
+
47
+ def initialize(sym,*argsreg)
48
+ super sym
49
+ @argsreg=::Reg[*argsreg]
50
+ end
51
+
52
+ def inspect
53
+ ":#{sym}"+@argsreg.inspect[1..-1]
54
+ end
55
+ def ===(obj)
56
+ super and huh "need some more help here"
57
+ end
58
+ def -@; self end
59
+ end
60
+
61
+ end
62
+
63
+ end
@@ -0,0 +1,195 @@
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
+
20
+
21
+
22
+ module Reg
23
+
24
+ #--------------------------
25
+ module Reg
26
+
27
+ #logical negation - returns a new reg that matches everything
28
+ #that the receiver doesn't.
29
+ def ~
30
+ Not.new(self)
31
+ end
32
+ alias not ~
33
+
34
+ #create a Reg matching either self or other (or both).
35
+ #(creates a Reg::Or.)
36
+ #Reg#|'s may be chained; the result is a single large
37
+ #Reg::Or containing all the subexpressions directly,
38
+ #rather than a linked list of Reg::Or's.
39
+ #
40
+ #if any subRegs of | are vector Regs, the larger | Reg
41
+ #is considered a vector as well. eats as much
42
+ #input as the subexpression which matched. unlike other
43
+ #vector Reg expressions, | is not greedy; instead, the
44
+ #first alternative which matches is accepted. (subject
45
+ #to later backtracking, of course.)
46
+ def |(other)
47
+ Or.new(self,other)
48
+ end
49
+
50
+ #create a Reg matching self and other. (creates a Reg::And).
51
+ #Reg#&'s may be chained; the result is a single large
52
+ #Reg::And containing all the subexpressions directly,
53
+ #rather than a linked list of Reg::And's.
54
+ #
55
+ #if any subRegs of & are vector Regs, the larger & Reg
56
+ #is considered a vector as well. eats as much
57
+ #input as the longest subexpression.
58
+ def &(other)
59
+ And.new(self,other)
60
+ end
61
+
62
+ #create a Reg matching either self or other, but not both.
63
+ #(creates a Reg::Xor).
64
+ #Reg#^'s may be chained; the result is a single large
65
+ #Reg::Xor containing all the subexpressions directly,
66
+ #rather than a linked list of Reg::Xor's. (believe it or not,
67
+ #this has the same semantics.) in an xor chain created this
68
+ #way, only one of the several alternatives may match. if
69
+ #2 or more alternatives both match, the larger xor
70
+ #expression will fail.
71
+ #
72
+ #if any subRegs of ^ are vector Regs, the larger ^ Reg
73
+ #is considered a vector as well. eats as much input as
74
+ #the (only) subReg which matches.
75
+ def ^(other)
76
+ Xor.new(self,other)
77
+ end
78
+
79
+ end
80
+
81
+
82
+ #--------------------------
83
+ class Not
84
+ include Reg
85
+ def initialize(reg)
86
+ @reg=reg
87
+ maybe_multiples(reg)
88
+ end
89
+ def ===(obj)
90
+ !(@reg===obj)
91
+ end
92
+ def ~
93
+ @reg
94
+ end
95
+
96
+ #deMorgan's methods...
97
+ def &(reg)
98
+ if Not===reg
99
+ ~( ~self | ~reg )
100
+ else
101
+ super reg
102
+ end
103
+ end
104
+ def |(reg)
105
+ if Not===reg
106
+ ~( ~self & ~reg )
107
+ else
108
+ super reg
109
+ end
110
+ end
111
+
112
+ #mmatch_multiple implementation needed
113
+
114
+ def inspect
115
+ "~("+@reg.inspect+")"
116
+ end
117
+
118
+ def subregs; [@reg] end
119
+ end
120
+
121
+ #--------------------------
122
+ class Or
123
+ include Reg
124
+ attr :regs
125
+ def initialize(*regs)
126
+ @regs=regs
127
+
128
+ maybe_multiples(*regs)
129
+ end
130
+ def ===(obj)
131
+ @regs.each {|reg| reg===obj and return obj }
132
+ return false
133
+ end
134
+
135
+ def |(reg)
136
+ newregs=@regs + ((self.class===reg )? reg.regs : [reg])
137
+ self.class.new(*newregs)
138
+ end
139
+
140
+
141
+ def inspect
142
+ @regs.collect{|r| r.inspect}.join' | '
143
+ end
144
+
145
+ def subregs; @regs end
146
+ end
147
+
148
+ #--------------------------
149
+ class And
150
+ include Reg
151
+ attr :regs
152
+ def initialize(*regs)
153
+ @regs=regs
154
+ maybe_multiples(*regs)
155
+ end
156
+ def ===(obj)
157
+ @regs.each {|reg| @reg===obj or return false }
158
+ return obj
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' & '
167
+ end
168
+
169
+ def subregs; @regs end
170
+ end
171
+
172
+ #--------------------------
173
+ class Xor
174
+ include Reg
175
+ attr :regs
176
+ def initialize(*regs)
177
+ @regs=regs
178
+ maybe_multiples(*regs)
179
+ end
180
+ def ===(obj)
181
+ @regs.find_all {|reg| reg===obj }.size==1
182
+ end
183
+ def ^(reg)
184
+ newregs=@regs + ((self.class===reg )? reg.regs : [reg])
185
+ self.class.new(*newregs)
186
+ end
187
+
188
+
189
+ def inspect
190
+ @regs.collect{|r| r.inspect}.join' ^ '
191
+ end
192
+
193
+ def subregs; @regs end
194
+ end
195
+ end
@@ -0,0 +1,94 @@
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 la; LookAhead.new self end
22
+ def lb; LookBack.new self end
23
+ end
24
+
25
+
26
+ class LookAhead
27
+ include Reg
28
+ include Multiple
29
+ def initialize(reg)
30
+ @reg=reg
31
+ end
32
+
33
+ def mmatch(pr)
34
+ @reg.mmatch(pr.subprogress pr.cursor.position,@reg)
35
+ end
36
+
37
+ def itemrange; 0..0 end
38
+
39
+ #most methods should be forwarded to @reg... tbd
40
+
41
+ end
42
+
43
+
44
+ class LookBack
45
+ include Reg
46
+ include Multiple
47
+
48
+ def initialize(reg)
49
+
50
+ r=reg.itemrange
51
+ @r=r.last-r.first
52
+ @reg=reg
53
+
54
+ end
55
+
56
+ def itemrange; 0..0 end
57
+
58
+ def regs
59
+ [*regs_ary(huh)]
60
+ end
61
+
62
+ def regs_ary(pos,r=@r)
63
+ [ (OB-r).l, @reg, Position[pos] ] #require unimplemented lazy operator....
64
+ end
65
+
66
+ def mmatch(pr)
67
+ cu=pr.cursor
68
+ ra=@reg.itemrange
69
+ pos=cu.pos
70
+ startpos=pos-ra.last
71
+ r=if startpos<0
72
+ huh 'dunno what to do here'
73
+ foo=ra.first+startpos
74
+ startpos=0
75
+ pos-ra.first
76
+ else
77
+ @r
78
+ end
79
+ reg= +regs_ary(pos,r)
80
+ cu=cu.position
81
+ cu.pos=startpos
82
+ reg.mmatch pr.subprogress(cu,reg)
83
+
84
+ #original pr.cursor position should be unchanged
85
+ assert pos==cu.pos
86
+
87
+ huh #result should be fixed up to remove evidence of
88
+ #the subsequence and all but it's middle element.
89
+ end
90
+
91
+ #most methods should be forwarded to @reg... tbd
92
+ end
93
+
94
+ end
@@ -0,0 +1,75 @@
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
+
20
+
21
+ #the names defined here are considered obsolete, and will not be supported
22
+ #anymore at some point in the future.
23
+
24
+ for name in ["And", "Array","Equals", "Fixed", "Hash", "Literal",
25
+ "Multiple", "Not", "Object", "Or", "Repeat",
26
+ "String", "Subseq", "Symbol", "Xor"] do
27
+
28
+ if Class===::Reg.const_get(name)
29
+ eval <<-endeval
30
+ class ::Reg#{name} < Reg::#{name}
31
+
32
+ def initialize(*args,&block)
33
+ @@warned_already ||= !warn("Reg#{name} is obsolete; use Reg::#{name} instead")
34
+ super
35
+ end
36
+ end
37
+ endeval
38
+ else
39
+ eval <<-endeval
40
+ module ::Reg#{name}; include Reg::#{name}
41
+ def self.included(*args,&block)
42
+ @@warned_already ||= !warn("Reg#{name} is obsolete; use Reg::#{name} instead")
43
+ super
44
+ end
45
+ end
46
+ endeval
47
+
48
+ end
49
+
50
+ # Object.const_set "Reg"+name, (Reg.const_get name)
51
+ end
52
+
53
+ #need a way to obsolete these...
54
+ Reg::Number=Range
55
+ Reg::Const=Reg::Deferred::Const
56
+
57
+ #--------------------------
58
+ #an executable Reg... a Proc that responds to ===
59
+ #obsolete: please use item_that instead.
60
+ def proceq(klass=Object,&block)
61
+ @@proceqWarnedAlready ||= !warn( "proceq is obsolete; please use item_that instead")
62
+
63
+ block.arity.abs==1 or raise ArgumentError.new( "must be 1 arg")
64
+ class <<block
65
+ include Reg::Reg
66
+ def klass=(k) @@klass=k end
67
+ alias === call
68
+ #eventually pass (more) params to call
69
+ def matches_class; @@klass end
70
+ alias starts_with matches_class
71
+ alias ends_with matches_class
72
+ end
73
+ block.klass=klass
74
+ block
75
+ end
@@ -0,0 +1,74 @@
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
+
20
+ =begin
21
+ path specification:
22
+ each path is relative to the reg it references into.
23
+
24
+ in order to support named subexpressions, most reg classes that participate
25
+ in paths (the ones listed below) will also take a subreg of the reg as a path
26
+ component. regs that cannot accept a subreg are marked with stars
27
+
28
+ reg classes participating in paths: and their respective path component:
29
+ RegString (=Regexp) backref integer index **
30
+ RegSymbol ditto **
31
+ RegArray integer index
32
+ RegSubseq ditto
33
+ RegHash hash key (of something that matched) ...value?
34
+ RegObject name (as sym) ...return value?
35
+ RegOr integer index
36
+ RegAnd ditto
37
+ RegXor ditto
38
+ RegNot nil only *
39
+ RegRepeat integer index *
40
+ (not really named) RegProc 1 of above, depending on what it returned
41
+
42
+ * doesn't take a subreg of the reg as index
43
+ ** doesn't take a subreg and must be end of path (or before -1)
44
+
45
+ special components:
46
+ -1 means go up one level (like .. in directory paths)
47
+
48
+
49
+
50
+
51
+ single ________fixed
52
+ _____multiple--------
53
+ vector-----variable
54
+
55
+
56
+ dataclasses
57
+ matchers--v String Array
58
+ Integer single single
59
+ String multiple single
60
+ Regexp variable single
61
+ Reg::Subseq var(=>any) var(=>any)
62
+ Reg::Array n/a? single
63
+ Reg::Hash n/a single
64
+ Reg::Repeat var(=>any) var(=>any)
65
+ Lookahead/back multiple multiple (0 really)
66
+ ~single single single
67
+ ~multiple multiple multiple
68
+ ~vector multiple multiple (0 really; lookahead automatic)
69
+
70
+
71
+ others n/a single
72
+
73
+
74
+ =end