| author | wenzelm |
| Fri, 27 Oct 2017 16:21:29 +0200 | |
| changeset 66926 | 4cf560a6dd84 |
| parent 66925 | 8b117dc73278 |
| child 66928 | 33f9133bed7c |
| permissions | -rw-r--r-- |
| 63992 | 1 |
/* Title: Pure/General/json.scala |
| 63644 | 2 |
Author: Makarius |
3 |
||
4 |
Support for JSON parsing. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
|
66925
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
10 |
import scala.util.parsing.combinator.Parsers |
| 66922 | 11 |
import scala.util.parsing.combinator.lexical.Scanners |
12 |
import scala.util.parsing.input.CharArrayReader.EofCh |
|
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
13 |
|
| 66922 | 14 |
|
| 63644 | 15 |
object JSON |
16 |
{
|
|
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
17 |
type T = Any |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
18 |
type S = String |
| 63644 | 19 |
|
20 |
||
| 66922 | 21 |
/* lexer */ |
22 |
||
23 |
object Kind extends Enumeration |
|
24 |
{
|
|
25 |
val KEYWORD, STRING, NUMBER, ERROR = Value |
|
26 |
} |
|
27 |
||
28 |
sealed case class Token(kind: Kind.Value, text: String) |
|
29 |
{
|
|
30 |
def is_keyword: Boolean = kind == Kind.KEYWORD |
|
|
66925
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
31 |
def is_keyword(name: String): Boolean = kind == Kind.KEYWORD && text == name |
| 66922 | 32 |
def is_string: Boolean = kind == Kind.STRING |
33 |
def is_number: Boolean = kind == Kind.NUMBER |
|
34 |
def is_error: Boolean = kind == Kind.ERROR |
|
35 |
} |
|
36 |
||
37 |
object Lexer extends Scanners with Scan.Parsers |
|
38 |
{
|
|
39 |
override type Elem = Char |
|
40 |
type Token = JSON.Token |
|
41 |
||
42 |
def errorToken(msg: String): Token = Token(Kind.ERROR, msg) |
|
43 |
||
44 |
override val whiteSpace = """\s+""".r |
|
45 |
def whitespace: Parser[Any] = many(character(Symbol.is_ascii_blank(_))) |
|
46 |
||
47 |
val letter: Parser[String] = one(character(Symbol.is_ascii_letter(_))) |
|
48 |
val letters1: Parser[String] = many1(character(Symbol.is_ascii_letter(_))) |
|
49 |
||
50 |
def digits: Parser[String] = many(character(Symbol.is_ascii_digit(_))) |
|
51 |
def digits1: Parser[String] = many1(character(Symbol.is_ascii_digit(_))) |
|
52 |
||
53 |
||
54 |
/* keyword */ |
|
55 |
||
56 |
def keyword: Parser[Token] = |
|
| 66924 | 57 |
(letters1 | one(character("{}[],:".contains(_)))) ^^ (s => Token(Kind.KEYWORD, s))
|
| 66922 | 58 |
|
59 |
||
60 |
/* string */ |
|
61 |
||
62 |
def string: Parser[Token] = |
|
63 |
'\"' ~> rep(string_body) <~ '\"' ^^ (cs => Token(Kind.STRING, cs.mkString)) |
|
64 |
||
65 |
def string_body: Parser[Char] = |
|
66 |
elem("", c => c != '\"' && c != '\\' && c != EofCh) | '\\' ~> string_escape
|
|
67 |
||
68 |
def string_escape: Parser[Char] = |
|
69 |
elem("", "\"\\/".contains(_)) |
|
|
70 |
elem("", "bfnrt".contains(_)) ^^
|
|
71 |
{ case 'b' => '\b' case 'f' => '\f' case 'n' => '\n' case 'r' => '\r' case 't' => '\t' } |
|
|
72 |
'u' ~> repeated(character("0123456789abcdefABCDEF".contains(_)), 4, 4) ^^
|
|
73 |
(s => Integer.parseInt(s, 16).toChar) |
|
74 |
||
75 |
def string_failure: Parser[Token] = '\"' ~> failure("Unterminated string")
|
|
76 |
||
77 |
||
78 |
/* number */ |
|
79 |
||
80 |
def number: Parser[Token] = |
|
81 |
opt("-" <~ whitespace) ~ number_body ~ opt(letter) ^^ {
|
|
82 |
case a ~ b ~ None => Token(Kind.NUMBER, a.getOrElse("") + b)
|
|
83 |
case a ~ b ~ Some(c) => |
|
84 |
errorToken("Invalid number format: " + quote(a.getOrElse("") + b + c))
|
|
85 |
} |
|
86 |
||
87 |
def number_body: Parser[String] = |
|
88 |
(zero | positive) ~ opt(number_fract) ~ opt(number_exp) ^^ |
|
89 |
{ case a ~ b ~ c => a + b.getOrElse("") + c.getOrElse("") }
|
|
90 |
||
91 |
def number_fract: Parser[String] = "." ~ digits1 ^^ { case a ~ b => a + b }
|
|
92 |
||
93 |
def number_exp: Parser[String] = |
|
94 |
one(character("eE".contains(_))) ~ maybe(character("-+".contains(_))) ~ digits1 ^^
|
|
95 |
{ case a ~ b ~ c => a + b + c }
|
|
96 |
||
97 |
def zero = one(character(c => c == '0')) |
|
98 |
def nonzero = one(character(c => c != '0' && Symbol.is_ascii_digit(c))) |
|
99 |
def positive: Parser[String] = nonzero ~ digits ^^ { case a ~ b => a + b }
|
|
100 |
||
101 |
||
102 |
/* token */ |
|
103 |
||
104 |
def token: Parser[Token] = |
|
105 |
keyword | (string | (string_failure | (number | failure("Illegal character"))))
|
|
106 |
} |
|
107 |
||
108 |
||
|
66925
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
109 |
/* parser */ |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
110 |
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
111 |
trait Parser extends Parsers |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
112 |
{
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
113 |
type Elem = Token |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
114 |
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
115 |
def $$$(name: String): Parser[Token] = elem(name, _.is_keyword(name)) |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
116 |
def string: Parser[String] = elem("string", _.is_string) ^^ (_.text)
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
117 |
def number: Parser[Double] = elem("number", _.is_number) ^^ (tok => tok.text.toDouble)
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
118 |
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
119 |
def json_object: Parser[Map[String, T]] = |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
120 |
$$$("{") ~>
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
121 |
repsep(string ~ ($$$(":") ~> json_value) ^^ { case a ~ b => (a, b) }, $$$(",")) <~
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
122 |
$$$("}") ^^ (_.toMap)
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
123 |
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
124 |
def json_array: Parser[List[T]] = |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
125 |
$$$("[") ~> repsep(json_value, $$$(",")) <~ $$$("]")
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
126 |
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
127 |
def json_value: Parser[T] = |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
128 |
json_object | (json_array | (number | (string | |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
129 |
($$$("true") ^^^ true | ($$$("false") ^^^ false | ($$$("null") ^^^ null))))))
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
130 |
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
131 |
def parse(input: CharSequence, strict: Boolean): T = |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
132 |
{
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
133 |
val scanner = new Lexer.Scanner(Scan.char_reader(input)) |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
134 |
phrase(if (strict) json_object | json_array else json_value)(scanner) match {
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
135 |
case Success(json, _) => json |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
136 |
case NoSuccess(_, next) => error("Malformed JSON input at " + next.pos)
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
137 |
} |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
138 |
} |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
139 |
} |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
140 |
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
141 |
object Parser extends Parser |
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
142 |
|
|
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
143 |
|
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
144 |
/* standard format */ |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
145 |
|
|
66925
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
146 |
def parse(s: S, strict: Boolean = true): T = Parser.parse(s, strict) |
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
147 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
148 |
object Format |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
149 |
{
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
150 |
def unapply(s: S): Option[T] = |
|
66925
8b117dc73278
separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents:
66924
diff
changeset
|
151 |
try { Some(parse(s, strict = false)) }
|
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
152 |
catch { case ERROR(_) => None }
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
153 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
154 |
def apply(json: T): S = |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
155 |
{
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
156 |
val result = new StringBuilder |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
157 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
158 |
def string(s: String) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
159 |
{
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
160 |
result += '"' |
| 64761 | 161 |
result ++= |
162 |
s.iterator.map {
|
|
163 |
case '"' => "\\\"" |
|
164 |
case '\\' => "\\\\" |
|
165 |
case '\b' => "\\b" |
|
166 |
case '\f' => "\\f" |
|
167 |
case '\n' => "\\n" |
|
168 |
case '\r' => "\\r" |
|
169 |
case '\t' => "\\t" |
|
170 |
case c => |
|
171 |
if (c <= '\u001f' || c >= '\u007f' && c <= '\u009f') "\\u%04x".format(c.toInt) |
|
172 |
else c |
|
173 |
}.mkString |
|
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
174 |
result += '"' |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
175 |
} |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
176 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
177 |
def array(list: List[T]) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
178 |
{
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
179 |
result += '[' |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
180 |
Library.separate(None, list.map(Some(_))).foreach({
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
181 |
case None => result += ',' |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
182 |
case Some(x) => json_format(x) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
183 |
}) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
184 |
result += ']' |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
185 |
} |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
186 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
187 |
def object_(obj: Map[String, T]) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
188 |
{
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
189 |
result += '{'
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
190 |
Library.separate(None, obj.toList.map(Some(_))).foreach({
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
191 |
case None => result += ',' |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
192 |
case Some((x, y)) => |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
193 |
string(x) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
194 |
result += ':' |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
195 |
json_format(y) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
196 |
}) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
197 |
result += '}' |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
198 |
} |
| 63644 | 199 |
|
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
200 |
def json_format(x: T) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
201 |
{
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
202 |
x match {
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
203 |
case null => result ++= "null" |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
204 |
case _: Int | _: Long | _: Boolean => result ++= x.toString |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
205 |
case n: Double => |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
206 |
val i = n.toLong |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
207 |
result ++= (if (i.toDouble == n) i.toString else n.toString) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
208 |
case s: String => string(s) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
209 |
case obj: Map[_, _] if obj.keySet.forall(_.isInstanceOf[String]) => |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
210 |
object_(obj.asInstanceOf[Map[String, T]]) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
211 |
case list: List[T] => array(list) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
212 |
case _ => error("Bad JSON value: " + x.toString)
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
213 |
} |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
214 |
} |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
215 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
216 |
json_format(json) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
217 |
result.toString |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
218 |
} |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
219 |
} |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
220 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
221 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
222 |
/* numbers */ |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
223 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
224 |
object Number |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
225 |
{
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
226 |
object Double {
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
227 |
def unapply(json: T): Option[scala.Double] = |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
228 |
json match {
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
229 |
case x: scala.Double => Some(x) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
230 |
case x: scala.Long => Some(x.toDouble) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
231 |
case x: scala.Int => Some(x.toDouble) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
232 |
case _ => None |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
233 |
} |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
234 |
} |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
235 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
236 |
object Long {
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
237 |
def unapply(json: T): Option[scala.Long] = |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
238 |
json match {
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
239 |
case x: scala.Double if x.toLong.toDouble == x => Some(x.toLong) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
240 |
case x: scala.Long => Some(x) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
241 |
case x: scala.Int => Some(x.toLong) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
242 |
case _ => None |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
243 |
} |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
244 |
} |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
245 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
246 |
object Int {
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
247 |
def unapply(json: T): Option[scala.Int] = |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
248 |
json match {
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
249 |
case x: scala.Double if x.toInt.toDouble == x => Some(x.toInt) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
250 |
case x: scala.Long if x.toInt.toLong == x => Some(x.toInt) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
251 |
case x: scala.Int => Some(x) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
252 |
case _ => None |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
253 |
} |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
254 |
} |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
255 |
} |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
256 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
257 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
258 |
/* object values */ |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
259 |
|
| 66926 | 260 |
val empty: Map[String, T] = Map.empty |
261 |
||
| 64878 | 262 |
def optional(entry: (String, Option[T])): Map[String, T] = |
263 |
entry match {
|
|
264 |
case (name, Some(x)) => Map(name -> x) |
|
| 66926 | 265 |
case (_, None) => empty |
| 64878 | 266 |
} |
267 |
||
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
268 |
def value(obj: T, name: String): Option[T] = |
| 63644 | 269 |
obj match {
|
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
270 |
case m: Map[_, _] if m.keySet.forall(_.isInstanceOf[String]) => |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
271 |
m.asInstanceOf[Map[String, T]].get(name) |
| 63644 | 272 |
case _ => None |
273 |
} |
|
274 |
||
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
275 |
def value[A](obj: T, name: String, unapply: T => Option[A]): Option[A] = |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
276 |
for {
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
277 |
json <- value(obj, name) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
278 |
x <- unapply(json) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
279 |
} yield x |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
280 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
281 |
def array(obj: T, name: String): Option[List[T]] = |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
282 |
value(obj, name) match {
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
283 |
case Some(a: List[T]) => Some(a) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
284 |
case _ => None |
| 63644 | 285 |
} |
286 |
||
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
287 |
def array[A](obj: T, name: String, unapply: T => Option[A]): Option[List[A]] = |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
288 |
for {
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
289 |
a0 <- array(obj, name) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
290 |
a = a0.map(unapply(_)) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
291 |
if a.forall(_.isDefined) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
292 |
} yield a.map(_.get) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
293 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
294 |
def string(obj: T, name: String): Option[String] = |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
295 |
value(obj, name) match {
|
| 63644 | 296 |
case Some(x: String) => Some(x) |
297 |
case _ => None |
|
298 |
} |
|
299 |
||
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
300 |
def double(obj: T, name: String): Option[Double] = |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
301 |
value(obj, name, Number.Double.unapply) |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
302 |
|
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
303 |
def long(obj: T, name: String): Option[Long] = |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
304 |
value(obj, name, Number.Long.unapply) |
| 63644 | 305 |
|
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
306 |
def int(obj: T, name: String): Option[Int] = |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
307 |
value(obj, name, Number.Int.unapply) |
| 63644 | 308 |
|
|
64545
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
309 |
def bool(obj: T, name: String): Option[Boolean] = |
|
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents:
63992
diff
changeset
|
310 |
value(obj, name) match {
|
| 63644 | 311 |
case Some(x: Boolean) => Some(x) |
312 |
case _ => None |
|
313 |
} |
|
314 |
} |