src/Pure/General/symbol_pos.ML
author wenzelm
Sat Apr 30 18:16:40 2011 +0200 (2011-04-30)
changeset 42503 27514b6fbe93
parent 41416 a2208d3e2bd6
child 43709 717e96cf9527
permissions -rw-r--r--
more uniform variations of scan_string;
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@42503
    20
  val scan_string_q: T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@42503
    21
  val scan_string_qq: T list -> (Position.T * (T list * Position.T)) * T list
wenzelm@42503
    22
  val scan_string_bq: T list -> (Position.T * (T list * Position.T)) * 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@42503
   115
val scan_string_q = scan_strs "'";
wenzelm@42503
   116
val scan_string_qq = scan_strs "\"";
wenzelm@42503
   117
val scan_string_bq = scan_strs "`";
wenzelm@30586
   118
wenzelm@30586
   119
end;
wenzelm@30586
   120
wenzelm@30586
   121
wenzelm@27763
   122
(* ML-style comments *)
wenzelm@27763
   123
wenzelm@27763
   124
local
wenzelm@27763
   125
wenzelm@27763
   126
val scan_cmt =
wenzelm@27763
   127
  Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
wenzelm@27763
   128
  Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
wenzelm@27763
   129
  Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
wenzelm@27763
   130
  Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
wenzelm@27763
   131
wenzelm@30586
   132
val scan_body = change_prompt (Scan.pass 0 (Scan.repeat scan_cmt >> flat));
wenzelm@27763
   133
wenzelm@27763
   134
in
wenzelm@27763
   135
wenzelm@27763
   136
fun scan_comment cut =
wenzelm@27763
   137
  $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
wenzelm@27763
   138
wenzelm@27763
   139
fun scan_comment_body cut =
wenzelm@27763
   140
  $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
wenzelm@27763
   141
wenzelm@27763
   142
end;
wenzelm@27763
   143
wenzelm@27763
   144
wenzelm@27763
   145
(* source *)
wenzelm@27763
   146
wenzelm@27763
   147
fun source pos =
wenzelm@27763
   148
  Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
wenzelm@27763
   149
    Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
wenzelm@27763
   150
wenzelm@27763
   151
wenzelm@27763
   152
(* compact representation -- with Symbol.DEL padding *)
wenzelm@27763
   153
wenzelm@27778
   154
type text = string;
wenzelm@27778
   155
wenzelm@27763
   156
fun pad [] = []
wenzelm@27763
   157
  | pad [(s, _)] = [s]
wenzelm@32784
   158
  | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
wenzelm@27763
   159
      let
wenzelm@27763
   160
        val end_pos1 = Position.advance s1 pos1;
wenzelm@27797
   161
        val d = Int.max (0, Position.distance_of end_pos1 pos2);
wenzelm@27763
   162
      in s1 :: replicate d Symbol.DEL @ pad rest end;
wenzelm@27763
   163
wenzelm@27797
   164
val implode = implode o pad;
wenzelm@27763
   165
wenzelm@27797
   166
fun range (syms as (_, pos) :: _) =
wenzelm@27763
   167
      let val pos' = List.last syms |-> Position.advance
wenzelm@27797
   168
      in Position.range pos pos' end
wenzelm@27797
   169
  | range [] = Position.no_range;
wenzelm@27763
   170
wenzelm@27797
   171
fun implode_range pos1 pos2 syms =
wenzelm@27797
   172
  let val syms' = (("", pos1) :: syms @ [("", pos2)])
wenzelm@27797
   173
  in (implode syms', range syms') end;
wenzelm@27778
   174
wenzelm@27763
   175
fun explode (str, pos) =
wenzelm@41416
   176
  let
wenzelm@41416
   177
    val (res, _) =
wenzelm@41416
   178
      fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
wenzelm@41416
   179
        (Symbol.explode str) ([], Position.reset_range pos);
wenzelm@41416
   180
  in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
wenzelm@27763
   181
wenzelm@27763
   182
end;
wenzelm@27763
   183
wenzelm@36957
   184
structure Basic_Symbol_Pos =   (*not open by default*)
wenzelm@36957
   185
struct
wenzelm@36957
   186
  val $$$ = Symbol_Pos.$$$;
wenzelm@36957
   187
  val ~$$$ = Symbol_Pos.~$$$;
wenzelm@36957
   188
end;
wenzelm@27763
   189