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