I'm trying to build a basic combinator parser in Scala and when trying to compile the most basic parser with it (the parser of the letter 'A') I get three syntax errors from scalac which I don't understand. I'm looking at my code and can't figure out which parts of the syntax are incorrect, even when comparing with examples online. Could someone please explain what parts of my Scala code are wrong here?
Code:
import lexer
import scala.Option
object Main {
def main(args: Array[String]) {
val Lexer = new lexer.Lexer
val tokens = Lexer.lex(args(0))
val parseA = satsify(t => t.key == "A")
println(parseA(tokens))
}
def satsify(predicate: Token => Bool): List(Token) => Option[(Token, List(Token))] = {
tl: List(Token) => match tl {
case tl.isEmpty => None
case predicate(tl(0)) => Some(tl(0), tl.tail)
case _ => None
}
}
}
Errors:
combParser.scala:2: error: '.' expected but ';' found.
import scala.Option
^
combParser.scala:12: error: '=' expected but '(' found.
def satsify(predicate: Token => Bool): List(Token) => Option[(Token, List(Token))] = {
^
combParser.scala:19: error: illegal start of simple expression
}
^
three errors found
There are quite a few syntax errors, mainly
List[Token]
instead of List(Token)
case v if predicate(tl(0)) =>
instead of case predicate(tl(0)) =>
Taking above into account try
def satsify(predicate: Token => Boolean): List[Token] => Option[(Token, List[Token])] = {
(tl: List[Token]) => tl match {
case Nil => None
case head :: tail if predicate(head) => Some(head, tail)
case _ => None
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments