| author | kuncar | 
| Tue, 18 Nov 2014 16:19:57 +0100 | |
| changeset 60218 | 7e24e172052e | 
| parent 59701 | 8ab877c91992 | 
| child 60624 | 5b6552e12421 | 
| 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" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 32 | val PRF_OPEN = "prf_open" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 33 | val PRF_CLOSE = "prf_close" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 34 | val PRF_CHAIN = "prf_chain" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 35 | val PRF_DECL = "prf_decl" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 36 | val PRF_ASM = "prf_asm" | 
| 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 37 | val PRF_ASM_GOAL = "prf_asm_goal" | 
| 53371 
47b23c582127
more explicit indication of 'guess' as improper Isar (aka "script") element;
 wenzelm parents: 
51274diff
changeset | 38 | val PRF_ASM_GOAL_SCRIPT = "prf_asm_goal_script" | 
| 46967 
499d9bbd8de9
uniform keyword names within ML/Scala -- produce elisp names via external conversion;
 wenzelm parents: 
46123diff
changeset | 39 | val PRF_SCRIPT = "prf_script" | 
| 29449 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 40 | |
| 36681 | 41 | |
| 59700 | 42 | /* command categories */ | 
| 36681 | 43 | |
| 58999 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58928diff
changeset | 44 | 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 | 45 | |
| 57835 | 46 | val diag = Set(DIAG) | 
| 47 | ||
| 58999 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58928diff
changeset | 48 | 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 | 49 | 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 | 50 | 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 | 51 | val document = Set(DOCUMENT_HEADING, DOCUMENT_BODY, DOCUMENT_RAW) | 
| 57835 | 52 | |
| 59700 | 53 | val theory_begin = Set(THY_BEGIN) | 
| 54 | val theory_end = Set(THY_END) | |
| 55 | ||
| 59701 | 56 | val theory_load = Set(THY_LOAD) | 
| 57 | ||
| 58868 
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
 wenzelm parents: 
58853diff
changeset | 58 | val theory = Set(THY_BEGIN, THY_END, THY_LOAD, THY_DECL, THY_DECL_BLOCK, THY_GOAL) | 
| 57837 | 59 | |
| 58800 
bfed1c26caed
explicit keyword category for commands that may start a block;
 wenzelm parents: 
57837diff
changeset | 60 | val theory_block = Set(THY_BEGIN, THY_DECL_BLOCK) | 
| 
bfed1c26caed
explicit keyword category for commands that may start a block;
 wenzelm parents: 
57837diff
changeset | 61 | |
| 58868 
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
 wenzelm parents: 
58853diff
changeset | 62 | val theory_body = Set(THY_LOAD, THY_DECL, THY_DECL_BLOCK, THY_GOAL) | 
| 57835 | 63 | |
| 40456 | 64 | val proof = | 
| 58868 
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
 wenzelm parents: 
