src/Pure/Thy/thy_header.scala
author wenzelm
Tue Dec 22 21:48:17 2009 +0100 (2009-12-22)
changeset 34169 7501b2910900
parent 32466 a393b7e2a2f8
child 34188 fbfc18be1f8c
permissions -rw-r--r--
basic setup for header scanning/parsing;
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@34169
    13
wenzelm@32466
    14
object Thy_Header
wenzelm@32450
    15
{
wenzelm@28495
    16
  val HEADER = "header"
wenzelm@28495
    17
  val THEORY = "theory"
wenzelm@28495
    18
  val IMPORTS = "imports"
wenzelm@28495
    19
  val USES = "uses"
wenzelm@28495
    20
  val BEGIN = "begin"
wenzelm@28495
    21
wenzelm@34169
    22
  val lexicon = Scan.Lexicon("%", "(", ")", ";", BEGIN, HEADER, IMPORTS, THEORY, USES)
wenzelm@28495
    23
}
wenzelm@34169
    24
wenzelm@34169
    25
wenzelm@34169
    26
class Thy_Header(symbols: Symbol.Interpretation) extends Outer_Parse.Parser
wenzelm@34169
    27
{
wenzelm@34169
    28
  import Thy_Header._
wenzelm@34169
    29
wenzelm@34169
    30
wenzelm@34169
    31
  /* header */
wenzelm@34169
    32
wenzelm@34169
    33
  val header: Parser[(String, List[String], List[String])] =
wenzelm@34169
    34
  {
wenzelm@34169
    35
    val file_name = atom("file name", _.is_name)
wenzelm@34169
    36
    val theory_name = atom("theory name", _.is_name)
wenzelm@34169
    37
wenzelm@34169
    38
    val file =
wenzelm@34169
    39
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name
wenzelm@34169
    40
wenzelm@34169
    41
    val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
wenzelm@34169
    42
wenzelm@34169
    43
    val args =
wenzelm@34169
    44
      theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
wenzelm@34169
    45
        { case x ~ (_ ~ (ys ~ zs ~ _)) => (x, ys, zs) }
wenzelm@34169
    46
wenzelm@34169
    47
    (keyword(HEADER) ~ tags) ~!
wenzelm@34169
    48
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
wenzelm@34169
    49
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    50
  }
wenzelm@34169
    51
wenzelm@34169
    52
wenzelm@34169
    53
  /* scan -- lazy approximation */
wenzelm@34169
    54
wenzelm@34169
    55
  def scan(text: CharSequence): List[Outer_Lex.Token] =
wenzelm@34169
    56
  {
wenzelm@34169
    57
    val token = lexicon.token(symbols, _ => false)
wenzelm@34169
    58
    val toks = new mutable.ListBuffer[Outer_Lex.Token]
wenzelm@34169
    59
    def scan_until_begin(in: Reader[Char])
wenzelm@34169
    60
    {
wenzelm@34169
    61
      token(in) match {
wenzelm@34169
    62
        case lexicon.Success(tok, rest) =>
wenzelm@34169
    63
          toks += tok
wenzelm@34169
    64
          if (!(tok.kind == Outer_Lex.Token_Kind.KEYWORD && tok.content == BEGIN))
wenzelm@34169
    65
            scan_until_begin(rest)
wenzelm@34169
    66
        case _ =>
wenzelm@34169
    67
      }
wenzelm@34169
    68
    }
wenzelm@34169
    69
    scan_until_begin(new CharSequenceReader(text))
wenzelm@34169
    70
    toks.toList
wenzelm@34169
    71
  }
wenzelm@34169
    72
}