src/Pure/Isar/antiquote.ML
author wenzelm
Tue, 12 Aug 2008 21:27:48 +0200
changeset 27835 ff8b8513965a
parent 27799 52f07d5292cd
child 27870 643673ebe065
permissions -rw-r--r--
Symbol.source/OuterLex.source: more explicit do_recover argument;
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 =
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    11
    Text of string | Antiq of SymbolPos.text * Position.T |
27342
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
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    14
  val read: SymbolPos.text * Position.T -> antiquote list
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    15
  val read_antiq: Scan.lexicon -> (OuterLex.token list -> 'a * OuterLex.token list) ->
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    16
    SymbolPos.text * 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 |
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    26
  Antiq of string * Position.T |
27342
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
27767
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    52
open BasicSymbolPos;
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    53
structure T = OuterLex;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    54
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    55
local
9138
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
val scan_txt =
27767
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    58
  $$$ "@" --| Scan.ahead (~$$$ "{") ||
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    59
  Scan.one (fn (s, _) => s <> "@" andalso s <> "\\<lbrace>" andalso s <> "\\<rbrace>"
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    60
    andalso Symbol.is_regular s) >> single;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    61
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    62
val scan_ant =
27767
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    63
  T.scan_quoted ||
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    64
  Scan.one (fn (s, _) => s <> "}" andalso Symbol.is_regular s) >> single;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    65
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    66
val scan_antiq =
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    67
  SymbolPos.scan_pos -- ($$$ "@" |-- $$$ "{" |--
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    68
    T.!!! "missing closing brace of antiquotation"
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    69
      (Scan.repeat scan_ant -- ($$$ "}" |-- SymbolPos.scan_pos)))
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    70
  >> (fn (pos1, (body, pos2)) =>
27799
52f07d5292cd tuned SymbolPos interface;
wenzelm
parents: 27779
diff changeset
    71
      let val (s, (pos, _)) = SymbolPos.implode_range pos1 pos2 (flat body)
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    72
      in Antiq (s, pos) end);
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    73
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    74
in
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    75
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    76
val scan_antiquote =
27799
52f07d5292cd tuned SymbolPos interface;
wenzelm
parents: 27779
diff changeset
    77
 (Scan.repeat1 scan_txt >> (Text o SymbolPos.content o flat) ||
27750
7f5fb39c6362 Antiq: inner vs. outer position;
wenzelm
parents: 27746
diff changeset
    78
  scan_antiq ||
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    79
  SymbolPos.scan_pos --| $$$ "\\<lbrace>" >> Open ||
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    80
  SymbolPos.scan_pos --| $$$ "\\<rbrace>" >> Close);
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    81
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    82
end;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    83
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    84
27767
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    85
(* read *)
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    86
27767
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    87
fun read ("", _) = []
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    88
  | read (s, pos) =
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    89
      (case Scan.read SymbolPos.stopper (Scan.repeat scan_antiquote) (SymbolPos.explode (s, pos)) of
27767
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    90
        SOME xs => (check_nesting xs; xs)
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    91
      | NONE => error ("Malformed quotation/antiquotation source" ^ Position.str_of pos));
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    92
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    93
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    94
(* read_antiq *)
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    95
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    96
fun read_antiq lex scan (s, pos) =
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    97
  let
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    98
    fun err msg = cat_error msg
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    99
      ("Malformed antiquotation: " ^ quote ("@{" ^ s ^ "}") ^ Position.str_of pos);
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   100
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   101
    val res =
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
   102
      Source.of_list (SymbolPos.explode (s, pos))
27835
ff8b8513965a Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27799
diff changeset
   103
      |> T.source' {do_recover = NONE} (K (lex, Scan.empty_lexicon))
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   104
      |> T.source_proper
27767
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
   105
      |> Source.source T.stopper (Scan.error (Scan.bulk scan)) NONE
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   106
      |> Source.exhaust;
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   107
  in (case res of [x] => x | _ => err "") handle ERROR msg => err msg end;
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   108
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   109
end;