CookBook/chunks.ML
author Christian Urban <urbanc@in.tum.de>
Mon, 20 Oct 2008 06:22:11 +0000
changeset 41 b11653b11bd3
parent 24 9d5d2f9d7c09
child 55 0b55402ae95e
permissions -rw-r--r--
further progress on the parsing section and tuning on the antiqu's
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     1
structure Chunks =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     2
struct
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     3
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     4
(** theory data **)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     5
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     6
structure ChunkData = TheoryDataFun
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     7
(
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     8
  type T = (ML_Lex.token list * stamp) NameSpace.table
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
     9
  val empty = NameSpace.empty_table;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    10
  val copy = I;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    11
  val extend = I;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    12
  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
    13
    error ("Attempt to merge different versions of ML chunk " ^ quote dup);
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    14
);
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
fun declare_chunk name thy =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    17
  (Sign.full_name thy name,
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    18
   ChunkData.map (NameSpace.extend_table (Sign.naming_of thy) [(name, ([], stamp ()))]) thy
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    19
   handle Symtab.DUP _ => error ("Duplicate definition of ML chunk " ^ quote name));
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    20
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    21
fun augment_chunk tok name =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    22
  ChunkData.map (apsnd (Symtab.map_entry name (apfst (cons tok))));
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    23
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    24
fun the_chunk thy name =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    25
  let val (space, tab) = ChunkData.get thy
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    26
  in case Symtab.lookup tab (NameSpace.intern space name) of
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    27
      NONE => error ("Unknown ML chunk " ^ quote name)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    28
    | SOME (toks, _) => rev toks
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    29
  end;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    30
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    31
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    32
(** parsers **)
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
val not_eof = ML_Lex.stopper |> Scan.is_stopper #> not;
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 scan_space = Scan.many Symbol.is_blank;
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
fun scan_cmt scan =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    39
  Scan.one (ML_Lex.kind_of #> equal ML_Lex.Comment) >>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    40
  (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
    41
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    42
val scan_chunks = Scan.depend (fn (open_chunks, thy) =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    43
  scan_cmt ((Scan.this_string "(*" -- scan_space -- Scan.this_string "@chunk" --
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    44
    scan_space) |-- Symbol.scan_id --|
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    45
    (scan_space -- Scan.this_string "*)") >> (fn name =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    46
      let val (name', thy') = declare_chunk name thy
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    47
      in ((name' :: open_chunks, thy'), "") end))
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    48
  ||
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    49
  scan_cmt (Scan.this_string "(*" -- scan_space -- Scan.this_string "@end" --
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    50
    scan_space -- Scan.this_string "*)" >> (fn _ =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    51
      (case open_chunks of
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    52
         [] => error "end without matching chunk encountered"
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    53
       | _ :: open_chunks' => ((open_chunks', thy), ""))))
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    54
  ||
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    55
  Scan.one not_eof >> (fn tok =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    56
    ((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
    57
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    58
fun chunks_of s thy =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    59
  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
    60
  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
    61
        (([], thy), toks) of
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    62
      (toks', (([], thy'), _)) => (implode toks', thy')
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    63
    | (_, ((open_chunks, _), _)) =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    64
        error ("Open chunks at end of text: " ^ commas_quote open_chunks)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    65
  end;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    66
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    67
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    68
(** use command **)
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
fun load_ml dir raw_path thy =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    71
  (case ThyLoad.check_ml dir raw_path of
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    72
    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
    73
  | SOME (path, _) =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    74
      let val (s, thy') = chunks_of (File.read path) thy
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    75
      in
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    76
        Context.theory_map (ThyInfo.exec_file false raw_path) thy'
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    77
      end);
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    78
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    79
fun use_chunks path thy =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    80
  load_ml (ThyInfo.master_directory (Context.theory_name thy)) path thy;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    81
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    82
val _ =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    83
  OuterSyntax.command "use_chunks" "eval ML text from file"
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    84
    (OuterKeyword.tag_ml OuterKeyword.thy_decl)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    85
    (OuterParse.path >> (Toplevel.theory o use_chunks));
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    86
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    87
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    88
(** antiquotation **)
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
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
    91
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    92
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
    93
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    94
val transform_cmts =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    95
  Scan.finite ML_Lex.stopper (Scan.repeat
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    96
    (   scan_cmt
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    97
          (Scan.this_string "(*{" |--
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    98
           Scan.repeat (Scan.unless (Scan.this_string "}*)")
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
    99
             (Scan.one (not o Symbol.is_eof))) --|
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   100
           Scan.this_string "}*)" >>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   101
           (enclose "\\hfill\\textrm{\\textit{" "}}" o implode))
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   102
     || Scan.one not_eof >> ML_Lex.content_of)) #>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   103
  fst;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   104
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   105
fun output_chunk src ctxt name =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   106
  let
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   107
    val toks = the_chunk (ProofContext.theory_of ctxt) name;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   108
    val (spc, toks') = (case toks of
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   109
        [] => ("", [])
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   110
      | [tok] => ("", [tok])
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   111
      | tok :: toks' =>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   112
          let
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   113
            val (toks'', tok') = split_last toks'
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   114
            val toks''' =
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   115
              if ML_Lex.kind_of tok' = ML_Lex.Space then toks''
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   116
              else toks'
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   117
          in
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   118
            if ML_Lex.kind_of tok = ML_Lex.Space then
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   119
              (tok |> ML_Lex.content_of |> Symbol.explode |>
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   120
               take_suffix (equal " ") |> snd |> implode,
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   121
               toks''')
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   122
            else ("", tok :: toks''')
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   123
          end)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   124
    val txt = spc ^ implode (transform_cmts toks')
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   125
  in
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   126
    ((if ! ThyOutput.source then str_of_source src else txt)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   127
    |> (if ! ThyOutput.quotes then quote else I)
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   128
    |> (if ! ThyOutput.display then enclose "\\begin{alltt}\n" "\n\\end{alltt}"
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   129
        else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   130
  end;
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   131
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   132
val _ = ThyOutput.add_commands
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   133
  [("ML_chunk", ThyOutput.args (Scan.lift Args.name) output_chunk)];
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   134
9d5d2f9d7c09 Antiquotation for referring to chunks of ML code.
berghofe
parents:
diff changeset
   135
end;