src/Pure/Thy/thy_header.scala
author wenzelm
Sun Jul 10 15:48:15 2011 +0200 (2011-07-10)
changeset 43725 bebcfcad12d4
parent 43724 4e58485fa320
child 43731 70072780e095
permissions -rw-r--r--
more abstract signature;
tuned;
wenzelm@28495
     1
/*  Title:      Pure/Thy/thy_header.scala
wenzelm@28495
     2
    Author:     Makarius
wenzelm@28495
     3
wenzelm@34169
     4
Theory headers -- independent of outer syntax.
wenzelm@28495
     5
*/
wenzelm@28495
     6
wenzelm@28495
     7
package isabelle
wenzelm@28495
     8
wenzelm@28495
     9
wenzelm@43611
    10
import scala.annotation.tailrec
wenzelm@34169
    11
import scala.collection.mutable
wenzelm@43646
    12
import scala.util.parsing.input.{Reader, CharSequenceReader}
wenzelm@38149
    13
import scala.util.matching.Regex
wenzelm@34169
    14
wenzelm@34188
    15
import java.io.File
wenzelm@34188
    16
wenzelm@34169
    17
wenzelm@43661
    18
object Thy_Header extends Parse.Parser
wenzelm@32450
    19
{
wenzelm@28495
    20
  val HEADER = "header"
wenzelm@28495
    21
  val THEORY = "theory"
wenzelm@28495
    22
  val IMPORTS = "imports"
wenzelm@28495
    23
  val USES = "uses"
wenzelm@28495
    24
  val BEGIN = "begin"
wenzelm@28495
    25
wenzelm@34169
    26
  val lexicon = Scan.Lexicon("%", "(", ")", ";", BEGIN, HEADER, IMPORTS, THEORY, USES)
wenzelm@34190
    27
wenzelm@43699
    28
  sealed case class Header(val name: String, val imports: List[String], val uses: List[String])
wenzelm@43611
    29
  {
wenzelm@43699
    30
    def map(f: String => String): Header =
wenzelm@43699
    31
      Header(f(name), imports.map(f), uses.map(f))
wenzelm@43611
    32
  }
wenzelm@38149
    33
wenzelm@43725
    34
  val make_xml_data: XML_Data.Make.T[Header] =
wenzelm@43725
    35
  {
wenzelm@43725
    36
    case Header(name, imports, uses) =>
wenzelm@43725
    37
      import XML_Data.Make._
wenzelm@43725
    38
      triple(string, list(string), list(string))(name, imports, uses)
wenzelm@43725
    39
  }
wenzelm@43722
    40
wenzelm@38149
    41
wenzelm@38149
    42
  /* file name */
wenzelm@38149
    43
wenzelm@43648
    44
  def thy_path(name: String): Path = Path.basic(name).ext("thy")
wenzelm@43648
    45
wenzelm@43697
    46
  def split_thy_path(path: Path): (Path, String) =
wenzelm@43697
    47
    path.split_ext match {
wenzelm@43697
    48
      case (path1, "thy") => (path1.dir, path1.base.implode)
wenzelm@43697
    49
      case _ => error("Bad theory file specification: " + path)
wenzelm@38149
    50
    }
wenzelm@34169
    51
wenzelm@34169
    52
wenzelm@34169
    53
  /* header */
wenzelm@34169
    54
wenzelm@34190
    55
  val header: Parser[Header] =
wenzelm@34169
    56
  {
wenzelm@43611
    57
    val file_name = atom("file name", _.is_name)
wenzelm@43611
    58
    val theory_name = atom("theory name", _.is_name)
wenzelm@34169
    59
wenzelm@34169
    60
    val file =
wenzelm@34169
    61
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name
wenzelm@34169
    62
wenzelm@34169
    63
    val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
wenzelm@34169
    64
wenzelm@34169
    65
    val args =
wenzelm@34169
    66
      theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
wenzelm@34190
    67
        { case x ~ (_ ~ (ys ~ zs ~ _)) => Header(x, ys, zs) }
wenzelm@34169
    68
wenzelm@34169
    69
    (keyword(HEADER) ~ tags) ~!
wenzelm@34169
    70
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
wenzelm@34169
    71
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    72
  }
wenzelm@34169
    73
wenzelm@34169
    74
wenzelm@34190
    75
  /* read -- lazy scanning */
wenzelm@34169
    76
wenzelm@43611
    77
  def read(reader: Reader[Char]): Header =
wenzelm@34169
    78
  {
wenzelm@43695
    79
    val token = lexicon.token(_ => false)
wenzelm@36956
    80
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
    81
wenzelm@43611
    82
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
    83
    {
wenzelm@34169
    84
      token(in) match {
wenzelm@34169
    85
        case lexicon.Success(tok, rest) =>
wenzelm@34169
    86
          toks += tok
wenzelm@43611
    87
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
    88
        case _ =>
wenzelm@34169
    89
      }
wenzelm@34169
    90
    }
wenzelm@43611
    91
    scan_to_begin(reader)
wenzelm@34190
    92
wenzelm@36956
    93
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
    94
      case Success(result, _) => result
wenzelm@34190
    95
      case bad => error(bad.toString)
wenzelm@34190
    96
    }
wenzelm@34169
    97
  }
wenzelm@43611
    98
wenzelm@43646
    99
  def read(source: CharSequence): Header =
wenzelm@43646
   100
    read(new CharSequenceReader(source))
wenzelm@43646
   101
wenzelm@43611
   102
  def read(file: File): Header =
wenzelm@43611
   103
  {
wenzelm@43611
   104
    val reader = Scan.byte_reader(file)
wenzelm@43699
   105
    try { read(reader).map(Standard_System.decode_permissive_utf8) }
wenzelm@43611
   106
    finally { reader.close }
wenzelm@43611
   107
  }
wenzelm@43648
   108
wenzelm@43648
   109
wenzelm@43648
   110
  /* check */
wenzelm@43648
   111
wenzelm@43648
   112
  def check(name: String, source: CharSequence): Header =
wenzelm@43648
   113
  {
wenzelm@43648
   114
    val header = read(source)
wenzelm@43648
   115
    val name1 = header.name
wenzelm@43672
   116
    if (name != name1) error("Bad file name " + thy_path(name) + " for theory " + quote(name1))
wenzelm@43672
   117
    Path.explode(name)
wenzelm@43672
   118
    header
wenzelm@43648
   119
  }
wenzelm@34169
   120
}