90 internalise(("a" | "ab") ~ ("b" | "")) |
90 internalise(("a" | "ab") ~ ("b" | "")) |
91 |
91 |
92 def decode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match { |
92 def decode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match { |
93 case (ONE, bs) => (Empty, bs) |
93 case (ONE, bs) => (Empty, bs) |
94 case (CHAR(f), C(c)::bs) => (Chr(c), bs) |
94 case (CHAR(f), C(c)::bs) => (Chr(c), bs) |
95 case (ALTS(r::Nil), bs) => decode_aux(r, bs)//this case seems tailor made for those who want to simplify the regex before der or simp |
95 //case (ALTS(r::Nil), bs) => decode_aux(r, bs)//this case seems only used when we simp a regex before derivatives and it contains things like alt("a") |
96 case (ALTS(rs), bs) => bs match { |
96 case (ALTS(rs), bs) => bs match { |
97 case Z::bs1 => { |
97 case Z::bs1 => { |
98 val (v, bs2) = decode_aux(rs.head, bs1) |
98 val (v, bs2) = decode_aux(rs.head, bs1) |
99 (Left(v), bs2) |
99 (Left(v), bs2) |
100 } |
100 } |
124 def decode(r: Rexp, bs: Bits) = decode_aux(r, bs) match { |
124 def decode(r: Rexp, bs: Bits) = decode_aux(r, bs) match { |
125 case (v, Nil) => v |
125 case (v, Nil) => v |
126 case _ => throw new Exception("Not decodable") |
126 case _ => throw new Exception("Not decodable") |
127 } |
127 } |
128 |
128 |
129 |
129 def code(v: Val): Bits = v match { |
|
130 case Empty => Nil |
|
131 case Left(v) => Z :: code(v) |
|
132 case Right(v) => S :: code(v) |
|
133 case Sequ(v1, v2) => code(v1) ::: code(v2) |
|
134 case Stars(Nil) => Z::Nil |
|
135 case Stars(v::vs) => S::code(v) ::: code(Stars(vs)) |
|
136 } |
|
137 |
|
138 |
|
139 def retrieve(r: ARexp, v: Val): Bits = (r,v) match { |
|
140 case (AONE(bs), Empty) => bs |
|
141 case (ACHAR(bs, c), Chr(d)) => bs |
|
142 case (AALTS(bs, as), Left(v)) => bs ++ retrieve(as.head,v) |
|
143 case (AALTS(bs, as), Right(v)) => bs ++ retrieve(AALTS(Nil,as.tail),v) |
|
144 case (ASEQ(bs, a1, a2), Sequ(v1, v2)) => bs ++ retrieve(a1, v1) ++ retrieve(a2, v2) |
|
145 case (ASTAR(bs, a), Stars(Nil)) => bs ++ List(Z) |
|
146 case (ASTAR(bs, a), Stars(v::vs)) => bs ++ List(S) ++ retrieve(a, v) ++ retrieve(ASTAR(Nil, a), Stars(vs)) |
|
147 } |
130 //erase function: extracts the regx from Aregex |
148 //erase function: extracts the regx from Aregex |
131 def erase(r:ARexp): Rexp = r match{ |
149 def erase(r:ARexp): Rexp = r match{ |
132 case AZERO => ZERO |
150 case AZERO => ZERO |
133 case AONE(_) => ONE |
151 case AONE(_) => ONE |
134 case ACHAR(bs, f) => CHAR(f) |
152 case ACHAR(bs, f) => CHAR(f) |
395 } |
413 } |
396 def super_bsimp(r: ARexp): ARexp = r match { |
414 def super_bsimp(r: ARexp): ARexp = r match { |
397 case ASEQ(bs1, r1, r2) => (super_bsimp(r1), super_bsimp(r2)) match { |
415 case ASEQ(bs1, r1, r2) => (super_bsimp(r1), super_bsimp(r2)) match { |
398 case (AZERO, _) => AZERO |
416 case (AZERO, _) => AZERO |
399 case (_, AZERO) => AZERO |
417 case (_, AZERO) => AZERO |
400 case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s) |
418 case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)//万一是(r1, alts(rs))这种形式呢 |
401 case (AALTS(bs2, rs), r2) => AALTS(bs1 ++ bs2, rs.map(r => r match {case AONE(bs3) => fuse(bs3, r2) case r => ASEQ(Nil, r, r2)}) ) |
419 case (AALTS(bs2, rs), r2) => AALTS(bs1 ++ bs2, rs.map(r => r match {case AONE(bs3) => fuse(bs3, r2) case r => ASEQ(Nil, r, r2)} ) ) |
402 case (r1s, r2s) => ASEQ(bs1, r1s, r2s) |
420 case (r1s, r2s) => ASEQ(bs1, r1s, r2s) |
403 } |
421 } |
404 case AALTS(bs1, rs) => { |
422 case AALTS(bs1, rs) => { |
405 val rs_simp = rs.map(super_bsimp) |
423 val rs_simp = rs.map(super_bsimp) |
406 val flat_res = flats(rs_simp) |
424 val flat_res = flats(rs_simp) |