fixes NotQuoted

This commit is contained in:
e.e d3si9n 2012-03-11 15:02:50 -04:00
parent feb315b878
commit 9239e2fd46
1 changed files with 14 additions and 16 deletions

View File

@ -48,10 +48,12 @@ trait Parsers
lazy val URIClass = URIChar.+.string !!! "Invalid URI" lazy val URIClass = URIChar.+.string !!! "Invalid URI"
lazy val VerbatimDQuotes = "\"\"\"" lazy val VerbatimDQuotes = "\"\"\""
lazy val DQuoteChar = '\"' lazy val DQuoteChar = '\"'
lazy val BackslashChar = '\\'
lazy val DQuoteClass = charClass(_ == DQuoteChar, "double-quote character") lazy val DQuoteClass = charClass(_ == DQuoteChar, "double-quote character")
lazy val NotDQuoteClass = charClass(_ != DQuoteChar, "non-double-quote character") lazy val NotDQuoteSpaceClass =
lazy val NotDQuoteBackslashClass = charClass({ c: Char => charClass({ c: Char => (c != DQuoteChar) && !c.isWhitespace }, "non-double-quote-space character")
c != DQuoteChar && c != '\\' }, "non-double-quote character") lazy val NotDQuoteBackslashClass =
charClass({ c: Char => (c != DQuoteChar) && (c != BackslashChar) }, "non-double-quote-backslash character")
lazy val URIChar = charClass(alphanum) | chars("_-!.~'()*,;:$&+=?/[]@%#") lazy val URIChar = charClass(alphanum) | chars("_-!.~'()*,;:$&+=?/[]@%#")
def alphanum(c: Char) = ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') def alphanum(c: Char) = ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9')
@ -69,19 +71,15 @@ trait Parsers
lazy val StringVerbatim: Parser[String] = VerbatimDQuotes ~> lazy val StringVerbatim: Parser[String] = VerbatimDQuotes ~>
any.+.string.filter(!_.contains(VerbatimDQuotes), _ => "Invalid verbatim string") <~ any.+.string.filter(!_.contains(VerbatimDQuotes), _ => "Invalid verbatim string") <~
VerbatimDQuotes VerbatimDQuotes
lazy val StringEscapable: Parser[String] = { lazy val StringEscapable: Parser[String] =
val p = DQuoteChar ~> (DQuoteChar ~> (NotDQuoteBackslashClass | EscapeSequence).+.string <~ DQuoteChar |
(EscapeSequence | NotDQuoteBackslashClass map {_.toString}).* <~ DQuoteChar (DQuoteChar ~ DQuoteChar) ^^^ "")
p map { _.mkString } lazy val EscapeSequence: Parser[Char] =
} BackslashChar ~> ('b' ^^^ '\b' | 't' ^^^ '\t' | 'n' ^^^ '\n' | 'f' ^^^ '\f' | 'r' ^^^ '\r' |
lazy val EscapeSequence: Parser[String] = '\"' ^^^ '\"' | '\'' ^^^ '\'' | '\\' ^^^ '\\' | UnicodeEscape)
"\\" ~> ("b" ^^^ "\b" | "t" ^^^ "\t" | "n" ^^^ "\n" | "f" ^^^ "\f" | "r" ^^^ "\r" | lazy val UnicodeEscape: Parser[Char] =
"\"" ^^^ "\"" | "'" ^^^ "\'" | "\\" ^^^ "\\" | UnicodeEscape) ("u" ~> repeat(HexDigit, 4, 4)) map { seq => Integer.parseInt(seq.mkString, 16).toChar }
lazy val UnicodeEscape: Parser[String] = lazy val NotQuoted = (NotDQuoteSpaceClass ~ NotSpace) map { case (c, s) => c.toString + s }
("u" ~> repeat(HexDigit, 4, 4)) map { seq =>
Integer.parseInt(seq.mkString, 16).asInstanceOf[Char].toString
}
lazy val NotQuoted = (NotDQuoteClass ~ NotSpace) map { case (c, s) => c.toString + s }
def repsep[T](rep: Parser[T], sep: Parser[_]): Parser[Seq[T]] = def repsep[T](rep: Parser[T], sep: Parser[_]): Parser[Seq[T]] =
rep1sep(rep, sep) ?? Nil rep1sep(rep, sep) ?? Nil