src/Pure/Thy/thy_header.scala
author wenzelm
Fri Feb 14 15:42:27 2014 +0100 (2014-02-14)
changeset 55492 28d4db6c6e79
parent 51627 589daaf48dba
child 55494 009b71c1ed23
permissions -rw-r--r--
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
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@50128
    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@48638
    70
      (opt(keyword(IMPORTS) ~! (rep1(theory_name))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@46939
    71
      (opt(keyword(KEYWORDS) ~! keyword_decls) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@46938
    72
      keyword(BEGIN) ^^
wenzelm@51294
    73
      { case x ~ ys ~ zs ~ _ => Thy_Header(x, ys, zs) }
wenzelm@34169
    74
wenzelm@34169
    75
    (keyword(HEADER) ~ tags) ~!
wenzelm@51627
    76
      ((document_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
wenzelm@34169
    77
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    78
  }
wenzelm@34169
    79
wenzelm@34169
    80
wenzelm@34190
    81
  /* read -- lazy scanning */
wenzelm@34169
    82
wenzelm@44159
    83
  def read(reader: Reader[Char]): Thy_Header =
wenzelm@34169
    84
  {
wenzelm@55492
    85
    val token = Scan.Parsers.token(lexicon, _ => false)
wenzelm@36956
    86
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
    87
wenzelm@43611
    88
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
    89
    {
wenzelm@34169
    90
      token(in) match {
wenzelm@55492
    91
        case Scan.Parsers.Success(tok, rest) =>
wenzelm@34169
    92
          toks += tok
wenzelm@43611
    93
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
    94
        case _ =>
wenzelm@34169
    95
      }
wenzelm@34169
    96
    }
wenzelm@43611
    97
    scan_to_begin(reader)
wenzelm@34190
    98
wenzelm@36956
    99
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
   100
      case Success(result, _) => result
wenzelm@34190
   101
      case bad => error(bad.toString)
wenzelm@34190
   102
    }
wenzelm@34169
   103
  }
wenzelm@43611
   104
wenzelm@44159
   105
  def read(source: CharSequence): Thy_Header =
wenzelm@43646
   106
    read(new CharSequenceReader(source))
wenzelm@43646
   107
wenzelm@48706
   108
wenzelm@48706
   109
  /* keywords */
wenzelm@48706
   110
wenzelm@50128
   111
  type Keywords = List[(String, Option[((String, List[String]), List[String])], Option[String])]
wenzelm@34169
   112
}
wenzelm@44159
   113
wenzelm@44159
   114
wenzelm@44185
   115
sealed case class Thy_Header(
wenzelm@50128
   116
  name: String,
wenzelm@50128
   117
  imports: List[String],
wenzelm@51294
   118
  keywords: Thy_Header.Keywords)
wenzelm@44159
   119
{
wenzelm@44159
   120
  def map(f: String => String): Thy_Header =
wenzelm@51294
   121
    Thy_Header(f(name), imports.map(f), keywords)
wenzelm@44159
   122
}
wenzelm@44159
   123