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