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