ProgTutorial/chunks.ML
author Norbert Schirmer <norbert.schirmer@web.de>
Wed, 22 May 2019 13:24:30 +0200
changeset 575 c3dbc04471a9
parent 256 1fb8d62c88a0
permissions -rw-r--r--
fixing some Line references
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
90
b071a0b88298 made chunks aware of the gray-option
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
     1
24
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     2
structure Chunks =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     3
struct
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     4
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     5
(** theory data **)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     6
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     7
structure ChunkData = TheoryDataFun
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     8
(
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     9
  type T = (ML_Lex.token list * stamp) NameSpace.table
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    10
  val empty = NameSpace.empty_table;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    11
  val copy = I;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    12
  val extend = I;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    13
  fun merge _ tables : T = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUP dup =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    14
    error ("Attempt to merge different versions of ML chunk " ^ quote dup);
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    15
);
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    16
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    17
fun declare_chunk name thy =
55
0b55402ae95e Adapted to changes in binding module.
berghofe
parents: 24
diff changeset
    18
  (Sign.full_bname thy name,
176
3da5f3f07d8b updated to new read_specification
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
    19
   ChunkData.map (snd o NameSpace.define (Sign.naming_of thy)
55
0b55402ae95e Adapted to changes in binding module.
berghofe
parents: 24
diff changeset
    20
     (Binding.name name, ([], stamp ()))) thy
24
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    21
   handle Symtab.DUP _ => error ("Duplicate definition of ML chunk " ^ quote name));
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    22
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    23
fun augment_chunk tok name =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    24
  ChunkData.map (apsnd (Symtab.map_entry name (apfst (cons tok))));
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    25
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    26
fun the_chunk thy name =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    27
  let val (space, tab) = ChunkData.get thy
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    28
  in case Symtab.lookup tab (NameSpace.intern space name) of
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    29
      NONE => error ("Unknown ML chunk " ^ quote name)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    30
    | SOME (toks, _) => rev toks
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    31
  end;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    32
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    33
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    34
(** parsers **)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    35
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    36
val not_eof = ML_Lex.stopper |> Scan.is_stopper #> not;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    37
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    38
val scan_space = Scan.many Symbol.is_blank;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    39
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    40
fun scan_cmt scan =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    41
  Scan.one (ML_Lex.kind_of #> equal ML_Lex.Comment) >>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    42
  (ML_Lex.content_of #> Symbol.explode #> Scan.finite Symbol.stopper scan #> fst);
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    43
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    44
val scan_chunks = Scan.depend (fn (open_chunks, thy) =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    45
  scan_cmt ((Scan.this_string "(*" -- scan_space -- Scan.this_string "@chunk" --
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    46
    scan_space) |-- Symbol.scan_id --|
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    47
    (scan_space -- Scan.this_string "*)") >> (fn name =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    48
      let val (name', thy') = declare_chunk name thy
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    49
      in ((name' :: open_chunks, thy'), "") end))
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    50
  ||
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    51
  scan_cmt (Scan.this_string "(*" -- scan_space -- Scan.this_string "@end" --
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    52
    scan_space -- Scan.this_string "*)" >> (fn _ =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    53
      (case open_chunks of
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    54
         [] => error "end without matching chunk encountered"
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    55
       | _ :: open_chunks' => ((open_chunks', thy), ""))))
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    56
  ||
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    57
  Scan.one not_eof >> (fn tok =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    58
    ((open_chunks, fold (augment_chunk tok) open_chunks thy), ML_Lex.content_of tok)));
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    59
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    60
fun chunks_of s thy =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    61
  let val toks = Source.exhaust (ML_Lex.source (Source.of_string s))
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    62
  in case Scan.finite' ML_Lex.stopper (Scan.repeat (Scan.error scan_chunks))
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    63
        (([], thy), toks) of
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    64
      (toks', (([], thy'), _)) => (implode toks', thy')
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    65
    | (_, ((open_chunks, _), _)) =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    66
        error ("Open chunks at end of text: " ^ commas_quote open_chunks)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    67
  end;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    68
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    69
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    70
(** use command **)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    71
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    72
fun load_ml dir raw_path thy =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    73
  (case ThyLoad.check_ml dir raw_path of
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    74
    NONE => error ("Could not find ML file " ^ quote (Path.implode raw_path))
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    75
  | SOME (path, _) =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    76
      let val (s, thy') = chunks_of (File.read path) thy
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    77
      in
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    78
        Context.theory_map (ThyInfo.exec_file false raw_path) thy'
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    79
      end);
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    80
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    81
fun use_chunks path thy =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    82
  load_ml (ThyInfo.master_directory (Context.theory_name thy)) path thy;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    83
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    84
val _ =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    85
  OuterSyntax.command "use_chunks" "eval ML text from file"
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    86
    (OuterKeyword.tag_ml OuterKeyword.thy_decl)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    87
    (OuterParse.path >> (Toplevel.theory o use_chunks));
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    88
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    89
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    90
(** antiquotation **)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    91
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    92
val str_of_source = space_implode " " o map OuterLex.unparse o #2 o #1 o Args.dest_src;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    93
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    94
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    95
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    96
val transform_cmts =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    97
  Scan.finite ML_Lex.stopper (Scan.repeat
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    98
    (   scan_cmt
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    99
          (Scan.this_string "(*{" |--
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   100
           Scan.repeat (Scan.unless (Scan.this_string "}*)")
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   101
             (Scan.one (not o Symbol.is_eof))) --|
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   102
           Scan.this_string "}*)" >>
57
065f472c09ab Repaired output of marginal comments in ML antiquotation.
berghofe
parents: 55
diff changeset
   103
           (Symbol.encode_raw o enclose "\\hfill\\textrm{\\textit{" "}}" o implode))
24
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   104
     || Scan.one not_eof >> ML_Lex.content_of)) #>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   105
  fst;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   106
165
890fbfef6d6b partially adapted to new antiquotation infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   107
fun output_chunk {state: Toplevel.state, source = src, context = ctxt} name =
24
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   108
  let
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   109
    val toks = the_chunk (ProofContext.theory_of ctxt) name;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   110
    val (spc, toks') = (case toks of
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   111
        [] => ("", [])
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   112
      | [tok] => ("", [tok])
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   113
      | tok :: toks' =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   114
          let
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   115
            val (toks'', tok') = split_last toks'
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   116
            val toks''' =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   117
              if ML_Lex.kind_of tok' = ML_Lex.Space then toks''
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   118
              else toks'
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   119
          in
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   120
            if ML_Lex.kind_of tok = ML_Lex.Space then
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   121
              (tok |> ML_Lex.content_of |> Symbol.explode |>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   122
               take_suffix (equal " ") |> snd |> implode,
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   123
               toks''')
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   124
            else ("", tok :: toks''')
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   125
          end)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   126
    val txt = spc ^ implode (transform_cmts toks')
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   127
  in
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
   128
    OutputTutorial.output (split_lines txt) 
24
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   129
  end;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   130
165
890fbfef6d6b partially adapted to new antiquotation infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   131
val _ = ThyOutput.antiquotation "ML_chunk" (Scan.lift Args.name) output_chunk;
24
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   132
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   133
end;