src/Pure/Thy/thy_header.scala
author wenzelm
Tue Aug 16 22:48:31 2011 +0200 (2011-08-16)
changeset 44225 a8f921e6484f
parent 44222 9d5ef6cd4ee1
child 44578 ca3844a3dcf7
permissions -rw-r--r--
more robust Thy_Header.base_name, with minimal assumptions about path syntax;
Isabelle.buffer_path: keep platform syntax;
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@44225
    31
  private val Base_Name = new Regex(""".*?([^/\\:]+)""")
wenzelm@44222
    32
  private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
wenzelm@44160
    33
wenzelm@44225
    34
  def base_name(s: String): String =
wenzelm@44225
    35
    s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
wenzelm@44225
    36
wenzelm@44222
    37
  def thy_name(s: String): Option[String] =
wenzelm@44222
    38
    s match { case Thy_Name(name) => Some(name) case _ => None }
wenzelm@38149
    39
wenzelm@44222
    40
  def thy_path(path: Path): Path = path.ext("thy")
wenzelm@43648
    41
  def thy_path(name: String): Path = Path.basic(name).ext("thy")
wenzelm@43648
    42
wenzelm@34169
    43
wenzelm@34169
    44
  /* header */
wenzelm@34169
    45
wenzelm@44159
    46
  val header: Parser[Thy_Header] =
wenzelm@34169
    47
  {
wenzelm@43611
    48
    val file_name = atom("file name", _.is_name)
wenzelm@43611
    49
    val theory_name = atom("theory name", _.is_name)
wenzelm@34169
    50
wenzelm@34169
    51
    val file =
wenzelm@44185
    52
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
wenzelm@44185
    53
      file_name ^^ (x => (x, true))
wenzelm@34169
    54
wenzelm@34169
    55
    val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
wenzelm@34169
    56
wenzelm@34169
    57
    val args =
wenzelm@34169
    58
      theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
wenzelm@44159
    59
        { case x ~ (_ ~ (ys ~ zs ~ _)) => Thy_Header(x, ys, zs) }
wenzelm@34169
    60
wenzelm@34169
    61
    (keyword(HEADER) ~ tags) ~!
wenzelm@34169
    62
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
wenzelm@34169
    63
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    64
  }
wenzelm@34169
    65
wenzelm@34169
    66
wenzelm@34190
    67
  /* read -- lazy scanning */
wenzelm@34169
    68
wenzelm@44159
    69
  def read(reader: Reader[Char]): Thy_Header =
wenzelm@34169
    70
  {
wenzelm@43695
    71
    val token = lexicon.token(_ => false)
wenzelm@36956
    72
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
    73
wenzelm@43611
    74
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
    75
    {
wenzelm@34169
    76
      token(in) match {
wenzelm@34169
    77
        case lexicon.Success(tok, rest) =>
wenzelm@34169
    78
          toks += tok
wenzelm@43611
    79
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
    80
        case _ =>
wenzelm@34169
    81
      }
wenzelm@34169
    82
    }
wenzelm@43611
    83
    scan_to_begin(reader)
wenzelm@34190
    84
wenzelm@36956
    85
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
    86
      case Success(result, _) => result
wenzelm@34190
    87
      case bad => error(bad.toString)
wenzelm@34190
    88
    }
wenzelm@34169
    89
  }
wenzelm@43611
    90
wenzelm@44159
    91
  def read(source: CharSequence): Thy_Header =
wenzelm@43646
    92
    read(new CharSequenceReader(source))
wenzelm@43646
    93
wenzelm@44159
    94
  def read(file: File): Thy_Header =
wenzelm@43611
    95
  {
wenzelm@43611
    96
    val reader = Scan.byte_reader(file)
wenzelm@43699
    97
    try { read(reader).map(Standard_System.decode_permissive_utf8) }
wenzelm@43611
    98
    finally { reader.close }
wenzelm@43611
    99
  }
wenzelm@43648
   100
wenzelm@43648
   101
wenzelm@43648
   102
  /* check */
wenzelm@43648
   103
wenzelm@44159
   104
  def check(name: String, source: CharSequence): Thy_Header =
wenzelm@43648
   105
  {
wenzelm@43648
   106
    val header = read(source)
wenzelm@43648
   107
    val name1 = header.name
wenzelm@43672
   108
    if (name != name1) error("Bad file name " + thy_path(name) + " for theory " + quote(name1))
wenzelm@43672
   109
    Path.explode(name)
wenzelm@43672
   110
    header
wenzelm@43648
   111
  }
wenzelm@34169
   112
}
wenzelm@44159
   113
wenzelm@44159
   114
wenzelm@44185
   115
sealed case class Thy_Header(
wenzelm@44185
   116
  val name: String, val imports: List[String], val uses: List[(String, Boolean)])
wenzelm@44159
   117
{
wenzelm@44159
   118
  def map(f: String => String): Thy_Header =
wenzelm@44185
   119
    Thy_Header(f(name), imports.map(f), uses.map(p => (f(p._1), p._2)))
wenzelm@44163
   120
wenzelm@44222
   121
  def norm_deps(f: String => String, g: String => String): Thy_Header =
wenzelm@44222
   122
    copy(imports = imports.map(name => f(name)), uses = uses.map(p => (g(p._1), p._2)))
wenzelm@44159
   123
}
wenzelm@44159
   124