我正在为我的程序语言开发词法分析。我想生成具有开引号但没有闭引号的失败字符串。例如:“你好
class SimpleLexer extends StdLexical {
import scala.util.parsing.input.CharArrayReader.EofCh
def regex(r: Regex): Parser[String] = new Parser[String] {
def apply(in: Input) = {
val source = in.source
val offset = in.offset
(r findPrefixMatchOf (source.subSequence(offset, source.length))) match {
case Some(matched) =>
Success(source.subSequence(offset, offset + matched.end).toString,
in.drop(matched.end))
case None =>
Failure("string matching regex `" + r + "' expected but `" + in.first + "' found", in.drop(0))
}
}
}
override def token: Parser[Token] = {
// Adapted from StdLexical
(
'\"' ~ rep( chrExcept('\"', '\n','\t','\b','\f','\r', EofCh) ) ~ '\"' ^^ { case '\"' ~ chars ~ '\"' => StringLit(chars mkString "") }
|'\"' ~> failure("Unclosed string: "+"??") // I want produce fail string
|EofCh ^^^ EOF
|delim
)
}
override def whitespace: Parser[Any] = rep(
whitespaceChar
| '/' ~ '*' ~ comment
| '/' ~ '*' ~> failure("unclosed comment"))
override protected def comment: Parser[Any] = (
'*' ~ '/' ^^ { case _ => ' ' }
| chrExcept(EofCh) ~ comment)
}
示例:输入:“ safs i 输出:ErrorToken(未闭合字符串:” safs i)
你能帮我解决这个问题吗?谢谢。