author | wenzelm |
Sat, 11 Nov 2023 21:06:54 +0100 | |
changeset 78948 | 20f2f34a81bd |
parent 78941 | bc7b7357f4bc |
child 78978 | 80fda74a045d |
permissions | -rw-r--r-- |
78316 | 1 |
/* Title: Pure/General/toml.scala |
78317 | 2 |
Author: Fabian Huch, TU Muenchen |
78316 | 3 |
|
4 |
Support for TOML: https://toml.io/en/v1.0.0 |
|
78317 | 5 |
*/ |
6 |
||
78316 | 7 |
package isabelle |
8 |
||
9 |
||
10 |
import TOML.Parsers.Keys |
|
11 |
import TOML.Parse_Context.Seen |
|
12 |
||
13 |
import java.lang.Long.parseLong |
|
14 |
import java.lang.{String => Str} |
|
15 |
import java.time.{LocalDate, LocalDateTime, LocalTime, OffsetDateTime} |
|
16 |
||
17 |
import scala.{Boolean => Bool} |
|
18 |
import scala.collection.immutable.ListMap |
|
19 |
import scala.reflect.{ClassTag, classTag} |
|
20 |
import scala.util.Try |
|
21 |
import scala.util.matching.Regex |
|
22 |
import scala.util.parsing.combinator |
|
23 |
import scala.util.parsing.combinator.lexical.Scanners |
|
24 |
import scala.util.parsing.input.CharArrayReader.EofCh |
|
25 |
||
26 |
||
27 |
object TOML { |
|
28 |
/* typed representation and access */ |
|
29 |
||
30 |
type Key = Str |
|
31 |
||
32 |
sealed trait T |
|
33 |
case class String(rep: Str) extends T |
|
34 |
case class Integer(rep: Long) extends T |
|
35 |
case class Float(rep: Double) extends T |
|
36 |
case class Boolean(rep: Bool) extends T |
|
37 |
case class Offset_Date_Time(rep: OffsetDateTime) extends T |
|
38 |
case class Local_Date_Time(rep: LocalDateTime) extends T |
|
39 |
case class Local_Date(rep: LocalDate) extends T |
|
40 |
case class Local_Time(rep: LocalTime) extends T |
|
41 |
||
78941 | 42 |
object Scalar { |
43 |
def unapply(t: T): Option[Str] = |
|
44 |
t match { |
|
45 |
case s: String => Some(s.rep) |
|
46 |
case i: Integer => Some(i.rep.toString) |
|
47 |
case f: Float => Some(f.rep.toString) |
|
48 |
case b: Boolean => Some(b.rep.toString) |
|
49 |
case o: Offset_Date_Time => Some(o.rep.toString) |
|
50 |
case l: Local_Date_Time => Some(l.rep.toString) |
|
51 |
case l: Local_Date => Some(l.rep.toString) |
|
52 |
case l: Local_Time => Some(l.rep.toString) |
|
53 |
case _ => None |
|
54 |
} |
|
55 |
} |
|
56 |
||
78316 | 57 |
class Array private(private val rep: List[T]) extends T { |
58 |
override def hashCode(): Int = rep.hashCode() |
|
59 |
override def equals(that: Any): Bool = that match { |
|
60 |
case other: Array => rep == other.rep |
|
61 |
case _ => false |
|
62 |
} |
|
63 |
||
64 |
class Values[A](pf: PartialFunction[T, A]) { def values: List[A] = rep.collect(pf).reverse } |
|
65 |
lazy val string = new Values({ case s: String => s }) |
|
66 |
lazy val integer = new Values({ case i: Integer => i }) |
|
67 |
lazy val float = new Values({ case f: Float => f }) |
|
68 |
lazy val boolean = new Values({ case b: Boolean => b }) |
|
69 |
lazy val offset_date_time = new Values({ case o: Offset_Date_Time => o }) |
|
70 |
lazy val local_date_time = new Values({ case l: Local_Date_Time => l }) |
|
71 |
lazy val local_date = new Values({ case l: Local_Date => l }) |
|
72 |
lazy val local_time = new Values({ case l: Local_Time => l }) |
|
73 |
lazy val array = new Values({ case a: Array => a }) |
|
74 |
lazy val table = new Values({ case t: Table => t }) |
|
75 |
lazy val any = new Values({ case t => t }) |
|
76 |
||
77 |
def +(elem: T): Array = new Array(elem :: rep) |
|
78 |
def ++(other: Array): Array = new Array(other.rep ::: rep) |
|
79 |
def length: Int = rep.length |
|
80 |
def is_empty: Bool = rep.isEmpty |
|
81 |
} |
|
82 |
||
83 |
object Array { |
|
84 |
def apply(elems: Iterable[T]): Array = new Array(elems.toList.reverse) |
|
85 |
def apply(elems: T*): Array = Array(elems) |
|
78919 | 86 |
val empty: Array = apply() |
78316 | 87 |
} |
88 |
||
89 |
class Table private(private val rep: Map[Key, T]) extends T { |
|
90 |
override def hashCode(): Int = rep.hashCode() |
|
91 |
override def equals(that: Any): Bool = |
|
92 |
that match { |
|
93 |
case other: Table => rep == other.rep |
|
94 |
case _ => false |
|
95 |
} |
|
96 |
||
97 |
class Value[A: ClassTag](pf: PartialFunction[T, A]) { |
|
98 |
def values: List[(Key, A)] = |
|
99 |
rep.toList.collect { case (k, v) if pf.isDefinedAt(v) => k -> pf(v) } |
|
100 |
def get(k: Key): Option[A] = rep.get(k).flatMap(v => PartialFunction.condOpt(v)(pf)) |
|
101 |
def apply(k: Key): A = |
|
102 |
rep.get(k) match { |
|
103 |
case Some(v) => PartialFunction.condOpt(v)(pf) match { |
|
104 |
case Some(value) => value |
|
105 |
case None => |
|
106 |
error("Expected" + classTag[A].runtimeClass.getName + |
|
78327 | 107 |
", got " + v.getClass.getSimpleName + " for key " + Format.key(k)) |
78316 | 108 |
} |
78327 | 109 |
case None => error("Key " + Format.key(k) + " does not exist") |
78316 | 110 |
} |
111 |
} |
|
112 |
||
113 |
lazy val string = new Value({ case s: String => s }) |
|
114 |
lazy val integer = new Value({ case i: Integer => i }) |
|
115 |
lazy val float = new Value({ case f: Float => f }) |
|
116 |
lazy val boolean = new Value({ case b: Boolean => b }) |
|
117 |
lazy val offset_date_time = new Value({ case o: Offset_Date_Time => o }) |
|
118 |
lazy val local_date_time = new Value({ case l: Local_Date_Time => l }) |
|
119 |
lazy val local_date = new Value({ case l: Local_Date => l }) |
|
120 |
lazy val local_time = new Value({ case l: Local_Time => l }) |
|
121 |
lazy val array = new Value({ case a: Array => a }) |
|
122 |
lazy val table = new Value({ case t: Table => t }) |
|
123 |
lazy val any = new Value({ case t => t }) |
|
124 |
||
125 |
def +(elem: (Key, T)): Table = { |
|
126 |
val (k, v) = elem |
|
127 |
val v1 = rep.get(k) match { |
|
128 |
case None => v |
|
129 |
case Some(v0) => |
|
130 |
(v0, v) match { |
|
131 |
case (t0: Table, t: Table) => t0 ++ t |
|
132 |
case (a0: Array, a: Array) => a0 ++ a |
|
78327 | 133 |
case _ => error("Key already present: " + Format.key(k)) |
78316 | 134 |
} |
135 |
} |
|
136 |
new Table(rep + (k -> v1)) |
|
137 |
} |
|
138 |
def -(k: Key): Table = new Table(rep - k) |
|
139 |
def ++(other: Table): Table = other.rep.foldLeft(this)(_ + _) |
|
140 |
def domain: Set[Key] = rep.keySet |
|
141 |
def is_empty: Bool = rep.isEmpty |
|
142 |
} |
|
143 |
||
144 |
object Table { |
|
145 |
def apply(elems: Iterable[(Key, T)]): Table = elems.foldLeft(new Table(ListMap.empty))(_ + _) |
|
146 |
def apply(elems: (Key, T)*): Table = Table(elems) |
|
78919 | 147 |
val empty: Table = apply() |
78316 | 148 |
} |
149 |
||
150 |
||
151 |
/* lexer */ |
|
152 |
||
78609 | 153 |
enum Kind { case KEYWORD, VALUE, STRING, MULTILINE_STRING, LINE_SEP, ERROR } |
78316 | 154 |
|
78609 | 155 |
sealed case class Token(kind: Kind, text: Str) { |
78316 | 156 |
def is_keyword(name: Str): Bool = kind == Kind.KEYWORD && text == name |
157 |
def is_value: Bool = kind == Kind.VALUE |
|
158 |
def is_string: Bool = kind == Kind.STRING |
|
159 |
def is_multiline_string: Bool = kind == Kind.MULTILINE_STRING |
|
160 |
def is_line_sep: Bool = kind == Kind.LINE_SEP |
|
161 |
} |
|
162 |
||
163 |
object Lexer extends Scanners with Scan.Parsers { |
|
164 |
override type Elem = Char |
|
165 |
type Token = TOML.Token |
|
166 |
||
167 |
def errorToken(msg: Str): Token = Token(Kind.ERROR, msg) |
|
168 |
||
169 |
val white_space: Str = " \t" |
|
170 |
override val whiteSpace: Regex = ("[" + white_space + "]+").r |
|
171 |
override def whitespace: Parser[Any] = rep(comment | many1(character(white_space.contains(_)))) |
|
172 |
||
173 |
def line_sep: Parser[Str] = rep1("\n" | s"\r\n" | EofCh) ^^ (cs => cs.mkString) |
|
174 |
def line_sep_token: Parser[Token] = line_sep ^^ (s => Token(Kind.LINE_SEP, s)) |
|
175 |
||
176 |
def is_control(e: Elem): Bool = |
|
177 |
e <= '\u0008' || ('\u000A' <= e && e <= '\u001F') || e == '\u007F' |
|
178 |
||
179 |
override def comment: Parser[Str] = '#' ~>! many(character(c => !is_control(c))) |
|
180 |
||
181 |
def keyword: Parser[Token] = one(character("{}[],=.".contains)) ^^ (s => Token(Kind.KEYWORD, s)) |
|
182 |
||
183 |
def is_value(c: Elem): Bool = |
|
184 |
Symbol.is_ascii_letter(c) || Symbol.is_ascii_digit(c) || "_-:+".contains(c) |
|
185 |
def value: Parser[Token] = |
|
186 |
many1(character(is_value)) ~ |
|
187 |
opt(' ' ~ many1(character(is_value)) ^^ { case ws ~ s => ws.toString + s }) ~ |
|
188 |
opt('.' ~ many1(character(is_value)) ^^ { case dot ~ s => dot.toString + s}) ^^ |
|
189 |
{ case s ~ ss ~ sd => Token(Kind.VALUE, s + ss.getOrElse("") + sd.getOrElse("")) } |
|
190 |
||
191 |
def string: Parser[Token] = |
|
192 |
multiline_basic_string | basic_string | multiline_literal_string | literal_string |
|
193 |
||
194 |
private def trim(s: Str): Str = |
|
195 |
if (s.startsWith("\n")) s.stripPrefix("\n") else s.stripPrefix("\r\n") |
|
196 |
||
197 |
def basic_string: Parser[Token] = |
|
198 |
'"' ~> rep(basic_string_elem) <~ '"' ^^ (cs => Token(Kind.STRING, cs.mkString)) |
|
199 |
||
200 |
def multiline_basic_string: Parser[Token] = |
|
201 |
"\"\"\"" ~> |
|
202 |
rep(multiline_basic_string_elem | |
|
203 |
("\"\"" | "\"") ~ multiline_basic_string_elem ^^ { case s ~ t => s + t }) ~ |
|
204 |
repeated(character(_ == '"'), 3, 5) ^^ { case cs ~ q => |
|
205 |
Token(Kind.MULTILINE_STRING, trim(cs.mkString + q.drop(3))) } |
|
206 |
||
207 |
private def multiline_basic_string_elem: Parser[Str] = |
|
208 |
('\\' ~ line_sep ~ rep(many1(character(white_space.contains)) | line_sep)) ^^ (_ => "") | |
|
209 |
basic_string_elem ^^ (_.toString) | line_sep |
|
210 |
||
211 |
def literal_string: Parser[Token] = |
|
212 |
'\'' ~> rep(literal_string_elem) <~ '\'' ^^ (cs => Token(Kind.STRING, cs.mkString)) |
|
213 |
||
214 |
def multiline_literal_string: Parser[Token] = |
|
215 |
"'''" ~> |
|
216 |
rep(multiline_literal_string_elem | |
|
217 |
("''" | "'") ~ multiline_literal_string_elem ^^ { case s ~ t => s + t }) ~ |
|
218 |
repeated(character(_ == '\''), 3, 5) ^^ { case cs ~ q => |
|
219 |
Token(Kind.MULTILINE_STRING, trim(cs.mkString + q.drop(3))) } |
|
220 |
||
221 |
private def multiline_literal_string_elem: Parser[Str] = |
|
222 |
line_sep | literal_string_elem ^^ (_.toString) |
|
223 |
||
224 |
private def basic_string_elem: Parser[Elem] = |
|
225 |
elem("", c => !is_control(c) && !"\"\\".contains(c)) | '\\' ~> string_escape |
|
226 |
||
227 |
private def string_escape: Parser[Elem] = |
|
228 |
elem("", "\"\\".contains(_)) | |
|
229 |
elem("", "btnfr".contains(_)) ^^ |
|
230 |
{ case 'b' => '\b' case 't' => '\t' case 'n' => '\n' case 'f' => '\f' case 'r' => '\r' } | |
|
78920 | 231 |
('u' ~> repeated(character(Symbol.is_ascii_hex), 4, 4) | |
232 |
'U' ~> repeated(character(Symbol.is_ascii_hex), 8, 8)) ^^ |
|
78316 | 233 |
(s => java.lang.Integer.parseInt(s, 16).toChar) |
234 |
||
235 |
private def literal_string_elem: Parser[Elem] = elem("", c => !is_control(c) && c != '\'') |
|
236 |
||
237 |
def string_failure: Parser[Token] = ("\"" | "'") ~> failure("Unterminated string") |
|
238 |
||
239 |
def token: Parser[Token] = |
|
240 |
line_sep_token | keyword | value | string | string_failure | failure("Unrecognized token") |
|
241 |
} |
|
242 |
||
243 |
||
244 |
/* parser */ |
|
245 |
||
246 |
trait Parsers extends combinator.Parsers { |
|
247 |
type Elem = Token |
|
78317 | 248 |
|
249 |
||
78316 | 250 |
/* parse structure */ |
78317 | 251 |
|
78316 | 252 |
type Keys = List[Key] |
253 |
||
254 |
sealed trait V |
|
255 |
case class Primitive(t: T) extends V |
|
256 |
case class Array(rep: List[V]) extends V |
|
257 |
case class Inline_Table(elems: List[(Keys, V)]) extends V |
|
258 |
||
259 |
sealed trait Def |
|
260 |
case class Table(key: Keys, elems: List[(Keys, V)]) extends Def |
|
261 |
case class Array_Of_Tables(key: Keys, elems: List[(Keys, V)]) extends Def |
|
78317 | 262 |
|
78316 | 263 |
case class File(elems: List[(Keys, V)], defs: List[Def]) |
78317 | 264 |
|
78316 | 265 |
|
266 |
/* top-level syntax structure */ |
|
78317 | 267 |
|
78316 | 268 |
def key: Parser[Keys] = rep1sep(keys, $$$(".")) ^^ (_.flatten) |
78317 | 269 |
|
78316 | 270 |
def string: Parser[String] = |
271 |
elem("string", e => e.is_string || e.is_multiline_string) ^^ (s => String(s.text)) |
|
272 |
def integer: Parser[Integer] = |
|
273 |
(decimal_int | binary_int | octal_int | hexadecimal_int) ^^ Integer.apply |
|
274 |
def float: Parser[Float] = (symbol_float | number_float) ^^ Float.apply |
|
275 |
def boolean: Parser[Boolean] = token("boolean", _.is_value, s => Boolean(Value.Boolean.parse(s))) |
|
276 |
||
277 |
def offset_date_time: Parser[Offset_Date_Time] = |
|
278 |
token("offset date-time", _.is_value, |
|
279 |
s => Offset_Date_Time(OffsetDateTime.parse(s.replace(" ", "T")))) |
|
280 |
def local_date_time: Parser[Local_Date_Time] = |
|
281 |
token("local date-time", _.is_value, |
|
282 |
s => Local_Date_Time(LocalDateTime.parse(s.replace(" ", "T")))) |
|
283 |
def local_date: Parser[Local_Date] = |
|
284 |
token("local date", _.is_value, s => Local_Date(LocalDate.parse(s))) |
|
285 |
def local_time: Parser[Local_Time] = |
|
286 |
token("local time", _.is_value, s => Local_Time(LocalTime.parse(s))) |
|
287 |
||
288 |
def array: Parser[Array] = |
|
289 |
$$$("[") ~>! repsep(opt(line_sep) ~> toml_value, opt(line_sep) ~ $$$(",")) <~! |
|
290 |
opt(line_sep) ~! opt($$$(",")) ~! opt(line_sep) <~! $$$("]") ^^ Array.apply |
|
291 |
||
292 |
def inline_table: Parser[Inline_Table] = |
|
293 |
$$$("{") ~>! repsep(pair, $$$(",")) <~! $$$("}") ^^ Inline_Table.apply |
|
78317 | 294 |
|
78316 | 295 |
def pair: Parser[(Keys, V)] = (key <~! $$$("=")) ~! toml_value ^^ { case ks ~ v => (ks, v) } |
296 |
||
297 |
def table: Parser[Table] = $$$("[") ~> (key <~! $$$("]") ~! line_sep) ~! content ^^ |
|
298 |
{ case key ~ content => Table(key, content) } |
|
299 |
||
300 |
def array_of_tables: Parser[Array_Of_Tables] = |
|
301 |
$$$("[") ~ $$$("[") ~>! (key <~! $$$("]") ~! $$$("]") ~! line_sep) ~! content ^^ |
|
302 |
{ case key ~ content => Array_Of_Tables(key, content) } |
|
303 |
||
304 |
def toml: Parser[File] = |
|
305 |
(opt(line_sep) ~>! content ~! rep(table | array_of_tables)) ^^ |
|
306 |
{ case content ~ defs => File(content, defs) } |
|
307 |
||
308 |
||
309 |
/* auxiliary */ |
|
310 |
||
311 |
private def $$$(name: Str): Parser[Token] = elem(name, _.is_keyword(name)) |
|
312 |
private def maybe[A, B](p: Parser[A], f: A => B): Parser[B] = |
|
313 |
p ^^ (a => Try(f(a))) ^? { case util.Success(v) => v } |
|
314 |
private def token[A](name: Str, p: Token => Bool, parser: Str => A): Parser[A] = |
|
315 |
maybe(elem(name, p), s => parser(s.text)) |
|
316 |
private def prefixed[A]( |
|
317 |
prefix: Str, name: Str, p: Str => Bool, parser: Str => A |
|
318 |
): Parser[A] = |
|
319 |
token(name, e => e.is_value && e.text.startsWith(prefix) && p(e.text.stripPrefix(prefix)), |
|
320 |
s => parser(s.stripPrefix(prefix))) |
|
321 |
||
322 |
private def is_key(e: Elem): Bool = e.is_value && !e.text.exists("+: ".contains(_)) |
|
323 |
private def keys: Parser[Keys] = |
|
324 |
token("string key", _.is_string, List(_)) | token("key", is_key, _.split('.').toList) |
|
325 |
||
326 |
private def sep_surrounded(s: Str): Bool = |
|
327 |
!s.startsWith("_") && !s.endsWith("_") && s.split('_').forall(_.nonEmpty) |
|
328 |
private def no_leading_zero(s: Str): Bool = { |
|
329 |
val t = s.replaceAll("_", "").takeWhile(_.isDigit) |
|
330 |
t == "0" || !t.startsWith("0") |
|
331 |
} |
|
332 |
||
333 |
private def is_int(s: Str): Bool = |
|
334 |
no_leading_zero(s.replaceAll("[-+]", "")) && sep_surrounded(s.replaceAll("[-+]", "")) |
|
335 |
private def decimal_int: Parser[Long] = |
|
336 |
token("integer", e => e.is_value && is_int(e.text), _.replace("_", "").toLong) |
|
337 |
private def binary_int: Parser[Long] = |
|
338 |
prefixed("0b", "integer", sep_surrounded, s => parseLong(s.replace("_", ""), 2)) |
|
339 |
private def octal_int: Parser[Long] = |
|
340 |
prefixed("0o", "integer", sep_surrounded, s => parseLong(s.replace("_", ""), 8)) |
|
341 |
private def hexadecimal_int: Parser[Long] = |
|
342 |
prefixed("0x", "integer", sep_surrounded, s => parseLong(s.replace("_", ""), 16)) |
|
343 |
||
344 |
private def is_float(s: Str): Bool = |
|
345 |
s.exists(".eE".contains) && s.count("eE".contains) <= 1 && |
|
346 |
no_leading_zero(s.replaceAll("[-+]", "")) && |
|
347 |
sep_surrounded(s.replaceAll("[-+]", "").replaceAll("[.eE][+\\-]?", "_")) |
|
348 |
private def number_float: Parser[Double] = |
|
349 |
token("float", e => e.is_value && is_float(e.text), _.replace("_", "").toDouble) |
|
350 |
private def symbol_float: Parser[Double] = |
|
351 |
token("float", _.is_value, { |
|
352 |
case "inf" | "+inf" => Double.PositiveInfinity |
|
353 |
case "-inf" => Double.NegativeInfinity |
|
354 |
case "nan" | "+nan" | "-nan" => Double.NaN |
|
355 |
}) |
|
356 |
||
357 |
private def toml_value: Parser[V] = (string | float | integer | boolean | offset_date_time | |
|
358 |
local_date_time | local_date | local_time) ^^ Primitive.apply | array | inline_table |
|
359 |
||
360 |
private def line_sep: Parser[Any] = rep1(elem("line sep", _.is_line_sep)) |
|
361 |
||
362 |
private def content: Parser[List[(Keys, V)]] = |
|
363 |
rep((key <~! $$$("=")) ~! toml_value <~! line_sep ^^ { case ks ~ v => ks -> v }) |
|
364 |
||
365 |
||
366 |
/* parse */ |
|
367 |
||
368 |
def parse(input: Str): File = { |
|
369 |
val scanner = new Lexer.Scanner(Scan.char_reader(input + EofCh)) |
|
370 |
val result = phrase(toml)(scanner) |
|
371 |
result match { |
|
372 |
case Success(toml, _) => toml |
|
373 |
case Failure(msg, next) => error("Malformed TOML input at " + next.pos + ": " + msg) |
|
374 |
case Error(msg, next) => error("Error parsing toml at " + next.pos + ": " + msg) |
|
375 |
} |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
object Parsers extends Parsers |
|
380 |
||
381 |
||
382 |
/* checking and translating parse structure into toml */ |
|
383 |
||
384 |
private def prefixes(ks: Keys): Set[Keys] = |
|
385 |
if (ks.isEmpty) Set.empty else Range.inclusive(1, ks.length).toSet.map(ks.take) |
|
386 |
||
387 |
class Parse_Context private(var seen_tables: Map[Keys, Seen]) { |
|
388 |
private def splits(ks: Keys): List[(Keys, Keys)] = |
|
389 |
Range.inclusive(0, ks.length).toList.map(n => (ks.dropRight(n), ks.takeRight(n))) |
|
390 |
||
391 |
private def recent_array(ks: Keys): (Keys, Seen, Keys) = |
|
392 |
splits(ks).collectFirst { |
|
393 |
case (ks1, ks2) if seen_tables.contains(ks1) => (ks1, seen_tables(ks1), ks2) |
|
394 |
}.get |
|
395 |
||
396 |
private def check_add(start: Int, ks: Keys, elem: Parsers.Def | Parsers.V): Unit = { |
|
397 |
val (to, seen, rest, split) = |
|
398 |
elem match { |
|
399 |
case _: Parsers.Array_Of_Tables => |
|
400 |
val (_, seen, rest) = recent_array(ks.dropRight(1)) |
|
401 |
(ks, seen, rest ::: ks.takeRight(1), 0) |
|
402 |
case _ => |
|
403 |
val (to, seen, rest) = recent_array(ks) |
|
404 |
(to, seen, rest, start - to.length) |
|
405 |
} |
|
406 |
val (rest0, rest1) = rest.splitAt(split) |
|
407 |
val implicitly_seen = prefixes(rest1.dropRight(1)).map(rest0 ::: _) |
|
408 |
||
78327 | 409 |
seen.inlines.find(rest.startsWith(_)).foreach(ks => |
410 |
error("Attempting to update an inline value at " + Format.keys(ks))) |
|
78316 | 411 |
|
412 |
val (inlines, tables) = |
|
413 |
elem match { |
|
414 |
case _: Parsers.Primitive => |
|
415 |
(seen.inlines, seen.tables ++ implicitly_seen) |
|
416 |
case _: Parsers.Array => |
|
78327 | 417 |
if (seen_tables.contains(ks)) |
418 |
error("Attempting to append with an inline array at " + Format.keys(ks)) |
|
78316 | 419 |
(seen.inlines + rest, seen.tables ++ implicitly_seen) |
420 |
case _: Parsers.Inline_Table => |
|
78327 | 421 |
seen.tables.find(_.startsWith(rest)).foreach(ks => |
422 |
error("Attempting to add with an inline table at " + Format.keys(ks))) |
|
78316 | 423 |
(seen.inlines + rest, seen.tables ++ implicitly_seen) |
424 |
case _: Parsers.Table => |
|
78327 | 425 |
if (seen.tables.contains(rest)) |
78332 | 426 |
error("Attempting to define a table twice at " + Format.keys(ks)) |
78316 | 427 |
(seen.inlines, seen.tables + rest) |
428 |
case _: Parsers.Array_Of_Tables => (Set.empty, Set.empty) |
|
429 |
} |
|
430 |
||
431 |
seen_tables += to -> Seen(inlines, tables) |
|
432 |
} |
|
433 |
def check_add_def(ks: Keys, defn: Parsers.Def): Unit = check_add(0, ks, defn) |
|
434 |
def check_add_value(ks0: Keys, ks1: Keys, v: Parsers.V): Unit = |
|
435 |
check_add(ks0.length - 1, ks0 ::: ks1, v) |
|
436 |
} |
|
437 |
||
438 |
object Parse_Context { |
|
439 |
case class Seen(inlines: Set[Keys], tables: Set[Keys]) |
|
440 |
||
78927 | 441 |
def apply(): Parse_Context = new Parse_Context(Map(Nil -> Seen(Set.empty, Set.empty))) |
78316 | 442 |
} |
443 |
||
78927 | 444 |
def parse(s: Str, context: Parse_Context = Parse_Context()): Table = { |
78316 | 445 |
val file = Parsers.parse(s) |
446 |
||
447 |
def convert(ks0: Keys, ks1: Keys, v: Parsers.V): T = { |
|
448 |
def to_table(ks: Keys, t: T): Table = |
|
449 |
ks.dropRight(1).foldRight(Table(ks.last -> t))((k, v) => Table(k -> v)) |
|
450 |
def to_toml(v: Parsers.V): (T, Set[Keys]) = v match { |
|
451 |
case Parsers.Primitive(t) => (t, Set.empty) |
|
452 |
case Parsers.Array(rep) => (Array(rep.map(to_toml(_)._1)), Set.empty) |
|
453 |
case Parsers.Inline_Table(elems) => |
|
78327 | 454 |
elems.find(e => elems.count(_._1 == e._1) > 1).foreach((ks, _) => |
455 |
error("Duplicate key " + Format.keys(ks) + " in inline table at " + |
|
456 |
Format.keys(ks0 ::: ks1))) |
|
78316 | 457 |
|
458 |
val (tables, pfxs, key_spaces) = |
|
459 |
elems.map { (ks, v) => |
|
460 |
val (t, keys) = to_toml(v) |
|
461 |
(to_table(ks, t), prefixes(ks), keys.map(ks ::: _) + ks) |
|
462 |
}.unzip3 |
|
463 |
||
464 |
pfxs.foreach(pfx => if (key_spaces.count(pfx.intersect(_).nonEmpty) > 1) |
|
78327 | 465 |
error("Inline table not self-contained at " + Format.keys(ks0 ::: ks1))) |
78316 | 466 |
|
467 |
(tables.foldLeft(Table())(_ ++ _), pfxs.toSet.flatten ++ key_spaces.toSet.flatten) |
|
468 |
} |
|
469 |
context.check_add_value(ks0, ks1, v) |
|
470 |
to_toml(v)._1 |
|
471 |
} |
|
472 |
||
78327 | 473 |
def update(map: Table, ks0: Keys, value: T): Table = { |
474 |
def update_rec(hd: Keys, map: Table, ks: Keys): Table = { |
|
475 |
val updated = |
|
476 |
if (ks.length == 1) { |
|
477 |
map.any.get(ks.head) match { |
|
478 |
case Some(a: Array) => |
|
479 |
value match { |
|
480 |
case a2: Array => a ++ a2 |
|
481 |
case _ => |
|
482 |
error("Table conflicts with previous array of tables at " + Format.keys(ks0)) |
|
483 |
} |
|
484 |
case Some(t: Table) => value match { |
|
485 |
case t2: Table => |
|
486 |
if (t.domain.intersect(t2.domain).nonEmpty) |
|
487 |
error("Attempting to redefine existing value in super-table at " + |
|
488 |
Format.keys(ks0)) |
|
489 |
else t ++ t2 |
|
490 |
case _ => error("Attempting to redefine a table at " + Format.keys(ks0)) |
|
78316 | 491 |
} |
78327 | 492 |
case Some(_) => error("Attempting to redefine a value at " + Format.keys(ks0)) |
493 |
case None => value |
|
78316 | 494 |
} |
495 |
} |
|
78327 | 496 |
else { |
497 |
val hd1 = hd :+ ks.head |
|
498 |
map.any.get(ks.head) match { |
|
499 |
case Some(t: Table) => update_rec(hd1, t, ks.tail) |
|
500 |
case Some(a: Array) => |
|
501 |
Array(a.table.values.dropRight(1) :+ update_rec(hd1, a.table.values.last, ks.tail)) |
|
502 |
case Some(_) => error("Attempting to redefine a value at " + Format.keys(hd1)) |
|
503 |
case None => update_rec(hd1, Table(), ks.tail) |
|
504 |
} |
|
78316 | 505 |
} |
78327 | 506 |
(map - ks.head) + (ks.head -> updated) |
507 |
} |
|
508 |
update_rec(Nil, map, ks0) |
|
78316 | 509 |
} |
510 |
||
511 |
def fold(elems: List[(Keys, T)]): Table = |
|
512 |
elems.foldLeft(Table()) { case (t0, (ks1, t1)) => update(t0, ks1, t1) } |
|
513 |
||
514 |
val t = fold(file.elems.map((ks, v) => (ks, convert(Nil, ks, v)))) |
|
515 |
file.defs.foldLeft(t) { |
|
516 |
case (t0, defn@Parsers.Table(ks0, elems)) => |
|
517 |
context.check_add_def(ks0, defn) |
|
518 |
update(t0, ks0, fold(elems.map((ks, v) => (ks, convert(ks0, ks, v))))) |
|
519 |
case (t0, defn@Parsers.Array_Of_Tables(ks0, elems)) => |
|
520 |
context.check_add_def(ks0, defn) |
|
521 |
update(t0, ks0, Array(fold(elems.map((ks, v) => (ks, convert(ks0, ks, v)))))) |
|
522 |
} |
|
523 |
} |
|
524 |
||
78939 | 525 |
def parse_files(files: Iterable[Path], context: Parse_Context = Parse_Context()): Table = { |
78948 | 526 |
// FIXME proper reset of table context for each file |
78939 | 527 |
val s = files.iterator.map(File.read).mkString("\n\n") |
528 |
parse(s, context = context) |
|
529 |
} |
|
530 |
||
78316 | 531 |
|
532 |
/* Format TOML */ |
|
533 |
||
534 |
object Format { |
|
78326
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
535 |
private def basic_string(s: Str): Str = |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
536 |
"\"" + s.iterator.map { |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
537 |
case '\b' => "\\b" case '\t' => "\\t" case '\n' => "\\n" case '\f' => "\\f" |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
538 |
case '\r' => "\\r" case '"' => "\\\"" case '\\' => "\\\\" case c => |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
539 |
if (c <= '\u001f' || c == '\u007f') "\\u%04x".format(c.toInt) else c |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
540 |
}.mkString + "\"" |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
541 |
|
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
542 |
private def multiline_basic_string(s: Str): Str = |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
543 |
"\"\"\"\n" + s.iterator.map { |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
544 |
case '\b' => "\\b" case '\t' => "\t" case '\n' => "\n" case '\f' => "\\f" |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
545 |
case '\r' => "\r" case '"' => "\\\"" case '\\' => "\\\\" case c => |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
546 |
if (c <= '\u001f' || c == '\u007f') "\\u%04x".format(c.toInt) else c |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
547 |
}.mkString.replace("\"\"\"", "\"\"\\\"") + "\"\"\"" |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
548 |
|
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
549 |
def key(k: Key): Str = { |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
550 |
val Bare_Key = """[A-Za-z0-9_-]+""".r |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
551 |
k match { |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
552 |
case Bare_Key() => k |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
553 |
case _ => basic_string(k) |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
554 |
} |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
555 |
} |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
556 |
|
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
557 |
def keys(ks: Keys): Str = ks.mkString(".") |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
558 |
|
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
559 |
def inline(t: T, indent: Int = 0): Str = { |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
560 |
val result = new StringBuilder |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
561 |
|
78917 | 562 |
result ++= Symbol.spaces(indent * 2) |
78941 | 563 |
(t: @unchecked) match { |
78326
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
564 |
case s: String => |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
565 |
if (s.rep.contains("\n") && s.rep.length > 20) result ++= multiline_basic_string(s.rep) |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
566 |
else result ++= basic_string(s.rep) |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
567 |
case a: Array => |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
568 |
if (a.is_empty) result ++= "[]" |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
569 |
else { |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
570 |
result ++= "[\n" |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
571 |
a.any.values.foreach { elem => |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
572 |
result ++= inline(elem, indent + 1) |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
573 |
result ++= ",\n" |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
574 |
} |
78917 | 575 |
result ++= Symbol.spaces(indent * 2) |
78326
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
576 |
result += ']' |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
577 |
} |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
578 |
case table: Table => |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
579 |
if (table.is_empty) result ++= "{}" |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
580 |
else { |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
581 |
result ++= "{ " |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
582 |
Library.separate(None, table.any.values.map(Some(_))).foreach { |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
583 |
case None => result ++= ", " |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
584 |
case Some((k, v)) => |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
585 |
result ++= key(k) |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
586 |
result ++= " = " |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
587 |
result ++= inline(v) |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
588 |
} |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
589 |
result ++= " }" |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
590 |
} |
78941 | 591 |
case Scalar(s) => result ++= s |
78326
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
592 |
} |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
593 |
result.toString() |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
594 |
} |
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
595 |
|
78316 | 596 |
def apply(toml: Table): Str = { |
597 |
val result = new StringBuilder |
|
598 |
||
599 |
def inline_values(path: List[Key], t: T): Unit = |
|
600 |
t match { |
|
601 |
case t: Table => t.any.values.foreach { case (k, v1) => inline_values(k :: path, v1) } |
|
602 |
case _ => |
|
78326
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
603 |
result ++= keys(path.reverse) |
78316 | 604 |
result ++= " = " |
78326
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
605 |
result ++= inline(t) |
78316 | 606 |
result += '\n' |
607 |
} |
|
608 |
||
609 |
def is_inline(elem: T): Bool = |
|
610 |
elem match { |
|
611 |
case _: String | _: Integer | _: Float | _: Boolean | _: Offset_Date_Time | |
|
612 |
_: Local_Date_Time | _: Local_Date | _: Local_Time => true |
|
613 |
case a: Array => a.any.values.forall(is_inline) |
|
614 |
case _ => false |
|
615 |
} |
|
616 |
def is_table(elem: T): Bool = elem match { case _: Table => true case _ => false } |
|
617 |
||
618 |
def array(path: List[Key], a: Array): Unit = |
|
619 |
if (a.any.values.forall(is_inline) || !a.any.values.forall(is_table)) |
|
620 |
inline_values(path.take(1), a) |
|
621 |
else a.table.values.foreach { t => |
|
622 |
result ++= "\n[[" |
|
78326
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
623 |
result ++= keys(path.reverse) |
78316 | 624 |
result ++= "]]\n" |
625 |
table(path, t, is_table_entry = true) |
|
626 |
} |
|
627 |
||
628 |
def table(path: List[Key], t: Table, is_table_entry: Bool = false): Unit = { |
|
629 |
val (values, nodes) = t.any.values.partition(kv => is_inline(kv._2)) |
|
630 |
||
631 |
if (!is_table_entry && path.nonEmpty) { |
|
632 |
result ++= "\n[" |
|
78326
1cdc65476c2e
more TOML formatting functions;
Fabian Huch <huch@in.tum.de>
parents:
78317
diff
changeset
|
633 |
result ++= keys(path.reverse) |
78316 | 634 |
result ++= "]\n" |
635 |
} |
|
636 |
||
637 |
values.foreach { case (k, v) => inline_values(List(k), v) } |
|
638 |
nodes.foreach { |
|
639 |
case (k, t: Table) => table(k :: path, t) |
|
640 |
case (k, arr: Array) => array(k :: path, arr) |
|
641 |
case _ => |
|
642 |
} |
|
643 |
} |
|
644 |
||
645 |
table(Nil, toml) |
|
646 |
result.toString |
|
647 |
} |
|
648 |
} |
|
649 |
} |