src/Pure/Thy/thy_header.scala
author wenzelm
Tue Jul 24 20:56:18 2012 +0200 (2012-07-24)
changeset 48484 70898d016538
parent 48409 0d2114eb412a
child 48638 22d65e375c01
permissions -rw-r--r--
more explicit checks during parsing;
wenzelm@28495
     1
/*  Title:      Pure/Thy/thy_header.scala
wenzelm@28495
     2
    Author:     Makarius
wenzelm@28495
     3
wenzelm@46939
     4
Static theory header information.
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@48409
    15
import java.io.{File => JFile}
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@34169
    49
wenzelm@46938
    50
    val keyword_kind =
wenzelm@46938
    51
      atom("outer syntax keyword kind", _.is_name) ~ tags ^^ { case x ~ y => (x, y) }
wenzelm@46938
    52
    val keyword_decl =
wenzelm@46943
    53
      rep1(string) ~ opt(keyword("::") ~! keyword_kind ^^ { case _ ~ x => x }) ^^
wenzelm@46939
    54
      { case xs ~ y => xs.map((_, y)) }
wenzelm@46939
    55
    val keyword_decls =
wenzelm@46938
    56
      keyword_decl ~ rep(keyword(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
wenzelm@46939
    57
      { case xs ~ yss => (xs :: yss).flatten }
wenzelm@46938
    58
wenzelm@34169
    59
    val file =
wenzelm@44185
    60
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
wenzelm@44185
    61
      file_name ^^ (x => (x, true))
wenzelm@34169
    62
wenzelm@34169
    63
    val args =
wenzelm@46938
    64
      theory_name ~
wenzelm@46938
    65
      (keyword(IMPORTS) ~! (rep1(theory_name)) ^^ { case _ ~ xs => xs }) ~
wenzelm@46939
    66
      (opt(keyword(KEYWORDS) ~! keyword_decls) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@46938
    67
      (opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@46938
    68
      keyword(BEGIN) ^^
wenzelm@46938
    69
      { case x ~ ys ~ zs ~ ws ~ _ => Thy_Header(x, ys, zs, ws) }
wenzelm@34169
    70
wenzelm@34169
    71
    (keyword(HEADER) ~ tags) ~!
wenzelm@34169
    72
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
wenzelm@34169
    73
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    74
  }
wenzelm@34169
    75
wenzelm@34169
    76
wenzelm@34190
    77
  /* read -- lazy scanning */
wenzelm@34169
    78
wenzelm@44159
    79
  def read(reader: Reader[Char]): Thy_Header =
wenzelm@34169
    80
  {
wenzelm@43695
    81
    val token = lexicon.token(_ => false)
wenzelm@36956
    82
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
    83
wenzelm@43611
    84
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
    85
    {
wenzelm@34169
    86
      token(in) match {
wenzelm@34169
    87
        case lexicon.Success(tok, rest) =>
wenzelm@34169
    88
          toks += tok
wenzelm@43611
    89
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
    90
        case _ =>
wenzelm@34169
    91
      }
wenzelm@34169
    92
    }
wenzelm@43611
    93
    scan_to_begin(reader)
wenzelm@34190
    94
wenzelm@36956
    95
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
    96
      case Success(result, _) => result
wenzelm@34190
    97
      case bad => error(bad.toString)
wenzelm@34190
    98
    }
wenzelm@34169
    99
  }
wenzelm@43611
   100
wenzelm@44159
   101
  def read(source: CharSequence): Thy_Header =
wenzelm@43646
   102
    read(new CharSequenceReader(source))
wenzelm@43646
   103
wenzelm@48409
   104
  def read(file: JFile): Thy_Header =
wenzelm@43611
   105
  {
wenzelm@43611
   106
    val reader = Scan.byte_reader(file)
wenzelm@43699
   107
    try { read(reader).map(Standard_System.decode_permissive_utf8) }
wenzelm@43611
   108
    finally { reader.close }
wenzelm@43611
   109
  }
wenzelm@34169
   110
}
wenzelm@44159
   111
wenzelm@44159
   112
wenzelm@44185
   113
sealed case class Thy_Header(
wenzelm@46938
   114
  name: String, imports: List[String],
wenzelm@46940
   115
  keywords: List[Outer_Syntax.Decl],
wenzelm@46938
   116
  uses: List[(String, Boolean)])
wenzelm@44159
   117
{
wenzelm@44159
   118
  def map(f: String => String): Thy_Header =
wenzelm@46938
   119
    Thy_Header(f(name), imports.map(f), keywords, uses.map(p => (f(p._1), p._2)))
wenzelm@44159
   120
}
wenzelm@44159
   121