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