| author | wenzelm | 
| Thu, 05 Aug 2021 20:24:42 +0200 | |
| changeset 74129 | c3794f56a2e2 | 
| parent 72841 | fd8d82c4433b | 
| child 74887 | 56247fdb8bbb | 
| permissions | -rw-r--r-- | 
| 67215 | 1 | (* Title: Pure/Thy/sessions.ML | 
| 2 | Author: Makarius | |
| 3 | ||
| 4 | Support for session ROOT syntax. | |
| 5 | *) | |
| 6 | ||
| 7 | signature SESSIONS = | |
| 8 | sig | |
| 9 | val root_name: string | |
| 10 | val theory_name: string | |
| 11 | val command_parser: (Toplevel.transition -> Toplevel.transition) parser | |
| 12 | end; | |
| 13 | ||
| 14 | structure Sessions: SESSIONS = | |
| 15 | struct | |
| 16 | ||
| 17 | val root_name = "ROOT"; | |
| 18 | val theory_name = "Pure.Sessions"; | |
| 19 | ||
| 20 | local | |
| 21 | ||
| 72841 | 22 | val theory_entry = Parse.input Parse.theory_name --| Parse.opt_keyword "global"; | 
| 67215 | 23 | |
| 24 | val theories = | |
| 25 | Parse.$$$ "theories" |-- Parse.!!! (Scan.optional Parse.options [] -- Scan.repeat1 theory_entry); | |
| 26 | ||
| 68292 | 27 | val in_path = | 
| 72841 | 28 |   Parse.$$$ "(" |-- Parse.!!! (Parse.$$$ "in" |-- Parse.path_input --| Parse.$$$ ")");
 | 
| 68292 | 29 | |
| 72600 
2fa4f25d9d07
official support for document theories from other sessions;
 wenzelm parents: 
70683diff
changeset | 30 | val document_theories = | 
| 72841 | 31 | Parse.$$$ "document_theories" |-- Scan.repeat1 (Parse.input Parse.theory_name); | 
| 72600 
2fa4f25d9d07
official support for document theories from other sessions;
 wenzelm parents: 
70683diff
changeset | 32 | |
| 67215 | 33 | val document_files = | 
| 34 | Parse.$$$ "document_files" |-- | |
| 72841 | 35 | Parse.!!! (Scan.optional in_path (Input.string "document") -- Scan.repeat1 Parse.path_input); | 
| 67215 | 36 | |
| 69671 | 37 | val prune = | 
| 38 | Scan.optional (Parse.$$$ "[" |-- Parse.!!! (Parse.nat --| Parse.$$$ "]")) 0; | |
| 39 | ||
| 68292 | 40 | val export_files = | 
| 41 | Parse.$$$ "export_files" |-- | |
| 72841 | 42 | Parse.!!! (Scan.optional in_path (Input.string "export") -- prune -- Scan.repeat1 Parse.embedded); | 
| 43 | ||
| 44 | fun path_source source path = | |
| 45 | Input.source (Input.is_delimited source) (Path.implode path) (Input.range_of source); | |
| 68292 | 46 | |
| 67215 | 47 | in | 
| 48 | ||
| 49 | val command_parser = | |
| 50 | Parse.session_name -- | |
| 51 |   Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Scan.repeat1 Parse.name --| Parse.$$$ ")")) [] --
 | |
| 72841 | 52 | Scan.optional (Parse.$$$ "in" |-- Parse.!!! Parse.path_input) (Input.string ".") -- | 
| 67220 | 53 | (Parse.$$$ "=" |-- | 
| 69349 
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
 wenzelm parents: 
68808diff
changeset | 54 | Parse.!!! (Scan.option (Parse.session_name --| Parse.!!! (Parse.$$$ "+")) -- | 
| 67220 | 55 | Scan.optional (Parse.$$$ "description" |-- Parse.!!! (Parse.input Parse.text)) Input.empty -- | 
| 56 | Scan.optional (Parse.$$$ "options" |-- Parse.!!! Parse.options) [] -- | |
| 57 | Scan.optional (Parse.$$$ "sessions" |-- | |
| 69349 
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
 wenzelm parents: 
68808diff
changeset | 58 | Parse.!!! (Scan.repeat1 Parse.session_name)) [] -- | 
| 72841 | 59 | Scan.optional (Parse.$$$ "directories" |-- Parse.!!! (Scan.repeat1 Parse.path_input)) [] -- | 
| 67220 | 60 | Scan.repeat theories -- | 
| 72600 
2fa4f25d9d07
official support for document theories from other sessions;
 wenzelm parents: 
70683diff
changeset | 61 | Scan.optional document_theories [] -- | 
| 68292 | 62 | Scan.repeat document_files -- | 
| 63 | Scan.repeat export_files)) | |
| 72600 
2fa4f25d9d07
official support for document theories from other sessions;
 wenzelm parents: 
