src/Pure/General/symbol_pos.ML
author wenzelm
Wed Jul 10 16:25:26 2013 +0200 (2013-07-10)
changeset 52616 3ac2878764f9
parent 50493 2bf3bfbb422d
child 52920 4539e4a06339
permissions -rw-r--r--
more robust identifier syntax: sub/superscript counts as modifier of LETDIG part instead of LETTER, both isub/isup and sub/sup are allowed;
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@48764
    19
  val scan_string_q: string -> T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@48764
    20
  val scan_string_qq: string -> T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@48764
    21
  val scan_string_bq: string -> 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@50239
    40
  val scan_ident: T list -> T list * T list
wenzelm@50239
    41
  val is_identifier: string -> bool
wenzelm@27763
    42
end;
wenzelm@27763
    43
wenzelm@30573
    44
structure Symbol_Pos: SYMBOL_POS =
wenzelm@27763
    45
struct
wenzelm@27763
    46
wenzelm@27763
    47
(* type T *)
wenzelm@27763
    48
wenzelm@27763
    49
type T = Symbol.symbol * Position.T;
wenzelm@27763
    50
wenzelm@27763
    51
fun symbol ((s, _): T) = s;
wenzelm@27852
    52
wenzelm@27797
    53
val content = implode o map symbol;
wenzelm@27763
    54
wenzelm@27763
    55
wenzelm@27763
    56
(* stopper *)
wenzelm@27763
    57
wenzelm@27763
    58
fun mk_eof pos = (Symbol.eof, pos);
wenzelm@27763
    59
val eof = mk_eof Position.none;
wenzelm@27763
    60
wenzelm@27763
    61
val is_eof = Symbol.is_eof o symbol;
wenzelm@27763
    62
wenzelm@27763
    63
val stopper =
wenzelm@27763
    64
  Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
wenzelm@27763
    65
wenzelm@27763
    66
wenzelm@27763
    67
(* basic scanners *)
wenzelm@27763
    68
wenzelm@27763
    69
fun !!! text scan =
wenzelm@27763
    70
  let
wenzelm@48911
    71
    fun get_pos [] = " (end-of-input)"
wenzelm@48992
    72
      | get_pos ((_, pos) :: _) = Position.here pos;
wenzelm@27763
    73
wenzelm@43947
    74
    fun err (syms, msg) = fn () =>
wenzelm@48770
    75
      text () ^ get_pos syms ^
wenzelm@50201
    76
      Markup.markup Markup.no_report (" at " ^ Symbol.beginning 10 (map symbol syms)) ^
wenzelm@43947
    77
      (case msg of NONE => "" | SOME m => "\n" ^ m ());
wenzelm@27763
    78
  in Scan.!! err scan end;
wenzelm@27763
    79
wenzelm@30586
    80
fun change_prompt scan = Scan.prompt "# " scan;
wenzelm@30586
    81
wenzelm@27763
    82
fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
wenzelm@27763
    83
fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
wenzelm@27763
    84
wenzelm@27778
    85
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
wenzelm@27763
    86
wenzelm@27763
    87
wenzelm@43773
    88
(* scan string literals *)
wenzelm@30586
    89
wenzelm@30586
    90
local
wenzelm@30586
    91
wenzelm@30586
    92
val char_code =
wenzelm@30586
    93
  Scan.one (Symbol.is_ascii_digit o symbol) --
wenzelm@30586
    94
  Scan.one (Symbol.is_ascii_digit o symbol) --
wenzelm@30586
    95
  Scan.one (Symbol.is_ascii_digit o symbol) :|--
