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