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