src/Pure/Thy/thy_header.scala
author wenzelm
Sun Nov 02 15:27:37 2014 +0100 (2014-11-02)
changeset 58868 c5e1cce7ace3
parent 58861 5ff61774df11
child 58899 0a793c580685
permissions -rw-r--r--
uniform heading commands work in any context, even in theory header;
discontinued obsolete 'sect', 'subsect', 'subsubsect';
marked obsolete 'header' as legacy;
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@34169
    15
wenzelm@43661
    16
object Thy_Header extends Parse.Parser
wenzelm@32450
    17
{
wenzelm@28495
    18
  val HEADER = "header"
wenzelm@58868
    19
  val CHAPTER = "chapter"
wenzelm@58868
    20
  val SECTION = "section"
wenzelm@58868
    21
  val SUBSECTION = "subsection"
wenzelm@58868
    22
  val SUBSUBSECTION = "subsubsection"
wenzelm@58868
    23
wenzelm@28495
    24
  val THEORY = "theory"
wenzelm@28495
    25
  val IMPORTS = "imports"
wenzelm@46938
    26
  val KEYWORDS = "keywords"
wenzelm@46938
    27
  val AND = "and"
wenzelm@28495
    28
  val BEGIN = "begin"
wenzelm@28495
    29
wenzelm@46938
    30
  private val lexicon =
wenzelm@58868
    31
    Scan.Lexicon("%", "(", ")", ",", "::", "==", AND, BEGIN,
wenzelm@58868
    32
      HEADER, CHAPTER, SECTION, SUBSECTION, SUBSUBSECTION, IMPORTS, KEYWORDS, THEORY)
wenzelm@34190
    33
wenzelm@38149
    34
wenzelm@44160
    35
  /* theory file name */
wenzelm@44160
    36
wenzelm@44225
    37
  private val Base_Name = new Regex(""".*?([^/\\:]+)""")
wenzelm@44222
    38
  private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
wenzelm@44160
    39
wenzelm@44225
    40
  def base_name(s: String): String =
wenzelm@44225
    41
    s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
wenzelm@44225
    42
wenzelm@44222
    43
  def thy_name(s: String): Option[String] =
wenzelm@44222
    44
    s match { case Thy_Name(name) => Some(name) case _ => None }
wenzelm@38149
    45
wenzelm@34169
    46
wenzelm@34169
    47
  /* header */
wenzelm@34169
    48
wenzelm@44159
    49
  val header: Parser[Thy_Header] =
wenzelm@34169
    50
  {
wenzelm@43611
    51
    val file_name = atom("file name", _.is_name)
wenzelm@34169
    52
wenzelm@48864
    53
    val opt_files =
wenzelm@48864
    54
      keyword("(") ~! (rep1sep(name, keyword(",")) <~ keyword(")")) ^^ { case _ ~ x => x } |
wenzelm@48864
    55
      success(Nil)
wenzelm@48864
    56
    val keyword_spec =
wenzelm@48864
    57
      atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
wenzelm@48864
    58
      { case x ~ y ~ z => ((x, y), z) }
wenzelm@48864
    59
wenzelm@46938
    60
    val keyword_decl =
wenzelm@50128
    61
      rep1(string) ~
wenzelm@50128
    62
      opt(keyword("::") ~! keyword_spec ^^ { case _ ~ x => x }) ~
wenzelm@50128
    63
      opt(keyword("==") ~! name ^^ { case _ ~ x => x }) ^^
wenzelm@50128
    64
      { case xs ~ y ~ z => xs.map((_, y, z)) }
wenzelm@46939
    65
    val keyword_decls =
wenzelm@46938
    66
      keyword_decl ~ rep(keyword(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
wenzelm@46939
    67
      { case xs ~ yss => (xs :: yss).flatten }
wenzelm@46938
    68
wenzelm@34169
    69
    val file =
wenzelm@44185
    70
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
wenzelm@44185
    71
      file_name ^^ (x => (x, true))
wenzelm@34169
    72
wenzelm@34169
    73
    val args =
wenzelm@46938
    74
      theory_name ~
wenzelm@56823
    75
      (opt(keyword(IMPORTS) ~! (rep1(theory_xname))) ^^
wenzelm@56823
    76
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@56823
    77
      (opt(keyword(KEYWORDS) ~! keyword_decls) ^^
wenzelm@56823
    78
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@46938
    79
      keyword(BEGIN) ^^
wenzelm@51294
    80
      { case x ~ ys ~ zs ~ _ => Thy_Header(x, ys, zs) }
wenzelm@34169
    81
wenzelm@58868
    82
    val heading =
wenzelm@58868
    83
      (keyword(HEADER) |
wenzelm@58868
    84
        keyword(CHAPTER) |
wenzelm@58868
    85
        keyword(SECTION) |
wenzelm@58868
    86
        keyword(SUBSECTION) |
wenzelm@58868
    87
        keyword(SUBSUBSECTION)) ~
wenzelm@58868
    88
      tags ~! document_source
wenzelm@58868
    89
wenzelm@58868
    90
    (rep(heading) ~ keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    91
  }
wenzelm@34169
    92
wenzelm@34169
    93
wenzelm@34190
    94
  /* read -- lazy scanning */
wenzelm@34169
    95
wenzelm@44159
    96
  def read(reader: Reader[Char]): Thy_Header =
wenzelm@34169
    97
  {
wenzelm@55494
    98
    val token = Token.Parsers.token(lexicon, _ => false)
wenzelm@36956
    99
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
   100
wenzelm@43611
   101
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
   102
    {
wenzelm@34169
   103
      token(in) match {
wenzelm@55494
   104
        case Token.Parsers.Success(tok, rest) =>
wenzelm@34169
   105
          toks += tok
wenzelm@43611
   106
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
   107
        case _ =>
wenzelm@34169
   108
      }
wenzelm@34169
   109
    }
wenzelm@43611
   110
    scan_to_begin(reader)
wenzelm@34190
   111
wenzelm@36956
   112
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
   113
      case Success(result, _) => result
wenzelm@34190
   114
      case bad => error(bad.toString)
wenzelm@34190
   115
    }
wenzelm@34169
   116
  }
wenzelm@43611
   117
wenzelm@44159
   118
  def read(source: CharSequence): Thy_Header =
wenzelm@43646
   119
    read(new CharSequenceReader(source))
wenzelm@43646
   120
wenzelm@48706
   121
wenzelm@48706
   122
  /* keywords */
wenzelm@48706
   123
wenzelm@50128
   124
  type Keywords = List[(String, Option[((String, List[String]), List[String])], Option[String])]
wenzelm@34169
   125
}
wenzelm@44159
   126
wenzelm@44159
   127
wenzelm@44185
   128
sealed case class Thy_Header(
wenzelm@50128
   129
  name: String,
wenzelm@50128
   130
  imports: List[String],
wenzelm@51294
   131
  keywords: Thy_Header.Keywords)
wenzelm@44159
   132
{
wenzelm@44159
   133
  def map(f: String => String): Thy_Header =
wenzelm@51294
   134
    Thy_Header(f(name), imports.map(f), keywords)
wenzelm@56823
   135
wenzelm@56823
   136
  def decode_symbols: Thy_Header =
wenzelm@56823
   137
  {
wenzelm@56823
   138
    val f = Symbol.decode _
wenzelm@56823
   139
    Thy_Header(f(name), imports.map(f),
wenzelm@56823
   140
      keywords.map({ case (a, b, c) =>
wenzelm@56823
   141
        (f(a), b.map({ case ((x, y), z) => ((f(x), y.map(f)), z.map(f))  }), c.map(f)) }))
wenzelm@56823
   142
  }
wenzelm@44159
   143
}
wenzelm@44159
   144