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