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