summaryrefslogtreecommitdiff
path: root/src/library/scala/util/parsing/Parsers.scala
blob: 9124c624abb451969445ba391bc1026a46f36dfe (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2007, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.util.parsing

/** Documentation for this class is currently missing.
 *  However, the Scala By Examples document contains a
 *  chapter on combinator parsing that comes close.
 *
 *  @author  Burak Emir
 *  @version 1.0
 *
 *  @deprecated use <a target="contentFrame" href="combinator/Parsers.html">
 *              <code>scala.util.parsing.combinator.Parsers</code></a>
 *              instead.
 */
@deprecated
abstract class Parsers {

  type inputType

  abstract class Parser[A] {

    type Result = Option[(A, inputType)]

    def apply(in: inputType): Result

    def filter(pred: A => Boolean) = new Parser[A] {
      def apply(in: inputType): Result = Parser.this.apply(in) match {
        case None => None
        case Some((x, in1)) => if (pred(x)) Some((x, in1)) else None
      }
    }

    def map[B](f: A => B) = new Parser[B] {
      def apply(in: inputType): Result = Parser.this.apply(in) match {
        case None => None
        case Some((x, in1)) => Some((f(x), in1))
      }
    }

    def flatMap[B](f: A => Parser[B]) = new Parser[B] {
      def apply(in: inputType): Result = Parser.this.apply(in) match {
        case None => None
        case Some((x, in1)) => f(x).apply(in1)
      }
    }

    def ||| (p: => Parser[A]) = new Parser[A] {
      def apply(in: inputType): Result = Parser.this.apply(in) match {
        case None => p(in)
        case s => s
      }
    }

    def &&& [B](p: => Parser[B]): Parser[B] =
      for (_ <- this; val x <- p) yield x
  }

  def not[A](p: Parser[A]) = new Parser[Unit] {
    def apply(in: inputType): Result = p.apply(in) match {
      case None => Some(((), in))
      case Some(_) => None
    }
  }

  def succeed[A](x: A) = new Parser[A] {
    def apply(in: inputType): Result = Some((x, in))
  }

  def rep[A](p: Parser[A]): Parser[List[A]] =
    rep1(p) ||| succeed(List())

  def rep1[A](p: Parser[A]): Parser[List[A]] =
    for (x <- p; val xs <- rep(p)) yield x :: xs

  def repWith[A, B](p: Parser[A], sep: Parser[B]): Parser[List[A]] =
    for (x <- p; val xs <- rep(sep &&& p)) yield x :: xs

  def opt[A](p: Parser[A]): Parser[List[A]] =
    (for (x <- p) yield List(x)) ||| succeed(List())
}