src/Pure/General/antiquote.ML
author wenzelm
Sun, 18 Oct 2015 20:48:24 +0200
changeset 61475 5b58a17c440a
parent 61473 34d1913f0b20
child 61476 1884c40f1539
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}
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    10
  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
    11
  datatype 'a antiquote = Text of 'a | Control of control | Antiq of antiq
61434
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    12
  type text_antiquote = Symbol_Pos.T list antiquote
61450
239a04ec2d4c more markup;
wenzelm
parents: 61440
diff changeset
    13
  val range: text_antiquote list -> Position.range
61434
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    14
  val split_lines: text_antiquote list -> text_antiquote list list
61457
3e21699bb83b clarified Antiquote.antiq_reports;
wenzelm
parents: 61456
diff changeset
    15
  val antiq_reports: 'a antiquote list -> Position.report list
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    16
  val scan_control: Symbol_Pos.T list -> control * Symbol_Pos.T list
55526
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
    17
  val scan_antiq: Symbol_Pos.T list -> antiq * Symbol_Pos.T list
61434
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    18
  val scan_antiquote: Symbol_Pos.T list -> text_antiquote * Symbol_Pos.T list
61456
b521b8b400f7 trim_blanks after read, before eval;
wenzelm
parents: 61450
diff changeset
    19
  val read': Position.T -> Symbol_Pos.T list -> text_antiquote list
61434
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    20
  val read: Input.source -> text_antiquote list
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    21
end;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    22
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    23
structure Antiquote: ANTIQUOTE =
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    24
struct
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
(* datatype antiquote *)
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    27
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    28
type control = {range: Position.range, name: string * Position.T, body: Symbol_Pos.T list};
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    29
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
    30
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
    31
61434
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    32
type text_antiquote = Symbol_Pos.T list antiquote;
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    33
61450
239a04ec2d4c more markup;
wenzelm
parents: 61440
diff changeset
    34
fun antiquote_range (Text ss) = Symbol_Pos.range ss
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    35
  | antiquote_range (Control {range, ...}) = range
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    36
  | antiquote_range (Antiq {range, ...}) = range;
61450
239a04ec2d4c more markup;
wenzelm
parents: 61440
diff changeset
    37
239a04ec2d4c more markup;
wenzelm
parents: 61440
diff changeset
    38
fun range ants =
239a04ec2d4c more markup;
wenzelm
parents: 61440
diff changeset
    39
  if null ants then Position.no_range
