src/Pure/General/symbol_pos.ML
author wenzelm
Thu Aug 09 14:37:43 2012 +0200 (2012-08-09)
changeset 48743 a72f8ffecf31
parent 43947 9b00f09f7721
child 48764 4fe0920d5049
permissions -rw-r--r--
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm@27763
     1
(*  Title:      Pure/General/symbol_pos.ML
wenzelm@27763
     2
    Author:     Makarius
wenzelm@27763
     3
wenzelm@27763
     4
Symbols with explicit position information.
wenzelm@27763
     5
*)
wenzelm@27763
     6
wenzelm@36957
     7
signature SYMBOL_POS =
wenzelm@27763
     8
sig
wenzelm@27763
     9
  type T = Symbol.symbol * Position.T
wenzelm@27763
    10
  val symbol: T -> Symbol.symbol
wenzelm@27763
    11
  val $$$ : Symbol.symbol -> T list -> T list * T list
wenzelm@27763
    12
  val ~$$$ : Symbol.symbol -> T list -> T list * T list
wenzelm@27797
    13
  val content: T list -> string
wenzelm@27763
    14
  val is_eof: T -> bool
wenzelm@27763
    15
  val stopper: T Scan.stopper
wenzelm@43947
    16
  val !!! : Scan.message -> (T list -> 'a) -> T list -> 'a
wenzelm@30586
    17
  val change_prompt: ('a -> 'b) -> 'a -> 'b
wenzelm@27778
    18
  val scan_pos: T list -> Position.T * T list
wenzelm@42503
    19
  val scan_string_q: T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@42503
    20
  val scan_string_qq: T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@42503
    21
  val scan_string_bq: T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@48743
    22
  val recover_string_q: T list -> T list * T list
wenzelm@48743
    23
  val recover_string_qq: T list -> T list * T list
wenzelm@48743
    24
  val recover_string_bq: T list -> T list * T list
wenzelm@43773
    25
  val quote_string_q: string -> string
wenzelm@43773
    26
  val quote_string_qq: string -> string
wenzelm@43773
    27
  val quote_string_bq: string -> string
wenzelm@27763
    28
  val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
wenzelm@27763
    29
    T list -> T list * T list
wenzelm@27763
    30
  val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
wenzelm@27763
    31
    T list -> T list * T list
wenzelm@48743
    32
  val recover_comment: T list -> T list * T list
wenzelm@27763
    33
  val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
wenzelm@27763
    34
    (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
wenzelm@27778
    35
  type text = string
wenzelm@27797
    36
  val implode: T list -> text
wenzelm@27797
    37
  val range: T list -> Position.range
wenzelm@27797
    38
  val implode_range: Position.T -> Position.T -> T list -> text * Position.range
wenzelm@27778
    39
  val explode: text * Position.T -> T list
wenzelm@27763
    40
end;
wenzelm@27763
    41
wenzelm@30573
    42
structure Symbol_Pos: SYMBOL_POS =
wenzelm@27763
    43
struct
wenzelm@27763
    44
wenzelm@27763
    45
(* type T *)
wenzelm@27763
    46
wenzelm@27763
    47
type T = Symbol.symbol * Position.T;
wenzelm@27763
    48
wenzelm@27763
    49
fun symbol ((s, _): T) = s;
wenzelm@27852
    50
wenzelm@27797
    51
val content = implode o map symbol;
wenzelm@27763
    52
wenzelm@27763
    53
wenzelm@27763
    54
(* stopper *)
wenzelm@27763
    55
wenzelm@27763
    56
fun mk_eof pos = (Symbol.eof, pos);
wenzelm@27763
    57
val eof = mk_eof Position.none;
wenzelm@27763
    58
wenzelm@27763
    59
val is_eof = Symbol.is_eof o symbol;
wenzelm@27763
    60
wenzelm@27763
    61
val stopper =
wenzelm@27763
    62
  Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
wenzelm@27763
    63
wenzelm@27763
    64
wenzelm@27763
    65
(* basic scanners *)
wenzelm@27763
    66
wenzelm@27763
    67
fun !!! text scan =
wenzelm@27763
    68
  let
wenzelm@27763
    69
    fun get_pos [] = " (past end-of-text!)"
wenzelm@27763
    70
      | get_pos ((_, pos) :: _) = Position.str_of pos;
wenzelm@27763
    71
wenzelm@43947
    72
    fun err (syms, msg) = fn () =>
wenzelm@43947
    73
      text () ^ get_pos syms ^ " at " ^ Symbol.beginning 10 (map symbol syms) ^
wenzelm@43947
    74
      (case msg of NONE => "" | SOME m => "\n" ^ m ());
wenzelm@27763
    75
  in Scan.!! err scan end;
wenzelm@27763
    76
wenzelm@30586
    77
fun change_prompt scan = Scan.prompt "# " scan;
wenzelm@30586
    78
wenzelm@27763
    79
fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
wenzelm@27763
    80
fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
wenzelm@27763
    81
wenzelm@27778
    82
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
wenzelm@27763
    83
wenzelm@27763
    84
wenzelm@43773
    85
(* scan string literals *)
wenzelm@30586
    86
wenzelm@30586
    87
local
wenzelm@30586
    88
wenzelm@30586
    89
val char_code =
wenzelm@30586
    90
  Scan.one (Symbol.is_ascii_digit o symbol) --
wenzelm@30586
    91
  Scan.one (Symbol.is_ascii_digit o symbol) --
wenzelm@30586
    92
  Scan.one (Symbol.is_ascii_digit o symbol) :|--
wenzelm@30586
    93
  (fn (((a, pos), (b, _)), (c, _)) =>
wenzelm@30586
    94
    let val (n, _) = Library.read_int [a, b, c]
wenzelm@30586
    95
    in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
wenzelm@30586
    96
wenzelm@30586
    97
fun scan_str q =
wenzelm@43947
    98
  $$$ "\\" |-- !!! (fn () => "bad escape character in string") ($$$ q || $$$ "\\" || char_code) ||
wenzelm@30586
    99
  Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
wenzelm@30586
   100
wenzelm@30586
   101
fun scan_strs q =
wenzelm@43947
   102
  (scan_pos --| $$$ q) -- !!! (fn () => "missing quote at end of string")
wenzelm@30586
   103
    (change_prompt ((Scan.repeat (scan_str q) >> flat) -- ($$$ q |-- scan_pos)));
wenzelm@30586
   104
wenzelm@48743
   105
fun recover_strs q =
wenzelm@48743
   106
  $$$ q @@@ (Scan.repeat (Scan.permissive (scan_str q)) >> flat);
wenzelm@48743
   107
wenzelm@30586
   108
in
wenzelm@30586
   109
wenzelm@42503
   110
val scan_string_q = scan_strs "'";
wenzelm@42503
   111
val scan_string_qq = scan_strs "\"";
wenzelm@42503
   112
val scan_string_bq = scan_strs "`";
wenzelm@30586
   113
wenzelm@48743
   114
val recover_string_q = recover_strs "'";
wenzelm@48743
   115
val recover_string_qq = recover_strs "\"";
wenzelm@48743
   116
val recover_string_bq = recover_strs "`";
wenzelm@48743
   117
wenzelm@30586
   118
end;
wenzelm@30586
   119
wenzelm@30586
   120
wenzelm@43773
   121
(* quote string literals *)
wenzelm@43773
   122
wenzelm@43773
   123
local
wenzelm@43773
   124
wenzelm@43773
   125
fun char_code i =
wenzelm@43773
   126
  (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
wenzelm@43773
   127
wenzelm@43773
   128
fun quote_str q s =
wenzelm@43773
   129
  if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
wenzelm@43773
   130
  else if s = q orelse s = "\\" then "\\" ^ s
wenzelm@43773
   131
  else s;
wenzelm@43773
   132
wenzelm@43773
   133
fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
wenzelm@43773
   134
wenzelm@43773
   135
in
wenzelm@43773
   136
wenzelm@43773
   137
val quote_string_q = quote_string "'";
wenzelm@43773
   138
val quote_string_qq = quote_string "\"";
wenzelm@43773
   139
val quote_string_bq = quote_string "`";
wenzelm@43773
   140
wenzelm@43773
   141
end;
wenzelm@43773
   142
wenzelm@43773
   143
wenzelm@27763
   144
(* ML-style comments *)
wenzelm@27763
   145
wenzelm@27763
   146
local
wenzelm@27763
   147
wenzelm@27763
   148
val scan_cmt =
wenzelm@27763
   149
  Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
wenzelm@27763
   150
  Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
wenzelm@27763
   151
  Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
wenzelm@27763
   152
  Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
wenzelm@27763
   153
wenzelm@48743
   154
val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
wenzelm@48743
   155
wenzelm@48743
   156
val scan_body = change_prompt scan_cmts;
wenzelm@27763
   157
wenzelm@27763
   158
in
wenzelm@27763
   159
wenzelm@27763
   160
fun scan_comment cut =
wenzelm@27763
   161
  $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
wenzelm@27763
   162
wenzelm@27763
   163
fun scan_comment_body cut =
wenzelm@27763
   164
  $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
wenzelm@27763
   165
wenzelm@48743
   166
val recover_comment =
wenzelm@48743
   167
  $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
wenzelm@48743
   168
wenzelm@27763
   169
end;
wenzelm@27763
   170
wenzelm@27763
   171
wenzelm@27763
   172
(* source *)
wenzelm@27763
   173
wenzelm@27763
   174
fun source pos =
wenzelm@27763
   175
  Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
wenzelm@27763
   176
    Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
wenzelm@27763
   177
wenzelm@27763
   178
wenzelm@27763
   179
(* compact representation -- with Symbol.DEL padding *)
wenzelm@27763
   180
wenzelm@27778
   181
type text = string;
wenzelm@27778
   182
wenzelm@27763
   183
fun pad [] = []
wenzelm@27763
   184
  | pad [(s, _)] = [s]
wenzelm@32784
   185
  | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
wenzelm@27763
   186
      let
wenzelm@27763
   187
        val end_pos1 = Position.advance s1 pos1;
wenzelm@27797
   188
        val d = Int.max (0, Position.distance_of end_pos1 pos2);
wenzelm@27763
   189
      in s1 :: replicate d Symbol.DEL @ pad rest end;
wenzelm@27763
   190
wenzelm@27797
   191
val implode = implode o pad;
wenzelm@27763
   192
wenzelm@27797
   193
fun range (syms as (_, pos) :: _) =
wenzelm@27763
   194
      let val pos' = List.last syms |-> Position.advance
wenzelm@27797
   195
      in Position.range pos pos' end
wenzelm@27797
   196
  | range [] = Position.no_range;
wenzelm@27763
   197
wenzelm@27797
   198
fun implode_range pos1 pos2 syms =
wenzelm@27797
   199
  let val syms' = (("", pos1) :: syms @ [("", pos2)])
wenzelm@27797
   200
  in (implode syms', range syms') end;
wenzelm@27778
   201
wenzelm@27763
   202
fun explode (str, pos) =
wenzelm@41416
   203
  let
wenzelm@41416
   204
    val (res, _) =
wenzelm@41416
   205
      fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
wenzelm@41416
   206
        (Symbol.explode str) ([], Position.reset_range pos);
wenzelm@41416
   207
  in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
wenzelm@27763
   208
wenzelm@27763
   209
end;
wenzelm@27763
   210
wenzelm@36957
   211
structure Basic_Symbol_Pos =   (*not open by default*)
wenzelm@36957
   212
struct
wenzelm@36957
   213
  val $$$ = Symbol_Pos.$$$;
wenzelm@36957
   214
  val ~$$$ = Symbol_Pos.~$$$;
wenzelm@36957
   215
end;
wenzelm@27763
   216