src/Pure/Thy/thy_load.ML
changeset 48867 e9beabf045ab
parent 46959 cdc791910460
child 48868 aeea516440c8
equal deleted inserted replaced
48866:034df7b05759 48867:e9beabf045ab
    20   val use_ml: Path.T -> unit
    20   val use_ml: Path.T -> unit
    21   val exec_ml: Path.T -> generic_theory -> generic_theory
    21   val exec_ml: Path.T -> generic_theory -> generic_theory
    22   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    22   val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
    23   val load_thy: int -> Path.T -> Thy_Header.header -> Position.T -> string ->
    23   val load_thy: int -> Path.T -> Thy_Header.header -> Position.T -> string ->
    24     theory list -> theory * unit future
    24     theory list -> theory * unit future
       
    25   val resolve_files: Path.T -> Thy_Syntax.span -> Thy_Syntax.span
       
    26   val parse_files: string -> (theory -> Token.files) parser
    25   val set_master_path: Path.T -> unit
    27   val set_master_path: Path.T -> unit
    26   val get_master_path: unit -> Path.T
    28   val get_master_path: unit -> Path.T
    27 end;
    29 end;
    28 
    30 
    29 structure Thy_Load: THY_LOAD =
    31 structure Thy_Load: THY_LOAD =
   156     in () end;
   158     in () end;
   157 
   159 
   158 fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
   160 fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
   159 
   161 
   160 
   162 
       
   163 (* inlined files *)
       
   164 
       
   165 fun read_files cmd dir tok =
       
   166   let
       
   167     val path = Path.explode (Token.content_of tok);
       
   168     val exts = Keyword.command_files cmd;
       
   169     val variants =
       
   170       if null exts then [path]
       
   171       else map (fn ext => Path.ext ext path) exts;
       
   172   in (dir, variants |> map (Path.append dir #> (fn p => (File.read p, Path.position p)))) end;
       
   173 
       
   174 fun resolve_files dir (span as Thy_Syntax.Span (Thy_Syntax.Command cmd, toks)) =
       
   175       if Keyword.is_theory_load cmd then
       
   176         (case find_index Token.is_name (rev toks) of  (* FIXME allow trailing cmt (!?!) *)
       
   177           ~1 => span
       
   178         | i' =>
       
   179             let
       
   180               val i = length toks - 1 - i';
       
   181               val toks' = toks |> map_index (fn (j, tok) =>
       
   182                 if i = j then Token.put_files (read_files cmd dir tok) tok
       
   183                 else tok);
       
   184             in Thy_Syntax.Span (Thy_Syntax.Command cmd, toks') end)
       
   185       else span
       
   186   | resolve_files _ span = span;
       
   187 
       
   188 fun parse_files cmd =
       
   189   Scan.ahead Parse.not_eof -- Parse.group (fn () => "file name/path specification") Parse.name
       
   190     >> (fn (tok, name) => fn thy =>
       
   191       (case Token.get_files tok of
       
   192         SOME files => files
       
   193       | NONE =>
       
   194           (warning ("Dynamic loading of files: " ^ quote name ^
       
   195               Position.str_of (Token.position_of tok));
       
   196             read_files cmd (master_directory thy) tok)));
       
   197 
       
   198 
   161 (* load_thy *)
   199 (* load_thy *)
   162 
   200 
   163 fun begin_theory dir {name, imports, keywords, uses} parents =
   201 fun begin_theory dir {name, imports, keywords, uses} parents =
   164   Theory.begin_theory name parents
   202   Theory.begin_theory name parents
   165   |> put_deps dir imports
   203   |> put_deps dir imports
   190 
   228 
   191 fun load_thy update_time dir header pos text parents =
   229 fun load_thy update_time dir header pos text parents =
   192   let
   230   let
   193     val time = ! Toplevel.timing;
   231     val time = ! Toplevel.timing;
   194 
   232 
   195     val {name, imports, uses, ...} = header;
   233     val {name, uses, ...} = header;
   196     val _ = Thy_Header.define_keywords header;
   234     val _ = Thy_Header.define_keywords header;
   197     val _ = Present.init_theory name;
   235     val _ = Present.init_theory name;
   198     fun init () =
   236     fun init () =
   199       begin_theory dir header parents
   237       begin_theory dir header parents
   200       |> Present.begin_theory update_time dir uses;
   238       |> Present.begin_theory update_time dir uses;
   201 
   239 
   202     val lexs = Keyword.get_lexicons ();
   240     val lexs = Keyword.get_lexicons ();
   203 
   241 
   204     val toks = Thy_Syntax.parse_tokens lexs pos text;
   242     val toks = Thy_Syntax.parse_tokens lexs pos text;
   205     val spans = Thy_Syntax.parse_spans toks;
   243     val spans = map (resolve_files dir) (Thy_Syntax.parse_spans toks);
   206     val elements = Thy_Syntax.parse_elements spans;
   244     val elements = Thy_Syntax.parse_elements spans;
   207 
   245 
   208     val _ = Present.theory_source name
   246     val _ = Present.theory_source name
   209       (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
   247       (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
   210 
   248