summaryrefslogtreecommitdiff
path: root/sources/examples/parsers.scala
blob: 3112e0346d7b85f441a4dad122025d7053393104 (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
package examples;
abstract class Parsers[intype] {

  abstract class Parser {

    type Result = Option[intype];

    def apply(in: intype): Result;

    /*** p &&& q applies first p, and if that succeeds, then q
     */
    def &&& (def q: Parser) = new Parser {
      def apply(in: intype): Result = Parser.this.apply(in) match {
        case None => None
        case Some(in1)  => q(in1)
      }
    }

    /*** p ||| q applies first p, and, if that fails, then q.
     */
    def ||| (def q: Parser) = new Parser {
      def apply(in: intype): Result = Parser.this.apply(in) match {
        case None => q(in)
        case s => s
      }
    }
  }

  val empty = new Parser {
    def apply(in: intype): Result = Some(in)
  }

  val fail = new Parser {
    def apply(in: intype): Result = None
  }

  def opt(p: Parser): Parser = p ||| empty;    // p? = (p | <empty>)
  def rep(p: Parser): Parser = opt(rep1(p));   // p* = [p+]
  def rep1(p: Parser): Parser = p &&& rep(p);  // p+ = p p*
}

abstract class ListParsers[intype] extends Parsers[intype] {

  def chr(p: char => boolean): Parser;

  def chr(c: char): Parser = chr(d: char => d == c);

  def letter    : Parser = chr(Character.isLetter);
  def digit     : Parser = chr(Character.isDigit);

  def ident     : Parser = letter &&& rep(letter ||| digit);
  def number    : Parser = digit &&& rep(digit);
  def list      : Parser = chr('(') &&& listElems &&& chr(')');
  def listElems : Parser = expr &&& (chr(',') &&& listElems ||| empty);
  def expr      : Parser = ident ||| number ||| list;
}

class ParseString(s: String) extends Parsers[int] {
  val input = 0;
  def chr(p: char => boolean) = new Parser {
    def apply(in: int): Parser#Result =
      if (in < s.length() && p(s charAt in)) Some(in + 1);
      else None;
  }
}

object Test {

  def main(args: Array[String]): unit =
    if (args.length == 1) {
      val ps = new ListParsers[int] with ParseString(args(0));
      ps.exprs(input) match {
	case Some(n) =>
	  System.out.println("parsed: " + args(0).substring(0, n));
	case None =>
	  System.out.println("nothing parsed");
      }
    } else System.out.println("usage: java examples.Test <expr-string>");
}