def number: Parser[Token] =
opt("-") ~ number_body ~ opt(letter) ^^ { case a ~ b ~ None => Token(Kind.NUMBER, a.getOrElse("") + b) case a ~ b ~ Some(c) =>
errorToken("Invalid number format: " + quote(a.getOrElse("") + b + c))
}
def number_body: Parser[String] =
(zero | positive) ~ opt(number_fract) ~ opt(number_exp) ^^
{ case a ~ b ~ c => a + b.getOrElse("") + c.getOrElse("") }
def number_fract: Parser[String] = "." ~ digits1 ^^ { case a ~ b => a + b }
def number_exp: Parser[String] =
one(character("eE".contains(_))) ~ maybe(character("-+".contains(_))) ~ digits1 ^^
{ case a ~ b ~ c => a + b + c }
def zero: Parser[String] = one(character(c => c == '0')) def nonzero: Parser[String] = one(character(c => c != '0' && Symbol.is_ascii_digit(c))) def positive: Parser[String] = nonzero ~ digits ^^ { case a ~ b => a + b }
privatedef bytes_string(s: String, builder: Bytes.Builder): Unit = {
builder += ('"': Byte)
builder +=
s.iterator.map { case'"' => "\\\"" case'\\' => "\\\\" case'\b' => "\\b" case'\f' => "\\f" case'\n' => "\\n" case'\r' => "\\r" case'\t' => "\\t" case c => if (c <= '\u001f' || c >= '\u007f' && c <= '\u009f') "\\u%04x".format(c.toInt) else c
}.mkString
builder += ('"': Byte)
}
privatedef bytes_atom(x: T, builder: Bytes.Builder): Boolean =
x match { casenull => builder += "null"; true case _: Int | _ : Long | _: Boolean => builder += x.toString; true case n: Double => val i = n.toLong
builder += (if (i.toDouble == n) i.toString else n.toString) true case s: String => bytes_string(s, builder); true case _ => false
}
def bytes(json: T, hint: Long = 0L): Bytes =
Bytes.Builder.use(hint = hint) { builder => def output(x: T): Unit = { if (!bytes_atom(x, builder)) {
x match { caseObject(obj) =>
builder += ('{': Byte)
Library.separate(None, obj.toList.map(Some(_))).foreach({ case None => builder += (',': Byte) case Some((x, y)) =>
bytes_string(x, builder)
builder += (':': Byte)
output(y)
})
builder += ('}': Byte) case list: List[T] =>
builder += ('[': Byte)
Library.separate(None, list.map(Some(_))).foreach({ case None => builder += (',': Byte) case Some(x) => output(x)
})
builder += (']': Byte) case _ => error("Bad JSON value: " + x.toString)
}
}
}
output(json)
}
privatedef pretty_atom(x: T): Option[XML.Tree] = { val builder = new Bytes.Builder() val ok = bytes_atom(x, builder) if (ok) Some(XML.Text(builder.done().text)) else None
}
object Value { object UUID { def unapply(json: T): Option[isabelle.UUID.T] =
json match { case x: java.lang.String => isabelle.UUID.unapply(x) case _ => None
}
}
object String { def unapply(json: T): Option[java.lang.String] =
json match { case x: java.lang.String => Some(x) case _ => None
}
}
object String0 { def unapply(json: T): Option[java.lang.String] =
json match { casenull => Some("") case x: java.lang.String => Some(x) case _ => None
}
}
object Double { def unapply(json: T): Option[scala.Double] =
json match { case x: scala.Double => Some(x) case x: scala.Long => Some(x.toDouble) case x: scala.Int => Some(x.toDouble) case _ => None
}
}
object Long { def unapply(json: T): Option[scala.Long] =
json match { case x: scala.Double if x.toLong.toDouble == x => Some(x.toLong) case x: scala.Long => Some(x) case x: scala.Int => Some(x.toLong) case _ => None
}
}
object Int { def unapply(json: T): Option[scala.Int] =
json match { case x: scala.Double if x.toInt.toDouble == x => Some(x.toInt) case x: scala.Long if x.toInt.toLong == x => Some(x.toInt) case x: scala.Int => Some(x) case _ => None
}
}
object Boolean { def unapply(json: T): Option[scala.Boolean] =
json match { case x: scala.Boolean => Some(x) case _ => None
}
}
object List { def unapply[A](json: T, unapply: T => Option[A]): Option[List[A]] =
json match { case xs: List[T] => val ys = xs.map(unapply) if (ys.forall(_.isDefined)) Some(ys.map(_.get)) else None case _ => None
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.