70683diff
changeset | 64 | >> (fn (((((session, _), _), dir), | 
| 
2fa4f25d9d07
official support for document theories from other sessions;
 wenzelm parents: 
70683diff
changeset | 65 | (((((((((parent, descr), options), sessions), directories), theories), | 
| 
2fa4f25d9d07
official support for document theories from other sessions;
 wenzelm parents: 
70683diff
changeset | 66 | document_theories), document_files), export_files)))) => | 
| 67215 | 67 | Toplevel.keep (fn state => | 
| 68 | let | |
| 69 | val ctxt = Toplevel.context_of state; | |
| 70049 | 70 | val session_dir = Resources.check_dir ctxt NONE dir; | 
| 67215 | 71 | |
| 72 | val _ = | |
| 67220 | 73 | (the_list parent @ sessions) |> List.app (fn arg => | 
| 74 | ignore (Resources.check_session ctxt arg) | |
| 75 | handle ERROR msg => Output.error_message msg); | |
| 76 | ||
| 77 | val _ = | |
| 67215 | 78 | Context_Position.report ctxt | 
| 79 | (Position.range_position (Symbol_Pos.range (Input.source_explode descr))) | |
| 80 | Markup.comment; | |
| 81 | ||
| 82 | val _ = | |
| 83 | (options @ maps #1 theories) |> List.app (fn (x, y) => | |
| 67220 | 84 | ignore (Completion.check_option_value ctxt x y (Options.default ())) | 
| 85 | handle ERROR msg => Output.error_message msg); | |
| 67215 | 86 | |
| 72841 | 87 | fun check_thy source = | 
| 88 | ignore (Resources.check_file ctxt (SOME Path.current) source) | |
| 72600 
2fa4f25d9d07
official support for document theories from other sessions;
 wenzelm parents: 
70683diff
changeset | 89 | handle ERROR msg => Output.error_message msg; | 
| 
2fa4f25d9d07
official support for document theories from other sessions;
 wenzelm parents: 
70683diff
changeset | 90 | |
| 67215 | 91 | val _ = | 
| 72841 | 92 | maps #2 theories |> List.app (fn source => | 
| 67215 | 93 | let | 
| 72841 | 94 | val s = Input.string_of source; | 
| 95 | val pos = Input.pos_of source; | |
| 67215 | 96 |               val {node_name, theory_name, ...} =
 | 
| 97 | Resources.import_name session session_dir s | |
| 98 | handle ERROR msg => error (msg ^ Position.here pos); | |
| 72841 | 99 | val thy_path = the_default node_name (Resources.find_theory_file theory_name); | 
| 100 | in check_thy (path_source source thy_path) end); | |
| 67215 | 101 | |
| 102 | val _ = | |
| 70681 | 103 | directories |> List.app (ignore o Resources.check_dir ctxt (SOME session_dir)); | 
| 70668 | 104 | |
| 105 | val _ = | |
| 72841 | 106 | document_theories |> List.app (fn source => | 
| 107 | let | |
| 108 | val thy = Input.string_of source; | |
| 109 | val pos = Input.pos_of source; | |
| 110 | in | |
| 111 | (case Resources.find_theory_file thy of | |
| 112 |                 NONE => Output.error_message ("Unknown theory " ^ quote thy ^ Position.here pos)
 | |
| 113 | | SOME path => check_thy (path_source source path)) | |
| 114 | end); | |
| 72600 
2fa4f25d9d07
official support for document theories from other sessions;
 wenzelm parents: 
70683diff
changeset | 115 | |
| 
2fa4f25d9d07
official support for document theories from other sessions;
 wenzelm parents: 
70683diff
changeset | 116 | val _ = | 
| 67215 | 117 | document_files |> List.app (fn (doc_dir, doc_files) => | 
| 118 | let | |
| 70049 | 119 | val dir = Resources.check_dir ctxt (SOME session_dir) doc_dir; | 
| 120 | val _ = List.app (ignore o Resources.check_file ctxt (SOME dir)) doc_files; | |
| 67215 | 121 | in () end); | 
| 68292 | 122 | |
| 123 | val _ = | |
| 69671 | 124 | export_files |> List.app (fn ((export_dir, _), _) => | 
| 70049 | 125 | ignore (Resources.check_path ctxt (SOME session_dir) export_dir)); | 
| 67215 | 126 | in () end)); | 
| 127 | ||
| 128 | end; | |
| 129 | ||
| 130 | end; |