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