src/Pure/Thy/thy_header.scala
changeset 46938 cda018294515
parent 46737 09ab89658a5d
child 46939 5b67ac48b384
equal deleted inserted replaced
46937:efb98d27dc1a 46938:cda018294515
    18 object Thy_Header extends Parse.Parser
    18 object Thy_Header extends Parse.Parser
    19 {
    19 {
    20   val HEADER = "header"
    20   val HEADER = "header"
    21   val THEORY = "theory"
    21   val THEORY = "theory"
    22   val IMPORTS = "imports"
    22   val IMPORTS = "imports"
       
    23   val KEYWORDS = "keywords"
       
    24   val AND = "and"
    23   val USES = "uses"
    25   val USES = "uses"
    24   val BEGIN = "begin"
    26   val BEGIN = "begin"
    25 
    27 
    26   private val lexicon = Scan.Lexicon("%", "(", ")", ";", BEGIN, HEADER, IMPORTS, THEORY, USES)
    28   private val lexicon =
       
    29     Scan.Lexicon("%", "(", ")", "::", ";", AND, BEGIN, HEADER, IMPORTS, KEYWORDS, THEORY, USES)
    27 
    30 
    28 
    31 
    29   /* theory file name */
    32   /* theory file name */
    30 
    33 
    31   private val Base_Name = new Regex(""".*?([^/\\:]+)""")
    34   private val Base_Name = new Regex(""".*?([^/\\:]+)""")
    43   val header: Parser[Thy_Header] =
    46   val header: Parser[Thy_Header] =
    44   {
    47   {
    45     val file_name = atom("file name", _.is_name)
    48     val file_name = atom("file name", _.is_name)
    46     val theory_name = atom("theory name", _.is_name)
    49     val theory_name = atom("theory name", _.is_name)
    47 
    50 
       
    51     val keyword_kind =
       
    52       atom("outer syntax keyword kind", _.is_name) ~ tags ^^ { case x ~ y => (x, y) }
       
    53     val keyword_decl =
       
    54       name ~ opt(keyword("::") ~! keyword_kind ^^ { case _ ~ x => x }) ^^
       
    55       { case x ~ y => (x, y) }
       
    56     val keywords =
       
    57       keyword_decl ~ rep(keyword(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
       
    58       { case x ~ ys => x :: ys }
       
    59 
    48     val file =
    60     val file =
    49       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
    61       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
    50       file_name ^^ (x => (x, true))
    62       file_name ^^ (x => (x, true))
    51 
    63 
    52     val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
       
    53 
       
    54     val args =
    64     val args =
    55       theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
    65       theory_name ~
    56         { case x ~ (_ ~ (ys ~ zs ~ _)) => Thy_Header(x, ys, zs) }
    66       (keyword(IMPORTS) ~! (rep1(theory_name)) ^^ { case _ ~ xs => xs }) ~
       
    67       (opt(keyword(KEYWORDS) ~! keywords) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
       
    68       (opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
       
    69       keyword(BEGIN) ^^
       
    70       { case x ~ ys ~ zs ~ ws ~ _ => Thy_Header(x, ys, zs, ws) }
    57 
    71 
    58     (keyword(HEADER) ~ tags) ~!
    72     (keyword(HEADER) ~ tags) ~!
    59       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    73       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    60     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    74     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    61   }
    75   }
    96   }
   110   }
    97 }
   111 }
    98 
   112 
    99 
   113 
   100 sealed case class Thy_Header(
   114 sealed case class Thy_Header(
   101   val name: String, val imports: List[String], val uses: List[(String, Boolean)])
   115   name: String, imports: List[String],
       
   116   keywords: List[(String, Option[(String, List[String])])],
       
   117   uses: List[(String, Boolean)])
   102 {
   118 {
   103   def map(f: String => String): Thy_Header =
   119   def map(f: String => String): Thy_Header =
   104     Thy_Header(f(name), imports.map(f), uses.map(p => (f(p._1), p._2)))
   120     Thy_Header(f(name), imports.map(f), keywords, uses.map(p => (f(p._1), p._2)))
   105 }
   121 }
   106 
   122