author | wenzelm |
Sun, 21 Jul 2024 13:44:05 +0200 | |
changeset 80604 | 67067490392d |
parent 80548 | d1662f1296db |
permissions | -rw-r--r-- |
63805 | 1 |
/* Title: Pure/General/value.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Plain values, represented as string. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
75393 | 10 |
object Value { |
11 |
object Boolean { |
|
63805 | 12 |
def apply(x: scala.Boolean): java.lang.String = x.toString |
13 |
def unapply(s: java.lang.String): Option[scala.Boolean] = |
|
14 |
s match { |
|
15 |
case "true" => Some(true) |
|
16 |
case "false" => Some(false) |
|
17 |
case _ => None |
|
18 |
} |
|
19 |
def parse(s: java.lang.String): scala.Boolean = |
|
20 |
unapply(s) getOrElse error("Bad boolean: " + quote(s)) |
|
21 |
} |
|
22 |
||
75393 | 23 |
object Nat { |
80505
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
77111
diff
changeset
|
24 |
def unapply(bs: Bytes): Option[scala.Int] = |
80548 | 25 |
if (bs.byte_iterator.forall(Symbol.is_ascii_digit)) unapply(bs.text) |
80505
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
77111
diff
changeset
|
26 |
else None |
69452 | 27 |
def unapply(s: java.lang.String): Option[scala.Int] = |
28 |
s match { |
|
29 |
case Int(n) if n >= 0 => Some(n) |
|
30 |
case _ => None |
|
31 |
} |
|
32 |
def parse(s: java.lang.String): scala.Int = |
|
33 |
unapply(s) getOrElse error("Bad natural number: " + quote(s)) |
|
34 |
} |
|
35 |
||
75393 | 36 |
object Int { |
63805 | 37 |
def apply(x: scala.Int): java.lang.String = Library.signed_string_of_int(x) |
38 |
def unapply(s: java.lang.String): Option[scala.Int] = |
|
39 |
try { Some(Integer.parseInt(s)) } |
|
40 |
catch { case _: NumberFormatException => None } |
|
41 |
def parse(s: java.lang.String): scala.Int = |
|
42 |
unapply(s) getOrElse error("Bad integer: " + quote(s)) |
|
43 |
} |
|
44 |
||
75393 | 45 |
object Long { |
63805 | 46 |
def apply(x: scala.Long): java.lang.String = Library.signed_string_of_long(x) |
47 |
def unapply(s: java.lang.String): Option[scala.Long] = |
|
48 |
try { Some(java.lang.Long.parseLong(s)) } |
|
49 |
catch { case _: NumberFormatException => None } |
|
50 |
def parse(s: java.lang.String): scala.Long = |
|
77111 | 51 |
unapply(s) getOrElse error("Bad long integer: " + quote(s)) |
63805 | 52 |
} |
53 |
||
75393 | 54 |
object Double { |
63805 | 55 |
def apply(x: scala.Double): java.lang.String = x.toString |
56 |
def unapply(s: java.lang.String): Option[scala.Double] = |
|
57 |
try { Some(java.lang.Double.parseDouble(s)) } |
|
58 |
catch { case _: NumberFormatException => None } |
|
59 |
def parse(s: java.lang.String): scala.Double = |
|
60 |
unapply(s) getOrElse error("Bad real: " + quote(s)) |
|
61 |
} |
|
68944 | 62 |
|
75393 | 63 |
object Seconds { |
64 |
def apply(t: Time): java.lang.String = { |
|
68946 | 65 |
val s = t.seconds |
66 |
if (s.toInt.toDouble == s) s.toInt.toString else t.toString |
|
67 |
} |
|
71601 | 68 |
def unapply(s: java.lang.String): Option[Time] = Double.unapply(s).map(Time.seconds) |
68944 | 69 |
def parse(s: java.lang.String): Time = |
70 |
unapply(s) getOrElse error("Bad real (for seconds): " + quote(s)) |
|
71 |
} |
|
63805 | 72 |
} |