src/Pure/System/registry.scala
author wenzelm
Sat, 11 Nov 2023 19:36:59 +0100
changeset 78942 409442cb7814
parent 78940 4cb67b3895b9
child 78945 73162a487f94
permissions -rw-r--r--
support interpreted/typed entries via Registry.Category and Registry.Table;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
78939
218929597048 support for global registry;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/System/registry.scala
218929597048 support for global registry;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
218929597048 support for global registry;
wenzelm
parents:
diff changeset
     3
218929597048 support for global registry;
wenzelm
parents:
diff changeset
     4
Hierarchic system configuration in TOML notation.
218929597048 support for global registry;
wenzelm
parents:
diff changeset
     5
*/
218929597048 support for global registry;
wenzelm
parents:
diff changeset
     6
218929597048 support for global registry;
wenzelm
parents:
diff changeset
     7
package isabelle
218929597048 support for global registry;
wenzelm
parents:
diff changeset
     8
218929597048 support for global registry;
wenzelm
parents:
diff changeset
     9
218929597048 support for global registry;
wenzelm
parents:
diff changeset
    10
object Registry {
218929597048 support for global registry;
wenzelm
parents:
diff changeset
    11
  def files(): List[Path] =
218929597048 support for global registry;
wenzelm
parents:
diff changeset
    12
    Path.split_permissive_files(Isabelle_System.getenv("ISABELLE_REGISTRY"))
218929597048 support for global registry;
wenzelm
parents:
diff changeset
    13
218929597048 support for global registry;
wenzelm
parents:
diff changeset
    14
  lazy val global: Registry = new Registry(TOML.parse_files(files()))
78942
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    15
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    16
  def err(msg: String, name: String): Nothing =
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    17
    error(msg + " for registry entry " + quote(name))
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    18
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    19
  abstract class Category[A](val prefix: String) {
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    20
    override def toString: String = "Registry.Category(" + quote(prefix) + ")"
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    21
    def default_value: A
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    22
    def value(name: String, t: TOML.T): A
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    23
  }
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    24
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    25
  abstract class Table[A](prefix: String) extends Category[A](prefix) {
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    26
    def table_value(name: String, table: TOML.Table): A
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    27
    override def default_value: A = table_value("", TOML.Table.empty)
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    28
    override def value(name: String, t: TOML.T): A =
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    29
      t match {
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    30
        case table: TOML.Table => table_value(name, table)
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    31
        case _ => err("Table expected", Long_Name.qualify(prefix, name))
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    32
      }
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    33
  }
78939
218929597048 support for global registry;
wenzelm
parents:
diff changeset
    34
}
218929597048 support for global registry;
wenzelm
parents:
diff changeset
    35
78942
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    36
class Registry private(val table: TOML.Table) {
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    37
  override def toString: String = TOML.Format(table)
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    38
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    39
  def get[A](category: Registry.Category[A], name: String): A = {
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    40
    table.any.get(category.prefix) match {
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    41
      case None => category.default_value
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    42
      case Some(t: TOML.Table) =>
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    43
        t.any.get(name) match {
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    44
          case None => category.default_value
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    45
          case Some(u) => category.value(name, u)
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    46
        }
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    47
      case Some(_) => Registry.err("Table expected", category.prefix)
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    48
    }
409442cb7814 support interpreted/typed entries via Registry.Category and Registry.Table;
wenzelm
parents: 78940
diff changeset
    49
  }
78940
4cb67b3895b9 clarified output;
wenzelm
parents: 78939
diff changeset
    50
}