src/Pure/Thy/thy_header.scala
author wenzelm
Mon, 29 Aug 2011 21:55:49 +0200
changeset 44574 24444588fddd
parent 44225 a8f921e6484f
child 44578 ca3844a3dcf7
permissions -rw-r--r--
actual auto loading of required files; eliminated File_Store in favour of Thy_Load; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28495
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/thy_header.scala
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     3
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
     4
Theory headers -- independent of outer syntax.
28495
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     5
*/
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     6
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     7
package isabelle
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     8
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     9
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    10
import scala.annotation.tailrec
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    11
import scala.collection.mutable
43646
598b2c6ce13f Thy_Header.read convenience;
wenzelm
parents: 43611
diff changeset
    12
import scala.util.parsing.input.{Reader, CharSequenceReader}
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    13
import scala.util.matching.Regex
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    14
34188
fbfc18be1f8c scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents: 34169
diff changeset
    15
import java.io.File
fbfc18be1f8c scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents: 34169
diff changeset
    16
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    17
43661
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43652
diff changeset
    18
object Thy_Header extends Parse.Parser
32450
375db037f4d2 misc tuning;
wenzelm
parents: 29140
diff changeset
    19
{
28495
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    20
  val HEADER = "header"
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    21
  val THEORY = "theory"
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    22
  val IMPORTS = "imports"
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    23
  val USES = "uses"
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    24
  val BEGIN = "begin"
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    25
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    26
  val lexicon = Scan.Lexicon("%", "(", ")", ";", BEGIN, HEADER, IMPORTS, THEORY, USES)
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    27
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    28
44160
8848867501fb clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents: 44159
diff changeset
    29
  /* theory file name */
8848867501fb clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents: 44159
diff changeset
    30
44225
a8f921e6484f more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents: 44222
diff changeset
    31
  private val Base_Name = new Regex(""".*?([^/\\:]+)""")
44222
9d5ef6cd4ee1 use full .thy file name as node name, which makes MiscUtilities.resolveSymlinks/File.getCanonicalPath more predictable;
wenzelm
parents: 44185
diff changeset
    32
  private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
44160
8848867501fb clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents: 44159
diff changeset
    33
44225
a8f921e6484f more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents: 44222
diff changeset
    34
  def base_name(s: String): String =
a8f921e6484f more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents: 44222
diff changeset
    35
    s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
a8f921e6484f more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents: 44222
diff changeset
    36
44222
9d5ef6cd4ee1 use full .thy file name as node name, which makes MiscUtilities.resolveSymlinks/File.getCanonicalPath more predictable;
wenzelm
parents: 44185
diff changeset
    37
  def thy_name(s: String): Option[String] =
9d5ef6cd4ee1 use full .thy file name as node name, which makes MiscUtilities.resolveSymlinks/File.getCanonicalPath more predictable;
wenzelm
parents: 44185
diff changeset
    38
    s match { case Thy_Name(name) => Some(name) case _ => None }
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    39
44222
9d5ef6cd4ee1 use full .thy file name as node name, which makes MiscUtilities.resolveSymlinks/File.getCanonicalPath more predictable;
wenzelm
parents: 44185
diff changeset
    40
  def thy_path(path: Path): Path = path.ext("thy")
43648
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
    41
  def thy_path(name: String): Path = Path.basic(name).ext("thy")
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
    42
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    43
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    44
  /* header */
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    45
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
    46
  val header: Parser[Thy_Header] =
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    47
  {
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    48
    val file_name = atom("file name", _.is_name)
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    49
    val theory_name = atom("theory name", _.is_name)
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    50
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    51
    val file =
44185
05641edb5d30 provide node header via Scala layer;
wenzelm
parents: 44163
diff changeset
    52
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
05641edb5d30 provide node header via Scala layer;
wenzelm
parents: 44163
diff changeset
    53
      file_name ^^ (x => (x, true))
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    54
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    55
    val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    56
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    57
    val args =
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    58
      theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
    59
        { case x ~ (_ ~ (ys ~ zs ~ _)) => Thy_Header(x, ys, zs) }
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    60
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    61
    (keyword(HEADER) ~ tags) ~!
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    62
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    63
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    64
  }
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    65
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    66
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    67
  /* read -- lazy scanning */
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    68
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
    69
  def read(reader: Reader[Char]): Thy_Header =
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    70
  {
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43672
diff changeset
    71
    val token = lexicon.token(_ => false)
36956
21be4832c362 renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents: 36948
diff changeset
    72
    val toks = new mutable.ListBuffer[Token]
34188
fbfc18be1f8c scan: operate on file (via Scan.byte_reader), more robust exception handling;
wenzelm
parents: 34169
diff changeset
    73
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    74
    @tailrec def scan_to_begin(in: Reader[Char])
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    75
    {
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    76
      token(in) match {
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    77
        case lexicon.Success(tok, rest) =>
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    78
          toks += tok
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    79
          if (!tok.is_begin) scan_to_begin(rest)
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    80
        case _ =>
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    81
      }
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    82
    }
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    83
    scan_to_begin(reader)
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    84
36956
21be4832c362 renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents: 36948
diff changeset
    85
    parse(commit(header), Token.reader(toks.toList)) match {
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    86
      case Success(result, _) => result
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    87
      case bad => error(bad.toString)
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    88
    }
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    89
  }
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    90
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
    91
  def read(source: CharSequence): Thy_Header =
43646
598b2c6ce13f Thy_Header.read convenience;
wenzelm
parents: 43611
diff changeset
    92
    read(new CharSequenceReader(source))
598b2c6ce13f Thy_Header.read convenience;
wenzelm
parents: 43611
diff changeset
    93
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
    94
  def read(file: File): Thy_Header =
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    95
  {
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    96
    val reader = Scan.byte_reader(file)
43699
fb3d99df4b1e tuned signature;
wenzelm
parents: 43697
diff changeset
    97
    try { read(reader).map(Standard_System.decode_permissive_utf8) }
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    98
    finally { reader.close }
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    99
  }
43648
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   100
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   101
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   102
  /* check */
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   103
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
   104
  def check(name: String, source: CharSequence): Thy_Header =
43648
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   105
  {
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   106
    val header = read(source)
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   107
    val name1 = header.name
43672
e9f26e66692d theory name needs to conform to Path syntax;
wenzelm
parents: 43661
diff changeset
   108
    if (name != name1) error("Bad file name " + thy_path(name) + " for theory " + quote(name1))
e9f26e66692d theory name needs to conform to Path syntax;
wenzelm
parents: 43661
diff changeset
   109
    Path.explode(name)
e9f26e66692d theory name needs to conform to Path syntax;
wenzelm
parents: 43661
diff changeset
   110
    header
43648
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   111
  }
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   112
}
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
   113
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
   114
