progs/parser-combinators/comb1.sc
changeset 801 7aab258bf72a
parent 799 85267be9a5ed
child 803 d4fb8c7fc3bf
equal deleted inserted replaced
800:9eea6a801e10 801:7aab258bf72a
    77 // StrParser(_some_string_) more conveniently as 
    77 // StrParser(_some_string_) more conveniently as 
    78 //
    78 //
    79 // p"<_some_string_>" 
    79 // p"<_some_string_>" 
    80 
    80 
    81 implicit def parser_interpolation(sc: StringContext) = new {
    81 implicit def parser_interpolation(sc: StringContext) = new {
    82   def p(args: Any*) = StrParser(sc.s(args:_*))
    82   def p(args: Any*) = TokParser(sc.s(args:_*))
    83 }
    83 }
    84 
    84 
       
    85 p"while"  ==> StrParser[String,....]
       
    86               TokParser[List[Token],....]
       
    87 
       
    88 for x := 3 to 10               
    85 
    89 
    86 // more convenient syntax for parser combinators
    90 // more convenient syntax for parser combinators
    87 implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
    91 implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
    88   def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
    92   def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
    89   def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
    93   def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
   100 
   104 
   101 val NumParserInt2 = NumParser.map(_.toInt)
   105 val NumParserInt2 = NumParser.map(_.toInt)
   102 
   106 
   103 
   107 
   104 // A parser for palindromes (just returns them as string)
   108 // A parser for palindromes (just returns them as string)
   105 lazy val Pal : Parser[String, String] = {
   109 lazy val Pal : Parser[List[Token], String] = {
   106   (p"a" ~ Pal ~ p"a").map{ case ((x, y), z) => s"$x$y$z" } || 
   110   (p"a" ~ Pal ~ p"a").map{ case ((x, y), z) => s"$x$y$z" } || 
   107   (p"b" ~ Pal ~ p"b").map{ case ((x, y), z) => s"$x$y$z" } || 
   111   (p"b" ~ Pal ~ p"b").map{ case ((x, y), z) => s"$x$y$z" } || 
   108   p"a" || p"b" || p""
   112   p"a" || p"b" || p""
   109 }  
   113 }  
   110 
   114