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