src/Pure/Thy/thy_header.scala
author wenzelm
Fri Aug 12 12:03:17 2011 +0200 (2011-08-12)
changeset 44159 9a35e88d9dc9
parent 44157 a21d3e1e64fd
child 44160 8848867501fb
permissions -rw-r--r--
simplified class Thy_Header;
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@38149
    28
wenzelm@38149
    29
  /* file name */
wenzelm@38149
    30
wenzelm@43648
    31
  def thy_path(name: String): Path = Path.basic(name).ext("thy")
wenzelm@43648
    32
wenzelm@43697
    33
  def split_thy_path(path: Path): (Path, String) =
wenzelm@43697
    34
    path.split_ext match {
wenzelm@43697
    35
      case (path1, "thy") => (path1.dir, path1.base.implode)
wenzelm@43697
    36
      case _ => error("Bad theory file specification: " + path)
wenzelm@38149
    37
    }
wenzelm@34169
    38
wenzelm@34169
    39
wenzelm@34169
    40
  /* header */
wenzelm@34169
    41
wenzelm@44159
    42
  val header: Parser[Thy_Header] =
wenzelm@34169
    43
  {
wenzelm@43611
    44
    val file_name = atom("file name", _.is_name)
wenzelm@43611
    45
    val theory_name = atom("theory name", _.is_name)
wenzelm@34169
    46
wenzelm@34169
    47
    val file =
wenzelm@34169
    48
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name
wenzelm@34169
    49
wenzelm@34169
    50
    val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
wenzelm@34169
    51
wenzelm@34169
    52
    val args =
wenzelm@34169
    53
      theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
wenzelm@44159
    54
        { case x ~ (_ ~ (ys ~ zs ~ _)) => Thy_Header(x, ys, zs) }
wenzelm@34169
    55
wenzelm@34169
    56
    (keyword(HEADER) ~ tags) ~!
wenzelm@34169
    57
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
wenzelm@34169
    58
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    59
  }
wenzelm@34169
    60
wenzelm@34169
    61
wenzelm@34190
    62
  /* read -- lazy scanning */
wenzelm@34169
    63
wenzelm@44159
    64
  def read(reader: Reader[Char]): Thy_Header =
wenzelm@34169
    65
  {
wenzelm@43695
    66
    val token = lexicon.token(_ => false)
wenzelm@36956
    67
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
    68
wenzelm@43611
    69
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
    70
    {
wenzelm@34169
    71
      token(in) match {
wenzelm@34169
    72
        case lexicon.Success(tok, rest) =>
wenzelm@34169
    73
          toks += tok
wenzelm@43611
    74
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
    75
        case _ =>
wenzelm@34169
    76
      }
wenzelm@34169
    77
    }
wenzelm@43611
    78
    scan_to_begin(reader)
wenzelm@34190
    79
wenzelm@36956
    80
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
    81
      case Success(result, _) => result
wenzelm@34190
    82
      case bad => error(bad.toString)
wenzelm@34190
    83
    }
wenzelm@34169
    84
  }
wenzelm@43611
    85
wenzelm@44159
    86
  def read(source: CharSequence): Thy_Header =
wenzelm@43646
    87
    read(new CharSequenceReader(source))
wenzelm@43646
    88
wenzelm@44159
    89
  def read(file: File): Thy_Header =
wenzelm@43611
    90
  {
wenzelm@43611
    91
    val reader = Scan.byte_reader(file)
wenzelm@43699
    92
    try { read(reader).map(Standard_System.decode_permissive_utf8) }
wenzelm@43611
    93
    finally { reader.close }
wenzelm@43611
    94
  }
wenzelm@43648
    95
wenzelm@43648
    96
wenzelm@43648
    97
  /* check */
wenzelm@43648
    98
wenzelm@44159
    99
  def check(name: String, source: CharSequence): Thy_Header =
wenzelm@43648
   100
  {
wenzelm@43648
   101
    val header = read(source)
wenzelm@43648
   102
    val name1 = header.name
wenzelm@43672
   103
    if (name != name1) error("Bad file name " + thy_path(name) + " for theory " + quote(name1))
wenzelm@43672
   104
    Path.explode(name)
wenzelm@43672
   105
    header
wenzelm@43648
   106
  }
wenzelm@34169
   107
}
wenzelm@44159
   108
wenzelm@44159
   109
wenzelm@44159
   110
sealed case class Thy_Header(val name: String, val imports: List[String], val uses: List[String])
wenzelm@44159
   111
{
wenzelm@44159
   112
  def map(f: String => String): Thy_Header =
wenzelm@44159
   113
    Thy_Header(f(name), imports.map(f), uses.map(f))
wenzelm@44159
   114
}
wenzelm@44159
   115