src/Pure/General/symbol_pos.ML
author wenzelm
Mon, 23 Aug 2021 14:24:57 +0200
changeset 74174 a3b0fc510705
parent 74171 a9e79c3645c4
child 74175 53e28c438f96
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/symbol_pos.ML
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     3
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     4
Symbols with explicit position information.
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     5
*)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     6
36957
cdb9e83abfbe tuned signature;
wenzelm
parents: 32784
diff changeset
     7
signature SYMBOL_POS =
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     8
sig
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     9
  type T = Symbol.symbol * Position.T
67426
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    10
  type 'a scanner = T list -> 'a * T list
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    11
  val symbol: T -> Symbol.symbol
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
    12
  val content: T list -> string
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54732
diff changeset
    13
  val range: T list -> Position.range
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    14
  val is_eof: T -> bool
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    15
  val stopper: T Scan.stopper
67426
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    16
  val !!! : Scan.message -> 'a scanner -> 'a scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    17
  val $$ : Symbol.symbol -> T scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    18
  val ~$$ : Symbol.symbol -> T scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    19
  val $$$ : Symbol.symbol -> T list scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    20
  val ~$$$ : Symbol.symbol -> T list scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    21
  val scan_pos: Position.T scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    22
  val scan_string_q: string -> (Position.T * (T list * Position.T)) scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    23
  val scan_string_qq: string -> (Position.T * (T list * Position.T)) scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    24
  val scan_string_bq: string -> (Position.T * (T list * Position.T)) scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    25
  val recover_string_q: T list scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    26
  val recover_string_qq: T list scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    27
  val recover_string_bq: T list scanner
43773
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
    28
  val quote_string_q: string -> string
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
    29
  val quote_string_qq: string -> string
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
    30
  val quote_string_bq: string -> string
62781
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
    31
  val cartouche_content: T list -> T list