58853diff
changeset | 65 | Set(QED, QED_SCRIPT, QED_BLOCK, QED_GLOBAL, PRF_GOAL, PRF_BLOCK, PRF_OPEN, PRF_CLOSE, | 
| 
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
 wenzelm parents: 
58853diff
changeset | 66 | PRF_CHAIN, PRF_DECL, PRF_ASM, PRF_ASM_GOAL, PRF_ASM_GOAL_SCRIPT, PRF_SCRIPT) | 
| 57835 | 67 | |
| 68 | val proof_body = | |
| 58999 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58928diff
changeset | 69 | Set(DIAG, DOCUMENT_HEADING, DOCUMENT_BODY, DOCUMENT_RAW, PRF_BLOCK, PRF_OPEN, PRF_CLOSE, | 
| 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58928diff
changeset | 70 | PRF_CHAIN, PRF_DECL, PRF_ASM, PRF_ASM_GOAL, PRF_ASM_GOAL_SCRIPT, PRF_SCRIPT) | 
| 57835 | 71 | |
| 72 | val theory_goal = Set(THY_GOAL) | |
| 73 | val proof_goal = Set(PRF_GOAL, PRF_ASM_GOAL, PRF_ASM_GOAL_SCRIPT) | |
| 74 | val qed = Set(QED, QED_SCRIPT, QED_BLOCK) | |
| 75 | val qed_global = Set(QED_GLOBAL) | |
| 58900 | 76 | |
| 77 | ||
| 78 | ||
| 79 | /** keyword tables **/ | |
| 80 | ||
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 81 | 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 | 82 | |
| 58900 | 83 | object Keywords | 
| 84 |   {
 | |
| 85 | def empty: Keywords = new Keywords() | |
| 86 | } | |
| 87 | ||
| 88 | class Keywords private( | |
| 89 | val minor: Scan.Lexicon = Scan.Lexicon.empty, | |
| 58901 | 90 | val major: Scan.Lexicon = Scan.Lexicon.empty, | 
| 59073 | 91 | protected val commands: Map[String, (String, List[String])] = Map.empty) | 
| 58900 | 92 |   {
 | 
| 58901 | 93 | override def toString: String = | 
| 94 |     {
 | |
| 58906 | 95 | val keywords1 = minor.iterator.map(quote(_)).toList | 
| 96 | val keywords2 = | |
| 97 |         for ((name, (kind, files)) <- commands.toList.sortBy(_._1)) yield {
 | |
| 58901 | 98 | quote(name) + " :: " + quote(kind) + | 
| 99 |           (if (files.isEmpty) "" else " (" + commas_quote(files) + ")")
 | |
| 100 | } | |
| 58906 | 101 |       (keywords1 ::: keywords2).mkString("keywords\n  ", " and\n  ", "")
 | 
| 58900 | 102 | } | 
| 103 | ||
| 104 | ||
| 59073 | 105 | /* merge */ | 
| 106 | ||
| 107 | def is_empty: Boolean = minor.is_empty && major.is_empty | |
| 108 | ||
| 109 | def ++ (other: Keywords): Keywords = | |
| 110 | if (this eq other) this | |
| 111 | else if (is_empty) other | |
| 112 |       else {
 | |
| 113 | val minor1 = minor ++ other.minor | |
| 114 | val major1 = major ++ other.major | |
| 115 | val commands1 = | |
| 116 | if (commands eq other.commands) commands | |
| 117 | else if (commands.isEmpty) other.commands | |
| 118 |           else (commands /: other.commands) { case (m, e) => if (m.isDefinedAt(e._1)) m else m + e }
 | |
| 119 | new Keywords(minor1, major1, commands1) | |
| 120 | } | |
| 121 | ||
| 122 | ||
| 58902 | 123 | /* add keywords */ | 
| 124 | ||
| 58907 | 125 | def + (name: String): Keywords = new Keywords(minor + name, major, commands) | 
| 126 | def + (name: String, kind: String): Keywords = this + (name, (kind, Nil)) | |
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 127 | def + (name: String, kind_tags: (String, List[String])): Keywords = | 
| 58902 | 128 |     {
 | 
| 129 | val major1 = major + name | |
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 130 | val commands1 = commands + (name -> kind_tags) | 
| 58906 | 131 | new Keywords(minor, major1, commands1) | 
| 58902 | 132 | } | 
| 133 | ||
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 134 | 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 | 135 |       (this /: header) {
 | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 136 | case (keywords, (name, None, _)) => | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 137 | keywords + Symbol.decode(name) + Symbol.encode(name) | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 138 | case (keywords, (name, Some((kind_tags, _)), _)) => | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 139 | keywords + (Symbol.decode(name), kind_tags) + (Symbol.encode(name), kind_tags) | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 140 | } | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58907diff
changeset | 141 | |
| 58902 | 142 | |
| 58901 | 143 | /* command kind */ | 
| 58900 | 144 | |
| 58906 | 145 | def command_kind(name: String): Option[String] = commands.get(name).map(_._1) | 
| 58900 | 146 | |
| 59701 | 147 | def is_command_kind(name: String, pred: String => Boolean): Boolean = | 
| 148 |       command_kind(name) match {
 | |
| 149 | case Some(kind) => pred(kind) | |
| 150 | case None => false | |
| 151 | } | |
| 152 | ||
| 58900 | 153 | |
| 154 | /* load commands */ | |
| 155 | ||
| 156 | def load_command(name: String): Option[List[String]] = | |
| 58906 | 157 |       commands.get(name) match {
 | 
| 58900 | 158 | case Some((THY_LOAD, exts)) => Some(exts) | 
| 159 | case _ => None | |
| 160 | } | |
| 161 | ||
| 162 | private lazy val load_commands: List[(String, List[String])] = | |
| 58906 | 163 | (for ((name, (THY_LOAD, files)) <- commands.iterator) yield (name, files)).toList | 
| 58900 | 164 | |
| 165 | def load_commands_in(text: String): Boolean = | |
| 166 |       load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })
 | |
| 167 | } | |
| 29449 
6e7745d35a30
added outer_keyword.scala: Isar command keyword classification;
 wenzelm parents: diff
changeset | 168 | } |