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