]> gitweb @ CieloNegro.org - task-reporter.git/blob - src/main/scala/jp/ymir/taskReporter/core/TSV.scala
The TSV parser is now monadic.
[task-reporter.git] / src / main / scala / jp / ymir / taskReporter / core / TSV.scala
1 package jp.ymir.taskReporter.core
2 import scala.collection._
3 import scala.collection.immutable.StringLike
4 import scala.language.higherKinds
5 import scala.language.implicitConversions
6 import scala.language.reflectiveCalls
7 import scalaz._
8 import Scalaz._
9
10 object TSV {
11
12   implicit val ParserFunctor = new Functor[Parser] {
13     def map[A, B](m: Parser[A])(f: A => B) = new Parser[B] {
14       def runParser[F[_], R](kf: Failure[F, R], ks: Success[B, F, R]): F[R] = {
15         def ks_(a: A) = ks(f(a))
16         return m.runParser(kf, ks_)
17       }
18     }
19   }
20
21   implicit val ParserApplicative = new Applicative[Parser] {
22     def point[A](a: => A) = new Parser[A] {
23       def runParser[F[_], R](kf: Failure[F, R], ks: Success[A, F, R]): F[R]
24         = ks(a)
25     }
26     def ap[A, B](m: => Parser[A])(fm: => Parser[A => B]) = new Parser[B] {
27       def runParser[F[_], R](kf: Failure[F, R], ks: Success[B, F, R]): F[R] = {
28         def ks_(f: A => B): F[R] = {
29           def ks__(a: A) = ks(f(a))
30           return m.runParser(kf, ks__)
31         }
32         return fm.runParser(kf, ks_)
33       }
34     }
35   }
36
37   implicit val ParserMonad = new Monad[Parser] {
38     def point[A](a: => A) = a.point[Parser]
39
40     def bind[A, B](m: Parser[A])(f: A => Parser[B]) = new Parser[B] {
41       def runParser[F[_], R](kf: Failure[F, R], ks: Success[B, F, R]): F[R] = {
42         def ks_(a: A) = f(a).runParser(kf, ks)
43         return m.runParser(kf, ks_)
44       }
45     }
46   }
47
48   implicit val ParserMonadError = new MonadError[({type λ[String, α] = Parser[α]})#λ, String] {
49     def point[A](a: => A) = a.point[Parser]
50     def bind[A, B](m: Parser[A])(f: A => Parser[B]) = m.flatMap(f)
51
52     def handleError[A](m: Parser[A])(f: String => Parser[A]) = new Parser[A] {
53       def runParser[F[_], R](kf: Failure[F, R], ks: Success[A, F, R]): F[R] = {
54         def kf_(e: String) = f(e).runParser(kf, ks)
55         return m.runParser(kf_, ks)
56       }
57     }
58
59     def raiseError[A](e: String) = new Parser[A] {
60       def runParser[F[_], R](kf: Failure[F, R], ks: Success[A, F, R]): F[R]
61         = kf(e)
62     }
63   }
64
65   implicit def ToMonadErrorOps[A](m: Parser[A]) = new {
66     def handleError(f: String => Parser[A]): Parser[A]
67       = implicitly[MonadError[({type λ[String, α] = Parser[α]})#λ, String]].handleError(m)(f)
68   }
69
70   implicit def ToMonadErrorIdOps(e: String) = new {
71     def raiseError[A]: Parser[A]
72       = implicitly[MonadError[({type λ[String, α] = Parser[α]})#λ, String]].raiseError(e)
73   }
74
75   type Header = Seq[Symbol]
76
77   case class Field(value: String) {
78     def parseField[A: FromField]
79       = implicitly[FromField[A]].parseField(Field(value))
80   }
81
82   case class Record(value: Map[Symbol, Field]) {
83     def parseRecord[A: FromRecord]
84       = implicitly[FromRecord[A]].parseRecord(Record(value))
85
86     def lookup[A: FromField](name: Symbol): Parser[A]
87       = value.get(name).fold(("no field named " + name.name).raiseError[A])(_.parseField[A])
88   }
89
90   type Failure[   F[_], R] = String => F[R]
91   type Success[A, F[_], R] = A      => F[R]
92
93   trait Parser[+A] {
94     def runParser[F[_], R](kf: Failure[F, R], ks: Success[A, F, R]): F[R]
95   }
96
97   def runParser[A](p: Parser[A]): Either[String, A] = {
98     type F[R] = Either[String, R]
99     def left (e: String): F[A] = Left(e)
100     def right(x: A     ): F[A] = Right(x)
101     return p.runParser(left, right)
102   }
103
104   trait FromRecord[A] {
105     def parseRecord(r: Record): Parser[A]
106   }
107
108   trait FromField[A] {
109     def parseField(f: Field): Parser[A]
110   }
111
112   trait ToRecord[A] {
113     def toRecord(a: A): Record
114   }
115
116   trait ToField[A] {
117     def toField(a: A): Field
118   }
119
120   class DecodeFailedException private(e: RuntimeException) extends RuntimeException(e) {
121     def this(msg: String) = this(new RuntimeException(msg))
122     def this(msg: String, cause: Throwable) = this(new RuntimeException(msg, cause))
123   }
124
125   def decode[A: FromRecord](tsv: StringLike[_]): Seq[A] = {
126     val lines  = tsv.split('\n').filter(!_.isEmpty)
127     val header = lines.head.split('\t').map(Symbol(_))
128     val body   = lines.tail.map(line => Record((header zip line.split('\t').map { Field(_) }).toMap))
129     return body.map { r =>
130       runParser(r.parseRecord[A]) match {
131         case Right(a) => a
132         case Left(e)  => throw new DecodeFailedException(e)
133       }
134     }
135   }
136
137   // Option[A]
138   implicit def OptionFromField[A: FromField] = new FromField[Option[A]] {
139     def parseField(f: Field): Parser[Option[A]] = {
140       if (f.value.isEmpty) {
141         return None.point[Parser]
142       }
143       else {
144         f.parseField[A].map { Some(_) }
145       }
146     }
147   }
148
149   // String
150   implicit val StringFromField = new FromField[String] {
151     def parseField(f: Field): Parser[String] = f.value.point[Parser]
152   }
153
154   // Int
155   implicit val IntFromField = new FromField[Int] {
156     def parseField(f: Field): Parser[Int] = {
157       try {
158         f.value.toInt.point[Parser]
159       }
160       catch {
161         case e: NumberFormatException =>
162           e.getMessage().raiseError[Int]
163       }
164     }
165   }
166 }