src/Pure/General/symbol_pos.ML
author wenzelm
Sat Jan 18 19:31:32 2014 +0100 (2014-01-18)
changeset 55035 68afbb5ce4ff
parent 55033 8e8243975860
child 55103 57d87ec3da4c
permissions -rw-r--r--
unused;
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@55033
    14
  val range: T list -> Position.range
wenzelm@27763
    15
  val is_eof: T -> bool
wenzelm@27763
    16
  val stopper: T Scan.stopper
wenzelm@43947
    17
  val !!! : Scan.message -> (T list -> 'a) -> T list -> 'a
wenzelm@30586
    18
  val change_prompt: ('a -> 'b) -> 'a -> 'b
wenzelm@27778
    19
  val scan_pos: T list -> Position.T * T list
wenzelm@48764
    20
  val scan_string_q: string -> T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@48764
    21
  val scan_string_qq: string -> T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@48764
    22
  val scan_string_bq: string -> T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@48743
    23
  val recover_string_q: T list -> T list * T list
wenzelm@48743
    24
  val recover_string_qq: T list -> T list * T list
wenzelm@48743
    25
  val recover_string_bq: T list -> T list * T list
wenzelm@43773
    26
  val quote_string_q: string -> string
wenzelm@43773
    27
  val quote_string_qq: string -> string
wenzelm@43773
    28
  val quote_string_bq: string -> string
wenzelm@55033
    29
  val scan_cartouche: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
wenzelm@55033
    30
    T list -> T list * T list
wenzelm@55033
    31
  val scan_cartouche_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
wenzelm@55033
    32
    T list -> T list * T list
wenzelm@55033
    33
  val recover_cartouche: T list -> T list * T list
wenzelm@55033
    34
  val cartouche_content: T list -> T list
wenzelm@27763
    35
  val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
wenzelm@27763
    36
    T list -> T list * T list
wenzelm@27763
    37
  val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
wenzelm@27763
    38
    T list -> T list * T list
wenzelm@48743
    39
  val recover_comment: T list -> T list * T list
wenzelm@27763
    40
  val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
wenzelm@27763
    41
    (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
wenzelm@27778
    42
  type text = string
wenzelm@27797
    43
  val implode: T list -> text
wenzelm@27797
    44
  val implode_range: Position.T -> Position.T -> T list -> text * Position.range
wenzelm@27778
    45
  val explode: text * Position.T -> T list
wenzelm@50239
    46
  val scan_ident: T list -> T list * T list
wenzelm@50239
    47
  val is_identifier: string -> bool
wenzelm@27763
    48
end;
wenzelm@27763
    49
wenzelm@30573
    50
structure Symbol_Pos: SYMBOL_POS =
wenzelm@27763
    51
struct
wenzelm@27763
    52
wenzelm@27763
    53
(* type T *)
wenzelm@27763
    54
wenzelm@27763
    55
type T = Symbol.symbol * Position.T;
wenzelm@27763
    56
wenzelm@27763
    57
fun symbol ((s, _): T) = s;
wenzelm@27852
    58
wenzelm@27797
    59
val content = implode o map symbol;
wenzelm@27763
    60
wenzelm@55033
    61
fun range (syms as (_, pos) :: _) =
wenzelm@55033
    62
      let val pos' = List.last syms |-> Position.advance
wenzelm@55033
    63
      in Position.range pos pos' end
wenzelm@55033
    64
  | range [] = Position.no_range;
wenzelm@55033
    65
wenzelm@27763
    66
wenzelm@27763
    67
(* stopper *)
wenzelm@27763
    68
wenzelm@27763
    69
fun mk_eof pos = (Symbol.eof, pos);
wenzelm@27763
    70
val eof = mk_eof Position.none;
wenzelm@27763
    71
wenzelm@27763
    72
val is_eof = Symbol.is_eof o symbol;
wenzelm@27763
    73
wenzelm@27763
    74
val stopper =
wenzelm@27763
    75
  Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
wenzelm@27763
    76
wenzelm@27763
    77
wenzelm@27763
    78
(* basic scanners *)
wenzelm@27763
    79
wenzelm@27763
    80
fun !!! text scan =
wenzelm@27763
    81
  let
wenzelm@48911
    82
    fun get_pos [] = " (end-of-input)"
wenzelm@48992
    83
      | get_pos ((_, pos) :: _) = Position.here pos;
wenzelm@27763
    84
wenzelm@43947
    85
    fun err (syms, msg) = fn () =>
wenzelm@48770
    86
      text () ^ get_pos syms ^
wenzelm@50201
    87
      Markup.markup Markup.no_report (" at " ^ Symbol.beginning 10 (map symbol syms)) ^
wenzelm@43947
    88
      (case msg of NONE => "" | SOME m => "\n" ^ m ());
wenzelm@27763
    89
  in Scan.!! err scan end;
wenzelm@27763
    90
wenzelm@30586
    91
fun change_prompt scan = Scan.prompt "# " scan;
wenzelm@30586
    92
wenzelm@55033
    93
fun $$ s = Scan.one (fn x => symbol x = s);
wenzelm@27763
    94
fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
wenzelm@27763
    95
fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
wenzelm@27763
    96
wenzelm@27778
    97
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
wenzelm@27763
    98
wenzelm@27763
    99
wenzelm@43773
   100
(* scan string literals *)
wenzelm@30586
   101
wenzelm@30586
   102
local
wenzelm@30586
   103
wenzelm@30586
   104
val char_code =
wenzelm@30586
   105
  Scan.one (Symbol.is_ascii_digit o symbol) --
wenzelm@30586
   106
  Scan.one (Symbol.is_ascii_digit o symbol) --
wenzelm@30586
   107
  Scan.one (Symbol.is_ascii_digit o symbol) :|--
wenzelm@30586
   108
  (fn (((a, pos), (b, _)), (c, _)) =>
wenzelm@30586
   109
    let val (n, _) = Library.read_int [a, b, c]
wenzelm@30586
   110
    in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
wenzelm@30586
   111
wenzelm@48764
   112
fun scan_str q err_prefix =
wenzelm@48764
   113
  $$$ "\\" |-- !!! (fn () => err_prefix ^ "bad escape character in string")
wenzelm@48764
   114
    ($$$ q || $$$ "\\" || char_code) ||
wenzelm@30586
   115
  Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
wenzelm@30586
   116
wenzelm@48764
   117
fun scan_strs q err_prefix =
wenzelm@48764
   118
  (scan_pos --| $$$ q) -- !!! (fn () => err_prefix ^ "missing quote at end of string")
wenzelm@48764
   119
    (change_prompt ((Scan.repeat (scan_str q err_prefix) >> flat) -- ($$$ q |-- scan_pos)));
wenzelm@30586
   120
wenzelm@48743
   121
fun recover_strs q =
wenzelm@48764
   122
  $$$ q @@@ (Scan.repeat (Scan.permissive (scan_str q "")) >> flat);
wenzelm@48743
   123
wenzelm@30586
   124
in
wenzelm@30586
   125
wenzelm@42503
   126
val scan_string_q = scan_strs "'";
wenzelm@42503
   127
val scan_string_qq = scan_strs "\"";
wenzelm@42503
   128
val scan_string_bq = scan_strs "`";
wenzelm@30586
   129
wenzelm@48743
   130
val recover_string_q = recover_strs "'";
wenzelm@48743
   131
val recover_string_qq = recover_strs "\"";
wenzelm@48743
   132
val recover_string_bq = recover_strs "`";
wenzelm@48743
   133
wenzelm@30586
   134
end;
wenzelm@30586
   135
wenzelm@30586
   136
wenzelm@43773
   137
(* quote string literals *)
wenzelm@43773
   138
wenzelm@43773
   139
local
wenzelm@43773
   140
wenzelm@43773
   141
fun char_code i =
wenzelm@43773
   142
  (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
wenzelm@43773
   143
wenzelm@43773
   144
fun quote_str q s =
wenzelm@43773
   145
  if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
wenzelm@43773
   146
  else if s = q orelse s = "\\" then "\\" ^ s
wenzelm@43773
   147
  else s;
wenzelm@43773
   148
wenzelm@43773
   149
fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
wenzelm@43773
   150
wenzelm@43773
   151
in
wenzelm@43773
   152
wenzelm@43773
   153
val quote_string_q = quote_string "'";
wenzelm@43773
   154
val quote_string_qq = quote_string "\"";
wenzelm@43773
   155
val quote_string_bq = quote_string "`";
wenzelm@43773
   156
wenzelm@43773
   157
end;
wenzelm@43773
   158
wenzelm@43773
   159
wenzelm@55033
   160
(* nested text cartouches *)
wenzelm@55033
   161
wenzelm@55033
   162
local
wenzelm@55033
   163
wenzelm@55033
   164
val scan_cart =
wenzelm@55033
   165
  Scan.depend (fn (d: int) => $$ "\\<open>" >> pair (d + 1)) ||
wenzelm@55033
   166
  Scan.depend (fn 0 => Scan.fail | d => $$ "\\<close>" >> pair (d - 1)) ||
wenzelm@55033
   167
  Scan.lift (Scan.one (fn (s, _) => s <> "\\<close>" andalso Symbol.is_regular s));
wenzelm@55033
   168
wenzelm@55033
   169
val scan_carts = Scan.pass 0 (Scan.repeat scan_cart);
wenzelm@55033
   170
wenzelm@55033
   171
val scan_body = change_prompt scan_carts;
wenzelm@55033
   172
wenzelm@55033
   173
in
wenzelm@55033
   174
wenzelm@55033
   175
fun scan_cartouche cut =
wenzelm@55033
   176
  $$ "\\<open>" ::: cut "unclosed text cartouche" (scan_body @@@ $$$ "\\<close>");
wenzelm@55033
   177
wenzelm@55033
   178
fun scan_cartouche_body cut =
wenzelm@55033
   179
  $$ "\\<open>" |-- cut "unclosed text cartouche" (scan_body --| $$ "\\<close>");
wenzelm@55033
   180
wenzelm@55033
   181
val recover_cartouche =
wenzelm@55033
   182
  $$$ "\\<open>" @@@ scan_carts;
wenzelm@55033
   183
wenzelm@55033
   184
end;
wenzelm@55033
   185
wenzelm@55033
   186
fun cartouche_content syms =
wenzelm@55033
   187
  let
wenzelm@55033
   188
    fun err () =
wenzelm@55033
   189
      error ("Malformed text cartouche: " ^ quote (content syms) ^
wenzelm@55033
   190
        Position.here (Position.set_range (range syms)));
wenzelm@55033
   191
  in
wenzelm@55033
   192
    (case syms of
wenzelm@55033
   193
      ("\\<open>", _) :: rest =>
wenzelm@55033
   194
        (case rev rest of
wenzelm@55033
   195
          ("\\<close>", _) :: rrest => rev rrest
wenzelm@55033
   196
        | _ => err ())
wenzelm@55033
   197
    | _ => err ())
wenzelm@55033
   198
  end;
wenzelm@55033
   199
wenzelm@55033
   200
wenzelm@27763
   201
(* ML-style comments *)
wenzelm@27763
   202
wenzelm@27763
   203
local
wenzelm@27763
   204
wenzelm@27763
   205
val scan_cmt =
wenzelm@27763
   206
  Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
wenzelm@27763
   207
  Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
wenzelm@27763
   208
  Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
wenzelm@27763
   209
  Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
wenzelm@27763
   210
wenzelm@48743
   211
val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
wenzelm@48743
   212
wenzelm@48743
   213
val scan_body = change_prompt scan_cmts;
wenzelm@27763
   214
wenzelm@27763
   215
in
wenzelm@27763
   216
wenzelm@27763
   217
fun scan_comment cut =
wenzelm@27763
   218
  $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
wenzelm@27763
   219
wenzelm@27763
   220
fun scan_comment_body cut =
wenzelm@27763
   221
  $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
wenzelm@27763
   222
wenzelm@48743
   223
val recover_comment =
wenzelm@48743
   224
  $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
wenzelm@48743
   225
wenzelm@27763
   226
end;
wenzelm@27763
   227
wenzelm@27763
   228
wenzelm@27763
   229
(* source *)
wenzelm@27763
   230
wenzelm@27763
   231
fun source pos =
wenzelm@27763
   232
  Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
wenzelm@27763
   233
    Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
wenzelm@27763
   234
wenzelm@27763
   235
wenzelm@27763
   236
(* compact representation -- with Symbol.DEL padding *)
wenzelm@27763
   237
wenzelm@27778
   238
type text = string;
wenzelm@27778
   239
wenzelm@27763
   240
fun pad [] = []
wenzelm@27763
   241
  | pad [(s, _)] = [s]
wenzelm@32784
   242
  | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
wenzelm@27763
   243
      let
wenzelm@27763
   244
        val end_pos1 = Position.advance s1 pos1;
wenzelm@27797
   245
        val d = Int.max (0, Position.distance_of end_pos1 pos2);
wenzelm@27763
   246
      in s1 :: replicate d Symbol.DEL @ pad rest end;
wenzelm@27763
   247
wenzelm@27797
   248
val implode = implode o pad;
wenzelm@27763
   249
wenzelm@27797
   250
fun implode_range pos1 pos2 syms =
wenzelm@27797
   251
  let val syms' = (("", pos1) :: syms @ [("", pos2)])
wenzelm@27797
   252
  in (implode syms', range syms') end;
wenzelm@27778
   253
wenzelm@27763
   254
fun explode (str, pos) =
wenzelm@41416
   255
  let
wenzelm@41416
   256
    val (res, _) =
wenzelm@41416
   257
      fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
wenzelm@41416
   258
        (Symbol.explode str) ([], Position.reset_range pos);
wenzelm@41416
   259
  in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
wenzelm@27763
   260
wenzelm@50239
   261
wenzelm@50239
   262
(* identifiers *)
wenzelm@50239
   263
wenzelm@50242
   264
local
wenzelm@50242
   265
wenzelm@52616
   266
val letter = Scan.one (symbol #> Symbol.is_letter);
wenzelm@52616
   267
val letdigs1 = Scan.many1 (symbol #> Symbol.is_letdig);
wenzelm@53016
   268
wenzelm@54732
   269
val sub = Scan.one (symbol #> (fn s => s = "\\<^sub>"));
wenzelm@50242
   270
wenzelm@50242
   271
in
wenzelm@50242
   272
wenzelm@52920
   273
val scan_ident = letter ::: (Scan.repeat (letdigs1 || sub ::: letdigs1) >> flat);
wenzelm@50242
   274
wenzelm@50242
   275
end;
wenzelm@50242
   276
wenzelm@50239
   277
fun is_identifier s =
wenzelm@50295
   278
  Symbol.is_ascii_identifier s orelse
wenzelm@50295
   279
    (case try (Scan.finite stopper scan_ident) (explode (s, Position.none)) of
wenzelm@50295
   280
      SOME (_, []) => true
wenzelm@50295
   281
    | _ => false);
wenzelm@50239
   282
wenzelm@27763
   283
end;
wenzelm@27763
   284
wenzelm@36957
   285
structure Basic_Symbol_Pos =   (*not open by default*)
wenzelm@36957
   286
struct
wenzelm@36957
   287
  val $$$ = Symbol_Pos.$$$;
wenzelm@36957
   288
  val ~$$$ = Symbol_Pos.~$$$;
wenzelm@36957
   289
end;
wenzelm@27763
   290