40 |
40 |
41 // abbreviations |
41 // abbreviations |
42 def CHAR(c: Char) = PRED(_ == c, c.toString) |
42 def CHAR(c: Char) = PRED(_ == c, c.toString) |
43 def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2)) |
43 def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2)) |
44 def PLUS(r: Rexp) = SEQ(r, STAR(r)) |
44 def PLUS(r: Rexp) = SEQ(r, STAR(r)) |
|
45 val ANYCHAR = PRED(_ => true, ".") |
45 |
46 |
46 // annotated regular expressions |
47 // annotated regular expressions |
47 abstract class ARexp |
48 abstract class ARexp |
48 case object AZERO extends ARexp |
49 case object AZERO extends ARexp |
49 case class AONE(bs: Bits) extends ARexp |
50 case class AONE(bs: Bits) extends ARexp |
527 |
528 |
528 def btokenise2_simp(r: Rexp, s: String) = |
529 def btokenise2_simp(r: Rexp, s: String) = |
529 env(blexing2_simp(r, s)).map(esc2) |
530 env(blexing2_simp(r, s)).map(esc2) |
530 |
531 |
531 |
532 |
|
533 // Parser for regexes |
|
534 |
|
535 case class Parser(s: String) { |
|
536 var i = 0 |
|
537 |
|
538 def peek() = s(i) |
|
539 def eat(c: Char) = |
|
540 if (c == s(i)) i = i + 1 else throw new Exception("Expected " + c + " got " + s(i)) |
|
541 def next() = { i = i + 1; s(i - 1) } |
|
542 def more() = s.length - i > 0 |
|
543 |
|
544 def Regex() : Rexp = { |
|
545 val t = Term(); |
|
546 if (more() && peek() == '|') { |
|
547 eat ('|') ; |
|
548 ALT(t, Regex()) |
|
549 } |
|
550 else t |
|
551 } |
|
552 |
|
553 def Term() : Rexp = { |
|
554 var f : Rexp = |
|
555 if (more() && peek() != ')' && peek() != '|') Factor() else ONE; |
|
556 while (more() && peek() != ')' && peek() != '|') { |
|
557 f = SEQ(f, Factor()) ; |
|
558 } |
|
559 f |
|
560 } |
|
561 |
|
562 def Factor() : Rexp = { |
|
563 var b = Base(); |
|
564 while (more() && peek() == '*') { |
|
565 eat('*') ; |
|
566 b = STAR(b) ; |
|
567 } |
|
568 while (more() && peek() == '?') { |
|
569 eat('?') ; |
|
570 b = ALT(b, ONE) ; |
|
571 } |
|
572 while (more() && peek() == '+') { |
|
573 eat('+') ; |
|
574 b = SEQ(b, STAR(b)) ; |
|
575 } |
|
576 b |
|
577 } |
|
578 |
|
579 def Base() : Rexp = { |
|
580 peek() match { |
|
581 case '(' => { eat('(') ; val r = Regex(); eat(')') ; r } // if groups should be groups RECD("",r) } |
|
582 case '.' => { eat('.'); ANYCHAR } |
|
583 case _ => CHAR(next()) |
|
584 } |
|
585 } |
|
586 } |
|
587 |
|
588 // two simple examples for the regex parser |
|
589 |
|
590 println("two simple examples for the regex parser") |
|
591 |
|
592 println(string(Parser("a|(bc)*").Regex())) |
|
593 println(string(Parser("(a|b)*(babab(a|b)*bab|bba(a|b)*bab)(a|b)*").Regex())) |
|
594 |
|
595 |
|
596 |
|
597 System.exit(0) |
532 |
598 |
533 // Testing |
599 // Testing |
534 //============ |
600 //============ |
535 |
601 |
536 def time[T](code: => T) = { |
602 def time[T](code: => T) = { |