src/Pure/Thy/thy_header.scala
author wenzelm
Sat May 15 22:15:57 2010 +0200 (2010-05-15)
changeset 36948 d2cdad45fd14
parent 34300 3f2e25dc99ab
child 36956 21be4832c362
permissions -rw-r--r--
renamed Outer_Parse to Parse (in Scala);
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@34169
    12
wenzelm@34188
    13
import java.io.File
wenzelm@34188
    14
wenzelm@34169
    15
wenzelm@32466
    16
object Thy_Header
wenzelm@32450
    17
{
wenzelm@28495
    18
  val HEADER = "header"
wenzelm@28495
    19
  val THEORY = "theory"
wenzelm@28495
    20
  val IMPORTS = "imports"
wenzelm@28495
    21
  val USES = "uses"
wenzelm@28495
    22
  val BEGIN = "begin"
wenzelm@28495
    23
wenzelm@34169
    24
  val lexicon = Scan.Lexicon("%", "(", ")", ";", BEGIN, HEADER, IMPORTS, THEORY, USES)
wenzelm@34190
    25
wenzelm@34300
    26
  final case class Header(val name: String, val imports: List[String], val uses: List[String])
wenzelm@28495
    27
}
wenzelm@34169
    28
wenzelm@34169
    29
wenzelm@36948
    30
class Thy_Header(symbols: Symbol.Interpretation) extends Parse.Parser
wenzelm@34169
    31
{
wenzelm@34169
    32
  import Thy_Header._
wenzelm@34169
    33
wenzelm@34169
    34
wenzelm@34169
    35
  /* header */
wenzelm@34169
    36
wenzelm@34190
    37
  val header: Parser[Header] =
wenzelm@34169
    38
  {
wenzelm@34201
    39
    val file_name = atom("file name", _.is_name) ^^ Standard_System.decode_permissive_utf8
wenzelm@34201
    40
    val theory_name = atom("theory name", _.is_name) ^^ Standard_System.decode_permissive_utf8
wenzelm@34169
    41
wenzelm@34169
    42
    val file =
wenzelm@34169
    43
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name
wenzelm@34169
    44
wenzelm@34169
    45
    val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
wenzelm@34169
    46
wenzelm@34169
    47
    val args =
wenzelm@34169
    48
      theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
wenzelm@34190
    49
        { case x ~ (_ ~ (ys ~ zs ~ _)) => Header(x, ys, zs) }
wenzelm@34169
    50
wenzelm@34169
    51
    (keyword(HEADER) ~ tags) ~!
wenzelm@34169
    52
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
wenzelm@34169
    53
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    54
  }
wenzelm@34169
    55
wenzelm@34169
    56
wenzelm@34190
    57
  /* read -- lazy scanning */
wenzelm@34169
    58
wenzelm@34190
    59
  def read(file: File): Header =
wenzelm@34169
    60
  {
wenzelm@34169
    61
    val token = lexicon.token(symbols, _ => false)
wenzelm@34169
    62
    val toks = new mutable.ListBuffer[Outer_Lex.Token]
wenzelm@34188
    63
wenzelm@34188
    64
    def scan_to_begin(in: Reader[Char])
wenzelm@34169
    65
    {
wenzelm@34169
    66
      token(in) match {
wenzelm@34169
    67
        case lexicon.Success(tok, rest) =>
wenzelm@34169
    68
          toks += tok
wenzelm@34169
    69
          if (!(tok.kind == Outer_Lex.Token_Kind.KEYWORD && tok.content == BEGIN))
wenzelm@34188
    70
            scan_to_begin(rest)
wenzelm@34169
    71
        case _ =>
wenzelm@34169
    72
      }
wenzelm@34169
    73
    }
wenzelm@34188
    74
    val reader = Scan.byte_reader(file)
wenzelm@34188
    75
    try { scan_to_begin(reader) } finally { reader.close }
wenzelm@34190
    76
wenzelm@34190
    77
    parse(commit(header), Outer_Lex.reader(toks.toList)) match {
wenzelm@34190
    78
      case Success(result, _) => result
wenzelm@34190
    79
      case bad => error(bad.toString)
wenzelm@34190
    80
    }
wenzelm@34169
    81
  }
wenzelm@34169
    82
}