src/Pure/General/antiquote.ML
author wenzelm
Fri, 31 Oct 2014 22:02:49 +0100
changeset 58854 b979c781c2db
parent 55653 528de9a20054
child 59065 8ce02aafc363
permissions -rw-r--r--
discontinued obsolete \<^sync> marker;
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
55526
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
     9
  type antiq = Symbol_Pos.T list * {start: Position.T, stop: Position.T, range: Position.range}
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
    10
  datatype 'a antiquote = Text of 'a | Antiq of antiq
30635
a7d175c48228 replaced Antiquote.is_antiq by Antiquote.is_text;
wenzelm
parents: 30613
diff changeset
    11
  val is_text: 'a antiquote -> bool
55612
517db8dd12c2 tuned signature;
wenzelm
parents: 55526
diff changeset
    12
  val antiq_reports: antiq -> Position.report list
517db8dd12c2 tuned signature;
wenzelm
parents: 55526
diff changeset
    13
  val antiquote_reports: ('a -> Position.report_text list) ->
48768
abc45de5bb22 clarified "bad" markup: proper body text, invent missing serial on Scala side (counting backwards);
wenzelm
parents: 48764
diff changeset
    14
    'a antiquote list -> Position.report_text list
55526
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
    15
  val scan_antiq: Symbol_Pos.T list -> antiq * Symbol_Pos.T list
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    16
  val scan_antiquote: Symbol_Pos.T list -> Symbol_Pos.T list antiquote * Symbol_Pos.T list
30641
72980f8d7ee8 export report -- version that actually covers all cases;
wenzelm
parents: 30635
diff changeset
    17
  val read: Symbol_Pos.T list * Position.T -> Symbol_Pos.T list antiquote list
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    18
end;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    19
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    20
structure Antiquote: ANTIQUOTE =
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    21
struct
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
(* datatype antiquote *)
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    24
55526
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
    25
type antiq = Symbol_Pos.T list * {start: Position.T, stop: Position.T, range: Position.range};
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
    26
datatype 'a antiquote = Text of 'a | Antiq of antiq;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    27
30635
a7d175c48228 replaced Antiquote.is_antiq by Antiquote.is_text;
wenzelm
parents: 30613
diff changeset
    28
fun is_text (Text _) = true
a7d175c48228 replaced Antiquote.is_antiq by Antiquote.is_text;
wenzelm
parents: 30613
diff changeset
    29
  | is_text _ = false;
27342
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    30
3945da15d410 added Open/Close -- checked blocks;
wenzelm
parents: 26002
diff changeset
    31
44736
c2a3f1c84179 bulk reports for improved message throughput;
wenzelm
parents: 43947
diff changeset
    32
(* reports *)
30641
72980f8d7ee8 export report -- version that actually covers all cases;
wenzelm
parents: 30635
diff changeset
    33
55612
517db8dd12c2 tuned signature;
wenzelm
parents: 55526
diff changeset
    34
fun antiq_reports ((_, {start, stop, range = (pos, _)}): antiq) =
55653
528de9a20054 more markup -- complete symbols within antiquotation, notably with broken arguments;
wenzelm
parents: 55612
diff changeset
    35
  [(start, Markup.antiquote), (stop, Markup.antiquote),
528de9a20054 more markup -- complete symbols within antiquotation, notably with broken arguments;
wenzelm
parents: 55612
diff changeset
    36
   (pos, Markup.antiquoted), (pos, Markup.language_antiquotation)];
55526
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
    37
55612
517db8dd12c2 tuned signature;
wenzelm
parents: 55526
diff changeset
    38
fun antiquote_reports text =
517db8dd12c2 tuned signature;
wenzelm
parents: 55526
diff changeset
    39
  maps (fn Text x => text x | Antiq antiq => map (rpair "") (antiq_reports antiq));
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    40
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    41
30590
1d9c9fcf8513 parameterized datatype antiquote and read operation;
wenzelm
parents: 30589
diff changeset
    42
(* scan *)
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    43
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
    44
open Basic_Symbol_Pos;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    45
22114
560c5b5dda1c tuned signature;
wenzelm
parents: 19305
diff changeset
    46
local
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    47
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 45666
diff changeset
    48
val err_prefix = "Antiquotation lexical error: ";
4fe0920d5049 proper error prefixes;
wenzelm
parents: 45666
diff changeset
    49
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    50
val scan_txt =
55511
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents: 55107
diff changeset
    51
  Scan.repeat1 ($$$ "@" --| Scan.ahead (~$$ "{") ||
58854
b979c781c2db discontinued obsolete \<^sync> marker;
wenzelm
parents: 55653
diff changeset
    52
    Scan.many1 (fn (s, _) => s <> "@" andalso Symbol.not_eof s)) >> flat;
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    53
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    54
val scan_antiq_body =
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 45666
diff changeset
    55
  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
    56
  Scan.trace (Symbol_Pos.scan_cartouche err_prefix) >> #2 ||
58854
b979c781c2db discontinued obsolete \<^sync> marker;
wenzelm
parents: 55653
diff changeset
    57
  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
    58
42508
e21362bf1d93 allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents: 42503
diff changeset
    59
in
e21362bf1d93 allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents: 42503
diff changeset
    60
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    61
val scan_antiq =
55526
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
    62
  Symbol_Pos.scan_pos -- ($$ "@" |-- $$ "{" |-- Symbol_Pos.scan_pos --
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 45666
diff changeset
    63
    Symbol_Pos.!!! (fn () => err_prefix ^ "missing closing brace")
55526
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
    64
      (Scan.repeat scan_antiq_body -- Symbol_Pos.scan_pos -- ($$ "}" |-- Symbol_Pos.scan_pos)))
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
    65
  >> (fn (pos1, (pos2, ((body, pos3), pos4))) =>
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
    66
      (flat body,
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
    67
        {start = Position.set_range (pos1, pos2),
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
    68
         stop = Position.set_range (pos3, pos4),
39708e59f4b0 more markup;
wenzelm
parents: 55512
diff changeset
    69
         range = Position.range pos1 pos4}));
30590
1d9c9fcf8513 parameterized datatype antiquote and read operation;
wenzelm
parents: 30589
diff changeset
    70
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    71
val scan_antiquote = scan_antiq >> Antiq || scan_txt >> Text;
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
end;
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    74
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    75
27767
b52c0c81dcf3 renamed scan_antiquotes to read;
wenzelm
parents: 27750
diff changeset
    76
(* read *)
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    77
30641
72980f8d7ee8 export report -- version that actually covers all cases;
wenzelm
parents: 30635
diff changeset
    78
fun read (syms, pos) =
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    79
  (case Scan.read Symbol_Pos.stopper (Scan.repeat scan_antiquote) syms of
55612
517db8dd12c2 tuned signature;
wenzelm
parents: 55526
diff changeset
    80
    SOME xs => (Position.reports_text (antiquote_reports (K []) xs); xs)
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 48768
diff changeset
    81
  | NONE => error ("Malformed quotation/antiquotation source" ^ Position.here pos));
9138
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    82
6a4fae41a75f Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff changeset
    83
end;