src/Pure/General/symbol_pos.ML
author wenzelm
Thu, 07 Aug 2008 19:21:40 +0200
changeset 27778 3ec7a4d9ef18
parent 27763 f49f6275cefa
child 27797 9861b39a2fd5
permissions -rw-r--r--
renamed SymbolPos.scan_position to SymbolPos.scan_pos; implode/explode: explicit type text = string; added implode_delim; explode: Position.reset_range;
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
    ID:         $Id$
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     4
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     5
Symbols with explicit position information.
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     6
*)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     7
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     8
signature BASIC_SYMBOL_POS =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
     9
sig
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    10
  type T = Symbol.symbol * Position.T
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    11
  val symbol: T -> Symbol.symbol
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
  val ~$$$ : Symbol.symbol -> T list -> T list * T list
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    14
end
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    15
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    16
signature SYMBOL_POS =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    17
sig
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    18
  include BASIC_SYMBOL_POS
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    19
  val is_eof: T -> bool
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    20
  val stopper: T Scan.stopper
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    21
  val !!! : string -> (T list -> 'a) -> T list -> 'a
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    22
  val scan_pos: T list -> Position.T * T list
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    23
  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
    24
    T list -> T list * T list
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    25
  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
    26
    T list -> T list * T list
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    27
  val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    28
    (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
    29
  type text = string
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    30
  val implode: T list -> text * Position.range
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    31
  val implode_delim: Position.T -> Position.T -> T list -> text * Position.range
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    32
  val explode: text * Position.T -> T list
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    33
end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    34
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    35
structure SymbolPos: SYMBOL_POS =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    36
struct
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    37
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    38
(* type T *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    39
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    40
type T = Symbol.symbol * Position.T;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    41
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    42
fun symbol ((s, _): T) = s;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    43
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    44
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    45
(* stopper *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    46
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    47
fun mk_eof pos = (Symbol.eof, pos);
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    48
val eof = mk_eof Position.none;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    49
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    50
val is_eof = Symbol.is_eof o symbol;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    51
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    52
val stopper =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    53
  Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    54
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    55
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    56
(* basic scanners *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    57
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    58
fun !!! text scan =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    59
  let
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    60
    fun get_pos [] = " (past end-of-text!)"
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    61
      | get_pos ((_, pos) :: _) = Position.str_of pos;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    62
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    63
    fun err (syms, msg) =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    64
      text ^ get_pos syms ^ " at " ^ Symbol.beginning 10 (map symbol syms) ^
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    65
      (case msg of NONE => "" | SOME s => "\n" ^ s);
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    66
  in Scan.!! err scan end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    67
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    68
fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    69
fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    70
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
    71
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
27763
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
(* ML-style comments *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    75
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    76
local
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    77
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    78
val scan_cmt =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    79
  Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    80
  Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    81
  Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    82
  Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    83
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    84
val scan_body = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    85
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    86
in
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    87
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    88
fun scan_comment cut =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    89
  $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    90
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    91
fun scan_comment_body cut =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    92
  $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    93
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    94
end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    95
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    96
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    97
(* source *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    98
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
    99
fun source pos =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   100
  Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   101
    Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   102
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   103
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   104
(* compact representation -- with Symbol.DEL padding *)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   105
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   106
type text = string;
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   107
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   108
local
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   109
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   110
fun pad [] = []
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   111
  | pad [(s, _)] = [s]
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   112
  | pad ((s1, pos1) :: (rest as (s2, pos2) :: _)) =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   113
      let
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   114
        fun err () =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   115
          raise Fail ("Misaligned symbols: " ^
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   116
            quote s1 ^ Position.str_of pos1 ^ " " ^
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   117
            quote s2 ^ Position.str_of pos2);
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   118
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   119
        val end_pos1 = Position.advance s1 pos1;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   120
        val _ = Position.line_of end_pos1 = Position.line_of pos2 orelse err ();
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   121
        val d =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   122
          (case (Position.column_of end_pos1, Position.column_of pos2) of
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   123
            (NONE, NONE) => 0
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   124
          | (SOME n1, SOME n2) => n2 - n1
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   125
          | _ => err ());
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   126
        val _ = d >= 0 orelse err ();
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   127
      in s1 :: replicate d Symbol.DEL @ pad rest end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   128
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   129
val orig_implode = implode;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   130
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   131
in
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   132
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   133
fun implode (syms as (_, pos) :: _) =
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   134
      let val pos' = List.last syms |-> Position.advance
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   135
      in (orig_implode (pad syms), Position.range pos pos') end
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   136
  | implode [] = ("", (Position.none, Position.none));
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   137
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   138
fun implode_delim pos1 pos2 syms = implode (("", pos1) :: syms @ [("", pos2)]);
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   139
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   140
fun explode (str, pos) =
27778
3ec7a4d9ef18 renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents: 27763
diff changeset
   141
  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
   142
    (Symbol.explode str) (Position.reset_range pos)
27763
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   143
  |> #1 |> filter_out (fn (s, _) => s = Symbol.DEL);
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   144
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   145
end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   146
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   147
end;
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   148
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   149
structure BasicSymbolPos: BASIC_SYMBOL_POS = SymbolPos;   (*not open by default*)
f49f6275cefa Symbols with explicit position information.
wenzelm
parents:
diff changeset
   150