src/Pure/General/symbol_pos.ML
author wenzelm
Sat Jul 23 16:37:17 2011 +0200 (2011-07-23)
changeset 43947 9b00f09f7721
parent 43773 e8ba493027a3
child 48743 a72f8ffecf31
permissions -rw-r--r--
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
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@27763
    11
  val $$$ : Symbol.symbol -> T list -> T list * T list
wenzelm@27763
    12
  val ~$$$ : Symbol.symbol -> T list -> T list * T list
wenzelm@27797
    13
  val content: T list -> string
wenzelm@27763
    14
  val is_eof: T -> bool
wenzelm@27763
    15
  val stopper: T Scan.stopper
wenzelm@43947
    16
  val !!! : Scan.message -> (T list -> 'a) -> T list -> 'a
wenzelm@30586
    17
  val change_prompt: ('a -> 'b) -> 'a -> 'b
wenzelm@27778
    18
  val scan_pos: T list -> Position.T * T list
wenzelm@42503
    19
  val scan_string_q: T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@42503
    20
  val scan_string_qq: T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@42503
    21
  val scan_string_bq: T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@43773
    22
  val quote_string_q: string -> string
wenzelm@43773
    23
  val quote_string_qq: string -> string
wenzelm@43773
    24
  val quote_string_bq: string -> string
wenzelm@27763
    25
  val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
wenzelm@27763
    26
    T list -> T list * T list
wenzelm@27763
    27
  val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
wenzelm@27763
    28
    T list -> T list * T list
wenzelm@27763
    29
  val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
wenzelm@27763
    30
    (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
wenzelm@27778
    31
  type text = string
wenzelm@27797
    32
  val implode: T list -> text
wenzelm@27797
    33
  val range: T list -> Position.range
wenzelm@27797
    34
  val implode_range: Position.T -> Position.T -> T list -> text * Position.range
wenzelm@27778
    35
  val explode: text * Position.T -> T list
wenzelm@27763
    36
end;
wenzelm@27763
    37
wenzelm@30573
    38
structure Symbol_Pos: SYMBOL_POS =
wenzelm@27763
    39
struct
wenzelm@27763
    40
wenzelm@27763
    41
(* type T *)
wenzelm@27763
    42
wenzelm@27763
    43
type T = Symbol.symbol * Position.T;
wenzelm@27763
    44
wenzelm@27763
    45
fun symbol ((s, _): T) = s;
wenzelm@27852
    46
wenzelm@27797
    47
val content = implode o map symbol;
wenzelm@27763
    48
wenzelm@27763
    49
wenzelm@27763
    50
(* stopper *)
wenzelm@27763
    51
wenzelm@27763
    52
fun mk_eof pos = (Symbol.eof, pos);
wenzelm@27763
    53
val eof = mk_eof Position.none;
wenzelm@27763
    54
wenzelm@27763
    55
val is_eof = Symbol.is_eof o symbol;
wenzelm@27763
    56
wenzelm@27763
    57
val stopper =
wenzelm@27763
    58
  Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
wenzelm@27763
    59
wenzelm@27763
    60
wenzelm@27763
    61
(* basic scanners *)
wenzelm@27763
    62
wenzelm@27763
    63
fun !!! text scan =
wenzelm@27763
    64
  let
wenzelm@27763
    65
    fun get_pos [] = " (past end-of-text!)"
wenzelm@27763
    66
      | get_pos ((_, pos) :: _) = Position.str_of pos;
wenzelm@27763
    67
wenzelm@43947
    68
    fun err (syms, msg) = fn () =>
wenzelm@43947
    69
      text () ^ get_pos syms ^ " at " ^ Symbol.beginning 10 (map symbol syms) ^
wenzelm@43947
    70
      (case msg of NONE => "" | SOME m => "\n" ^ m ());
wenzelm@27763
    71
  in Scan.!! err scan end;
wenzelm@27763
    72
wenzelm@30586
    73
fun change_prompt scan = Scan.prompt "# " scan;
wenzelm@30586
    74
wenzelm@27763
    75
fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
wenzelm@27763
    76
fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
wenzelm@27763
    77
wenzelm@27778
    78
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
wenzelm@27763
    79
wenzelm@27763
    80
wenzelm@43773
    81
(* scan string literals *)
wenzelm@30586
    82
wenzelm@30586
    83
local
wenzelm@30586
    84
wenzelm@30586
    85
val char_code =
wenzelm@30586
    86
  Scan.one (Symbol.is_ascii_digit o symbol) --
wenzelm@30586
    87
  Scan.one (Symbol.is_ascii_digit o symbol) --
wenzelm@30586
    88
  Scan.one (Symbol.is_ascii_digit o symbol) :|--
wenzelm@30586
    89
  (fn (((a, pos), (b, _)), (c, _)) =>
wenzelm@30586
    90
    let val (n, _) = Library.read_int [a, b, c]
wenzelm@30586
    91
    in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
wenzelm@30586
    92
wenzelm@30586
    93
fun scan_str q =
wenzelm@43947
    94
  $$$ "\\" |-- !!! (fn () => "bad escape character in string") ($$$ q || $$$ "\\" || char_code) ||
wenzelm@30586
    95
  Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
wenzelm@30586
    96
wenzelm@30586
    97
fun scan_strs q =
wenzelm@43947
    98
  (scan_pos --| $$$ q) -- !!! (fn () => "missing quote at end of string")
wenzelm@30586
    99
    (change_prompt ((Scan.repeat (scan_str q) >> flat) -- ($$$ q |-- scan_pos)));
wenzelm@30586
   100
wenzelm@30586
   101
in
wenzelm@30586
   102
wenzelm@42503
   103
val scan_string_q = scan_strs "'";
wenzelm@42503
   104
val scan_string_qq = scan_strs "\"";
wenzelm@42503
   105
val scan_string_bq = scan_strs "`";
wenzelm@30586
   106
wenzelm@30586
   107
end;
wenzelm@30586
   108
wenzelm@30586
   109
wenzelm@43773
   110
(* quote string literals *)
wenzelm@43773
   111
wenzelm@43773
   112
local
wenzelm@43773
   113
wenzelm@43773
   114
fun char_code i =
wenzelm@43773
   115
  (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
wenzelm@43773
   116
wenzelm@43773
   117
fun quote_str q s =
wenzelm@43773
   118
  if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
wenzelm@43773
   119
  else if s = q orelse s = "\\" then "\\" ^ s
wenzelm@43773
   120
  else s;
wenzelm@43773
   121
wenzelm@43773
   122
fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
wenzelm@43773
   123
wenzelm@43773
   124
in
wenzelm@43773
   125
wenzelm@43773
   126
val quote_string_q = quote_string "'";
wenzelm@43773
   127
val quote_string_qq = quote_string "\"";
wenzelm@43773
   128
val quote_string_bq = quote_string "`";
wenzelm@43773
   129
wenzelm@43773
   130
end;
wenzelm@43773
   131
wenzelm@43773
   132
wenzelm@27763
   133
(* ML-style comments *)
wenzelm@27763
   134
wenzelm@27763
   135
local
wenzelm@27763
   136
wenzelm@27763
   137
val scan_cmt =
wenzelm@27763
   138
  Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
wenzelm@27763
   139
  Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
wenzelm@27763
   140
  Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
wenzelm@27763
   141
  Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
wenzelm@27763
   142
wenzelm@30586
   143
val scan_body = change_prompt (Scan.pass 0 (Scan.repeat scan_cmt >> flat));
wenzelm@27763
   144
wenzelm@27763
   145
in
wenzelm@27763
   146
wenzelm@27763
   147
fun scan_comment cut =
wenzelm@27763
   148
  $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
wenzelm@27763
   149
wenzelm@27763
   150
fun scan_comment_body cut =
wenzelm@27763
   151
  $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
wenzelm@27763
   152
wenzelm@27763
   153
end;
wenzelm@27763
   154
wenzelm@27763
   155
wenzelm@27763
   156
(* source *)
wenzelm@27763
   157
wenzelm@27763
   158
fun source pos =
wenzelm@27763
   159
  Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
wenzelm@27763
   160
    Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
wenzelm@27763
   161
wenzelm@27763
   162
wenzelm@27763
   163
(* compact representation -- with Symbol.DEL padding *)
wenzelm@27763
   164
wenzelm@27778
   165
type text = string;
wenzelm@27778
   166
wenzelm@27763
   167
fun pad [] = []
wenzelm@27763
   168
  | pad [(s, _)] = [s]
wenzelm@32784
   169
  | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
wenzelm@27763
   170
      let
wenzelm@27763
   171
        val end_pos1 = Position.advance s1 pos1;
wenzelm@27797
   172
        val d = Int.max (0, Position.distance_of end_pos1 pos2);
wenzelm@27763
   173
      in s1 :: replicate d Symbol.DEL @ pad rest end;
wenzelm@27763
   174
wenzelm@27797
   175
val implode = implode o pad;
wenzelm@27763
   176
wenzelm@27797
   177
fun range (syms as (_, pos) :: _) =
wenzelm@27763
   178
      let val pos' = List.last syms |-> Position.advance
wenzelm@27797
   179
      in Position.range pos pos' end
wenzelm@27797
   180
  | range [] = Position.no_range;
wenzelm@27763
   181
wenzelm@27797
   182
fun implode_range pos1 pos2 syms =
wenzelm@27797
   183
  let val syms' = (("", pos1) :: syms @ [("", pos2)])
wenzelm@27797
   184
  in (implode syms', range syms') end;
wenzelm@27778
   185
wenzelm@27763
   186
fun explode (str, pos) =
wenzelm@41416
   187
  let
wenzelm@41416
   188
    val (res, _) =
wenzelm@41416
   189
      fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
wenzelm@41416
   190
        (Symbol.explode str) ([], Position.reset_range pos);
wenzelm@41416
   191
  in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
wenzelm@27763
   192
wenzelm@27763
   193
end;
wenzelm@27763
   194
wenzelm@36957
   195
structure Basic_Symbol_Pos =   (*not open by default*)
wenzelm@36957
   196
struct
wenzelm@36957
   197
  val $$$ = Symbol_Pos.$$$;
wenzelm@36957
   198
  val ~$$$ = Symbol_Pos.~$$$;
wenzelm@36957
   199
end;
wenzelm@27763
   200