src/Pure/Thy/thy_header.scala
author wenzelm
Thu Mar 15 00:10:45 2012 +0100 (2012-03-15)
changeset 46938 cda018294515
parent 46737 09ab89658a5d
child 46939 5b67ac48b384
permissions -rw-r--r--
some support for outer syntax keyword declarations within theory header;
more uniform Thy_Header.header as argument for begin_theory etc.;
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@46938
    23
  val KEYWORDS = "keywords"
wenzelm@46938
    24
  val AND = "and"
wenzelm@28495
    25
  val USES = "uses"
wenzelm@28495
    26
  val BEGIN = "begin"
wenzelm@28495
    27
wenzelm@46938
    28
  private val lexicon =
wenzelm@46938
    29
    Scan.Lexicon("%", "(", ")", "::", ";", AND, BEGIN, HEADER, IMPORTS, KEYWORDS, THEORY, USES)
wenzelm@34190
    30
wenzelm@38149
    31
wenzelm@44160
    32
  /* theory file name */
wenzelm@44160
    33
wenzelm@44225
    34
  private val Base_Name = new Regex(""".*?([^/\\:]+)""")
wenzelm@44222
    35
  private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
wenzelm@44160
    36
wenzelm@44225
    37
  def base_name(s: String): String =
wenzelm@44225
    38
    s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
wenzelm@44225
    39
wenzelm@44222
    40
  def thy_name(s: String): Option[String] =
wenzelm@44222
    41
    s match { case Thy_Name(name) => Some(name) case _ => None }
wenzelm@38149
    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@46938
    51
    val keyword_kind =
wenzelm@46938
    52
      atom("outer syntax keyword kind", _.is_name) ~ tags ^^ { case x ~ y => (x, y) }
wenzelm@46938
    53
    val keyword_decl =
wenzelm@46938
    54
      name ~ opt(keyword("::") ~! keyword_kind ^^ { case _ ~ x => x }) ^^
wenzelm@46938
    55
      { case x ~ y => (x, y) }
wenzelm@46938
    56
    val keywords =
wenzelm@46938
    57
      keyword_decl ~ rep(keyword(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
wenzelm@46938
    58
      { case x ~ ys => x :: ys }
wenzelm@46938
    59
wenzelm@34169
    60
    val file =
wenzelm@44185
    61
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
wenzelm@44185
    62
      file_name ^^ (x => (x, true))
wenzelm@34169
    63
wenzelm@34169
    64
    val args =
wenzelm@46938
    65
      theory_name ~
wenzelm@46938
    66
      (keyword(IMPORTS) ~! (rep1(theory_name)) ^^ { case _ ~ xs => xs }) ~
wenzelm@46938
    67
      (opt(keyword(KEYWORDS) ~! keywords) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@46938
    68
      (opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@46938
    69
      keyword(BEGIN) ^^
wenzelm@46938
    70
      { case x ~ ys ~ zs ~ ws ~ _ => Thy_Header(x, ys, zs, ws) }
wenzelm@34169
    71
wenzelm@34169
    72
    (keyword(HEADER) ~ tags) ~!
wenzelm@34169
    73
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
wenzelm@34169
    74
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    75
  }
wenzelm@34169
    76
wenzelm@34169
    77
wenzelm@34190
    78
  /* read -- lazy scanning */
wenzelm@34169
    79
wenzelm@44159
    80
  def read(reader: Reader[Char]): Thy_Header =
wenzelm@34169
    81
  {
wenzelm@43695
    82
    val token = lexicon.token(_ => false)
wenzelm@36956
    83
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
    84
wenzelm@43611
    85
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
    86
    {
wenzelm@34169
    87
      token(in) match {
wenzelm@34169
    88
        case lexicon.Success(tok, rest) =>
wenzelm@34169
    89
          toks += tok
wenzelm@43611
    90
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
    91
        case _ =>
wenzelm@34169
    92
      }
wenzelm@34169
    93
    }
wenzelm@43611
    94
    scan_to_begin(reader)
wenzelm@34190
    95
wenzelm@36956
    96
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
    97
      case Success(result, _) => result
wenzelm@34190
    98
      case bad => error(bad.toString)
wenzelm@34190
    99
    }
wenzelm@34169
   100
  }
wenzelm@43611
   101
wenzelm@44159
   102
  def read(source: CharSequence): Thy_Header =
wenzelm@43646
   103
    read(new CharSequenceReader(source))
wenzelm@43646
   104
wenzelm@44159
   105
  def read(file: File): Thy_Header =
wenzelm@43611
   106
  {
wenzelm@43611
   107
    val reader = Scan.byte_reader(file)
wenzelm@43699
   108
    try { read(reader).map(Standard_System.decode_permissive_utf8) }
wenzelm@43611
   109
    finally { reader.close }
wenzelm@43611
   110
  }
wenzelm@34169
   111
}
wenzelm@44159
   112
wenzelm@44159
   113
wenzelm@44185
   114
sealed case class Thy_Header(
wenzelm@46938
   115
  name: String, imports: List[String],
wenzelm@46938
   116
  keywords: List[(String, Option[(String, List[String])])],
wenzelm@46938
   117
  uses: List[(String, Boolean)])
wenzelm@44159
   118
{
wenzelm@44159
   119
  def map(f: String => String): Thy_Header =
wenzelm@46938
   120
    Thy_Header(f(name), imports.map(f), keywords, uses.map(p => (f(p._1), p._2)))
wenzelm@44159
   121
}
wenzelm@44159
   122