src/Pure/General/symbol_pos.ML
author wenzelm
Wed, 18 Mar 2009 21:55:38 +0100
changeset 30573 49899f26fbd1
parent 29606 fedb8be05f24
child 30586 9674f64a0702
permissions -rw-r--r--
de-camelized Symbol_Pos;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/symbol_pos.ML
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     3
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     4
Symbols with explicit position information.
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     5
*)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     6
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     7
signature BASIC_SYMBOL_POS =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     8
sig
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     9
  type T = Symbol.symbol * Position.T
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    10
  val symbol: T -> Symbol.symbol
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    11
  val $$$ : Symbol.symbol -> T list -> T list * T list
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    12
  val ~$$$ : Symbol.symbol -> T list -> T list * T list
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    13
end
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    14
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    15
signature SYMBOL_POS =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    16
sig
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    17
  include BASIC_SYMBOL_POS
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
    18
  val content: T list -> string
27852
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    19
  val untabify_content: T list -> string
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    20
  val is_eof: T -> bool
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    21
  val stopper: T Scan.stopper
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    22
  val !!! : string -> (T list -> 'a) -> T list -> 'a
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    23
  val scan_pos: T list -> Position.T * T list
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    24
  val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    25
    T list -> T list * T list
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    26
  val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    27
    T list -> T list * T list
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    28
  val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    29
    (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    30
  type text = string
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
    31
  val implode: T list -> text
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
    32
  val range: T list -> Position.range
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
    33
  val implode_range: Position.T -> Position.T -> T list -> text * Position.range
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    34
  val explode: text * Position.T -> T list
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    35
end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    36
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
    37
structure Symbol_Pos: SYMBOL_POS =
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    38
struct
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    39
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    40
(* type T *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    41
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    42
type T = Symbol.symbol * Position.T;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    43
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    44
fun symbol ((s, _): T) = s;
27852
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    45
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    46
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    47
(* content *)
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    48
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
    49
val content = implode o map symbol;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    50
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    51
27864
827730aea9e8 made SML/NJ happy;
wenzelm
parents: 27852
diff changeset
    52
val tab_width = (8: int);
27852
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    53
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    54
fun untabify ("\t", pos) =
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    55
      (case Position.column_of pos of
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    56
        SOME n => Symbol.spaces (tab_width - ((n - 1) mod tab_width))
27984
b4dd58cff97c untabify: silently turn tab into space if column information is unavailable;
wenzelm
parents: 27864
diff changeset
    57
      | NONE => Symbol.space)
27852
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    58
  | untabify (s, _) = s;
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    59
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    60
val untabify_content = implode o map untabify;
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    61
6454fef6a293 added untabify_content;
wenzelm
parents: 27797
diff changeset
    62
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    63
(* stopper *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    64
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    65
fun mk_eof pos = (Symbol.eof, pos);
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    66
val eof = mk_eof Position.none;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    67
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    68
val is_eof = Symbol.is_eof o symbol;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    69
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    70
val stopper =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    71
  Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    72
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    73
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    74
(* basic scanners *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    75
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    76
fun !!! text scan =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    77
  let
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    78
    fun get_pos [] = " (past end-of-text!)"
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    79
      | get_pos ((_, pos) :: _) = Position.str_of pos;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    80
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    81
    fun err (syms, msg) =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    82
      text ^ get_pos syms ^ " at " ^ Symbol.beginning 10 (map symbol syms) ^
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    83
      (case msg of NONE => "" | SOME s => "\n" ^ s);
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    84
  in Scan.!! err scan end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    85
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    86
fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    87
fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    88
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    89
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    90
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    91
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    92
(* ML-style comments *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    93
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    94
local
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    95
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    96
val scan_cmt =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    97
  Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    98
  Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    99
  Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   100
  Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   101
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   102
val scan_body = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   103
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   104
in
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   105
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   106
fun scan_comment cut =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   107
  $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   108
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   109
fun scan_comment_body cut =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   110
  $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   111
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   112
end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   113
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   114
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   115
(* source *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   116
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   117
fun source pos =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   118
  Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   119
    Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   120
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   121
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   122
(* compact representation -- with Symbol.DEL padding *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   123
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   124
type text = string;
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   125
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   126
fun pad [] = []
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   127
  | pad [(s, _)] = [s]
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   128
  | pad ((s1, pos1) :: (rest as (s2, pos2) :: _)) =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   129
      let
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   130
        val end_pos1 = Position.advance s1 pos1;
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   131
        val d = Int.max (0, Position.distance_of end_pos1 pos2);
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   132
      in s1 :: replicate d Symbol.DEL @ pad rest end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   133
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   134
val implode = implode o pad;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   135
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   136
fun range (syms as (_, pos) :: _) =
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   137
      let val pos' = List.last syms |-> Position.advance
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   138
      in Position.range pos pos' end
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   139
  | range [] = Position.no_range;
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   140
27797
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   141
fun implode_range pos1 pos2 syms =
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   142
  let val syms' = (("", pos1) :: syms @ [("", pos2)])
9861b39a2fd5 added content;
wenzelm
parents: 27778
diff changeset
   143
  in (implode syms', range syms') end;
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   144
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   145
fun explode (str, pos) =
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   146
  fold_map (fn s => fn p => ((s, p), (Position.advance s p)))
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   147
    (Symbol.explode str) (Position.reset_range pos)
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   148
  |> #1 |> filter_out (fn (s, _) => s = Symbol.DEL);
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   149
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   150
end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   151
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
   152
structure Basic_Symbol_Pos: BASIC_SYMBOL_POS = Symbol_Pos;   (*not open by default*)
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   153