src/Pure/General/symbol_pos.ML
author wenzelm
Wed Jan 21 23:21:44 2009 +0100 (2009-01-21)
changeset 29606 fedb8be05f24
parent 27984 b4dd58cff97c
child 30573 49899f26fbd1
permissions -rw-r--r--
removed Ids;
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@27778
    23
  val scan_pos: T list -> Position.T * T list
wenzelm@27763
    24
  val scan_comment: (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 scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
wenzelm@27763
    27
    T list -> T list * T list
wenzelm@27763
    28
  val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
wenzelm@27763
    29
    (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
wenzelm@27778
    30
  type text = string
wenzelm@27797
    31
  val implode: T list -> text
wenzelm@27797
    32
  val range: T list -> Position.range
wenzelm@27797
    33
  val implode_range: Position.T -> Position.T -> T list -> text * Position.range
wenzelm@27778
    34
  val explode: text * Position.T -> T list
wenzelm@27763
    35
end;
wenzelm@27763
    36
wenzelm@27763
    37
structure SymbolPos: SYMBOL_POS =
wenzelm@27763
    38
struct
wenzelm@27763
    39
wenzelm@27763
    40
(* type T *)
wenzelm@27763
    41
wenzelm@27763
    42
type T = Symbol.symbol * Position.T;
wenzelm@27763
    43
wenzelm@27763
    44
fun symbol ((s, _): T) = s;
wenzelm@27852
    45
wenzelm@27852
    46
wenzelm@27852
    47
(* content *)
wenzelm@27852
    48
wenzelm@27797
    49
val content = implode o map symbol;
wenzelm@27763
    50
wenzelm@27763
    51
wenzelm@27864
    52
val tab_width = (8: int);
wenzelm@27852
    53
wenzelm@27852
    54
fun untabify ("\t", pos) =
wenzelm@27852
    55
      (case Position.column_of pos of
wenzelm@27852
    56
        SOME n => Symbol.spaces (tab_width - ((n - 1) mod tab_width))
wenzelm@27984
    57
      | NONE => Symbol.space)
wenzelm@27852
    58
  | untabify (s, _) = s;
wenzelm@27852
    59
wenzelm@27852
    60
val untabify_content = implode o map untabify;
wenzelm@27852
    61
wenzelm@27852
    62
wenzelm@27763
    63
(* stopper *)
wenzelm@27763
    64
wenzelm@27763
    65
fun mk_eof pos = (Symbol.eof, pos);
wenzelm@27763
    66
val eof = mk_eof Position.none;
wenzelm@27763
    67
wenzelm@27763
    68
val is_eof = Symbol.is_eof o symbol;
wenzelm@27763
    69
wenzelm@27763
    70
val stopper =
wenzelm@27763
    71
  Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
wenzelm@27763
    72
wenzelm@27763
    73
wenzelm@27763
    74
(* basic scanners *)
wenzelm@27763
    75
wenzelm@27763
    76
fun !!! text scan =
wenzelm@27763
    77
  let
wenzelm@27763
    78
    fun get_pos [] = " (past end-of-text!)"
wenzelm@27763
    79
      | get_pos ((_, pos) :: _) = Position.str_of pos;
wenzelm@27763
    80
wenzelm@27763
    81
    fun err (syms, msg) =
wenzelm@27763
    82
      text ^ get_pos syms ^ " at " ^ Symbol.beginning 10 (map symbol syms) ^
wenzelm@27763
    83
      (case msg of NONE => "" | SOME s => "\n" ^ s);
wenzelm@27763
    84
  in Scan.!! err scan end;
wenzelm@27763
    85
wenzelm@27763
    86
fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
wenzelm@27763
    87
fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
wenzelm@27763
    88
wenzelm@27778
    89
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
wenzelm@27763
    90
wenzelm@27763
    91
wenzelm@27763
    92
(* ML-style comments *)
wenzelm@27763
    93
wenzelm@27763
    94
local
wenzelm@27763
    95
wenzelm@27763
    96
val scan_cmt =
wenzelm@27763
    97
  Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
wenzelm@27763
    98
  Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
wenzelm@27763
    99
  Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
wenzelm@27763
   100
  Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
wenzelm@27763
   101
wenzelm@27763
   102
val scan_body = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
wenzelm@27763
   103
wenzelm@27763
   104
in
wenzelm@27763
   105
wenzelm@27763
   106
fun scan_comment cut =
wenzelm@27763
   107
  $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
wenzelm@27763
   108
wenzelm@27763
   109
fun scan_comment_body cut =
wenzelm@27763
   110
  $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
wenzelm@27763
   111
wenzelm@27763
   112
end;
wenzelm@27763
   113
wenzelm@27763
   114
wenzelm@27763
   115
(* source *)
wenzelm@27763
   116
wenzelm@27763
   117
fun source pos =
wenzelm@27763
   118
  Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
wenzelm@27763
   119
    Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
wenzelm@27763
   120
wenzelm@27763
   121
wenzelm@27763
   122
(* compact representation -- with Symbol.DEL padding *)
wenzelm@27763
   123
wenzelm@27778
   124
type text = string;
wenzelm@27778
   125
wenzelm@27763
   126
fun pad [] = []
wenzelm@27763
   127
  | pad [(s, _)] = [s]
wenzelm@27763
   128
  | pad ((s1, pos1) :: (rest as (s2, pos2) :: _)) =
wenzelm@27763
   129
      let
wenzelm@27763
   130
        val end_pos1 = Position.advance s1 pos1;
wenzelm@27797
   131
        val d = Int.max (0, Position.distance_of end_pos1 pos2);
wenzelm@27763
   132
      in s1 :: replicate d Symbol.DEL @ pad rest end;
wenzelm@27763
   133
wenzelm@27797
   134
val implode = implode o pad;
wenzelm@27763
   135
wenzelm@27797
   136
fun range (syms as (_, pos) :: _) =
wenzelm@27763
   137
      let val pos' = List.last syms |-> Position.advance
wenzelm@27797
   138
      in Position.range pos pos' end
wenzelm@27797
   139
  | range [] = Position.no_range;
wenzelm@27763
   140
wenzelm@27797
   141
fun implode_range pos1 pos2 syms =
wenzelm@27797
   142
  let val syms' = (("", pos1) :: syms @ [("", pos2)])
wenzelm@27797
   143
  in (implode syms', range syms') end;
wenzelm@27778
   144
wenzelm@27763
   145
fun explode (str, pos) =
wenzelm@27778
   146
  fold_map (fn s => fn p => ((s, p), (Position.advance s p)))
wenzelm@27778
   147
    (Symbol.explode str) (Position.reset_range pos)
wenzelm@27763
   148
  |> #1 |> filter_out (fn (s, _) => s = Symbol.DEL);
wenzelm@27763
   149
wenzelm@27763
   150
end;
wenzelm@27763
   151
wenzelm@27763
   152
structure BasicSymbolPos: BASIC_SYMBOL_POS = SymbolPos;   (*not open by default*)
wenzelm@27763
   153