src/Pure/Thy/thy_header.scala
author wenzelm
Thu Jan 13 17:30:52 2011 +0100 (2011-01-13)
changeset 41535 0112f14d75ec
parent 39630 44181423183a
child 43611 21a57a0c5f25
permissions -rw-r--r--
clarified split_thy_path;
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@34169
    10
import scala.collection.mutable
wenzelm@34169
    11
import scala.util.parsing.input.{Reader, CharSequenceReader}
wenzelm@38149
    12
import scala.util.matching.Regex
wenzelm@34169
    13
wenzelm@34188
    14
import java.io.File
wenzelm@34188
    15
wenzelm@34169
    16
wenzelm@32466
    17
object Thy_Header
wenzelm@32450
    18
{
wenzelm@28495
    19
  val HEADER = "header"
wenzelm@28495
    20
  val THEORY = "theory"
wenzelm@28495
    21
  val IMPORTS = "imports"
wenzelm@28495
    22
  val USES = "uses"
wenzelm@28495
    23
  val BEGIN = "begin"
wenzelm@28495
    24
wenzelm@34169
    25
  val lexicon = Scan.Lexicon("%", "(", ")", ";", BEGIN, HEADER, IMPORTS, THEORY, USES)
wenzelm@34190
    26
wenzelm@34300
    27
  final case class Header(val name: String, val imports: List[String], val uses: List[String])
wenzelm@38149
    28
wenzelm@38149
    29
wenzelm@38149
    30
  /* file name */
wenzelm@38149
    31
wenzelm@38149
    32
  val Thy_Path1 = new Regex("([^/]*)\\.thy")
wenzelm@41535
    33
  val Thy_Path2 = new Regex("(.*)/([^/]*)\\.thy")
wenzelm@38149
    34
wenzelm@39630
    35
  def split_thy_path(path: String): Option[(String, String)] =
wenzelm@38149
    36
    path match {
wenzelm@39630
    37
      case Thy_Path1(name) => Some(("", name))
wenzelm@39630
    38
      case Thy_Path2(dir, name) => Some((dir, name))
wenzelm@39630
    39
      case _ => None
wenzelm@38149
    40
    }
wenzelm@28495
    41
}
wenzelm@34169
    42
wenzelm@34169
    43
wenzelm@36948
    44
class Thy_Header(symbols: Symbol.Interpretation) extends Parse.Parser
wenzelm@34169
    45
{
wenzelm@34169
    46
  import Thy_Header._
wenzelm@34169
    47
wenzelm@34169
    48
wenzelm@34169
    49
  /* header */
wenzelm@34169
    50
wenzelm@34190
    51
  val header: Parser[Header] =
wenzelm@34169
    52
  {
wenzelm@34201
    53
    val file_name = atom("file name", _.is_name) ^^ Standard_System.decode_permissive_utf8
wenzelm@34201
    54
    val theory_name = atom("theory name", _.is_name) ^^ Standard_System.decode_permissive_utf8
wenzelm@34169
    55
wenzelm@34169
    56
    val file =
wenzelm@34169
    57
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name
wenzelm@34169
    58
wenzelm@34169
    59
    val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
wenzelm@34169
    60
wenzelm@34169
    61
    val args =
wenzelm@34169
    62
      theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
wenzelm@34190
    63
        { case x ~ (_ ~ (ys ~ zs ~ _)) => Header(x, ys, zs) }
wenzelm@34169
    64
wenzelm@34169
    65
    (keyword(HEADER) ~ tags) ~!
wenzelm@34169
    66
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
wenzelm@34169
    67
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    68
  }
wenzelm@34169
    69
wenzelm@34169
    70
wenzelm@34190
    71
  /* read -- lazy scanning */
wenzelm@34169
    72
wenzelm@34190
    73
  def read(file: File): Header =
wenzelm@34169
    74
  {
wenzelm@34169
    75
    val token = lexicon.token(symbols, _ => false)
wenzelm@36956
    76
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
    77
wenzelm@34188
    78
    def scan_to_begin(in: Reader[Char])
wenzelm@34169
    79
    {
wenzelm@34169
    80
      token(in) match {
wenzelm@34169
    81
        case lexicon.Success(tok, rest) =>
wenzelm@34169
    82
          toks += tok
wenzelm@36956
    83
          if (!(tok.kind == Token.Kind.KEYWORD && tok.content == BEGIN))
wenzelm@34188
    84
            scan_to_begin(rest)
wenzelm@34169
    85
        case _ =>
wenzelm@34169
    86
      }
wenzelm@34169
    87
    }
wenzelm@34188
    88
    val reader = Scan.byte_reader(file)
wenzelm@34188
    89
    try { scan_to_begin(reader) } finally { reader.close }
wenzelm@34190
    90
wenzelm@36956
    91
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
    92
      case Success(result, _) => result
wenzelm@34190
    93
      case bad => error(bad.toString)
wenzelm@34190
    94
    }
wenzelm@34169
    95
  }
wenzelm@34169
    96
}