| author | wenzelm | 
| Wed, 17 Jan 2018 14:40:18 +0100 | |
| changeset 67460 | dfc93f2b01ea | 
| parent 67090 | 0ec94bb9cec4 | 
| child 69913 | ca515cf61651 | 
| permissions | -rw-r--r-- | 
| 36947 | 1 | /* Title: Pure/Isar/keyword.scala | 
| 29449 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 2 | Author: Makarius | 
| 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 3 | |
| 58900 | 4 | Isar keyword classification. | 
| 29449 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 5 | */ | 
| 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 6 | |
| 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 7 | package isabelle | 
| 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 8 | |
| 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 9 | |
| 36947 | 10 | object Keyword | 
| 32450 | 11 | {
 | 
| 58900 | 12 | /** keyword classification **/ | 
| 13 | ||
| 36681 | 14 | /* kinds */ | 
| 15 | ||
| 29449 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 16 | val DIAG = "diag" | 
| 58999 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58928diff
changeset | 17 | val DOCUMENT_HEADING = "document_heading" | 
| 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58928diff
changeset | 18 | val DOCUMENT_BODY = "document_body" | 
| 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58928diff
changeset | 19 | val DOCUMENT_RAW = "document_raw" | 
| 46967 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 20 | val THY_BEGIN = "thy_begin" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 21 | val THY_END = "thy_end" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 22 | val THY_DECL = "thy_decl" | 
| 58800 
bfed1c26caed
explicit keyword category for commands that may start a block;
 wenzelm parents: 
57837diff
changeset | 23 | val THY_DECL_BLOCK = "thy_decl_block" | 
| 48867 
e9beabf045ab
some support for inlining file content into outer syntax token language;
 wenzelm parents: 
46969diff
changeset | 24 | val THY_LOAD = "thy_load" | 
| 46967 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 25 | val THY_GOAL = "thy_goal" | 
| 29449 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 26 | val QED = "qed" | 
| 53571 
e58ca0311c0f
more explicit indication of 'done' as proof script element;
 wenzelm parents: 
53371diff
changeset | 27 | val QED_SCRIPT = "qed_script" | 
| 46967 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 28 | val QED_BLOCK = "qed_block" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 29 | val QED_GLOBAL = "qed_global" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 30 | val PRF_GOAL = "prf_goal" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 31 | val PRF_BLOCK = "prf_block" | 
| 60694 
b3fa4a8cdb5f
clarified text folds: proof ... qed counts as extra block;
 wenzelm parents: 
60692diff
changeset | 32 | val NEXT_BLOCK = "next_block" | 
| 46967 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 33 | val PRF_OPEN = "prf_open" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 34 | val PRF_CLOSE = "prf_close" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 35 | val PRF_CHAIN = "prf_chain" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 36 | val PRF_DECL = "prf_decl" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 37 | val PRF_ASM = "prf_asm" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 38 | val PRF_ASM_GOAL = "prf_asm_goal" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 39 | val PRF_SCRIPT = "prf_script" | 
| 60624 | 40 | val PRF_SCRIPT_GOAL = "prf_script_goal" | 
| 41 | val PRF_SCRIPT_ASM_GOAL = "prf_script_asm_goal" | |
| 63441 | 42 | |
| 43 | val BEFORE_COMMAND = "before_command" | |
| 63430 | 44 | val QUASI_COMMAND = "quasi_command" | 
| 29449 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 45 | |
| 36681 | 46 | |
| 59700 | 47 | /* command categories */ | 
| 36681 | 48 | |
| 58999 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58928diff
changeset | 49 | val vacous = Set(DIAG, DOCUMENT_HEADING, DOCUMENT_BODY, DOCUMENT_RAW) | 
| 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58928diff
changeset | 50 | |
| 57835 | 51 | val diag = Set(DIAG) | 
| 52 | ||
| 58999 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58928diff
changeset | 53 | val document_heading = Set(DOCUMENT_HEADING) | 
| 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58928diff
changeset | 54 | val document_body = Set(DOCUMENT_BODY) | 
| 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58928diff
changeset | 55 | val document_raw = Set(DOCUMENT_RAW) | 
| 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58928diff
changeset | 56 | val document = Set(DOCUMENT_HEADING, DOCUMENT_BODY, DOCUMENT_RAW) | 
| 57835 | 57 | |
| 59700 | 58 | val theory_begin = Set(THY_BEGIN) | 
| 59 | val theory_end = Set(THY_END) | |
| 60 | ||
| 59701 | 61 | val theory_load = Set(THY_LOAD) | 
| 62 | ||
| 58868 
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
 wenzelm parents: 
58853diff
changeset | 63 | val theory = Set(THY_BEGIN, THY_END, THY_LOAD, THY_DECL, THY_DECL_BLOCK, THY_GOAL) | 
| 57837 | 64 | |
| 58800 
bfed1c26caed
explicit keyword category for commands that may start a block;
 wenzelm parents: 
57837diff
changeset | 65 | val theory_block = Set(THY_BEGIN, THY_DECL_BLOCK) | 
| 
bfed1c26caed
explicit keyword category for commands that may start a block;
 wenzelm parents: 
57837diff
changeset | 66 | |
| 58868 
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
 wenzelm parents: 
58853diff
changeset | 67 | val theory_body = Set(THY_LOAD, THY_DECL, THY_DECL_BLOCK, THY_GOAL) | 
| 57835 | 68 | |
| 63479 | 69 | val prf_script = Set(PRF_SCRIPT) | 
| 70 | ||
| 40456 | 71 | val proof = | 
| 60694 
b3fa4a8cdb5f
clarified text folds: proof ... qed counts as extra block;
 wenzelm parents: 
60692diff
changeset | 72 | Set(QED, QED_SCRIPT, QED_BLOCK, QED_GLOBAL, PRF_GOAL, PRF_BLOCK, NEXT_BLOCK, PRF_OPEN, | 
| 
b3fa4a8cdb5f
clarified text folds: proof ... qed counts as extra block;
 wenzelm parents: 
60692diff
changeset | 73 | PRF_CLOSE, PRF_CHAIN, PRF_DECL, PRF_ASM, PRF_ASM_GOAL, PRF_SCRIPT, PRF_SCRIPT_GOAL, | 
| 60624 | 74 | PRF_SCRIPT_ASM_GOAL) | 
| 57835 | 75 | |
| 76 | val proof_body = | |
| 60694 
b3fa4a8cdb5f
clarified text folds: proof ... qed counts as extra block;
 wenzelm parents: 
60692diff
changeset | 77 | Set(DIAG, DOCUMENT_HEADING, DOCUMENT_BODY, DOCUMENT_RAW, PRF_BLOCK, NEXT_BLOCK, PRF_OPEN, | 
| 
b3fa4a8cdb5f
clarified text folds: proof ... qed counts as extra block;
 wenzelm parents: 
60692diff
changeset | 78 | PRF_CLOSE, PRF_CHAIN, PRF_DECL, PRF_ASM, PRF_ASM_GOAL, PRF_SCRIPT, PRF_SCRIPT_GOAL, | 
| 60624 | 79 | PRF_SCRIPT_ASM_GOAL) | 
| 57835 | 80 | |
| 81 | val theory_goal = Set(THY_GOAL) | |
| 60624 | 82 | val proof_goal = Set(PRF_GOAL, PRF_ASM_GOAL, PRF_SCRIPT_GOAL, PRF_SCRIPT_ASM_GOAL) | 
| 57835 | 83 | val qed = Set(QED, QED_SCRIPT, QED_BLOCK) | 
| 84 | val qed_global = Set(QED_GLOBAL) | |
| 58900 | 85 | |
| 60692 | 86 | val proof_open = proof_goal + PRF_OPEN | 
| 87 | val proof_close = qed + PRF_CLOSE | |
| 63428 
005b490f0ce2
indentation in reminiscence to Proof General (see proof-indent.el);
 wenzelm parents: 
63424diff
changeset | 88 | val proof_enclose = Set(PRF_BLOCK, NEXT_BLOCK, QED_BLOCK, PRF_CLOSE) | 
| 60692 | 89 | |
| 63603 | 90 | val close_structure = Set(NEXT_BLOCK, QED_BLOCK, PRF_CLOSE, THY_END) | 
| 91 | ||
| 58900 | 92 | |
| 93 | ||
| 94 | /** keyword tables **/ | |
| 95 | ||
| 65384 | 96 | object Spec | 
| 97 |   {
 | |
| 98 |     val none: Spec = Spec("")
 | |
| 99 | } | |
| 100 | sealed case class Spec(kind: String, exts: List[String] = Nil, tags: List[String] = Nil) | |
| 101 |   {
 | |
| 102 | def is_none: Boolean = kind == "" | |
| 65385 | 103 | |
| 104 | override def toString: String = | |
| 105 | kind + | |
| 106 |         (if (exts.isEmpty) "" else " (" + commas_quote(exts) + ")") +
 | |
| 107 |         (if (tags.isEmpty) "" else tags.map(quote).mkString(" % ", " % ", ""))
 | |
| 65384 | 108 | } | 
| 63429 | 109 | |
| 58900 | 110 | object Keywords | 
| 111 |   {
 | |
| 112 | def empty: Keywords = new Keywords() | |
| 113 | } | |
| 114 | ||
| 115 | class Keywords private( | |
| 63441 | 116 | val kinds: Map[String, String] = Map.empty, | 
| 117 | val load_commands: Map[String, List[String]] = Map.empty) | |
| 58900 | 118 |   {
 | 
| 58901 | 119 | override def toString: String = | 
| 120 |     {
 | |
| 63441 | 121 | val entries = | 
| 122 |         for ((name, kind) <- kinds.toList.sortBy(_._1)) yield {
 | |
| 123 | val exts = load_commands.getOrElse(name, Nil) | |
| 124 | val kind_decl = | |
| 125 | if (kind == "") "" | |
| 126 |             else " :: " + quote(kind) + (if (exts.isEmpty) "" else " (" + commas_quote(exts) + ")")
 | |
| 127 | quote(name) + kind_decl | |
| 63430 | 128 | } | 
| 63441 | 129 |       entries.mkString("keywords\n  ", " and\n  ", "")
 | 
| 58900 | 130 | } | 
| 131 | ||
| 132 | ||
| 59073 | 133 | /* merge */ | 
| 134 | ||
| 67090 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 135 | def is_empty: Boolean = kinds.isEmpty | 
| 59073 | 136 | |
| 137 | def ++ (other: Keywords): Keywords = | |
| 138 | if (this eq other) this | |
| 139 | else if (is_empty) other | |
| 140 |       else {
 | |
| 63441 | 141 | val kinds1 = | 
| 142 | if (kinds eq other.kinds) kinds | |
| 143 | else if (kinds.isEmpty) other.kinds | |
| 144 |           else (kinds /: other.kinds) { case (m, e) => if (m.isDefinedAt(e._1)) m else m + e }
 | |
| 145 | val load_commands1 = | |
| 146 | if (load_commands eq other.load_commands) load_commands | |
| 147 | else if (load_commands.isEmpty) other.load_commands | |
| 148 | else | |
| 149 |             (load_commands /: other.load_commands) {
 | |
| 150 | case (m, e) => if (m.isDefinedAt(e._1)) m else m + e } | |
| 67090 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 151 | new Keywords(kinds1, load_commands1) | 
| 59073 | 152 | } | 
| 153 | ||
| 154 | ||
| 58902 | 155 | /* add keywords */ | 
| 156 | ||
| 63441 | 157 | def + (name: String, kind: String = "", exts: List[String] = Nil): Keywords = | 
| 158 |     {
 | |
| 159 | val kinds1 = kinds + (name -> kind) | |
| 160 | val load_commands1 = | |
| 66919 | 161 |         if (kind == THY_LOAD) {
 | 
| 162 | if (!Symbol.iterator(name).forall(Symbol.is_ascii(_))) | |
| 163 |             error("Bad theory load command " + quote(name))
 | |
| 164 | load_commands + (name -> exts) | |
| 165 | } | |
| 63441 | 166 | else load_commands | 
| 67090 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 167 | new Keywords(kinds1, load_commands1) | 
| 63441 | 168 | } | 
| 58902 | 169 | |
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 170 | def add_keywords(header: Thy_Header.Keywords): Keywords = | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 171 |       (this /: header) {
 | 
| 65384 | 172 | case (keywords, (name, spec)) => | 
| 173 | if (spec.is_none) | |
| 174 | keywords + Symbol.decode(name) + Symbol.encode(name) | |
| 175 | else | |
| 176 | keywords + | |
| 177 | (Symbol.decode(name), spec.kind, spec.exts) + | |
| 178 | (Symbol.encode(name), spec.kind, spec.exts) | |
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 179 | } | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 180 | |
| 58902 | 181 | |
| 58901 | 182 | /* command kind */ | 
| 58900 | 183 | |
| 63424 | 184 | def is_command(token: Token, check_kind: String => Boolean): Boolean = | 
| 185 | token.is_command && | |
| 63441 | 186 |         (kinds.get(token.source) match { case Some(k) => check_kind(k) case None => false })
 | 
| 187 | ||
| 188 | def is_before_command(token: Token): Boolean = | |
| 189 | token.is_keyword && kinds.get(token.source) == Some(BEFORE_COMMAND) | |
| 59701 | 190 | |
| 63430 | 191 | def is_quasi_command(token: Token): Boolean = | 
| 63441 | 192 | token.is_keyword && kinds.get(token.source) == Some(QUASI_COMMAND) | 
| 63430 | 193 | |
| 63809 | 194 | def is_indent_command(token: Token): Boolean = | 
| 195 | token.is_begin_or_command || is_quasi_command(token) | |
| 196 | ||
| 58900 | 197 | |
| 198 | /* load commands */ | |
| 199 | ||
| 200 | def load_commands_in(text: String): Boolean = | |
| 201 |       load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })
 | |
| 67090 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 202 | |
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 203 | |
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 204 | /* lexicons */ | 
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 205 | |
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 206 | private def make_lexicon(is_minor: Boolean): Scan.Lexicon = | 
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 207 | (Scan.Lexicon.empty /: kinds)( | 
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 208 |         {
 | 
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 209 | case (lex, (name, kind)) => | 
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 210 | if ((kind == "" || kind == BEFORE_COMMAND || kind == QUASI_COMMAND) == is_minor) | 
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 211 | lex + name | 
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 212 | else lex | 
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 213 | }) | 
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 214 | |
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 215 | lazy val minor: Scan.Lexicon = make_lexicon(true) | 
| 
0ec94bb9cec4
clarified lazy lexicons within Keywords: measurable speedup of Sessions.deps;
 wenzelm parents: 
66919diff
changeset | 216 | lazy val major: Scan.Lexicon = make_lexicon(false) | 
| 58900 | 217 | } | 
| 29449 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 218 | } |