src/Pure/General/toml.scala
changeset 78317 fcabbb45b272
parent 78316 be8aaaa4ac25
child 78326 1cdc65476c2e
equal deleted inserted replaced
78316:be8aaaa4ac25 78317:fcabbb45b272
     1 /*  Title:      Pure/General/toml.scala
     1 /*  Title:      Pure/General/toml.scala
     2     Author: Fabian Huch, TU Muenchen
     2     Author:     Fabian Huch, TU Muenchen
     3 
     3 
     4 Support for TOML: https://toml.io/en/v1.0.0
     4 Support for TOML: https://toml.io/en/v1.0.0
     5  */
     5 */
       
     6 
     6 package isabelle
     7 package isabelle
     7 
     8 
     8 
     9 
     9 import TOML.Parsers.Keys
    10 import TOML.Parsers.Keys
    10 import TOML.Parse_Context.Seen
    11 import TOML.Parse_Context.Seen
   227 
   228 
   228   /* parser */
   229   /* parser */
   229 
   230 
   230   trait Parsers extends combinator.Parsers {
   231   trait Parsers extends combinator.Parsers {
   231     type Elem = Token
   232     type Elem = Token
   232     
   233 
   233     
   234 
   234     /* parse structure */
   235     /* parse structure */
   235     
   236 
   236     type Keys = List[Key]
   237     type Keys = List[Key]
   237 
   238 
   238     sealed trait V
   239     sealed trait V
   239     case class Primitive(t: T) extends V
   240     case class Primitive(t: T) extends V
   240     case class Array(rep: List[V]) extends V
   241     case class Array(rep: List[V]) extends V
   241     case class Inline_Table(elems: List[(Keys, V)]) extends V
   242     case class Inline_Table(elems: List[(Keys, V)]) extends V
   242 
   243 
   243     sealed trait Def
   244     sealed trait Def
   244     case class Table(key: Keys, elems: List[(Keys, V)]) extends Def
   245     case class Table(key: Keys, elems: List[(Keys, V)]) extends Def
   245     case class Array_Of_Tables(key: Keys, elems: List[(Keys, V)]) extends Def
   246     case class Array_Of_Tables(key: Keys, elems: List[(Keys, V)]) extends Def
   246     
   247 
   247     case class File(elems: List[(Keys, V)], defs: List[Def])
   248     case class File(elems: List[(Keys, V)], defs: List[Def])
   248     
   249 
   249 
   250 
   250     /* top-level syntax structure */
   251     /* top-level syntax structure */
   251     
   252 
   252     def key: Parser[Keys] = rep1sep(keys, $$$(".")) ^^ (_.flatten)
   253     def key: Parser[Keys] = rep1sep(keys, $$$(".")) ^^ (_.flatten)
   253     
   254 
   254     def string: Parser[String] =
   255     def string: Parser[String] =
   255       elem("string", e => e.is_string || e.is_multiline_string) ^^ (s => String(s.text))
   256       elem("string", e => e.is_string || e.is_multiline_string) ^^ (s => String(s.text))
   256     def integer: Parser[Integer] =
   257     def integer: Parser[Integer] =
   257       (decimal_int | binary_int | octal_int | hexadecimal_int) ^^ Integer.apply
   258       (decimal_int | binary_int | octal_int | hexadecimal_int) ^^ Integer.apply
   258     def float: Parser[Float] = (symbol_float | number_float) ^^ Float.apply
   259     def float: Parser[Float] = (symbol_float | number_float) ^^ Float.apply
   273       $$$("[") ~>! repsep(opt(line_sep) ~> toml_value, opt(line_sep) ~ $$$(",")) <~!
   274       $$$("[") ~>! repsep(opt(line_sep) ~> toml_value, opt(line_sep) ~ $$$(",")) <~!
   274         opt(line_sep) ~! opt($$$(",")) ~! opt(line_sep) <~! $$$("]") ^^ Array.apply
   275         opt(line_sep) ~! opt($$$(",")) ~! opt(line_sep) <~! $$$("]") ^^ Array.apply
   275 
   276 
   276     def inline_table: Parser[Inline_Table] =
   277     def inline_table: Parser[Inline_Table] =
   277       $$$("{") ~>! repsep(pair, $$$(",")) <~! $$$("}") ^^ Inline_Table.apply
   278       $$$("{") ~>! repsep(pair, $$$(",")) <~! $$$("}") ^^ Inline_Table.apply
   278     
   279 
   279     def pair: Parser[(Keys, V)] = (key <~! $$$("=")) ~! toml_value ^^ { case ks ~ v => (ks, v) }
   280     def pair: Parser[(Keys, V)] = (key <~! $$$("=")) ~! toml_value ^^ { case ks ~ v => (ks, v) }
   280 
   281 
   281     def table: Parser[Table] = $$$("[") ~> (key <~! $$$("]") ~! line_sep) ~! content ^^
   282     def table: Parser[Table] = $$$("[") ~> (key <~! $$$("]") ~! line_sep) ~! content ^^
   282       { case key ~ content => Table(key, content) }
   283       { case key ~ content => Table(key, content) }
   283 
   284