forked from epfl-lara/scallion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.scala
145 lines (115 loc) · 3.89 KB
/
Calculator.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Copyright 2019 EPFL, Lausanne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.calculator
import scallion.input._
import scallion.lexical._
import scallion.syntactic._
sealed trait Token
case class NumberToken(value: Int) extends Token
case class OperatorToken(operator: Char) extends Token
case class ParenthesisToken(isOpen: Boolean) extends Token
case object SpaceToken extends Token
case class UnknownToken(content: String) extends Token
object CalcLexer extends Lexers[Token, Char, Unit] with CharRegExps {
val lexer = Lexer(
// Operators
oneOf("-+/*!")
|> { cs => OperatorToken(cs.head) },
// Parentheses
elem('(') |> ParenthesisToken(true),
elem(')') |> ParenthesisToken(false),
// Spaces
many1(whiteSpace) |> SpaceToken,
// Numbers
{
elem('0') |
nonZero ~ many(digit)
}
|> { cs => NumberToken(cs.mkString.toInt) }
) onError {
(cs, _) => UnknownToken(cs.mkString)
}
def apply(it: Iterator[Char]): Iterator[Token] = {
val source = Source.fromIterator(it, NoPositioner)
val tokens = lexer(source)
tokens.filter((token: Token) => token != SpaceToken)
}
}
sealed abstract class TokenClass(text: String) {
override def toString = text
}
case object NumberClass extends TokenClass("<number>")
case class OperatorClass(op: Char) extends TokenClass(op.toString)
case class ParenthesisClass(isOpen: Boolean) extends TokenClass(if (isOpen) "(" else ")")
case object OtherClass extends TokenClass("?")
sealed abstract class Expr
case class LitExpr(value: Int) extends Expr
case class BinaryExpr(op: Char, left: Expr, right: Expr) extends Expr
case class UnaryExpr(op: Char, inner: Expr) extends Expr
object CalcSyntax extends Syntaxes[Token, TokenClass] with Operators {
import SafeImplicits._
override def getKind(token: Token): TokenClass = token match {
case NumberToken(_) => NumberClass
case OperatorToken(c) => OperatorClass(c)
case ParenthesisToken(o) => ParenthesisClass(o)
case _ => OtherClass
}
val number: Syntax[Expr] = accept(NumberClass)({
case NumberToken(n) => LitExpr(n)
}, {
case LitExpr(n) => Seq(NumberToken(n))
case _ => Seq()
})
def binOp(char: Char): Syntax[Char] = accept(OperatorClass(char))({
case _ => char
}, {
case `char` => Seq(OperatorToken(char))
case _ => Seq()
})
val plus = binOp('+')
val minus = binOp('-')
val times = binOp('*')
val div = binOp('/')
val fac: Syntax[Char] = accept(OperatorClass('!'))({
case _ => '!'
}, {
case '!' => Seq(OperatorToken('!'))
case _ => Seq()
})
def parens(isOpen: Boolean) = elem(ParenthesisClass(isOpen)).unit(ParenthesisToken(isOpen))
val open = parens(true)
val close = parens(false)
lazy val basic: Syntax[Expr] = number | open.skip ~ value ~ close.skip
lazy val postfixed: Syntax[Expr] = postfixes(basic, fac)({
case (e, op) => UnaryExpr(op, e)
}, {
case UnaryExpr(op, e) => (e, op)
})
lazy val value: Syntax[Expr] = recursive {
operators(postfixed)(
times | div is LeftAssociative,
plus | minus is LeftAssociative
)({
case (l, op, r) => BinaryExpr(op, l, r)
}, {
case BinaryExpr(op, l, r) => (l, op, r)
})
}
def unapply(expr: Expr): Iterator[Seq[Token]] = value.unapply(expr)
def apply(it: Iterator[Token]): Option[Expr] = value(it) match {
case Parsed(value, _) => Some(value)
case _ => None
}
}