src/Pure/Thy/thy_header.scala
author wenzelm
Sat Jul 02 23:31:07 2011 +0200 (2011-07-02)
changeset 43646 598b2c6ce13f
parent 43611 21a57a0c5f25
child 43648 e32de528b5ef
permissions -rw-r--r--
Thy_Header.read convenience;
wenzelm@28495
     1
/*  Title:      Pure/Thy/thy_header.scala
wenzelm@28495
     2
    Author:     Makarius
wenzelm@28495
     3
wenzelm@34169
     4
Theory headers -- independent of outer syntax.
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@34188
    15
import java.io.File
wenzelm@34188
    16
wenzelm@34169
    17
wenzelm@32466
    18
object Thy_Header
wenzelm@32450
    19
{
wenzelm@28495
    20
  val HEADER = "header"
wenzelm@28495
    21
  val THEORY = "theory"
wenzelm@28495
    22
  val IMPORTS = "imports"
wenzelm@28495
    23
  val USES = "uses"
wenzelm@28495
    24
  val BEGIN = "begin"
wenzelm@28495
    25
wenzelm@34169
    26
  val lexicon = Scan.Lexicon("%", "(", ")", ";", BEGIN, HEADER, IMPORTS, THEORY, USES)
wenzelm@34190
    27
wenzelm@34300
    28
  final case class Header(val name: String, val imports: List[String], val uses: List[String])
wenzelm@43611
    29
  {
wenzelm@43611
    30
    def decode_permissive_utf8: Header =
wenzelm@43611
    31
      Header(Standard_System.decode_permissive_utf8(name),
wenzelm@43611
    32
        imports.map(Standard_System.decode_permissive_utf8),
wenzelm@43611
    33
        uses.map(Standard_System.decode_permissive_utf8))
wenzelm@43611
    34
  }
wenzelm@38149
    35
wenzelm@38149
    36
wenzelm@38149
    37
  /* file name */
wenzelm@38149
    38
wenzelm@38149
    39
  val Thy_Path1 = new Regex("([^/]*)\\.thy")
wenzelm@41535
    40
  val Thy_Path2 = new Regex("(.*)/([^/]*)\\.thy")
wenzelm@38149
    41
wenzelm@39630
    42
  def split_thy_path(path: String): Option[(String, String)] =
wenzelm@38149
    43
    path match {
wenzelm@39630
    44
      case Thy_Path1(name) => Some(("", name))
wenzelm@39630
    45
      case Thy_Path2(dir, name) => Some((dir, name))
wenzelm@39630
    46
      case _ => None
wenzelm@38149
    47
    }
wenzelm@28495
    48
}
wenzelm@34169
    49
wenzelm@34169
    50
wenzelm@36948
    51
class Thy_Header(symbols: Symbol.Interpretation) extends Parse.Parser
wenzelm@34169
    52
{
wenzelm@34169
    53
  import Thy_Header._
wenzelm@34169
    54
wenzelm@34169
    55
wenzelm@34169
    56
  /* header */
wenzelm@34169
    57
wenzelm@34190
    58
  val header: Parser[Header] =
wenzelm@34169
    59
  {
wenzelm@43611
    60
    val file_name = atom("file name", _.is_name)
wenzelm@43611
    61
    val theory_name = atom("theory name", _.is_name)
wenzelm@34169
    62
wenzelm@34169
    63
    val file =
wenzelm@34169
    64
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name
wenzelm@34169
    65
wenzelm@34169
    66
    val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
wenzelm@34169
    67
wenzelm@34169
    68
    val args =
wenzelm@34169
    69
      theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
wenzelm@34190
    70
        { case x ~ (_ ~ (ys ~ zs ~ _)) => Header(x, ys, zs) }
wenzelm@34169
    71
wenzelm@34169
    72
    (keyword(HEADER) ~ tags) ~!
wenzelm@34169
    73
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
wenzelm@34169
    74
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    75
  }
wenzelm@34169
    76
wenzelm@34169
    77
wenzelm@34190
    78
  /* read -- lazy scanning */
wenzelm@34169
    79
wenzelm@43611
    80
  def read(reader: Reader[Char]): Header =
wenzelm@34169
    81
  {
wenzelm@34169
    82
    val token = lexicon.token(symbols, _ => false)
wenzelm@36956
    83
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
    84
wenzelm@43611
    85
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
    86
    {
wenzelm@34169
    87
      token(in) match {
wenzelm@34169
    88
        case lexicon.Success(tok, rest) =>
wenzelm@34169
    89
          toks += tok
wenzelm@43611
    90
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
    91
        case _ =>
wenzelm@34169
    92
      }
wenzelm@34169
    93
    }
wenzelm@43611
    94
    scan_to_begin(reader)
wenzelm@34190
    95
wenzelm@36956
    96
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
    97
      case Success(result, _) => result
wenzelm@34190
    98
      case bad => error(bad.toString)
wenzelm@34190
    99
    }
wenzelm@34169
   100
  }
wenzelm@43611
   101
wenzelm@43646
   102
  def read(source: CharSequence): Header =
wenzelm@43646
   103
    read(new CharSequenceReader(source))
wenzelm@43646
   104
wenzelm@43611
   105
  def read(file: File): Header =
wenzelm@43611
   106
  {
wenzelm@43611
   107
    val reader = Scan.byte_reader(file)
wenzelm@43611
   108
    try { read(reader).decode_permissive_utf8 }
wenzelm@43611
   109
    finally { reader.close }
wenzelm@43611
   110
  }
wenzelm@34169
   111
}