summaryrefslogtreecommitdiff
path: root/src/library/scala/util/regexp/Base.scala
blob: cfdaaac10f49eef430ef3d6d0c118396378c7695 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// $Id$

package scala.util.regexp ;

/** basic regular expressions */

trait Base {

  type _regexpT <: RegExp;

  abstract class RegExp {
    val isNullable:Boolean;
  }

  /** Alt( R,R,R* ) */
  case class  Alt(rs: _regexpT*)  extends RegExp {

    // check rs \in R,R,R*
    // @todo: flattening
    if({ val it = rs.elements; !it.hasNext || {it.next; !it.hasNext }})
      throw new SyntaxError("need at least 2 branches in Alt");

    final val isNullable = {
      val it = rs.elements;
      while( it.hasNext && it.next.isNullable ) {}
      !it.hasNext
    }
  }
  case class  Sequ(rs: _regexpT*) extends RegExp {
    // @todo: flattening
    // check rs \in R,R*
    if({ val it = rs.elements; !it.hasNext })
      throw new SyntaxError("need at least 1 item in Sequ");

    final val isNullable = {
      val it = rs.elements;
      while( it.hasNext && it.next.isNullable ) {}
      !it.hasNext
    }
  }

  case class  Star(r: _regexpT)   extends RegExp {
    final val isNullable = true;
  }

  case object Eps               extends RegExp {
    final val isNullable = true;
    override def toString() = "Eps";
  }

  /** this class can be used to add meta information to regexps */
  class Meta( r1: _regexpT )       extends RegExp {
    final val isNullable = r1.isNullable;
    def r = r1;
  }

  final def mkSequ(rs: _regexpT *): RegExp =
    if(!rs.elements.hasNext)
      Eps
    else
      Sequ(rs:_*);

}