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