src/Pure/Thy/thy_header.scala
author wenzelm
Tue, 05 Jul 2011 22:38:44 +0200
changeset 43672 e9f26e66692d
parent 43661 39fdbd814c7f
child 43695 5130dfe1b7be
permissions -rw-r--r--
theory name needs to conform to Path syntax;
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
34300
3f2e25dc99ab misc tuning;
wenzelm
parents: 34201
diff changeset
    28
  final case class Header(val name: String, val imports: List[String], val uses: List[String])
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    29
  {
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    30
    def decode_permissive_utf8: Header =
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    31
      Header(Standard_System.decode_permissive_utf8(name),
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    32
        imports.map(Standard_System.decode_permissive_utf8),
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    33
        uses.map(Standard_System.decode_permissive_utf8))
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    34
  }
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    35
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    36
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    37
  /* file name */
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    38
43648
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
    39
  def thy_path(name: String): Path = Path.basic(name).ext("thy")
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
    40
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
    41
  private val Thy_Path1 = new Regex("([^/]*)\\.thy")
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
    42
  private val Thy_Path2 = new Regex("(.*)/([^/]*)\\.thy")
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    43
39630
44181423183a explicit Session.Phase indication with associated event bus;
wenzelm
parents: 38149
diff changeset
    44
  def split_thy_path(path: String): Option[(String, String)] =
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    45
    path match {
39630
44181423183a explicit Session.Phase indication with associated event bus;
wenzelm
parents: 38149
diff changeset
    46
      case Thy_Path1(name) => Some(("", name))
44181423183a explicit Session.Phase indication with associated event bus;
wenzelm
parents: 38149
diff changeset
    47
      case Thy_Path2(dir, name) => Some((dir, name))
44181423183a explicit Session.Phase indication with associated event bus;
wenzelm
parents: 38149
diff changeset
    48
      case _ => None
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    49
    }
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
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    52
  /* header */
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    53
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    54
  val header: Parser[Header] =
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    55
  {
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    56
    val file_name = atom("file name", _.is_name)
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    57
    val theory_name = atom("theory name", _.is_name)
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    58
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    59
    val file =
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    60
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    61
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    62
    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
    63
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    64
    val args =
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    65
      theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    66
        { case x ~ (_ ~ (ys ~ zs ~ _)) => Header(x, ys, zs) }
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    67
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    68
    (keyword(HEADER) ~ tags) ~!
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    69
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    70
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    71
  }
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    72
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    73
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    74
  /* read -- lazy scanning */
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    75
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    76
  def read(reader: Reader[Char]): Header =
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    77
  {
43661
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43652
diff changeset
    78
    val token = lexicon.token(Isabelle_System.symbols, _ => false)
36956
21be4832c362 renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents: 36948
diff changeset
    79
    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
    80
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    81
    @tailrec def scan_to_begin(in: Reader[Char])
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    82
    {
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    83
      token(in) match {
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    84
        case lexicon.Success(tok, rest) =>
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    85
          toks += tok
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    86
          if (!tok.is_begin) scan_to_begin(rest)
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    87
        case _ =>
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    88
      }
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
    scan_to_begin(reader)
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    91
36956
21be4832c362 renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents: 36948
diff changeset
    92
    parse(commit(header), Token.reader(toks.toList)) match {
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    93
      case Success(result, _) => result
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    94
      case bad => error(bad.toString)
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    95
    }
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    96
  }
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    97
43646
598b2c6ce13f Thy_Header.read convenience;
wenzelm
parents: 43611
diff changeset
    98
  def read(source: CharSequence): Header =
598b2c6ce13f Thy_Header.read convenience;
wenzelm
parents: 43611
diff changeset
    99
    read(new CharSequenceReader(source))
598b2c6ce13f Thy_Header.read convenience;
wenzelm
parents: 43611
diff changeset
   100
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
   101
  def read(file: File): Header =
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
   102
  {
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
   103
    val reader = Scan.byte_reader(file)
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
   104
    try { read(reader).decode_permissive_utf8 }
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
   105
    finally { reader.close }
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
   106
  }
43648
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   107
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   108
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   109
  /* check */
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   110
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   111
  def check(name: String, source: CharSequence): Header =
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   112
  {
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   113
    val header = read(source)
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   114
    val name1 = header.name
43672
e9f26e66692d theory name needs to conform to Path syntax;
wenzelm
parents: 43661
diff changeset
   115
    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
   116
    Path.explode(name)
e9f26e66692d theory name needs to conform to Path syntax;
wenzelm
parents: 43661
diff changeset
   117
    header
43648
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43646
diff changeset
   118
  }
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   119
}