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