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