| author | wenzelm | 
| Tue, 20 Dec 2016 10:06:18 +0100 | |
| changeset 64614 | 88211daacf93 | 
| parent 63809 | 56670ab6f55e | 
| child 65384 | 36255c43c64c | 
| 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 | ||
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 96 | type Spec = ((String, List[String]), List[String]) | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 97 | |
| 63429 | 98 |   val no_spec: Spec = (("", Nil), Nil)
 | 
| 63441 | 99 | val before_command_spec: Spec = ((BEFORE_COMMAND, Nil), Nil) | 
| 63430 | 100 | val quasi_command_spec: Spec = ((QUASI_COMMAND, Nil), Nil) | 
| 63429 | 101 | |
| 58900 | 102 | object Keywords | 
| 103 |   {
 | |
| 104 | def empty: Keywords = new Keywords() | |
| 105 | } | |
| 106 | ||
| 107 | class Keywords private( | |
| 108 | val minor: Scan.Lexicon = Scan.Lexicon.empty, | |
| 58901 | 109 | val major: Scan.Lexicon = Scan.Lexicon.empty, | 
| 63441 | 110 | val kinds: Map[String, String] = Map.empty, | 
| 111 | val load_commands: Map[String, List[String]] = Map.empty) | |
| 58900 | 112 |   {
 | 
| 58901 | 113 | override def toString: String = | 
| 114 |     {
 | |
| 63441 | 115 | val entries = | 
| 116 |         for ((name, kind) <- kinds.toList.sortBy(_._1)) yield {
 | |
| 117 | val exts = load_commands.getOrElse(name, Nil) | |
| 118 | val kind_decl = | |
| 119 | if (kind == "") "" | |
| 120 |             else " :: " + quote(kind) + (if (exts.isEmpty) "" else " (" + commas_quote(exts) + ")")
 | |
| 121 | quote(name) + kind_decl | |
| 63430 | 122 | } | 
| 63441 | 123 |       entries.mkString("keywords\n  ", " and\n  ", "")
 | 
| 58900 | 124 | } | 
| 125 | ||
| 126 | ||
| 59073 | 127 | /* merge */ | 
| 128 | ||
| 129 | def is_empty: Boolean = minor.is_empty && major.is_empty | |
| 130 | ||
| 131 | def ++ (other: Keywords): Keywords = | |
| 132 | if (this eq other) this | |
| 133 | else if (is_empty) other | |
| 134 |       else {
 | |
| 135 | val minor1 = minor ++ other.minor | |
| 136 | val major1 = major ++ other.major | |
| 63441 | 137 | val kinds1 = | 
| 138 | if (kinds eq other.kinds) kinds | |
| 139 | else if (kinds.isEmpty) other.kinds | |
| 140 |           else (kinds /: other.kinds) { case (m, e) => if (m.isDefinedAt(e._1)) m else m + e }
 | |
| 141 | val load_commands1 = | |
| 142 | if (load_commands eq other.load_commands) load_commands | |
| 143 | else if (load_commands.isEmpty) other.load_commands | |
| 144 | else | |
| 145 |             (load_commands /: other.load_commands) {
 | |
| 146 | case (m, e) => if (m.isDefinedAt(e._1)) m else m + e } | |
| 147 | new Keywords(minor1, major1, kinds1, load_commands1) | |
| 59073 | 148 | } | 
| 149 | ||
| 150 | ||
| 58902 | 151 | /* add keywords */ | 
| 152 | ||
| 63441 | 153 | def + (name: String, kind: String = "", exts: List[String] = Nil): Keywords = | 
| 154 |     {
 | |
| 155 | val kinds1 = kinds + (name -> kind) | |
| 156 | val (minor1, major1) = | |
| 157 | if (kind == "" || kind == BEFORE_COMMAND || kind == QUASI_COMMAND) | |
| 158 | (minor + name, major) | |
| 159 | else (minor, major + name) | |
| 160 | val load_commands1 = | |
| 161 | if (kind == THY_LOAD) load_commands + (name -> exts) | |
| 162 | else load_commands | |
| 163 | new Keywords(minor1, major1, kinds1, load_commands1) | |
| 164 | } | |
| 58902 | 165 | |
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 166 | 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 | 167 |       (this /: header) {
 | 
| 63579 | 168 | case (keywords, (name, ((kind, exts), _))) => | 
| 63429 | 169 | if (kind == "") keywords + Symbol.decode(name) + Symbol.encode(name) | 
| 63441 | 170 | else keywords + (Symbol.decode(name), kind, exts) + (Symbol.encode(name), kind, exts) | 
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 171 | } | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 172 | |
| 58902 | 173 | |
| 58901 | 174 | /* command kind */ | 
| 58900 | 175 | |
| 63424 | 176 | def is_command(token: Token, check_kind: String => Boolean): Boolean = | 
| 177 | token.is_command && | |
| 63441 | 178 |         (kinds.get(token.source) match { case Some(k) => check_kind(k) case None => false })
 | 
| 179 | ||
| 180 | def is_before_command(token: Token): Boolean = | |
| 181 | token.is_keyword && kinds.get(token.source) == Some(BEFORE_COMMAND) | |
| 59701 | 182 | |
| 63430 | 183 | def is_quasi_command(token: Token): Boolean = | 
| 63441 | 184 | token.is_keyword && kinds.get(token.source) == Some(QUASI_COMMAND) | 
| 63430 | 185 | |
| 63809 | 186 | def is_indent_command(token: Token): Boolean = | 
| 187 | token.is_begin_or_command || is_quasi_command(token) | |
| 188 | ||
| 58900 | 189 | |
| 190 | /* load commands */ | |
| 191 | ||
| 192 | def load_commands_in(text: String): Boolean = | |
| 193 |       load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })
 | |
| 194 | } | |
| 29449 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 195 | } |