@@ -24,138 +24,138 @@ package scala.tools.scalap.rules
2424 * Inspired by the Scala parser combinator.
2525 */
2626trait Rule [- In , + Out , + A , + X ] extends (In => Result [Out , A , X ]) {
27- val factory : Rules
27+ val factory : Rules
2828 import factory ._
2929
30- def as (name : String ) = ruleWithName(name, this )
30+ def as (name : String ) = ruleWithName(name, this )
3131
32- def flatMap [Out2 , B , X2 >: X ](fa2ruleb : A => Out => Result [Out2 , B , X2 ]) = mapResult {
32+ def flatMap [Out2 , B , X2 >: X ](fa2ruleb : A => Out => Result [Out2 , B , X2 ]) = mapResult {
3333 case Success (out, a) => fa2ruleb(a)(out)
3434 case Failure => Failure
3535 case err @ Error (_) => err
3636 }
3737
38- def map [B ](fa2b : A => B ) = flatMap { a => out => Success (out, fa2b(a)) }
38+ def map [B ](fa2b : A => B ) = flatMap { a => out => Success (out, fa2b(a)) }
3939
40- def filter (f : A => Boolean ) = flatMap { a => out => if (f(a)) Success (out, a) else Failure }
40+ def filter (f : A => Boolean ) = flatMap { a => out => if (f(a)) Success (out, a) else Failure }
4141
42- def mapResult [Out2 , B , Y ](f : Result [Out , A , X ] => Result [Out2 , B , Y ]) = rule {
43- in : In => f(apply(in))
42+ def mapResult [Out2 , B , Y ](f : Result [Out , A , X ] => Result [Out2 , B , Y ]) = rule {
43+ in : In => f(apply(in))
4444 }
4545
46- def orElse [In2 <: In , Out2 >: Out , A2 >: A , X2 >: X ](other : => Rule [In2 , Out2 , A2 , X2 ]) : Rule [In2 , Out2 , A2 , X2 ] = new Choice [In2 , Out2 , A2 , X2 ] {
46+ def orElse [In2 <: In , Out2 >: Out , A2 >: A , X2 >: X ](other : => Rule [In2 , Out2 , A2 , X2 ]): Rule [In2 , Out2 , A2 , X2 ] = new Choice [In2 , Out2 , A2 , X2 ] {
4747 val factory = Rule .this .factory
4848 lazy val choices = Rule .this :: other :: Nil
4949 }
5050
5151 def orError [In2 <: In ] = this orElse error[Any ]
5252
53- def | [In2 <: In , Out2 >: Out , A2 >: A , X2 >: X ](other : => Rule [In2 , Out2 , A2 , X2 ]) = orElse(other)
53+ def | [In2 <: In , Out2 >: Out , A2 >: A , X2 >: X ](other : => Rule [In2 , Out2 , A2 , X2 ]) = orElse(other)
5454
55- def ^^ [B ](fa2b : A => B ) = map(fa2b)
55+ def ^^ [B ](fa2b : A => B ) = map(fa2b)
5656
57- def ^^? [B ](pf : PartialFunction [A , B ]) = filter (pf.isDefinedAt(_)) ^^ pf
57+ def ^^? [B ](pf : PartialFunction [A , B ]) = filter (pf.isDefinedAt(_)) ^^ pf
5858
59- def ?? (pf : PartialFunction [A , Any ]) = filter (pf.isDefinedAt(_))
59+ def ?? (pf : PartialFunction [A , Any ]) = filter (pf.isDefinedAt(_))
6060
61- def -^ [B ](b : B ) = map { any => b }
61+ def -^ [B ](b : B ) = map { any => b }
6262
6363 /** Maps an Error */
64- def !^ [Y ](fx2y : X => Y ) = mapResult {
64+ def !^ [Y ](fx2y : X => Y ) = mapResult {
6565 case s @ Success (_, _) => s
6666 case Failure => Failure
6767 case Error (x) => Error (fx2y(x))
6868 }
6969
70- def >> [Out2 , B , X2 >: X ](fa2ruleb : A => Out => Result [Out2 , B , X2 ]) = flatMap(fa2ruleb)
70+ def >> [Out2 , B , X2 >: X ](fa2ruleb : A => Out => Result [Out2 , B , X2 ]) = flatMap(fa2ruleb)
7171
72- def >-> [Out2 , B , X2 >: X ](fa2resultb : A => Result [Out2 , B , X2 ]) = flatMap { a => any => fa2resultb(a) }
72+ def >-> [Out2 , B , X2 >: X ](fa2resultb : A => Result [Out2 , B , X2 ]) = flatMap { a => any => fa2resultb(a) }
7373
74- def >>? [Out2 , B , X2 >: X ](pf : PartialFunction [A , Rule [Out , Out2 , B , X2 ]]) = filter(pf isDefinedAt _) flatMap pf
74+ def >>? [Out2 , B , X2 >: X ](pf : PartialFunction [A , Rule [Out , Out2 , B , X2 ]]) = filter(pf isDefinedAt _) flatMap pf
7575
76- def >>& [B , X2 >: X ](fa2ruleb : A => Out => Result [Any , B , X2 ]) = flatMap { a => out => fa2ruleb(a)(out) mapOut { any => out } }
76+ def >>& [B , X2 >: X ](fa2ruleb : A => Out => Result [Any , B , X2 ]) = flatMap { a => out => fa2ruleb(a)(out) mapOut { any => out } }
7777
78- def ~ [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , B , X2 ]) = for (a <- this ; b <- next) yield new ~ (a, b)
78+ def ~ [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , B , X2 ]) = for (a <- this ; b <- next) yield new ~ (a, b)
7979
80- def ~- [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , B , X2 ]) = for (a <- this ; b <- next) yield a
80+ def ~- [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , B , X2 ]) = for (a <- this ; b <- next) yield a
8181
82- def -~ [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , B , X2 ]) = for (a <- this ; b <- next) yield b
82+ def -~ [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , B , X2 ]) = for (a <- this ; b <- next) yield b
8383
84- def ~++ [Out2 , B >: A , X2 >: X ](next : => Rule [Out , Out2 , Seq [B ], X2 ]) = for (a <- this ; b <- next) yield a :: b.toList
84+ def ~++ [Out2 , B >: A , X2 >: X ](next : => Rule [Out , Out2 , Seq [B ], X2 ]) = for (a <- this ; b <- next) yield a :: b.toList
8585
8686 /** Apply the result of this rule to the function returned by the next rule */
87- def ~> [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , A => B , X2 ]) = for (a <- this ; fa2b <- next) yield fa2b(a)
87+ def ~> [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , A => B , X2 ]) = for (a <- this ; fa2b <- next) yield fa2b(a)
8888
8989 /** Apply the result of this rule to the function returned by the previous rule */
90- def <~: [InPrev , B , X2 >: X ](prev : => Rule [InPrev , In , A => B , X2 ]) = for (fa2b <- prev; a <- this ) yield fa2b(a)
90+ def <~: [InPrev , B , X2 >: X ](prev : => Rule [InPrev , In , A => B , X2 ]) = for (fa2b <- prev; a <- this ) yield fa2b(a)
9191
92- def ~! [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , B , X2 ]) = for (a <- this ; b <- next.orError) yield new ~ (a, b)
92+ def ~! [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , B , X2 ]) = for (a <- this ; b <- next.orError) yield new ~ (a, b)
9393
94- def ~-! [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , B , X2 ]) = for (a <- this ; b <- next.orError) yield a
94+ def ~-! [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , B , X2 ]) = for (a <- this ; b <- next.orError) yield a
9595
96- def -~! [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , B , X2 ]) = for (a <- this ; b <- next.orError) yield b
96+ def -~! [Out2 , B , X2 >: X ](next : => Rule [Out , Out2 , B , X2 ]) = for (a <- this ; b <- next.orError) yield b
9797
98- def - [In2 <: In ](exclude : => Rule [In2 , Any , Any , Any ]) = ! exclude -~ this
98+ def - [In2 <: In ](exclude : => Rule [In2 , Any , Any , Any ]) = ! exclude -~ this
9999
100100 /** ^~^(f) is equivalent to ^^ { case b1 ~ b2 => f(b1, b2) }
101101 */
102- def ^~^ [B1 , B2 , B >: A <% B1 ~ B2 , C ](f : (B1 , B2 ) => C ) = map { a =>
103- (a : B1 ~ B2 ) match { case b1 ~ b2 => f(b1, b2) }
102+ def ^~^ [B1 , B2 , B >: A <% B1 ~ B2 , C ](f : (B1 , B2 ) => C ) = map { a =>
103+ (a : B1 ~ B2 ) match { case b1 ~ b2 => f(b1, b2) }
104104 }
105105
106106 /** ^~~^(f) is equivalent to ^^ { case b1 ~ b2 ~ b3 => f(b1, b2, b3) }
107107 */
108- def ^~~^ [B1 , B2 , B3 , B >: A <% B1 ~ B2 ~ B3 , C ](f : (B1 , B2 , B3 ) => C ) = map { a =>
109- (a : B1 ~ B2 ~ B3 ) match { case b1 ~ b2 ~ b3 => f(b1, b2, b3) }
108+ def ^~~^ [B1 , B2 , B3 , B >: A <% B1 ~ B2 ~ B3 , C ](f : (B1 , B2 , B3 ) => C ) = map { a =>
109+ (a : B1 ~ B2 ~ B3 ) match { case b1 ~ b2 ~ b3 => f(b1, b2, b3) }
110110 }
111111
112112 /** ^~~~^(f) is equivalent to ^^ { case b1 ~ b2 ~ b3 ~ b4 => f(b1, b2, b3, b4) }
113113 */
114- def ^~~~^ [B1 , B2 , B3 , B4 , B >: A <% B1 ~ B2 ~ B3 ~ B4 , C ](f : (B1 , B2 , B3 , B4 ) => C ) = map { a =>
115- (a : B1 ~ B2 ~ B3 ~ B4 ) match { case b1 ~ b2 ~ b3 ~ b4 => f(b1, b2, b3, b4) }
114+ def ^~~~^ [B1 , B2 , B3 , B4 , B >: A <% B1 ~ B2 ~ B3 ~ B4 , C ](f : (B1 , B2 , B3 , B4 ) => C ) = map { a =>
115+ (a : B1 ~ B2 ~ B3 ~ B4 ) match { case b1 ~ b2 ~ b3 ~ b4 => f(b1, b2, b3, b4) }
116116 }
117117
118118 /** ^~~~~^(f) is equivalent to ^^ { case b1 ~ b2 ~ b3 ~ b4 ~ b5 => f(b1, b2, b3, b4, b5) }
119119 */
120- def ^~~~~^ [B1 , B2 , B3 , B4 , B5 , B >: A <% B1 ~ B2 ~ B3 ~ B4 ~ B5 , C ](f : (B1 , B2 , B3 , B4 , B5 ) => C ) = map { a =>
121- (a : B1 ~ B2 ~ B3 ~ B4 ~ B5 ) match { case b1 ~ b2 ~ b3 ~ b4 ~ b5 => f(b1, b2, b3, b4, b5) }
120+ def ^~~~~^ [B1 , B2 , B3 , B4 , B5 , B >: A <% B1 ~ B2 ~ B3 ~ B4 ~ B5 , C ](f : (B1 , B2 , B3 , B4 , B5 ) => C ) = map { a =>
121+ (a : B1 ~ B2 ~ B3 ~ B4 ~ B5 ) match { case b1 ~ b2 ~ b3 ~ b4 ~ b5 => f(b1, b2, b3, b4, b5) }
122122 }
123123
124124 /** ^~~~~~^(f) is equivalent to ^^ { case b1 ~ b2 ~ b3 ~ b4 ~ b5 ~ b6 => f(b1, b2, b3, b4, b5, b6) }
125125 */
126- def ^~~~~~^ [B1 , B2 , B3 , B4 , B5 , B6 , B >: A <% B1 ~ B2 ~ B3 ~ B4 ~ B5 ~ B6 , C ](f : (B1 , B2 , B3 , B4 , B5 , B6 ) => C ) = map { a =>
127- (a : B1 ~ B2 ~ B3 ~ B4 ~ B5 ~ B6 ) match { case b1 ~ b2 ~ b3 ~ b4 ~ b5 ~ b6 => f(b1, b2, b3, b4, b5, b6) }
126+ def ^~~~~~^ [B1 , B2 , B3 , B4 , B5 , B6 , B >: A <% B1 ~ B2 ~ B3 ~ B4 ~ B5 ~ B6 , C ](f : (B1 , B2 , B3 , B4 , B5 , B6 ) => C ) = map { a =>
127+ (a : B1 ~ B2 ~ B3 ~ B4 ~ B5 ~ B6 ) match { case b1 ~ b2 ~ b3 ~ b4 ~ b5 ~ b6 => f(b1, b2, b3, b4, b5, b6) }
128128 }
129129
130130 /** ^~~~~~~^(f) is equivalent to ^^ { case b1 ~ b2 ~ b3 ~ b4 ~ b5 ~ b6 => f(b1, b2, b3, b4, b5, b6) }
131131 */
132- def ^~~~~~~^ [B1 , B2 , B3 , B4 , B5 , B6 , B7 , B >: A <% B1 ~ B2 ~ B3 ~ B4 ~ B5 ~ B6 ~ B7 , C ](f : (B1 , B2 , B3 , B4 , B5 , B6 , B7 ) => C ) = map { a =>
133- (a : B1 ~ B2 ~ B3 ~ B4 ~ B5 ~ B6 ~ B7 ) match { case b1 ~ b2 ~ b3 ~ b4 ~ b5 ~ b6 ~ b7 => f(b1, b2, b3, b4, b5, b6, b7) }
132+ def ^~~~~~~^ [B1 , B2 , B3 , B4 , B5 , B6 , B7 , B >: A <% B1 ~ B2 ~ B3 ~ B4 ~ B5 ~ B6 ~ B7 , C ](f : (B1 , B2 , B3 , B4 , B5 , B6 , B7 ) => C ) = map { a =>
133+ (a : B1 ~ B2 ~ B3 ~ B4 ~ B5 ~ B6 ~ B7 ) match { case b1 ~ b2 ~ b3 ~ b4 ~ b5 ~ b6 ~ b7 => f(b1, b2, b3, b4, b5, b6, b7) }
134134 }
135135
136136 /** >~>(f) is equivalent to >> { case b1 ~ b2 => f(b1, b2) }
137137 */
138- def >~> [Out2 , B1 , B2 , B >: A <% B1 ~ B2 , C , X2 >: X ](f : (B1 , B2 ) => Out => Result [Out2 , C , X2 ]) = flatMap { a =>
139- (a : B1 ~ B2 ) match { case b1 ~ b2 => f(b1, b2) }
138+ def >~> [Out2 , B1 , B2 , B >: A <% B1 ~ B2 , C , X2 >: X ](f : (B1 , B2 ) => Out => Result [Out2 , C , X2 ]) = flatMap { a =>
139+ (a : B1 ~ B2 ) match { case b1 ~ b2 => f(b1, b2) }
140140 }
141141
142142 /** ^-^(f) is equivalent to ^^ { b2 => b1 => f(b1, b2) }
143143 */
144- def ^-^ [B1 , B2 >: A , C ](f : (B1 , B2 ) => C ) = map { b2 : B2 => b1 : B1 => f(b1, b2) }
144+ def ^-^ [B1 , B2 >: A , C ](f : (B1 , B2 ) => C ) = map { b2 : B2 => b1 : B1 => f(b1, b2) }
145145
146146 /** ^~>~^(f) is equivalent to ^^ { case b2 ~ b3 => b1 => f(b1, b2, b3) }
147147 */
148- def ^~>~^ [B1 , B2 , B3 , B >: A <% B2 ~ B3 , C ](f : (B1 , B2 , B3 ) => C ) = map { a =>
149- (a : B2 ~ B3 ) match { case b2 ~ b3 => b1 : B1 => f(b1, b2, b3) }
148+ def ^~>~^ [B1 , B2 , B3 , B >: A <% B2 ~ B3 , C ](f : (B1 , B2 , B3 ) => C ) = map { a =>
149+ (a : B2 ~ B3 ) match { case b2 ~ b3 => b1 : B1 => f(b1, b2, b3) }
150150 }
151151}
152152
153153
154154trait Choice [- In , + Out , + A , + X ] extends Rule [In , Out , A , X ] {
155- def choices : List [Rule [In , Out , A , X ]]
155+ def choices : List [Rule [In , Out , A , X ]]
156156
157- def apply (in : In ) = {
158- def oneOf (list : List [Rule [In , Out , A , X ]]) : Result [Out , A , X ] = list match {
157+ def apply (in : In ) = {
158+ def oneOf (list : List [Rule [In , Out , A , X ]]): Result [Out , A , X ] = list match {
159159 case Nil => Failure
160160 case first :: rest => first(in) match {
161161 case Failure => oneOf(rest)
@@ -165,7 +165,7 @@ trait Choice[-In, +Out, +A, +X] extends Rule[In, Out, A, X] {
165165 oneOf(choices)
166166 }
167167
168- override def orElse [In2 <: In , Out2 >: Out , A2 >: A , X2 >: X ](other : => Rule [In2 , Out2 , A2 , X2 ]) : Rule [In2 , Out2 , A2 , X2 ] = new Choice [In2 , Out2 , A2 , X2 ] {
168+ override def orElse [In2 <: In , Out2 >: Out , A2 >: A , X2 >: X ](other : => Rule [In2 , Out2 , A2 , X2 ]): Rule [In2 , Out2 , A2 , X2 ] = new Choice [In2 , Out2 , A2 , X2 ] {
169169 val factory = Choice .this .factory
170170 lazy val choices = Choice .this .choices ::: other :: Nil
171171 }
0 commit comments