-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.scala
32 lines (25 loc) · 958 Bytes
/
parser.scala
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
package arith
import scala.util.parsing.combinator._
import terms.Term, Term._
object Parser extends RegexParsers {
def tmTrue: Parser[True.type] = "true" ^^ (_ => True)
def tmFalse: Parser[False.type] = "false" ^^ (_ => False)
def tmIf: Parser[If] = "if" ~ term ~ "then" ~ term ~ "else" ~ term ^^ {
case "if" ~ t1 ~ "then" ~ t2 ~ "else" ~ t3 => If(t1, t2, t3)
}
def tmZero: Parser[Zero.type] = "0" ^^ (_ => Zero)
def tmSucc: Parser[Succ] = "succ" ~ term ^^ {
case _ ~ t => Succ(t)
}
def tmPred: Parser[Pred] = "pred" ~ term ^^ {
case _ ~ t => Pred(t)
}
def tmIsZero: Parser[IsZero] = "iszero" ~ term ^^ {
case _ ~ t => IsZero(t)
}
def term: Parser[Term] = tmTrue | tmFalse | tmZero | tmIf | tmSucc | tmPred | tmIsZero
def apply(input: String): Either[String, Term] = parseAll(term, input) match {
case Success(t, _) => Right(t)
case failure: NoSuccess => Left(s"Parsing error: ${failure.msg}")
}
}