progs/fun/fun_tokens.sc
changeset 954 4a7ed272d46e
parent 870 1ea379515c6d
child 960 791f4d9f53e1
equal deleted inserted replaced
953:b569edaf7e43 954:4a7ed272d46e
     6 //     amm fun_tokens.sc fact.fun
     6 //     amm fun_tokens.sc fact.fun
     7 //
     7 //
     8 //     amm fun_tokens.sc defs.fun
     8 //     amm fun_tokens.sc defs.fun
     9 //
     9 //
    10 
    10 
    11 
    11  
    12 
    12 
    13 import scala.language.implicitConversions    
    13 import scala.language.implicitConversions    
    14 import scala.language.reflectiveCalls 
    14 import scala.language.reflectiveCalls 
    15 
    15 
    16 abstract class Rexp 
    16 abstract class Rexp 
    38   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    38   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    39 }
    39 }
    40 implicit def string2rexp(s : String) : Rexp = 
    40 implicit def string2rexp(s : String) : Rexp = 
    41   charlist2rexp(s.toList)
    41   charlist2rexp(s.toList)
    42 
    42 
    43 implicit def RexpOps(r: Rexp) = new {
    43 extension (s: String) {
       
    44   def $ (r: Rexp) = RECD(s, r)
       
    45 }
       
    46 
       
    47 extension (r: Rexp) {
    44   def | (s: Rexp) = ALT(r, s)
    48   def | (s: Rexp) = ALT(r, s)
    45   def % = STAR(r)
    49   def % = STAR(r)
    46   def ~ (s: Rexp) = SEQ(r, s)
    50   def ~ (s: Rexp) = SEQ(r, s)
    47 }
    51 }
    48 
    52 
    49 implicit def stringOps(s: String) = new {
       
    50   def | (r: Rexp) = ALT(s, r)
       
    51   def | (r: String) = ALT(s, r)
       
    52   def % = STAR(s)
       
    53   def ~ (r: Rexp) = SEQ(s, r)
       
    54   def ~ (r: String) = SEQ(s, r)
       
    55   def $ (r: Rexp) = RECD(s, r)
       
    56 }
       
    57 
    53 
    58 def nullable (r: Rexp) : Boolean = r match {
    54 def nullable (r: Rexp) : Boolean = r match {
    59   case ZERO => false
    55   case ZERO => false
    60   case ONE => true
    56   case ONE => true
    61   case CHAR(_) => false
    57   case CHAR(_) => false
    79 
    75 
    80 
    76 
    81 // extracts a string from value
    77 // extracts a string from value
    82 def flatten(v: Val) : String = v match {
    78 def flatten(v: Val) : String = v match {
    83   case Empty => ""
    79   case Empty => ""
    84   case Chr(c) => c.toString
    80   case Chr(c) => c.toString  
    85   case Left(v) => flatten(v)
    81   case Left(v) => flatten(v)
    86   case Right(v) => flatten(v)
    82   case Right(v) => flatten(v)
    87   case Sequ(v1, v2) => flatten(v1) + flatten(v2)
    83   case Sequ(v1, v2) => flatten(v1) + flatten(v2)
    88   case Stars(vs) => vs.map(flatten).mkString
    84   case Stars(vs) => vs.map(flatten).mkString
    89   case Rec(_, v) => flatten(v)
    85   case Rec(_, v) => flatten(v)