67426
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    32
  val scan_cartouche: string -> T list scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    33
  val scan_cartouche_content: string -> T list scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    34
  val recover_cartouche: T list scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    35
  val scan_comment: string -> T list scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    36
  val scan_comment_body: string -> T list scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    37
  val recover_comment: T list scanner
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    38
  val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    39
    (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    40
  type text = string
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
    41
  val implode: T list -> text
59112
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59064
diff changeset
    42
  val implode_range: Position.range -> T list -> text * Position.range
68298
2c3ce27cf4a8 markup for deleted fragments of token source (NB: quoted tokens transform "\123" implicitly);
wenzelm
parents: 68177
diff changeset
    43
  val explode_delete: text * Position.T -> T list * Position.T list
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    44
  val explode: text * Position.T -> T list
62751
24e2b098bf44 tuned signature;
wenzelm
parents: 62529
diff changeset
    45
  val explode0: string -> T list
67426
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    46
  val scan_ident: T list scanner
50239
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
    47
  val is_identifier: string -> bool
67426
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    48
  val scan_nat: T list scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    49
  val scan_float: T list scanner
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    50
end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    51
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
    52
structure Symbol_Pos: SYMBOL_POS =
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    53
struct
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    54
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    55
(* type T *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    56
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    57
type T = Symbol.symbol * Position.T;
67426
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    58
type 'a scanner = T list -> 'a * T list;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    59
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    60
fun symbol ((s, _): T) = s;
27852
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    61
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
    62
val content = implode o map symbol;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    63
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54732
diff changeset
    64
fun range (syms as (_, pos) :: _) =
74174
a3b0fc510705 clarified signature;
wenzelm
parents: 74171
diff changeset
    65
      let val pos' = List.last syms |-> Position.symbol
62797
e08c44eed27f tuned signature;
wenzelm
parents: 62782
diff changeset
    66
      in Position.range (pos, pos') end
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54732
diff changeset
    67
  | range [] = Position.no_range;
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54732
diff changeset
    68
61705
546e6494049f trim lines for @{theory_text} similarly to @{text};
wenzelm
parents: 61502
diff changeset
    69
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    70
(* stopper *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    71
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    72
fun mk_eof pos = (Symbol.eof, pos);
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    73
val eof = mk_eof Position.none;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    74
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    75
val is_eof = Symbol.is_eof o symbol;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    76
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    77
val stopper =
74174
a3b0fc510705 clarified signature;
wenzelm
parents: 74171
diff changeset
    78
  Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.symbol)) is_eof;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    79
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    80
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    81
(* basic scanners *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    82
67426
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    83
fun !!! text (scan: 'a scanner) =
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    84
  let
48911
5debc3e4fa81 tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents: 48770
diff changeset
    85
    fun get_pos [] = " (end-of-input)"
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 48911
diff changeset
    86
      | get_pos ((_, pos) :: _) = Position.here pos;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    87
43947
9b00f09f7721 defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents: 43773
diff changeset
    88
    fun err (syms, msg) = fn () =>
48770
85eeb06ec1c4 tuned markup;
wenzelm
parents: 48764
diff changeset
    89
      text () ^ get_pos syms ^
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 48992
diff changeset
    90
      Markup.markup Markup.no_report (" at " ^ Symbol.beginning 10 (map symbol syms)) ^
43947
9b00f09f7721 defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents: 43773
diff changeset
    91
      (case msg of NONE => "" | SOME m => "\n" ^ m ());
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    92
  in Scan.!! err scan end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    93
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54732
diff changeset
    94
fun $$ s = Scan.one (fn x => symbol x = s);
55107
1a29ea173bf9 tuned signature;
wenzelm
parents: 55106
diff changeset
    95
fun ~$$ s = Scan.one (fn x => symbol x <> s);
1a29ea173bf9 tuned signature;
wenzelm
parents: 55106
diff changeset
    96
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    97
fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    98
fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    99
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   100
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   101
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   102
43773
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   103
(* scan string literals *)
30586
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   104
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   105
local
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   106
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   107
val char_code =
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   108
  Scan.one (Symbol.is_ascii_digit o symbol) --
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   109
  Scan.one (Symbol.is_ascii_digit o symbol) --
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   110
  Scan.one (Symbol.is_ascii_digit o symbol) :|--
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   111
  (fn (((a, pos), (b, _)), (c, _)) =>
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   112
    let val (n, _) = Library.read_int [a, b, c]
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   113
    in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   114
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
   115
fun scan_str q err_prefix =
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
   116
  $$$ "\\" |-- !!! (fn () => err_prefix ^ "bad escape character in string")
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
   117
    ($$$ q || $$$ "\\" || char_code) ||
58854
b979c781c2db discontinued obsolete \<^sync> marker;
wenzelm
parents: 58850
diff changeset
   118
  Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.not_eof s) >> single;
30586
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   119
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
   120
fun scan_strs q err_prefix =
55103
57d87ec3da4c tuned errors;
wenzelm
parents: 55035
diff changeset
   121
  Scan.ahead ($$ q) |--
57d87ec3da4c tuned errors;
wenzelm
parents: 55035
diff changeset
   122
    !!! (fn () => err_prefix ^ "unclosed string literal")
61476
1884c40f1539 tuned signature;
wenzelm
parents: 61456
diff changeset
   123
      ((scan_pos --| $$$ q) -- (Scan.repeats (scan_str q err_prefix) -- ($$$ q |-- scan_pos)));
30586
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   124
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   125
fun recover_strs q =
61476
1884c40f1539 tuned signature;
wenzelm
parents: 61456
diff changeset
   126
  $$$ q @@@ Scan.repeats (Scan.permissive (scan_str q ""));
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   127
30586
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   128
in
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   129
42503
27514b6fbe93 more uniform variations of scan_string;
wenzelm
parents: 41416
diff changeset
   130
val scan_string_q = scan_strs "'";
27514b6fbe93 more uniform variations of scan_string;
wenzelm
parents: 41416
diff changeset
   131
val scan_string_qq = scan_strs "\"";
27514b6fbe93 more uniform variations of scan_string;
wenzelm
parents: 41416
diff changeset
   132
val scan_string_bq = scan_strs "`";
30586
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   133
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   134
val recover_string_q = recover_strs "'";
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   135
val recover_string_qq = recover_strs "\"";
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   136
val recover_string_bq = recover_strs "`";
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   137
30586
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   138
end;
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   139
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   140
43773
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   141
(* quote string literals *)
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   142
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   143
local
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   144
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   145
fun char_code i =
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   146
  (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   147
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   148
fun quote_str q s =
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   149
  if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   150
  else if s = q orelse s = "\\" then "\\" ^ s
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   151
  else s;
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   152
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   153
fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   154
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   155
in
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   156
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   157
val quote_string_q = quote_string "'";
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   158
val quote_string_qq = quote_string "\"";
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   159
val quote_string_bq = quote_string "`";
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   160
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   161
end;
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   162
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   163
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54732
diff changeset
   164
(* nested text cartouches *)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54732
diff changeset
   165
62781
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   166
fun cartouche_content syms =
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   167
  let
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   168
    fun err () =
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   169
      error ("Malformed text cartouche: "
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   170
        ^ quote (content syms) ^ Position.here (#1 (range syms)));
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   171
  in
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   172
    (case syms of
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   173
      ("\<open>", _) :: rest =>
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   174
        (case rev rest of
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   175
          ("\<close>", _) :: rrest => rev rrest
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   176
        | _ => err ())
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   177
    | _ => err ())
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   178
  end;
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   179
55104
8284c0d5bf52 clarified scan_cartouche_depth, according to Scala version;
wenzelm
parents: 55103
diff changeset
   180
val scan_cartouche_depth =
61502
760e21900b01 clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents: 61476
diff changeset
   181
  Scan.repeat1 (Scan.depend (fn (depth: int option) =>
760e21900b01 clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents: 61476
diff changeset
   182
    (case depth of
760e21900b01 clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents: 61476
diff changeset
   183
      SOME d =>
62210
e068ea693678 tuned signature (according to Scala version);
wenzelm
parents: 61707
diff changeset
   184
        $$ Symbol.open_ >> pair (SOME (d + 1)) ||
61502
760e21900b01 clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents: 61476
diff changeset
   185
          (if d > 0 then
62210
e068ea693678 tuned signature (according to Scala version);
wenzelm
parents: 61707
diff changeset
   186
            Scan.one (fn (s, _) => s <> Symbol.close andalso Symbol.not_eof s) >> pair depth ||
e068ea693678 tuned signature (according to Scala version);
wenzelm
parents: 61707
diff changeset
   187
            $$ Symbol.close >> pair (if d = 1 then NONE else SOME (d - 1))
61502
760e21900b01 clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents: 61476
diff changeset
   188
          else Scan.fail)
760e21900b01 clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents: 61476
diff changeset
   189
    | NONE => Scan.fail)));
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54732
diff changeset
   190
55105
75815b3b38a1 tuned -- more direct err_prefix;
wenzelm
parents: 55104
diff changeset
   191
fun scan_cartouche err_prefix =
62210
e068ea693678 tuned signature (according to Scala version);
wenzelm
parents: 61707
diff changeset
   192
  Scan.ahead ($$ Symbol.open_) |--
55105
75815b3b38a1 tuned -- more direct err_prefix;
wenzelm
parents: 55104
diff changeset
   193
    !!! (fn () => err_prefix ^ "unclosed text cartouche")
61502
760e21900b01 clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents: 61476
diff changeset
   194
      (Scan.provide is_none (SOME 0) scan_cartouche_depth);
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54732
diff changeset
   195
62781
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   196
fun scan_cartouche_content err_prefix =
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   197
  scan_cartouche err_prefix >> cartouche_content;
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54732
diff changeset
   198
62781
7ba8b944d093 tuned signature;
wenzelm
parents: 62751
diff changeset
   199
val recover_cartouche = Scan.pass (SOME 0) scan_cartouche_depth;
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54732
diff changeset
   200
67413
2555713586c8 added \<^cancel> operator for unused text;
wenzelm
parents: 67361
diff changeset
   201
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   202
(* ML-style comments *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   203
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   204
local
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   205
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   206
val scan_cmt =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   207
  Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   208
  Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   209
  Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
58854
b979c781c2db discontinued obsolete \<^sync> marker;
wenzelm
parents: 58850
diff changeset
   210
  Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.not_eof s)) >> single;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   211
61476
1884c40f1539 tuned signature;
wenzelm
parents: 61456
diff changeset
   212
val scan_cmts = Scan.pass 0 (Scan.repeats scan_cmt);
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   213
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   214
in
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   215
55105
75815b3b38a1 tuned -- more direct err_prefix;
wenzelm
parents: 55104
diff changeset
   216
fun scan_comment err_prefix =
55106
080c0006e917 tuned error messages, more accurate position;
wenzelm
parents: 55105
diff changeset
   217
  Scan.ahead ($$ "(" -- $$ "*") |--
080c0006e917 tuned error messages, more accurate position;
wenzelm
parents: 55105
diff changeset
   218
    !!! (fn () => err_prefix ^ "unclosed comment")
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 58047
diff changeset
   219
      ($$$ "(" @@@ $$$ "*" @@@ scan_cmts @@@ $$$ "*" @@@ $$$ ")");
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   220
55105
75815b3b38a1 tuned -- more direct err_prefix;
wenzelm
parents: 55104
diff changeset
   221
fun scan_comment_body err_prefix =
55106
080c0006e917 tuned error messages, more accurate position;
wenzelm
parents: 55105
diff changeset
   222
  Scan.ahead ($$ "(" -- $$ "*") |--
080c0006e917 tuned error messages, more accurate position;
wenzelm
parents: 55105
diff changeset
   223
    !!! (fn () => err_prefix ^ "unclosed comment")
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 58047
diff changeset
   224
      ($$ "(" |-- $$ "*" |-- scan_cmts --| $$ "*" --| $$ ")");
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   225
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   226
val recover_comment =
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   227
  $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   228
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   229
end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   230
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   231
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   232
(* source *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   233
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   234
fun source pos =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   235
  Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
74174
a3b0fc510705 clarified signature;
wenzelm
parents: 74171
diff changeset
   236
    Scan.one Symbol.not_eof >> (fn s => (Position.symbol s pos, (s, pos))))));
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   237
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   238
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   239
(* compact representation -- with Symbol.DEL padding *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   240
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   241
type text = string;
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   242
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   243
fun pad [] = []
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   244
  | pad [(s, _)] = [s]
32784
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 30586
diff changeset
   245
  | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   246
      let
74174
a3b0fc510705 clarified signature;
wenzelm
parents: 74171
diff changeset
   247
        val end_pos1 = Position.symbol s1 pos1;
68177
6e40f5d43936 clarified signature;
wenzelm
parents: 67464
diff changeset
   248
        val d = Int.max (0, the_default 0 (Position.distance_of (end_pos1, pos2)));
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   249
      in s1 :: replicate d Symbol.DEL @ pad rest end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   250
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   251
val implode = implode o pad;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   252
59112
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59064
diff changeset
   253
fun implode_range (pos1, pos2) syms =
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   254
  let val syms' = (("", pos1) :: syms @ [("", pos2)])
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   255
  in (implode syms', range syms') end;
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   256
68298
2c3ce27cf4a8 markup for deleted fragments of token source (NB: quoted tokens transform "\123" implicitly);
wenzelm
parents: 68177
diff changeset
   257
fun explode_delete (str, pos) =
41416
a2208d3e2bd6 more scalable Symbol_Pos.explode;
wenzelm
parents: 40525
diff changeset
   258
  let
a2208d3e2bd6 more scalable Symbol_Pos.explode;
wenzelm
parents: 40525
diff changeset
   259
    val (res, _) =
74174
a3b0fc510705 clarified signature;
wenzelm
parents: 74171
diff changeset
   260
      fold (fn s => fn (res, p) => ((s, p) :: res, Position.symbol s p))
62800
7ac100f86863 tuned signature;
wenzelm
parents: 62797
diff changeset
   261
        (Symbol.explode str) ([], Position.no_range_position pos);
68298
2c3ce27cf4a8 markup for deleted fragments of token source (NB: quoted tokens transform "\123" implicitly);
wenzelm
parents: 68177
diff changeset
   262
  in
2c3ce27cf4a8 markup for deleted fragments of token source (NB: quoted tokens transform "\123" implicitly);
wenzelm
parents: 68177
diff changeset
   263
    fold (fn (s, p) => if s = Symbol.DEL then apsnd (cons p) else apfst (cons (s, p)))
2c3ce27cf4a8 markup for deleted fragments of token source (NB: quoted tokens transform "\123" implicitly);
wenzelm
parents: 68177
diff changeset
   264
      res ([], [])
2c3ce27cf4a8 markup for deleted fragments of token source (NB: quoted tokens transform "\123" implicitly);
wenzelm
parents: 68177
diff changeset
   265
  end;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   266
68298
2c3ce27cf4a8 markup for deleted fragments of token source (NB: quoted tokens transform "\123" implicitly);
wenzelm
parents: 68177
diff changeset
   267
val explode = explode_delete #> #1;
62751
24e2b098bf44 tuned signature;
wenzelm
parents: 62529
diff changeset
   268
fun explode0 str = explode (str, Position.none);
24e2b098bf44 tuned signature;
wenzelm
parents: 62529
diff changeset
   269
50239
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
   270
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
   271
(* identifiers *)
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
   272
50242
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   273
local
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   274
52616
3ac2878764f9 more robust identifier syntax: sub/superscript counts as modifier of LETDIG part instead of LETTER, both isub/isup and sub/sup are allowed;
wenzelm
parents: 50493
diff changeset
   275
val letter = Scan.one (symbol #> Symbol.is_letter);
3ac2878764f9 more robust identifier syntax: sub/superscript counts as modifier of LETDIG part instead of LETTER, both isub/isup and sub/sup are allowed;
wenzelm
parents: 50493
diff changeset
   276
val letdigs1 = Scan.many1 (symbol #> Symbol.is_letdig);
53016
fa9c38891cf2 disable old identifier syntax by default, legacy_isub_isup := true may be used temporarily as fall-back;
wenzelm
parents: 52920
diff changeset
   277
62529
8b7bdfc09f3b clarified treatment of fragments of Isabelle symbols during bootstrap;
wenzelm
parents: 62239
diff changeset
   278
val sub = Scan.one (symbol #> (fn s => s = "\<^sub>"));
50242
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   279
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   280
in
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   281
61476
1884c40f1539 tuned signature;
wenzelm
parents: 61456
diff changeset
   282
val scan_ident = letter ::: Scan.repeats (letdigs1 || sub ::: letdigs1);
50242
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   283
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   284
end;
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   285
50239
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
   286
fun is_identifier s =
50295
3d6a4135a54f eliminated redundant is_ident -- more official is_identifier;
wenzelm
parents: 50253
diff changeset
   287
  Symbol.is_ascii_identifier s orelse
62751
24e2b098bf44 tuned signature;
wenzelm
parents: 62529
diff changeset
   288
    (case try (Scan.finite stopper scan_ident) (explode0 s) of
50295
3d6a4135a54f eliminated redundant is_ident -- more official is_identifier;
wenzelm
parents: 50253
diff changeset
   289
      SOME (_, []) => true
3d6a4135a54f eliminated redundant is_ident -- more official is_identifier;
wenzelm
parents: 50253
diff changeset
   290
    | _ => false);
50239
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
   291
62782
057e8dbe4326 clarified modules;
wenzelm
parents: 62781
diff changeset
   292
057e8dbe4326 clarified modules;
wenzelm
parents: 62781
diff changeset
   293
(* numerals *)
057e8dbe4326 clarified modules;
wenzelm
parents: 62781
diff changeset
   294
057e8dbe4326 clarified modules;
wenzelm
parents: 62781
diff changeset
   295
val scan_nat = Scan.many1 (Symbol.is_digit o symbol);
057e8dbe4326 clarified modules;
wenzelm
parents: 62781
diff changeset
   296
val scan_float = scan_nat @@@ $$$ "." @@@ scan_nat;
057e8dbe4326 clarified modules;
wenzelm
parents: 62781
diff changeset
   297
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   298
end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   299
36957
cdb9e83abfbe tuned signature;
wenzelm
parents: 32784
diff changeset
   300
structure Basic_Symbol_Pos =   (*not open by default*)
cdb9e83abfbe tuned signature;
wenzelm
parents: 32784
diff changeset
   301
struct
55103
57d87ec3da4c tuned errors;
wenzelm
parents: 55035
diff changeset
   302
  val $$ = Symbol_Pos.$$;
55107
1a29ea173bf9 tuned signature;
wenzelm
parents: 55106
diff changeset
   303
  val ~$$ = Symbol_Pos.~$$;
36957
cdb9e83abfbe tuned signature;
wenzelm
parents: 32784
diff changeset
   304
  val $$$ = Symbol_Pos.$$$;
cdb9e83abfbe tuned signature;
wenzelm
parents: 32784
diff changeset
   305
  val ~$$$ = Symbol_Pos.~$$$;
cdb9e83abfbe tuned signature;
wenzelm
parents: 32784
diff changeset
   306
end;
67426
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
   307
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
   308
type 'a scanner = 'a Symbol_Pos.scanner;