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