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