src/Pure/General/symbol_pos.ML
author wenzelm
Wed, 12 Dec 2012 17:44:10 +0100
changeset 50493 2bf3bfbb422d
parent 50295 3d6a4135a54f
child 52616 3ac2878764f9
permissions -rw-r--r--
more systematic identifier variants to facilitate experimentation;
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
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    10
  val symbol: T -> Symbol.symbol
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    11
  val $$$ : Symbol.symbol -> T list -> T list * T list
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    12
  val ~$$$ : Symbol.symbol -> T list -> T list * T list
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
    13
  val content: T list -> string
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
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
    16
  val !!! : Scan.message -> (T list -> 'a) -> T list -> 'a
30586
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
    17
  val change_prompt: ('a -> 'b) -> 'a -> 'b
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    18
  val scan_pos: T list -> Position.T * T list
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
    19
  val scan_string_q: string -> T list -> (Position.T * (T list * Position.T)) * T list
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
    20
  val scan_string_qq: string -> T list -> (Position.T * (T list * Position.T)) * T list
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
    21
  val scan_string_bq: string -> T list -> (Position.T * (T list * Position.T)) * T list
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
    22
  val recover_string_q: T list -> T list * T list
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
    23
  val recover_string_qq: T list -> T list * T list
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
    24
  val recover_string_bq: T list -> T list * T list
43773
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
    25
  val quote_string_q: string -> string
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
    26
  val quote_string_qq: string -> string
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
    27
  val quote_string_bq: string -> string
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    28
  val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    29
    T list -> T list * T list
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    30
  val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    31
    T list -> T list * T list
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
    32
  val recover_comment: T list -> T list * T list
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    33
  val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    34
    (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
    35
  type text = string
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
    36
  val implode: T list -> text
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
    37
  val range: T list -> Position.range
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
    38
  val implode_range: Position.T -> Position.T -> T list -> text * Position.range
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    39
  val explode: text * Position.T -> T list
50239
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
    40
  val scan_ident: T list -> T list * T list
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
    41
  val is_identifier: string -> bool
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    42
end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    43
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
    44
structure Symbol_Pos: SYMBOL_POS =
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    45
struct
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    46
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    47
(* type T *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    48
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    49
type T = Symbol.symbol * Position.T;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    50
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    51
fun symbol ((s, _): T) = s;
27852
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    52
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
    53
val content = implode o map symbol;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    54
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    55
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    56
(* stopper *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    57
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    58
fun mk_eof pos = (Symbol.eof, pos);
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    59
val eof = mk_eof Position.none;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    60
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    61
val is_eof = Symbol.is_eof o symbol;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    62
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    63
val stopper =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    64
  Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    65
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    66
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    67
(* basic scanners *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    68
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    69
fun !!! text scan =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    70
  let
48911
5debc3e4fa81 tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents: 48770
diff changeset
    71
    fun get_pos [] = " (end-of-input)"
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 48911
diff changeset
    72
      | get_pos ((_, pos) :: _) = Position.here pos;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    73
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
    74
    fun err (syms, msg) = fn () =>
48770
85eeb06ec1c4 tuned markup;
wenzelm
parents: 48764
diff changeset
    75
      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
    76
      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
    77
      (case msg of NONE => "" | SOME m => "\n" ^ m ());
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    78
  in Scan.!! err scan end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    79
30586
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
    80
fun change_prompt scan = Scan.prompt "# " scan;
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
    81
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    82
fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    83
fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    84
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    85
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    86
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    87
43773
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
    88
(* 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
    89
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
    90
local
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
    91
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
    92
val char_code =
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
    93
  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
    94
  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
    95
  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
    96
  (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
    97
    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
    98
    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
    99
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
   100
fun scan_str q err_prefix =
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
   101
  $$$ "\\" |-- !!! (fn () => err_prefix ^ "bad escape character in string")
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
   102
    ($$$ q || $$$ "\\" || char_code) ||
30586
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   103
  Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   104
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
   105
fun scan_strs q err_prefix =
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
   106
  (scan_pos --| $$$ q) -- !!! (fn () => err_prefix ^ "missing quote at end of string")
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
   107
    (change_prompt ((Scan.repeat (scan_str q err_prefix) >> flat) -- ($$$ 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
   108
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   109
fun recover_strs q =
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48743
diff changeset
   110
  $$$ q @@@ (Scan.repeat (Scan.permissive (scan_str q "")) >> flat);
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   111
30586
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   112
in
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   113
42503
27514b6fbe93 more uniform variations of scan_string;
wenzelm
parents: 41416
diff changeset
   114
val scan_string_q = scan_strs "'";
27514b6fbe93 more uniform variations of scan_string;
wenzelm
parents: 41416
diff changeset
   115
val scan_string_qq = scan_strs "\"";
27514b6fbe93 more uniform variations of scan_string;
wenzelm
parents: 41416
diff changeset
   116
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
   117
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   118
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
   119
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
   120
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
   121
30586
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   122
end;
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   123
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   124
43773
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   125
(* quote string literals *)
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   126
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   127
local
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   128
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   129
fun char_code i =
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   130
  (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
   131
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   132
fun quote_str q s =
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   133
  if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   134
  else if s = q orelse s = "\\" then "\\" ^ s
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   135
  else s;
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   136
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   137
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
   138
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   139
in
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   140
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   141
val quote_string_q = quote_string "'";
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   142
val quote_string_qq = quote_string "\"";
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   143
val quote_string_bq = quote_string "`";
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
end;
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   146
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43709
diff changeset
   147
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   148
(* ML-style comments *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   149
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   150
local
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   151
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   152
val scan_cmt =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   153
  Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   154
  Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   155
  Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   156
  Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   157
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   158
val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   159
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   160
val scan_body = change_prompt scan_cmts;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   161
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   162
in
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   163
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   164
fun scan_comment cut =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   165
  $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   166
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   167
fun scan_comment_body cut =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   168
  $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   169
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   170
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
   171
  $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 43947
diff changeset
   172
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   173
end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   174
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   175
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   176
(* source *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   177
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   178
fun source pos =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   179
  Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   180
    Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   181
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   182
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   183
(* compact representation -- with Symbol.DEL padding *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   184
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   185
type text = string;
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   186
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   187
fun pad [] = []
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   188
  | pad [(s, _)] = [s]
32784
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 30586
diff changeset
   189
  | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   190
      let
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   191
        val end_pos1 = Position.advance s1 pos1;
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   192
        val d = Int.max (0, Position.distance_of end_pos1 pos2);
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   193
      in s1 :: replicate d Symbol.DEL @ pad rest end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   194
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   195
val implode = implode o pad;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   196
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   197
fun range (syms as (_, pos) :: _) =
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   198
      let val pos' = List.last syms |-> Position.advance
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   199
      in Position.range pos pos' end
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   200
  | range [] = Position.no_range;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   201
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   202
fun implode_range pos1 pos2 syms =
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   203
  let val syms' = (("", pos1) :: syms @ [("", pos2)])
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   204
  in (implode syms', range syms') end;
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   205
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   206
fun explode (str, pos) =
41416
a2208d3e2bd6 more scalable Symbol_Pos.explode;
wenzelm
parents: 40525
diff changeset
   207
  let
a2208d3e2bd6 more scalable Symbol_Pos.explode;
wenzelm
parents: 40525
diff changeset
   208
    val (res, _) =
a2208d3e2bd6 more scalable Symbol_Pos.explode;
wenzelm
parents: 40525
diff changeset
   209
      fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
a2208d3e2bd6 more scalable Symbol_Pos.explode;
wenzelm
parents: 40525
diff changeset
   210
        (Symbol.explode str) ([], Position.reset_range pos);
a2208d3e2bd6 more scalable Symbol_Pos.explode;
wenzelm
parents: 40525
diff changeset
   211
  in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   212
50239
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
   213
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
   214
(* identifiers *)
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
   215
50242
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   216
local
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   217
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   218
val latin = Symbol.is_ascii_letter;
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   219
val digit = Symbol.is_ascii_digit;
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   220
fun underscore s = s = "_";
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   221
fun prime s = s = "'";
50493
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   222
fun subscript s = s = "\\<^sub>" orelse s = "\\<^isub>";
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   223
fun script s = s = "\\<^sub>" orelse s = "\\<^isub>" orelse s = "\\<^isup>";
50242
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   224
fun special_letter s = Symbol.is_letter_symbol s andalso not (script s);
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   225
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   226
val scan_plain = Scan.one ((latin orf digit orf prime) o symbol) >> single;
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   227
val scan_digit = Scan.one (digit o symbol) >> single;
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   228
val scan_prime = Scan.one (prime o symbol) >> single;
50493
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   229
val scan_extended =
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   230
  Scan.one ((latin orf digit orf prime orf underscore orf special_letter) o symbol) >> single;
50242
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   231
50493
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   232
val scan_subscript =
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   233
  Scan.one (subscript o symbol) --
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   234
  Scan.one ((latin orf digit orf prime orf special_letter) o symbol)
50242
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   235
  >> (fn (x, y) => [x, y]);
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   236
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   237
val scan_ident_part1 =
50493
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   238
  Scan.one (latin o symbol) ::: (Scan.repeat (scan_plain || scan_subscript) >> flat) ||
50242
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   239
  Scan.one (special_letter o symbol) :::
50493
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   240
    (Scan.repeat (scan_digit || scan_prime || scan_subscript) >> flat);
50242
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   241
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   242
val scan_ident_part2 =
50493
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   243
  Scan.repeat1 (scan_plain || scan_subscript) >> flat ||
50242
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   244
  scan_ident_part1;
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   245
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   246
in
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   247
50493
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   248
val scan_ident0 =
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   249
  Scan.one (Symbol.is_letter o symbol) ::: Scan.many (Symbol.is_letdig o symbol);
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   250
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   251
val scan_ident1 =
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   252
  Scan.one ((latin orf special_letter) o symbol) :::
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   253
    (Scan.repeat (scan_extended || Scan.one (subscript o symbol) ::: scan_extended) >> flat);
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   254
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   255
val scan_ident2 =
50242
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   256
  scan_ident_part1 @@@
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   257
    (Scan.repeat (Scan.many1 (underscore o symbol) @@@ scan_ident_part2) >> flat);
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   258
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   259
end;
56b9c792a98b support for sub-structured identifier syntax (inactive);
wenzelm
parents: 50239
diff changeset
   260
50493
2bf3bfbb422d more systematic identifier variants to facilitate experimentation;
wenzelm
parents: 50295
diff changeset
   261
val scan_ident = scan_ident0;
50239
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
   262
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
   263
fun is_identifier s =
50295
3d6a4135a54f eliminated redundant is_ident -- more official is_identifier;
wenzelm
parents: 50253
diff changeset
   264
  Symbol.is_ascii_identifier s orelse
3d6a4135a54f eliminated redundant is_ident -- more official is_identifier;
wenzelm
parents: 50253
diff changeset
   265
    (case try (Scan.finite stopper scan_ident) (explode (s, Position.none)) of
3d6a4135a54f eliminated redundant is_ident -- more official is_identifier;
wenzelm
parents: 50253
diff changeset
   266
      SOME (_, []) => true
3d6a4135a54f eliminated redundant is_ident -- more official is_identifier;
wenzelm
parents: 50253
diff changeset
   267
    | _ => false);
50239
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
   268
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   269
end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   270
36957
cdb9e83abfbe tuned signature;
wenzelm
parents: 32784
diff changeset
   271
structure Basic_Symbol_Pos =   (*not open by default*)
cdb9e83abfbe tuned signature;
wenzelm
parents: 32784
diff changeset
   272
struct
cdb9e83abfbe tuned signature;
wenzelm
parents: 32784
diff changeset
   273
  val $$$ = Symbol_Pos.$$$;
cdb9e83abfbe tuned signature;
wenzelm
parents: 32784
diff changeset
   274
  val ~$$$ = Symbol_Pos.~$$$;
cdb9e83abfbe tuned signature;
wenzelm
parents: 32784
diff changeset
   275
end;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   276