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