src/Pure/Isar/antiquote.ML
author ballarin
Mon, 24 Nov 2008 14:23:04 +0100
changeset 28877 9ff624bd4fe1
parent 27880 4ab10bfe8a7f
child 29606 fedb8be05f24
permissions -rw-r--r--
More ramifications of removal of 'includes' element.
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 =
27870
643673ebe065 use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents: 27835
diff changeset
    11
    Text of string | Antiq of SymbolPos.T list * 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
27870
643673ebe065 use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents: 27835
diff changeset
    14
  val read: SymbolPos.T list * Position.T -> antiquote list
27779
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) ->
27870
643673ebe065 use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents: 27835
diff changeset
    16
    SymbolPos.T list * 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 |
27870
643673ebe065 use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents: 27835
diff changeset
    26
  Antiq of SymbolPos.T list * 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)))
27870
643673ebe065 use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents: 27835
diff changeset
    70
  >> (fn (pos1, (body, pos2)) => Antiq (flat body, Position.encode_range (pos1, pos2)));
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    71
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    72
in
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    73
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    74
val scan_antiquote =
27799
52f07d5292cd tuned SymbolPos interface;
wenzelm
parents: 27779
diff changeset
    75
 (Scan.repeat1 scan_txt >> (Text o SymbolPos.content o flat) ||
27750
7f5fb39c6362 Antiq: inner vs. outer position;
wenzelm
parents: 27746
diff changeset
    76
  scan_antiq ||
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    77
  SymbolPos.scan_pos --| $$$ "\\<lbrace>" >> Open ||
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    78
  SymbolPos.scan_pos --| $$$ "\\<rbrace>" >> Close);
9138
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
end;
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
27767
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    83
(* read *)
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    84
27880
4ab10bfe8a7f report antiquotations;
wenzelm
parents: 27870
diff changeset
    85
fun report_antiq (Antiq (_, pos)) = Position.report Markup.antiq pos
4ab10bfe8a7f report antiquotations;
wenzelm
parents: 27870
diff changeset
    86
  | report_antiq _ = ();
4ab10bfe8a7f report antiquotations;
wenzelm
parents: 27870
diff changeset
    87
27870
643673ebe065 use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents: 27835
diff changeset
    88
fun read ([], _) = []
643673ebe065 use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents: 27835
diff changeset
    89
  | read (syms, pos) =
643673ebe065 use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents: 27835
diff changeset
    90
      (case Scan.read SymbolPos.stopper (Scan.repeat scan_antiquote) syms of
27880
4ab10bfe8a7f report antiquotations;
wenzelm
parents: 27870
diff changeset
    91
        SOME xs => (List.app report_antiq xs; check_nesting xs; xs)
27767
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    92
      | 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
    93
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    94
27779
4569003b8813 simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents: 27767
diff changeset
    95
(* read_antiq *)
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    96
27870
643673ebe065 use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents: 27835
diff changeset
    97
fun read_antiq lex scan (syms, pos) =
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    98
  let
27870
643673ebe065 use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents: 27835
diff changeset
    99
    fun err msg = cat_error msg ("Malformed antiquotation" ^ Position.str_of pos ^ ":\n" ^
643673ebe065 use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents: 27835
diff changeset
   100
      "@{" ^ SymbolPos.content syms ^ "}");
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   101
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   102
    val res =
27870
643673ebe065 use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents: 27835
diff changeset
   103
      Source.of_list syms
27835
ff8b8513965a Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27799
diff changeset
   104
      |> T.source' {do_recover = NONE} (K (lex, Scan.empty_lexicon))
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   105
      |> T.source_proper
27767
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
   106
      |> Source.source T.stopper (Scan.error (Scan.bulk scan)) NONE
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   107
      |> Source.exhaust;
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   108
  in (case res of [x] => x | _ => err "") handle ERROR msg => err msg end;
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
   109
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   110
end;