44185
05641edb5d30 provide node header via Scala layer;
wenzelm
parents: 44163
diff changeset
   115
sealed case class Thy_Header(
05641edb5d30 provide node header via Scala layer;
wenzelm
parents: 44163
diff changeset
   116
  val name: String, val imports: List[String], val uses: List[(String, Boolean)])
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
   117
{
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
   118
  def map(f: String => String): Thy_Header =
44185
05641edb5d30 provide node header via Scala layer;
wenzelm
parents: 44163
diff changeset
   119
    Thy_Header(f(name), imports.map(f), uses.map(p => (f(p._1), p._2)))
44163
32e0c150c010 normalized theory dependencies wrt. file_store;
wenzelm
parents: 44160
diff changeset
   120
44222
9d5ef6cd4ee1 use full .thy file name as node name, which makes MiscUtilities.resolveSymlinks/File.getCanonicalPath more predictable;
wenzelm
parents: 44185
diff changeset
   121
  def norm_deps(f: String => String, g: String => String): Thy_Header =
9d5ef6cd4ee1 use full .thy file name as node name, which makes MiscUtilities.resolveSymlinks/File.getCanonicalPath more predictable;
wenzelm
parents: 44185
diff changeset
   122
    copy(imports = imports.map(name => f(name)), uses = uses.map(p => (g(p._1), p._2)))
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
   123
}
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
   124