src/Pure/General/antiquote.ML
author wenzelm
Wed, 02 Mar 2022 21:14:09 +0100
changeset 75189 f304a2a5080f
parent 74373 6e4093927dbb
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30587
ad19c99529eb moved Isar/antiquote.ML to General/antiquote.ML, which is loaded early;
wenzelm
parents: 30573
diff changeset
     1
(*  Title:      Pure/General/antiquote.ML
55511
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents: 55107
diff changeset
     2
    Author:     Makarius
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
     3
55511
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents: 55107
diff changeset
     4
Antiquotations within plain text.
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
     5
*)
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
signature ANTIQUOTE =
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
     8
sig
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
     9
  type control = {range: Position.range, name: string * Position.T, body: Symbol_Pos.T list}
74373
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
    10
  val no_control: control
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
    11
  val control_symbols: control -> Symbol_Pos.T list
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    12
  type antiq = {start: Position.T, stop: Position.T, range: Position.range, body: Symbol_Pos.T list}
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    13
  datatype 'a antiquote = Text of 'a | Control of control | Antiq of antiq
73551
53c148e39819 proper treatment of nested antiquotations;
wenzelm
parents: 73550
diff changeset
    14
  val is_text: 'a antiquote -> bool
53c148e39819 proper treatment of nested antiquotations;
wenzelm
parents: 73550
diff changeset
    15
  val the_text: string -> 'a antiquote -> 'a
61434
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    16
  type text_antiquote = Symbol_Pos.T list antiquote
67467
482b62d694ca clarified signature;
wenzelm
parents: 67426
diff changeset
    17
  val text_antiquote_range: text_antiquote -> Position.range
482b62d694ca clarified signature;
wenzelm
parents: 67426
diff changeset
    18
  val text_range: text_antiquote list -> Position.range
61434
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    19
  val split_lines: text_antiquote list -> text_antiquote list list
61457
3e21699bb83b clarified Antiquote.antiq_reports;
wenzelm
parents: 61456
diff changeset
    20
  val antiq_reports: 'a antiquote list -> Position.report list
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
    21
  val update_reports: bool -> Position.T -> string list -> Position.report_text list
74373
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
    22
  val err_prefix: string
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
    23
  val scan_control: string -> control scanner
67426
6311cf9dc943 clarified signature;
wenzelm
parents: 67193
diff changeset
    24
  val scan_antiq: antiq scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67193
diff changeset
    25
  val scan_antiquote: text_antiquote scanner
67571
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
    26
  val scan_antiquote_comments: text_antiquote scanner
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
    27
  val parse_comments: Position.T -> Symbol_Pos.T list -> text_antiquote list
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
    28
  val read_comments: Input.source -> text_antiquote list
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    29
end;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    30
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    31
structure Antiquote: ANTIQUOTE =
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    32
struct
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    33
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    34
(* datatype antiquote *)
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    35
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    36
type control = {range: Position.range, name: string * Position.T, body: Symbol_Pos.T list};
74373
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
    37
val no_control = {range = Position.no_range, name = ("", Position.none), body = []};
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
    38
fun control_symbols ({name = (name, pos), body, ...}: control) =
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
    39
  (Symbol.encode (Symbol.Control name), pos) :: body;
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
    40
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    41
type antiq = {start: Position.T, stop: Position.T, range: Position.range, body: Symbol_Pos.T list};
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    42
datatype 'a antiquote = Text of 'a | Control of control | Antiq of antiq;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    43
73551
53c148e39819 proper treatment of nested antiquotations;
wenzelm
parents: 73550
diff changeset
    44
val is_text = fn Text _ => true | _ => false;
53c148e39819 proper treatment of nested antiquotations;
wenzelm
parents: 73550
diff changeset
    45
53c148e39819 proper treatment of nested antiquotations;
wenzelm
parents: 73550
diff changeset
    46
fun the_text msg antiq =
53c148e39819 proper treatment of nested antiquotations;
wenzelm
parents: 73550
diff changeset
    47
  let fun err pos = error (msg ^ Position.here pos) in
