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