src/Pure/Isar/antiquote.ML
author wenzelm
Sat, 28 Jul 2007 20:40:27 +0200
changeset 24022 ab76c73b3b58
parent 23937 66e1f24d655d
child 26002 469ee728a5d7
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/antiquote.ML
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
     4
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
     5
Text with antiquotations of inner items (terms, types etc.).
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
     6
*)
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
     7
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
     8
signature ANTIQUOTE =
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
     9
sig
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    10
  datatype antiquote = Text of string | Antiq of string * Position.T
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    11
  val is_antiq: antiquote -> bool
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    12
  val scan_antiquotes: string * Position.T -> antiquote list
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    13
  val scan_arguments: Scan.lexicon -> (OuterLex.token list -> 'a * OuterLex.token list) ->
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    14
    string * Position.T -> 'a
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    15
end;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    16
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    17
structure Antiquote: ANTIQUOTE =
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    18
struct
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    19
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    20
(* datatype antiquote *)
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    21
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    22
datatype antiquote =
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    23
  Text of string |
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    24
  Antiq of string * Position.T;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    25
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    26
fun is_antiq (Text _) = false
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    27
  | is_antiq (Antiq _) = true;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    28
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    29
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    30
(* scan_antiquote *)
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    31
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    32
structure T = OuterLex;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    33
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    34
local
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    35
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    36
val scan_txt =
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    37
  T.scan_blank ||
19305
5c16895d548b avoid polymorphic equality;
wenzelm
parents: 14981
diff changeset
    38
  T.keep_line ($$ "@" --| Scan.ahead (~$$ "{")) ||
23784
75e6b9dd5336 Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents: 23677
diff changeset
    39
  T.keep_line (Scan.one (fn s => s <> "@" andalso Symbol.is_regular s));
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    40
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    41
fun escape "\\" = "\\\\"
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    42
  | escape "\"" = "\\\""
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    43
  | escape s = s;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    44
14598
7009f59711e3 Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents: 12881
diff changeset
    45
val quote_escape = Library.quote o implode o map escape o Symbol.explode;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    46
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    47
val scan_ant =
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    48
  T.scan_blank ||
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    49
  T.scan_string >> quote_escape ||
23784
75e6b9dd5336 Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents: 23677
diff changeset
    50
  T.keep_line (Scan.one (fn s => s <> "}" andalso Symbol.is_regular s));
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    51
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    52
val scan_antiq =
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    53
  T.keep_line ($$ "@" -- $$ "{") |--
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    54
    T.!!! "missing closing brace of antiquotation"
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    55
      (Scan.repeat scan_ant >> implode) --| T.keep_line ($$ "}");
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    56
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    57
in
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    58
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    59
fun scan_antiquote (pos, ss) = (pos, ss) |>
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    60
  (Scan.repeat1 scan_txt >> (Text o implode) ||
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    61
    scan_antiq >> (fn s => Antiq (s, pos)));
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    62
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    63
end;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    64
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    65
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    66
(* scan_antiquotes *)
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    67
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    68
fun scan_antiquotes (s, pos) =
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    69
  (case Scan.error (Scan.finite' Symbol.stopper (Scan.repeat scan_antiquote))
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    70
      (pos, Symbol.explode s) of
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    71
    (xs, (_, [])) => xs
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    72
  | (_, (pos', ss)) => error ("Malformed quotation/antiquotation source at: " ^
14729
0e987111a17e changed Symbol.beginning;
wenzelm
parents: 14598
diff changeset
    73
      quote (Symbol.beginning 10 ss) ^ Position.str_of pos'));
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    74
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    75
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    76
(* scan_arguments *)
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    77
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    78
fun scan_arguments lex antiq (s, pos) =
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    79
  let
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    80
    fun err msg = cat_error msg
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    81
      ("Malformed antiquotation: " ^ quote ("@{" ^ s ^ "}") ^ Position.str_of pos);
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    82
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    83
    val res =
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    84
      Source.of_string s
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    85
      |> Symbol.source false
23677
1114cc909800 adapted OuterLex/T.source;
wenzelm
parents: 22114
diff changeset
    86
      |> T.source NONE (K (lex, Scan.empty_lexicon)) pos
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    87
      |> T.source_proper
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    88
      |> Source.source T.stopper (Scan.error (Scan.bulk antiq)) NONE
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    89
      |> Source.exhaust;
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    90
  in (case res of [x] => x | _ => err "") handle ERROR msg => err msg end;
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    91
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    92
end;