src/Pure/Thy/thy_header.scala
author wenzelm
Sat Aug 13 20:20:36 2011 +0200 (2011-08-13)
changeset 44185 05641edb5d30
parent 44163 32e0c150c010
child 44222 9d5ef6cd4ee1
permissions -rw-r--r--
provide node header via Scala layer;
clarified node edit Clear: retain header information;
run_command: node info via document model, error handling within transaction;
node names without ".thy" suffix, to coincide with traditional theory loader treatment;
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@44185
    31
  private val Thy_Name = new Regex("""(.*?)([^/\\:]+)\.thy""")
wenzelm@44160
    32
wenzelm@44185
    33
  def thy_name(s: String): Option[(String, String)] =
wenzelm@44185
    34
    s match { case Thy_Name(prefix, name) => Some((prefix, 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@44185
    47
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
wenzelm@44185
    48
      file_name ^^ (x => (x, true))
wenzelm@34169
    49
wenzelm@34169
    50
    val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
wenzelm@34169
    51
wenzelm@34169
    52
    val args =
wenzelm@34169
    53
      theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
wenzelm@44159
    54
        { case x ~ (_ ~ (ys ~ zs ~ _)) => Thy_Header(x, ys, zs) }
wenzelm@34169
    55
wenzelm@34169
    56
    (keyword(HEADER) ~ tags) ~!
wenzelm@34169
    57
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
wenzelm@34169
    58
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    59
  }
wenzelm@34169
    60
wenzelm@34169
    61
wenzelm@34190
    62
  /* read -- lazy scanning */
wenzelm@34169
    63
wenzelm@44159
    64
  def read(reader: Reader[Char]): Thy_Header =
wenzelm@34169
    65
  {
wenzelm@43695
    66
    val token = lexicon.token(_ => false)
wenzelm@36956
    67
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
    68
wenzelm@43611
    69
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
    70
    {
wenzelm@34169
    71
      token(in) match {
wenzelm@34169
    72
        case lexicon.Success(tok, rest) =>
wenzelm@34169
    73
          toks += tok
wenzelm@43611
    74
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
    75
        case _ =>
wenzelm@34169
    76
      }
wenzelm@34169
    77
    }
wenzelm@43611
    78
    scan_to_begin(reader)
wenzelm@34190
    79
wenzelm@36956
    80
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
    81
      case Success(result, _) => result
wenzelm@34190
    82
      case bad => error(bad.toString)
wenzelm@34190
    83
    }
wenzelm@34169
    84
  }
wenzelm@43611
    85
wenzelm@44159
    86
  def read(source: CharSequence): Thy_Header =
wenzelm@43646
    87
    read(new CharSequenceReader(source))
wenzelm@43646
    88
wenzelm@44159
    89
  def read(file: File): Thy_Header =
wenzelm@43611
    90
  {
wenzelm@43611
    91
    val reader = Scan.byte_reader(file)
wenzelm@43699
    92
    try { read(reader).map(Standard_System.decode_permissive_utf8) }
wenzelm@43611
    93
    finally { reader.close }
wenzelm@43611
    94
  }
wenzelm@43648
    95
wenzelm@43648
    96
wenzelm@43648
    97
  /* check */
wenzelm@43648
    98
wenzelm@44159
    99
  def check(name: String, source: CharSequence): Thy_Header =
wenzelm@43648
   100
  {
wenzelm@43648
   101
    val header = read(source)
wenzelm@43648
   102
    val name1 = header.name
wenzelm@43672
   103
    if (name != name1) error("Bad file name " + thy_path(name) + " for theory " + quote(name1))
wenzelm@43672
   104
    Path.explode(name)
wenzelm@43672
   105
    header
wenzelm@43648
   106
  }
wenzelm@34169
   107
}
wenzelm@44159
   108
wenzelm@44159
   109
wenzelm@44185
   110
sealed case class Thy_Header(
wenzelm@44185
   111
  val name: String, val imports: List[String], val uses: List[(String, Boolean)])
wenzelm@44159
   112
{
wenzelm@44159
   113
  def map(f: String => String): Thy_Header =
wenzelm@44185
   114
    Thy_Header(f(name), imports.map(f), uses.map(p => (f(p._1), p._2)))
wenzelm@44163
   115
wenzelm@44163
   116
  def norm_deps(f: String => String): Thy_Header =
wenzelm@44185
   117
    copy(imports = imports.map(name => f(name)), uses = uses.map(p => (f(p._1), p._2)))
wenzelm@44159
   118
}
wenzelm@44159
   119