2012-09-19 46 views
2

我在新阶。我在字符串文字中遇到问题。这里是我的代码:修改/改写ErrorToken字符串中阶

import scala.util.matching.Regex 
import scala.util.parsing.combinator.lexical.StdLexical 
import scala.util.parsing.combinator.token.StdTokens 
import scala.util.parsing.input.CharArrayReader.EofCh 

trait SimpleTokens extends StdTokens { 
    // Adapted from StdTokens 
    case class FloatLit(chars: String) extends Token { 
    override def toString = "FloatLit "+chars 
    } 
    case class IntLit(chars: String) extends Token { 
    override def toString = "IntLit "+chars 
    } 
    case class BooleanLit(chars: String) extends Token { 
    override def toString = "BooleanLit " + chars 
    } 
    case class StrLit(chars: String) extends Token { 

    override def toString = "\"" + chars.slice(1,chars.length-1) + "\"" 
    } 

} 

class SimpleLexer extends StdLexical with SimpleTokens { 
    import scala.util.parsing.input.CharArrayReader.EofCh 

    reserved ++= List("mod", "div", "array","if","then") 

    delimiters ++= List(";", "(", ")", "+", "-", "*", "/",".") 

    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 
    (
    regex("true|false".r)     ^^ { BooleanLit(_)} 
    |regex("[a-z][a-z]*".r)     ^^ { processIdent(_) }     
    |regex("""([0-9]*)(((\.)?[0-9]+(e|E)(\+|-)?[0-9]+)|(\.[0-9]+))""".r)    ^^ { FloatLit(_) } 
    |regex("\\d+".r)      ^^ { IntLit(_) } 
    |regex("""'([^'\"]|'')*'""".r)   ^^ {StrLit(_) } 
    |EofCh ^^^ EOF 
    |delim 
    ) 
    } 

    override def whitespace: Parser[Any] = rep(
    whitespaceChar 
     | '/' ~ '*' ~ comment 
     | '/' ~ '*' ~> failure("unclosed comment")) 

    override protected def comment: Parser[Any] = (
    '*' ~ '/' ^^ { case _ => ' ' } 
    | chrExcept(EofCh) ~ comment) 


} 

输入的是: '这是串

输出应该是:ErrorToken(未关闭的字符串:' 这是字符串)

但是当我跑,我收到此:

ErrorToken ' 

identifier this 

identifier is 

identifier string 

我有什么做的就是正确的输出?请帮帮我!!!谢谢您考虑我的问题

回答

2

您可以在正则表达式修改:

|regex("""(')[^']*(\t)""".r) ^^ ("Illegal tab in string:"+_) ^^ ErrorToken 
|regex("""(')[^']*""".r) ^^ ("Unclosed string:"+_) ^^ ErrorToken 

PPL-2012?

+0

哦,非常感谢你! – user1680791