src/Pure/Isar/antiquote.ML
author wenzelm
Tue, 05 Aug 2008 19:29:08 +0200
changeset 27746 c7aa4c18739d
parent 27342 3945da15d410
child 27750 7f5fb39c6362
permissions -rw-r--r--
moved OuterLex.count here;
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
27342
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    10
  datatype antiquote =
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    11
    Text of string | Antiq of string * Position.T |
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    12
    Open of Position.T | Close of Position.T
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    13
  val is_antiq: antiquote -> bool
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    14
  val scan_antiquotes: string * Position.T -> antiquote list
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    15
  val scan_arguments: Scan.lexicon -> (OuterLex.token list -> 'a * OuterLex.token list) ->
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    16
    string * Position.T -> 'a
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    17
end;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    18
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    19
structure Antiquote: ANTIQUOTE =
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    20
struct
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
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    24
datatype antiquote =
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    25
  Text of string |
27342
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    26
  Antiq of string * Position.T |
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    27
  Open of Position.T |
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    28
  Close of Position.T;
9138
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
fun is_antiq (Text _) = false
27342
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    31
  | is_antiq _ = true;
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    32
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    33
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    34
(* check_nesting *)
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    35
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    36
fun err_unbalanced pos =
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    37
  error ("Unbalanced antiquotation block parentheses" ^ Position.str_of pos);
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    38
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    39
fun check_nesting antiqs =
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    40
  let
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    41
    fun check [] [] = ()
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    42
      | check [] (pos :: _) = err_unbalanced pos
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    43
      | check (Open pos :: ants) ps = check ants (pos :: ps)
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    44
      | check (Close pos :: _) [] = err_unbalanced pos
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    45
      | check (Close _ :: ants) (_ :: ps) = check ants ps
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    46
      | check (_ :: ants) ps = check ants ps;
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    47
  in check antiqs [] end;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    48
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    49
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    50
(* scan_antiquote *)
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    51
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    52
structure T = OuterLex;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    53
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    54
local
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    55
27746
c7aa4c18739d moved OuterLex.count here;
wenzelm
parents: 27342
diff changeset
    56
fun count scan = Scan.depend (fn pos => scan >> (fn x => (Position.advance [x] pos, x)));
c7aa4c18739d moved OuterLex.count here;
wenzelm
parents: 27342
diff changeset
    57
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    58
val scan_txt =
27746
c7aa4c18739d moved OuterLex.count here;
wenzelm
parents: 27342
diff changeset
    59
  count ($$ "@" --| Scan.ahead (~$$ "{")) ||
c7aa4c18739d moved OuterLex.count here;
wenzelm
parents: 27342
diff changeset
    60
  count (Scan.one (fn s =>
27342
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    61
    s <> "@" andalso s <> "\\<lbrace>" andalso s <> "\\<rbrace>" andalso Symbol.is_regular s));
9138
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
fun escape "\\" = "\\\\"
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    64
  | escape "\"" = "\\\""
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    65
  | escape s = s;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    66
27746
c7aa4c18739d moved OuterLex.count here;
wenzelm
parents: 27342
diff changeset
    67
val quote_escape = Library.quote o implode o map escape o T.clean_value;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    68
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    69
val scan_ant =
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    70
  T.scan_string >> quote_escape ||
27746
c7aa4c18739d moved OuterLex.count here;
wenzelm
parents: 27342
diff changeset
    71
  count (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
    72
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    73
val scan_antiq =
27746
c7aa4c18739d moved OuterLex.count here;
wenzelm
parents: 27342
diff changeset
    74
  count ($$ "@") |-- count ($$ "{") |--
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    75
    T.!!! "missing closing brace of antiquotation"
27746
c7aa4c18739d moved OuterLex.count here;
wenzelm
parents: 27342
diff changeset
    76
      (Scan.repeat scan_ant >> implode) --| count ($$ "}");
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    77
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    78
in
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    79
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    80
fun scan_antiquote (pos, ss) = (pos, ss) |>
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    81
  (Scan.repeat1 scan_txt >> (Text o implode) ||
27342
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    82
    scan_antiq >> (fn s => Antiq (s, pos)) ||
27746
c7aa4c18739d moved OuterLex.count here;
wenzelm
parents: 27342
diff changeset
    83
    count ($$ "\\<lbrace>") >> K (Open pos) ||
c7aa4c18739d moved OuterLex.count here;
wenzelm
parents: 27342
diff changeset
    84
    count ($$ "\\<rbrace>") >> K (Close pos));
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    85
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    86
end;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    87
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    88
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    89
(* scan_antiquotes *)
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    90
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    91
fun scan_antiquotes (s, pos) =
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    92
  (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
    93
      (pos, Symbol.explode s) of
27342
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    94
    (xs, (_, [])) => (check_nesting xs; xs)
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    95
  | (_, (pos', ss)) => error ("Malformed quotation/antiquotation source at: " ^
14729
0e987111a17e changed Symbol.beginning;
wenzelm
parents: 14598
diff changeset
    96
      quote (Symbol.beginning 10 ss) ^ Position.str_of pos'));
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    97
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    98
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    99
(* scan_arguments *)
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   100
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   101
fun scan_arguments lex antiq (s, pos) =
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   102
  let
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   103
    fun err msg = cat_error msg
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   104
      ("Malformed antiquotation: " ^ quote ("@{" ^ s ^ "}") ^ Position.str_of pos);
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   105
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   106
    val res =
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   107
      Source.of_string s
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   108
      |> Symbol.source false
23677
1114cc909800 adapted OuterLex/T.source;
wenzelm
parents: 22114
diff changeset
   109
      |> T.source NONE (K (lex, Scan.empty_lexicon)) pos
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   110
      |> T.source_proper
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   111
      |> Source.source T.stopper (Scan.error (Scan.bulk antiq)) NONE
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   112
      |> Source.exhaust;
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   113
  in (case res of [x] => x | _ => err "") handle ERROR msg => err msg end;
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   114
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   115
end;