239a04ec2d4c more markup;
wenzelm
parents: 61440
diff changeset
    40
  else Position.range (#1 (antiquote_range (hd ants))) (#2 (antiquote_range (List.last ants)));
239a04ec2d4c more markup;
wenzelm
parents: 61440
diff changeset
    41
61434
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    42
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    43
(* split lines *)
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    44
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    45
fun split_lines input =
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    46
  let
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    47
    fun add a (line, lines) = (a :: line, lines);
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    48
    fun flush (line, lines) = ([], rev line :: lines);
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    49
    fun split (a as Text ss) =
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    50
          (case take_prefix (fn ("\n", _) => false | _ => true) ss of
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    51
            ([], []) => I
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    52
          | (_, []) => add a
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    53
          | ([], _ :: rest) => flush #> split (Text rest)
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    54
          | (prefix, _ :: rest) => add (Text prefix) #> flush #> split (Text rest))
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    55
      | split a = add a;
61440
8626c2fed037 clarified;
wenzelm
parents: 61434
diff changeset
    56
  in if null input then [] else rev (#2 (flush (fold split input ([], [])))) end;
61434
46d6586eb04c added split_lines;
wenzelm
parents: 59112
diff changeset
    57
27342
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    58
44736
c2a3f1c84179 bulk reports for improved message throughput;
wenzelm
parents: 43947
diff changeset
    59
(* reports *)
30641
72980f8d7ee8 export report -- version that actually covers all cases;
wenzelm
parents: 30635
diff changeset
    60
61457
3e21699bb83b clarified Antiquote.antiq_reports;
wenzelm
parents: 61456
diff changeset
    61
fun antiq_reports ants = ants |> maps
61471
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    62
  (fn Text _ => []
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    63
    | Control {range = (pos, _), ...} => [(pos, Markup.antiquoted)]
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    64
    | Antiq {start, stop, range = (pos, _), ...} =>
61471
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    65
        [(start, Markup.antiquote),
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    66
         (stop, Markup.antiquote),
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    67
         (pos, Markup.antiquoted),
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    68
         (pos, Markup.language_antiquotation)]);
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    69
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    70
30590
1d9c9fcf8513 parameterized datatype antiquote and read operation;
wenzelm
parents: 30589
diff changeset
    71
(* scan *)
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    72
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
    73
open Basic_Symbol_Pos;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    74
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    75
local
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    76
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 45666
diff changeset
    77
val err_prefix = "Antiquotation lexical error: ";
4fe0920d5049 proper error prefixes;
wenzelm
parents: 45666
diff changeset
    78
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    79
val scan_txt =
61471
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    80
  Scan.repeat1
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    81
   (Scan.many1 (fn (s, _) => not (Symbol.is_control s) andalso s <> "@" andalso Symbol.not_eof s) ||
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    82
    Scan.one (fn (s, _) => Symbol.is_control s) --| Scan.ahead (~$$ "\\<open>") >> single ||
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    83
    $$$ "@" --| Scan.ahead (~$$ "{")) >> flat;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    84
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    85
val scan_antiq_body =
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 45666
diff changeset
    86
  Scan.trace (Symbol_Pos.scan_string_qq err_prefix || Symbol_Pos.scan_string_bq err_prefix) >> #2 ||
55105
75815b3b38a1 tuned -- more direct err_prefix;
wenzelm
parents: 55046
diff changeset
    87
  Scan.trace (Symbol_Pos.scan_cartouche err_prefix) >> #2 ||
58854
b979c781c2db discontinued obsolete \<^sync> marker;
wenzelm
parents: 55653
diff changeset
    88
  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
    89
42508
e21362bf1d93 allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents: 42503
diff changeset
    90
in
e21362bf1d93 allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents: 42503
diff changeset
    91
61471
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    92
val scan_control =
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
    93
  Scan.one (Symbol.is_control o Symbol_Pos.symbol) --
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    94
  Scan.trace (Symbol_Pos.scan_cartouche err_prefix) >>
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    95
    (fn ((control, pos), (_, body)) =>
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    96
      let
61475
5b58a17c440a tuned signature;
wenzelm
parents: 61473
diff changeset
    97
        val Symbol.Control name = Symbol.decode control;
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    98
        val range = Symbol_Pos.range ((control, pos) :: body);
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
    99
      in {name = (name, pos), range = range, body = body} end);
61471
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
   100
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   101
val scan_antiq =
55526
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
   102
  Symbol_Pos.scan_pos -- ($$ "@" |-- $$ "{" |-- Symbol_Pos.scan_pos --
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 45666
diff changeset
   103
    Symbol_Pos.!!! (fn () => err_prefix ^ "missing closing brace")
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   104
      (Scan.repeat scan_antiq_body -- Symbol_Pos.scan_pos -- ($$ "}" |-- Symbol_Pos.scan_pos))) >>
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   105
    (fn (pos1, (pos2, ((body, pos3), pos4))) =>
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   106
      {start = Position.set_range (pos1, pos2),
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   107
       stop = Position.set_range (pos3, pos4),
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   108
       range = Position.range pos1 pos4,
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   109
       body = flat body});
30590
1d9c9fcf8513 parameterized datatype antiquote and read operation;
wenzelm
parents: 30589
diff changeset
   110
61471
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
   111
val scan_antiquote =
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 61457
diff changeset
   112
  scan_control >> Control || scan_antiq >> Antiq || scan_txt >> Text;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   113
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   114
end;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   115
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   116
27767
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
   117
(* read *)
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   118
61456
b521b8b400f7 trim_blanks after read, before eval;
wenzelm
parents: 61450
diff changeset
   119
fun read' pos syms =
b521b8b400f7 trim_blanks after read, before eval;
wenzelm
parents: 61450
diff changeset
   120
  (case Scan.read Symbol_Pos.stopper (Scan.repeat scan_antiquote) syms of
61457
3e21699bb83b clarified Antiquote.antiq_reports;
wenzelm
parents: 61456
diff changeset
   121
    SOME ants => (Position.reports (antiq_reports ants); ants)
61456
b521b8b400f7 trim_blanks after read, before eval;
wenzelm
parents: 61450
diff changeset
   122
  | NONE => error ("Malformed quotation/antiquotation source" ^ Position.here pos));
b521b8b400f7 trim_blanks after read, before eval;
wenzelm
parents: 61450
diff changeset
   123
b521b8b400f7 trim_blanks after read, before eval;
wenzelm
parents: 61450
diff changeset
   124
fun read source = read' (Input.pos_of source) (Input.source_explode source);
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   125
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
   126
end;