53c148e39819 proper treatment of nested antiquotations;
wenzelm
parents: 73550
diff changeset
    48
    (case antiq of
53c148e39819 proper treatment of nested antiquotations;
wenzelm
parents: 73550
diff changeset
    49
      Text x => x
53c148e39819 proper treatment of nested antiquotations;
wenzelm
parents: 73550
diff changeset
    50
    | Control {range, ...} => err (#1 range)
53c148e39819 proper treatment of nested antiquotations;
wenzelm
parents: 73550
diff changeset
    51
    | Antiq {range, ...} => err (#1 range))
53c148e39819 proper treatment of nested antiquotations;
wenzelm
parents: 73550
diff changeset
    52
  end;
73550
2f6855142a8c support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents: 69891
diff changeset
    53
61434
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    54
type text_antiquote = Symbol_Pos.T list antiquote;
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    55
67467
482b62d694ca clarified signature;
wenzelm
parents: 67426
diff changeset
    56
fun text_antiquote_range (Text ss) = Symbol_Pos.range ss
482b62d694ca clarified signature;
wenzelm
parents: 67426
diff changeset
    57
  | text_antiquote_range (Control {range, ...}) = range
482b62d694ca clarified signature;
wenzelm
parents: 67426
diff changeset
    58
  | text_antiquote_range (Antiq {range, ...}) = range;
61450
239a04ec2d4c more markup;
wenzelm
parents: 61440
diff changeset
    59
67467
482b62d694ca clarified signature;
wenzelm
parents: 67426
diff changeset
    60
fun text_range ants =
61450
239a04ec2d4c more markup;
wenzelm
parents: 61440
diff changeset
    61
  if null ants then Position.no_range
67467
482b62d694ca clarified signature;
wenzelm
parents: 67426
diff changeset
    62
  else
482b62d694ca clarified signature;
wenzelm
parents: 67426
diff changeset
    63
    Position.range (#1 (text_antiquote_range (hd ants)), #2 (text_antiquote_range (List.last ants)));
61450
239a04ec2d4c more markup;
wenzelm
parents: 61440
diff changeset
    64
61434
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    65
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    66
(* split lines *)
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    67
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    68
fun split_lines input =
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    69
  let
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    70
    fun add a (line, lines) = (a :: line, lines);
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    71
    fun flush (line, lines) = ([], rev line :: lines);
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    72
    fun split (a as Text ss) =
67522
9e712280cc37 clarified take/drop/chop prefix/suffix;
wenzelm
parents: 67467
diff changeset
    73
          (case chop_prefix (fn ("\n", _) => false | _ => true) ss of
61434
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    74
            ([], []) => I
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    75
          | (_, []) => add a
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    76
          | ([], _ :: rest) => flush #> split (Text rest)
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    77
          | (prefix, _ :: rest) => add (Text prefix) #> flush #> split (Text rest))
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    78
      | split a = add a;
61440
8626c2fed037 clarified;
wenzelm
parents: 61434
diff changeset
    79
  in if null input then [] else rev (#2 (flush (fold split input ([], [])))) end;
61434
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    80
27342
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    81
44736
c2a3f1c84179 bulk reports for improved message throughput;
wenzelm
parents: 43947
diff changeset
    82
(* reports *)
30641
72980f8d7ee8 export report -- version that actually covers all cases;
wenzelm
parents: 30635
diff changeset
    83
61457
3e21699bb83b clarified Antiquote.antiq_reports;
wenzelm
parents: 61456
diff changeset
    84
fun antiq_reports ants = ants |> maps
61471
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    85
  (fn Text _ => []
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    86
    | Control {range = (pos, _), ...} => [(pos, Markup.antiquoted)]
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    87
    | Antiq {start, stop, range = (pos, _), ...} =>
61471
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    88
        [(start, Markup.antiquote),
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    89
         (stop, Markup.antiquote),
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    90
         (pos, Markup.antiquoted),
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    91
         (pos, Markup.language_antiquotation)]);
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
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
    94
(* update *)
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
    95
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
    96
fun update_reports embedded pos src =
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
    97
  let
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
    98
    val n = length src;
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
    99
    val no_arg = n = 1;
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   100
    val embedded_arg = n = 2 andalso embedded;
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   101
    val control =
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   102
      (case src of
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   103
        name :: _ =>
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   104
          if Symbol.is_ascii_identifier name andalso name <> "cartouche" andalso
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   105
            (no_arg orelse embedded_arg)
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   106
          then SOME (Symbol.control_prefix ^ name ^ Symbol.control_suffix)
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   107
          else NONE
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   108
      | [] => NONE);
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   109
    val arg = if embedded_arg then cartouche (nth src 1) else "";
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   110
  in
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   111
    (case control of
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   112
      SOME sym => [((pos, Markup.update), sym ^ arg)]
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   113
    | NONE => [])
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   114
  end;
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   115
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   116
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 67735
diff changeset
   117
30590
1d9c9fcf8513 parameterized datatype antiquote and read operation;
wenzelm
parents: 30589
diff changeset
   118
(* scan *)
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   119
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
   120
open Basic_Symbol_Pos;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   121
74373
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
   122
val err_prefix = "Antiquotation lexical error: ";
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   123
74373
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
   124
local
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 45666
diff changeset
   125
67193
4ade0d387429 scan only one line, for more detailed positions;
wenzelm
parents: 62797
diff changeset
   126
val scan_nl = Scan.one (fn (s, _) => s = "\n") >> single;
67571
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   127
val scan_nl_opt = Scan.optional scan_nl [];
67193
4ade0d387429 scan only one line, for more detailed positions;
wenzelm
parents: 62797
diff changeset
   128
67571
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   129
val scan_plain_txt =
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   130
  Scan.many1 (fn (s, _) =>
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   131
    not (Comment.is_symbol s) andalso
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   132
    not (Symbol.is_control s) andalso
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   133
    s <> Symbol.open_ andalso
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   134
    s <> "@" andalso
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   135
    s <> "\n" andalso
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   136
    Symbol.not_eof s) ||
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   137
  Scan.one (Comment.is_symbol o Symbol_Pos.symbol) >> single ||
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   138
  $$$ "@" --| Scan.ahead (~$$ "{");
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   139
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   140
val scan_text =
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   141
  scan_nl || Scan.repeats1 scan_plain_txt @@@ scan_nl_opt;
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   142
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   143
val scan_text_comments =
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69592
diff changeset
   144
  scan_nl || Scan.repeats1 (Comment.scan_inner >> #2 || scan_plain_txt) @@@ scan_nl_opt;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   145
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
   146
val scan_antiq_body =
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 45666
diff changeset
   147
  Scan.trace (Symbol_Pos.scan_string_qq err_prefix || Symbol_Pos.scan_string_bq err_prefix) >> #2 ||
61481
wenzelm
parents: 61476
diff changeset
   148
  Symbol_Pos.scan_cartouche err_prefix ||
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69592
diff changeset
   149
  Comment.scan_inner --
67735
e2e002d4a4de clarified syntax: reject formal comments explicitly, instead of ignoring them silently;
wenzelm
parents: 67571
diff changeset
   150
    Symbol_Pos.!!! (fn () => err_prefix ^ "bad formal comment in antiquote body") Scan.fail
e2e002d4a4de clarified syntax: reject formal comments explicitly, instead of ignoring them silently;
wenzelm
parents: 67571
diff changeset
   151
    >> K [] ||
58854
b979c781c2db discontinued obsolete \<^sync> marker;
wenzelm
parents: 55653
diff changeset
   152
  Scan.one (fn (s, _) => s <> "}" andalso Symbol.not_eof s) >> single;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   153
61491
97261e6c1d42 another antiquotation short form: undecorated cartouche as alias for @{text};
wenzelm
parents: 61481
diff changeset
   154
fun control_name sym = (case Symbol.decode sym of Symbol.Control name => name);
97261e6c1d42 another antiquotation short form: undecorated cartouche as alias for @{text};
wenzelm
parents: 61481
diff changeset
   155
42508
e21362bf1d93 allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents: 42503
diff changeset
   156
in
e21362bf1d93 allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents: 42503
diff changeset
   157
74373
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
   158
fun scan_control err =
61491
97261e6c1d42 another antiquotation short form: undecorated cartouche as alias for @{text};
wenzelm
parents: 61481
diff changeset
   159
  Scan.option (Scan.one (Symbol.is_control o Symbol_Pos.symbol)) --
74373
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
   160
  Symbol_Pos.scan_cartouche err >>
61491
97261e6c1d42 another antiquotation short form: undecorated cartouche as alias for @{text};
wenzelm
parents: 61481
diff changeset
   161
    (fn (opt_control, body) =>
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   162
      let
61491
97261e6c1d42 another antiquotation short form: undecorated cartouche as alias for @{text};
wenzelm
parents: 61481
diff changeset
   163
        val (name, range) =
97261e6c1d42 another antiquotation short form: undecorated cartouche as alias for @{text};
wenzelm
parents: 61481
diff changeset
   164
          (case opt_control of
97261e6c1d42 another antiquotation short form: undecorated cartouche as alias for @{text};
wenzelm
parents: 61481
diff changeset
   165
            SOME (sym, pos) => ((control_name sym, pos), Symbol_Pos.range ((sym, pos) :: body))
97261e6c1d42 another antiquotation short form: undecorated cartouche as alias for @{text};
wenzelm
parents: 61481
diff changeset
   166
          | NONE => (("cartouche", #2 (hd body)), Symbol_Pos.range body));
61595
3591274c607e more formal treatment of control symbols;
wenzelm
parents: 61491
diff changeset
   167
      in {name = name, range = range, body = body} end) ||
3591274c607e more formal treatment of control symbols;
wenzelm
parents: 61491
diff changeset
   168
  Scan.one (Symbol.is_control o Symbol_Pos.symbol) >>
3591274c607e more formal treatment of control symbols;
wenzelm
parents: 61491
diff changeset
   169
    (fn (sym, pos) =>
3591274c607e more formal treatment of control symbols;
wenzelm
parents: 61491
diff changeset
   170
      {name = (control_name sym, pos), range = Symbol_Pos.range [(sym, pos)], body = []});
61471
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
   171
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   172
val scan_antiq =
55526
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
   173
  Symbol_Pos.scan_pos -- ($$ "@" |-- $$ "{" |-- Symbol_Pos.scan_pos --
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 45666
diff changeset
   174
    Symbol_Pos.!!! (fn () => err_prefix ^ "missing closing brace")
61476
1884c40f1539 tuned signature;
wenzelm
parents: 61475
diff changeset
   175
      (Scan.repeats scan_antiq_body -- Symbol_Pos.scan_pos -- ($$ "}" |-- Symbol_Pos.scan_pos))) >>
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   176
    (fn (pos1, (pos2, ((body, pos3), pos4))) =>
62797
e08c44eed27f tuned signature;
wenzelm
parents: 62749
diff changeset
   177
      {start = Position.range_position (pos1, pos2),
e08c44eed27f tuned signature;
wenzelm
parents: 62749
diff changeset
   178
       stop = Position.range_position (pos3, pos4),
e08c44eed27f tuned signature;
wenzelm
parents: 62749
diff changeset
   179
       range = Position.range (pos1, pos4),
61476
1884c40f1539 tuned signature;
wenzelm
parents: 61475
diff changeset
   180
       body = body});
30590
1d9c9fcf8513 parameterized datatype antiquote and read operation;
wenzelm
parents: 30589
diff changeset
   181
61471
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
   182
val scan_antiquote =
74373
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
   183
  scan_text >> Text || scan_control err_prefix >> Control || scan_antiq >> Antiq;
67571
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   184
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   185
val scan_antiquote_comments =
74373
6e4093927dbb outer syntax: support for control-cartouche tokens;
wenzelm
parents: 73551
diff changeset
   186
  scan_text_comments >> Text || scan_control err_prefix >> Control || scan_antiq >> Antiq;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   187
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   188
end;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   189
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   190
67571
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   191
(* parse and read (with formal comments) *)
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   192
67571
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   193
fun parse_comments pos syms =
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   194
  (case Scan.read Symbol_Pos.stopper (Scan.repeat scan_antiquote_comments) syms of
62749
eba34ff9671c clarified reports;
wenzelm
parents: 62213
diff changeset
   195
    SOME ants => ants
61456
b521b8b400f7 trim_blanks after read, before eval;
wenzelm
parents: 61450
diff changeset
   196
  | NONE => error ("Malformed quotation/antiquotation source" ^ Position.here pos));
b521b8b400f7 trim_blanks after read, before eval;
wenzelm
parents: 61450
diff changeset
   197
67571
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   198
fun read_comments source =
62749
eba34ff9671c clarified reports;
wenzelm
parents: 62213
diff changeset
   199
  let
67571
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67522
diff changeset
   200
    val ants = parse_comments (Input.pos_of source) (Input.source_explode source);
62749
eba34ff9671c clarified reports;
wenzelm
parents: 62213
diff changeset
   201
    val _ = Position.reports (antiq_reports ants);
eba34ff9671c clarified reports;
wenzelm
parents: 62213
diff changeset
   202
  in ants end;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   203
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   204
end;