src/Pure/General/symbol_pos.ML
author wenzelm
Sat Nov 13 19:47:23 2010 +0100 (2010-11-13)
changeset 40525 14a2e686bdac
parent 36957 cdb9e83abfbe
child 41416 a2208d3e2bd6
permissions -rw-r--r--
eliminated slightly odd pervasive Symbol_Pos.symbol;
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@27852
    14
  val untabify_content: T list -> string
wenzelm@27763
    15
  val is_eof: T -> bool
wenzelm@27763
    16
  val stopper: T Scan.stopper
wenzelm@27763
    17
  val !!! : string -> (T list -> 'a) -> T list -> 'a
wenzelm@30586
    18
  val change_prompt: ('a -> 'b) -> 'a -> 'b
wenzelm@27778
    19
  val scan_pos: T list -> Position.T * T list
wenzelm@30586
    20
  val scan_string: T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@30586
    21
  val scan_alt_string: T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@30586
    22
  val scan_quoted: T list -> T list * T list
wenzelm@27763
    23
  val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
wenzelm@27763
    24
    T list -> T list * T list
wenzelm@27763
    25
  val scan_comment_body: (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 source: Position.T -> (Symbol.symbol, 'a) Source.source ->
wenzelm@27763
    28
    (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
wenzelm@27778
    29
  type text = string
wenzelm@27797
    30
  val implode: T list -> text
wenzelm@27797
    31
  val range: T list -> Position.range
wenzelm@27797
    32
  val implode_range: Position.T -> Position.T -> T list -> text * Position.range
wenzelm@27778
    33
  val explode: text * Position.T -> T list
wenzelm@27763
    34
end;
wenzelm@27763
    35
wenzelm@30573
    36
structure Symbol_Pos: SYMBOL_POS =
wenzelm@27763
    37
struct
wenzelm@27763
    38
wenzelm@27763
    39
(* type T *)
wenzelm@27763
    40
wenzelm@27763
    41
type T = Symbol.symbol * Position.T;
wenzelm@27763
    42
wenzelm@27763
    43
fun symbol ((s, _): T) = s;
wenzelm@27852
    44
wenzelm@27852
    45
wenzelm@27852
    46
(* content *)
wenzelm@27852
    47
wenzelm@27797
    48
val content = implode o map symbol;
wenzelm@27763
    49
wenzelm@27763
    50
wenzelm@27864
    51
val tab_width = (8: int);
wenzelm@27852
    52
wenzelm@27852
    53
fun untabify ("\t", pos) =
wenzelm@27852
    54
      (case Position.column_of pos of
wenzelm@27852
    55
        SOME n => Symbol.spaces (tab_width - ((n - 1) mod tab_width))
wenzelm@27984
    56
      | NONE => Symbol.space)
wenzelm@27852
    57
  | untabify (s, _) = s;
wenzelm@27852
    58
wenzelm@27852
    59
val untabify_content = implode o map untabify;
wenzelm@27852
    60
wenzelm@27852
    61
wenzelm@27763
    62
(* stopper *)
wenzelm@27763
    63
wenzelm@27763
    64
fun mk_eof pos = (Symbol.eof, pos);
wenzelm@27763
    65
val eof = mk_eof Position.none;
wenzelm@27763
    66
wenzelm@27763
    67
val is_eof = Symbol.is_eof o symbol;
wenzelm@27763
    68
wenzelm@27763
    69
val stopper =
wenzelm@27763
    70
  Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
wenzelm@27763
    71
wenzelm@27763
    72
wenzelm@27763
    73
(* basic scanners *)
wenzelm@27763
    74
wenzelm@27763
    75
fun !!! text scan =
wenzelm@27763
    76
  let
wenzelm@27763
    77
    fun get_pos [] = " (past end-of-text!)"
wenzelm@27763
    78
      | get_pos ((_, pos) :: _) = Position.str_of pos;
wenzelm@27763
    79
wenzelm@27763
    80
    fun err (syms, msg) =
wenzelm@27763
    81
      text ^ get_pos syms ^ " at " ^ Symbol.beginning 10 (map symbol syms) ^
wenzelm@27763
    82
      (case msg of NONE => "" | SOME s => "\n" ^ s);
wenzelm@27763
    83
  in Scan.!! err scan end;
wenzelm@27763
    84
wenzelm@30586
    85
fun change_prompt scan = Scan.prompt "# " scan;
wenzelm@30586
    86
wenzelm@27763
    87
fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
wenzelm@27763
    88
fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
wenzelm@27763
    89
wenzelm@27778
    90
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
wenzelm@27763
    91
wenzelm@27763
    92
wenzelm@30586
    93
(* Isabelle strings *)
wenzelm@30586
    94
wenzelm@30586
    95
local
wenzelm@30586
    96
wenzelm@30586
    97
val char_code =
wenzelm@30586
    98
  Scan.one (Symbol.is_ascii_digit o symbol) --
wenzelm@30586
    99
  Scan.one (Symbol.is_ascii_digit o symbol) --
wenzelm@30586
   100
  Scan.one (Symbol.is_ascii_digit o symbol) :|--
wenzelm@30586
   101
  (fn (((a, pos), (b, _)), (c, _)) =>
wenzelm@30586
   102
    let val (n, _) = Library.read_int [a, b, c]
wenzelm@30586
   103
    in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
wenzelm@30586
   104
wenzelm@30586
   105
fun scan_str q =
wenzelm@30586
   106
  $$$ "\\" |-- !!! "bad escape character in string" ($$$ q || $$$ "\\" || char_code) ||
wenzelm@30586
   107
  Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
wenzelm@30586
   108
wenzelm@30586
   109
fun scan_strs q =
wenzelm@30586
   110
  (scan_pos --| $$$ q) -- !!! "missing quote at end of string"
wenzelm@30586
   111
    (change_prompt ((Scan.repeat (scan_str q) >> flat) -- ($$$ q |-- scan_pos)));
wenzelm@30586
   112
wenzelm@30586
   113
in
wenzelm@30586
   114
wenzelm@30586
   115
val scan_string = scan_strs "\"";
wenzelm@30586
   116
val scan_alt_string = scan_strs "`";
wenzelm@30586
   117
wenzelm@30586
   118
val scan_quoted = Scan.trace (scan_string || scan_alt_string) >> #2;
wenzelm@30586
   119
wenzelm@30586
   120
end;
wenzelm@30586
   121
wenzelm@30586
   122
wenzelm@27763
   123
(* ML-style comments *)
wenzelm@27763
   124
wenzelm@27763
   125
local
wenzelm@27763
   126
wenzelm@27763
   127
val scan_cmt =
wenzelm@27763
   128
  Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
wenzelm@27763
   129
  Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
wenzelm@27763
   130
  Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
wenzelm@27763
   131
  Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
wenzelm@27763
   132
wenzelm@30586
   133
val scan_body = change_prompt (Scan.pass 0 (Scan.repeat scan_cmt >> flat));
wenzelm@27763
   134
wenzelm@27763
   135
in
wenzelm@27763
   136
wenzelm@27763
   137
fun scan_comment cut =
wenzelm@27763
   138
  $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
wenzelm@27763
   139
wenzelm@27763
   140
fun scan_comment_body cut =
wenzelm@27763
   141
  $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
wenzelm@27763
   142
wenzelm@27763
   143
end;
wenzelm@27763
   144
wenzelm@27763
   145
wenzelm@27763
   146
(* source *)
wenzelm@27763
   147
wenzelm@27763
   148
fun source pos =
wenzelm@27763
   149
  Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
wenzelm@27763
   150
    Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
wenzelm@27763
   151
wenzelm@27763
   152
wenzelm@27763
   153
(* compact representation -- with Symbol.DEL padding *)
wenzelm@27763
   154
wenzelm@27778
   155
type text = string;
wenzelm@27778
   156
wenzelm@27763
   157
fun pad [] = []
wenzelm@27763
   158
  | pad [(s, _)] = [s]
wenzelm@32784
   159
  | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
wenzelm@27763
   160
      let
wenzelm@27763
   161
        val end_pos1 = Position.advance s1 pos1;
wenzelm@27797
   162
        val d = Int.max (0, Position.distance_of end_pos1 pos2);
wenzelm@27763
   163
      in s1 :: replicate d Symbol.DEL @ pad rest end;
wenzelm@27763
   164
wenzelm@27797
   165
val implode = implode o pad;
wenzelm@27763
   166
wenzelm@27797
   167
fun range (syms as (_, pos) :: _) =
wenzelm@27763
   168
      let val pos' = List.last syms |-> Position.advance
wenzelm@27797
   169
      in Position.range pos pos' end
wenzelm@27797
   170
  | range [] = Position.no_range;
wenzelm@27763
   171
wenzelm@27797
   172
fun implode_range pos1 pos2 syms =
wenzelm@27797
   173
  let val syms' = (("", pos1) :: syms @ [("", pos2)])
wenzelm@27797
   174
  in (implode syms', range syms') end;
wenzelm@27778
   175
wenzelm@27763
   176
fun explode (str, pos) =
wenzelm@27778
   177
  fold_map (fn s => fn p => ((s, p), (Position.advance s p)))
wenzelm@27778
   178
    (Symbol.explode str) (Position.reset_range pos)
wenzelm@27763
   179
  |> #1 |> filter_out (fn (s, _) => s = Symbol.DEL);
wenzelm@27763
   180
wenzelm@27763
   181
end;
wenzelm@27763
   182
wenzelm@36957
   183
structure Basic_Symbol_Pos =   (*not open by default*)
wenzelm@36957
   184
struct
wenzelm@36957
   185
  val $$$ = Symbol_Pos.$$$;
wenzelm@36957
   186
  val ~$$$ = Symbol_Pos.~$$$;
wenzelm@36957
   187
end;
wenzelm@27763
   188