wenzelm@30586
    96
  (fn (((a, pos), (b, _)), (c, _)) =>
wenzelm@30586
    97
    let val (n, _) = Library.read_int [a, b, c]
wenzelm@30586
    98
    in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
wenzelm@30586
    99
wenzelm@48764
   100
fun scan_str q err_prefix =
wenzelm@48764
   101
  $$$ "\\" |-- !!! (fn () => err_prefix ^ "bad escape character in string")
wenzelm@48764
   102
    ($$$ q || $$$ "\\" || char_code) ||
wenzelm@30586
   103
  Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
wenzelm@30586
   104
wenzelm@48764
   105
fun scan_strs q err_prefix =
wenzelm@48764
   106
  (scan_pos --| $$$ q) -- !!! (fn () => err_prefix ^ "missing quote at end of string")
wenzelm@48764
   107
    (change_prompt ((Scan.repeat (scan_str q err_prefix) >> flat) -- ($$$ q |-- scan_pos)));
wenzelm@30586
   108
wenzelm@48743
   109
fun recover_strs q =
wenzelm@48764
   110
  $$$ q @@@ (Scan.repeat (Scan.permissive (scan_str q "")) >> flat);
wenzelm@48743
   111
wenzelm@30586
   112
in
wenzelm@30586
   113
wenzelm@42503
   114
val scan_string_q = scan_strs "'";
wenzelm@42503
   115
val scan_string_qq = scan_strs "\"";
wenzelm@42503
   116
val scan_string_bq = scan_strs "`";
wenzelm@30586
   117
wenzelm@48743
   118
val recover_string_q = recover_strs "'";
wenzelm@48743
   119
val recover_string_qq = recover_strs "\"";
wenzelm@48743
   120
val recover_string_bq = recover_strs "`";
wenzelm@48743
   121
wenzelm@30586
   122
end;
wenzelm@30586
   123
wenzelm@30586
   124
wenzelm@43773
   125
(* quote string literals *)
wenzelm@43773
   126
wenzelm@43773
   127
local
wenzelm@43773
   128
wenzelm@43773
   129
fun char_code i =
wenzelm@43773
   130
  (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
wenzelm@43773
   131
wenzelm@43773
   132
fun quote_str q s =
wenzelm@43773
   133
  if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
wenzelm@43773
   134
  else if s = q orelse s = "\\" then "\\" ^ s
wenzelm@43773
   135
  else s;
wenzelm@43773
   136
wenzelm@43773
   137
fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
wenzelm@43773
   138
wenzelm@43773
   139
in
wenzelm@43773
   140
wenzelm@43773
   141
val quote_string_q = quote_string "'";
wenzelm@43773
   142
val quote_string_qq = quote_string "\"";
wenzelm@43773
   143
val quote_string_bq = quote_string "`";
wenzelm@43773
   144
wenzelm@43773
   145
end;
wenzelm@43773
   146
wenzelm@43773
   147
wenzelm@27763
   148
(* ML-style comments *)
wenzelm@27763
   149
wenzelm@27763
   150
local
wenzelm@27763
   151
wenzelm@27763
   152
val scan_cmt =
wenzelm@27763
   153
  Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
wenzelm@27763
   154
  Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
wenzelm@27763
   155
  Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
wenzelm@27763
   156
  Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
wenzelm@27763
   157
wenzelm@48743
   158
val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
wenzelm@48743
   159
wenzelm@48743
   160
val scan_body = change_prompt scan_cmts;
wenzelm@27763
   161
wenzelm@27763
   162
in
wenzelm@27763
   163
wenzelm@27763
   164
fun scan_comment cut =
wenzelm@27763
   165
  $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
wenzelm@27763
   166
wenzelm@27763
   167
fun scan_comment_body cut =
wenzelm@27763
   168
  $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
wenzelm@27763
   169
wenzelm@48743
   170
val recover_comment =
wenzelm@48743
   171
  $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
wenzelm@48743
   172
wenzelm@27763
   173
end;
wenzelm@27763
   174
wenzelm@27763
   175
wenzelm@27763
   176
(* source *)
wenzelm@27763
   177
wenzelm@27763
   178
fun source pos =
wenzelm@27763
   179
  Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
wenzelm@27763
   180
    Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
wenzelm@27763
   181
wenzelm@27763
   182
wenzelm@27763
   183
(* compact representation -- with Symbol.DEL padding *)
wenzelm@27763
   184
wenzelm@27778
   185
type text = string;
wenzelm@27778
   186
wenzelm@27763
   187
fun pad [] = []
wenzelm@27763
   188
  | pad [(s, _)] = [s]
wenzelm@32784
   189
  | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
wenzelm@27763
   190
      let
wenzelm@27763
   191
        val end_pos1 = Position.advance s1 pos1;
wenzelm@27797
   192
        val d = Int.max (0, Position.distance_of end_pos1 pos2);
wenzelm@27763
   193
      in s1 :: replicate d Symbol.DEL @ pad rest end;
wenzelm@27763
   194
wenzelm@27797
   195
val implode = implode o pad;
wenzelm@27763
   196
wenzelm@27797
   197
fun range (syms as (_, pos) :: _) =
wenzelm@27763
   198
      let val pos' = List.last syms |-> Position.advance
wenzelm@27797
   199
      in Position.range pos pos' end
wenzelm@27797
   200
  | range [] = Position.no_range;
wenzelm@27763
   201
wenzelm@27797
   202
fun implode_range pos1 pos2 syms =
wenzelm@27797
   203
  let val syms' = (("", pos1) :: syms @ [("", pos2)])
wenzelm@27797
   204
  in (implode syms', range syms') end;
wenzelm@27778
   205
wenzelm@27763
   206
fun explode (str, pos) =
wenzelm@41416
   207
  let
wenzelm@41416
   208
    val (res, _) =
wenzelm@41416
   209
      fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
wenzelm@41416
   210
        (Symbol.explode str) ([], Position.reset_range pos);
wenzelm@41416
   211
  in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
wenzelm@27763
   212
wenzelm@50239
   213
wenzelm@50239
   214
(* identifiers *)
wenzelm@50239
   215
wenzelm@50242
   216
local
wenzelm@50242
   217
wenzelm@52616
   218
val letter = Scan.one (symbol #> Symbol.is_letter);
wenzelm@52616
   219
val letdigs1 = Scan.many1 (symbol #> Symbol.is_letdig);
wenzelm@52616
   220
val sub_sup =
wenzelm@52616
   221
  Scan.one (symbol #>
wenzelm@52616
   222
    (fn s => s = "\\<^sub>" orelse s = "\\<^isub>" orelse s = "\\<^sup>" orelse s = "\\<^isup>"));
wenzelm@50242
   223
wenzelm@50242
   224
in
wenzelm@50242
   225
wenzelm@52616
   226
val scan_ident =
wenzelm@52616
   227
  letter ::: (Scan.repeat (letdigs1 || sub_sup ::: letdigs1) >> flat);
wenzelm@50242
   228
wenzelm@50242
   229
end;
wenzelm@50242
   230
wenzelm@50239
   231
fun is_identifier s =
wenzelm@50295
   232
  Symbol.is_ascii_identifier s orelse
wenzelm@50295
   233
    (case try (Scan.finite stopper scan_ident) (explode (s, Position.none)) of
wenzelm@50295
   234
      SOME (_, []) => true
wenzelm@50295
   235
    | _ => false);
wenzelm@50239
   236
wenzelm@27763
   237
end;
wenzelm@27763
   238
wenzelm@36957
   239
structure Basic_Symbol_Pos =   (*not open by default*)
wenzelm@36957
   240
struct
wenzelm@36957
   241
  val $$$ = Symbol_Pos.$$$;
wenzelm@36957
   242
  val ~$$$ = Symbol_Pos.~$$$;
wenzelm@36957
   243
end;
wenzelm@27763
   244