src/Pure/Thy/thy_header.scala
author wenzelm
Sun Feb 26 15:18:48 2012 +0100 (2012-02-26)
changeset 46676 b4bc54d4624b
parent 44578 ca3844a3dcf7
child 46737 09ab89658a5d
permissions -rw-r--r--
tuned signature;
clarified check;
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@46676
    26
  private 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
wenzelm@34169
    42
wenzelm@34169
    43
  /* header */
wenzelm@34169
    44
wenzelm@44159
    45
  val header: Parser[Thy_Header] =
wenzelm@34169
    46
  {
wenzelm@43611
    47
    val file_name = atom("file name", _.is_name)
wenzelm@43611
    48
    val theory_name = atom("theory name", _.is_name)
wenzelm@34169
    49
wenzelm@34169
    50
    val file =
wenzelm@44185
    51
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
wenzelm@44185
    52
      file_name ^^ (x => (x, true))
wenzelm@34169
    53
wenzelm@34169
    54
    val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
wenzelm@34169
    55
wenzelm@34169
    56
    val args =
wenzelm@34169
    57
      theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
wenzelm@44159
    58
        { case x ~ (_ ~ (ys ~ zs ~ _)) => Thy_Header(x, ys, zs) }
wenzelm@34169
    59
wenzelm@34169
    60
    (keyword(HEADER) ~ tags) ~!
wenzelm@34169
    61
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
wenzelm@34169
    62
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    63
  }
wenzelm@34169
    64
wenzelm@34169
    65
wenzelm@34190
    66
  /* read -- lazy scanning */
wenzelm@34169
    67
wenzelm@44159
    68
  def read(reader: Reader[Char]): Thy_Header =
wenzelm@34169
    69
  {
wenzelm@43695
    70
    val token = lexicon.token(_ => false)
wenzelm@36956
    71
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
    72
wenzelm@43611
    73
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
    74
    {
wenzelm@34169
    75
      token(in) match {
wenzelm@34169
    76
        case lexicon.Success(tok, rest) =>
wenzelm@34169
    77
          toks += tok
wenzelm@43611
    78
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
    79
        case _ =>
wenzelm@34169
    80
      }
wenzelm@34169
    81
    }
wenzelm@43611
    82
    scan_to_begin(reader)
wenzelm@34190
    83
wenzelm@36956
    84
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
    85
      case Success(result, _) => result
wenzelm@34190
    86
      case bad => error(bad.toString)
wenzelm@34190
    87
    }
wenzelm@34169
    88
  }
wenzelm@43611
    89
wenzelm@44159
    90
  def read(source: CharSequence): Thy_Header =
wenzelm@43646
    91
    read(new CharSequenceReader(source))
wenzelm@43646
    92
wenzelm@44159
    93
  def read(file: File): Thy_Header =
wenzelm@43611
    94
  {
wenzelm@43611
    95
    val reader = Scan.byte_reader(file)
wenzelm@43699
    96
    try { read(reader).map(Standard_System.decode_permissive_utf8) }
wenzelm@43611
    97
    finally { reader.close }
wenzelm@43611
    98
  }
wenzelm@43648
    99
wenzelm@43648
   100
wenzelm@43648
   101
  /* check */
wenzelm@43648
   102
wenzelm@44159
   103
  def check(name: String, source: CharSequence): Thy_Header =
wenzelm@43648
   104
  {
wenzelm@43648
   105
    val header = read(source)
wenzelm@43648
   106
    val name1 = header.name
wenzelm@46676
   107
    val path = Path.explode(name)
wenzelm@46676
   108
    if (name != name1) error("Bad file name " + thy_path(path) + " for theory " + quote(name1))
wenzelm@43672
   109
    header
wenzelm@43648
   110
  }
wenzelm@34169
   111
}
wenzelm@44159
   112
wenzelm@44159
   113
wenzelm@44185
   114
sealed case class Thy_Header(
wenzelm@44185
   115
  val name: String, val imports: List[String], val uses: List[(String, Boolean)])
wenzelm@44159
   116
{
wenzelm@44159
   117
  def map(f: String => String): Thy_Header =
wenzelm@44185
   118
    Thy_Header(f(name), imports.map(f), uses.map(p => (f(p._1), p._2)))
wenzelm@44163
   119
wenzelm@44222
   120
  def norm_deps(f: String => String, g: String => String): Thy_Header =
wenzelm@44578
   121
    copy(imports = imports.map(name => f(name)))
wenzelm@44578
   122
    // FIXME
wenzelm@44578
   123
    // copy(imports = imports.map(name => f(name)), uses = uses.map(p => (g(p._1), p._2)))
wenzelm@44159
   124
}
wenzelm@44159
   125