src/Pure/General/symbol_pos.ML
author wenzelm
Tue Jul 12 14:33:08 2011 +0200 (2011-07-12)
changeset 43773 e8ba493027a3
parent 43709 717e96cf9527
child 43947 9b00f09f7721
permissions -rw-r--r--
more precise Symbol_Pos.quote_string;
     1 (*  Title:      Pure/General/symbol_pos.ML
     2     Author:     Makarius
     3 
     4 Symbols with explicit position information.
     5 *)
     6 
     7 signature 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   val content: T list -> string
    14   val is_eof: T -> bool
    15   val stopper: T Scan.stopper
    16   val !!! : string -> (T list -> 'a) -> T list -> 'a
    17   val change_prompt: ('a -> 'b) -> 'a -> 'b
    18   val scan_pos: T list -> Position.T * T list
    19   val scan_string_q: T list -> (Position.T * (T list * Position.T)) * T list
    20   val scan_string_qq: T list -> (Position.T * (T list * Position.T)) * T list
    21   val scan_string_bq: T list -> (Position.T * (T list * Position.T)) * T list
    22   val quote_string_q: string -> string
    23   val quote_string_qq: string -> string
    24   val quote_string_bq: string -> string
    25   val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    26     T list -> T list * T list
    27   val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    28     T list -> T list * T list
    29   val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
    30     (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
    31   type text = string
    32   val implode: T list -> text
    33   val range: T list -> Position.range
    34   val implode_range: Position.T -> Position.T -> T list -> text * Position.range
    35   val explode: text * Position.T -> T list
    36 end;
    37 
    38 structure Symbol_Pos: SYMBOL_POS =
    39 struct
    40 
    41 (* type T *)
    42 
    43 type T = Symbol.symbol * Position.T;
    44 
    45 fun symbol ((s, _): T) = s;
    46 
    47 val content = implode o map symbol;
    48 
    49 
    50 (* stopper *)
    51 
    52 fun mk_eof pos = (Symbol.eof, pos);
    53 val eof = mk_eof Position.none;
    54 
    55 val is_eof = Symbol.is_eof o symbol;
    56 
    57 val stopper =
    58   Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
    59 
    60 
    61 (* basic scanners *)
    62 
    63 fun !!! text scan =
    64   let
    65     fun get_pos [] = " (past end-of-text!)"
    66       | get_pos ((_, pos) :: _) = Position.str_of pos;
    67 
    68     fun err (syms, msg) =
    69       text ^ get_pos syms ^ " at " ^ Symbol.beginning 10 (map symbol syms) ^
    70       (case msg of NONE => "" | SOME s => "\n" ^ s);
    71   in Scan.!! err scan end;
    72 
    73 fun change_prompt scan = Scan.prompt "# " scan;
    74 
    75 fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
    76 fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
    77 
    78 val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
    79 
    80 
    81 (* scan string literals *)
    82 
    83 local
    84 
    85 val char_code =
    86   Scan.one (Symbol.is_ascii_digit o symbol) --
    87   Scan.one (Symbol.is_ascii_digit o symbol) --
    88   Scan.one (Symbol.is_ascii_digit o symbol) :|--
    89   (fn (((a, pos), (b, _)), (c, _)) =>
    90     let val (n, _) = Library.read_int [a, b, c]
    91     in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
    92 
    93 fun scan_str q =
    94   $$$ "\\" |-- !!! "bad escape character in string" ($$$ q || $$$ "\\" || char_code) ||
    95   Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
    96 
    97 fun scan_strs q =
    98   (scan_pos --| $$$ q) -- !!! "missing quote at end of string"
    99     (change_prompt ((Scan.repeat (scan_str q) >> flat) -- ($$$ q |-- scan_pos)));
   100 
   101 in
   102 
   103 val scan_string_q = scan_strs "'";
   104 val scan_string_qq = scan_strs "\"";
   105 val scan_string_bq = scan_strs "`";
   106 
   107 end;
   108 
   109 
   110 (* quote string literals *)
   111 
   112 local
   113 
   114 fun char_code i =
   115   (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
   116 
   117 fun quote_str q s =
   118   if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
   119   else if s = q orelse s = "\\" then "\\" ^ s
   120   else s;
   121 
   122 fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
   123 
   124 in
   125 
   126 val quote_string_q = quote_string "'";
   127 val quote_string_qq = quote_string "\"";
   128 val quote_string_bq = quote_string "`";
   129 
   130 end;
   131 
   132 
   133 (* ML-style comments *)
   134 
   135 local
   136 
   137 val scan_cmt =
   138   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
   139   Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
   140   Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
   141   Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
   142 
   143 val scan_body = change_prompt (Scan.pass 0 (Scan.repeat scan_cmt >> flat));
   144 
   145 in
   146 
   147 fun scan_comment cut =
   148   $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
   149 
   150 fun scan_comment_body cut =
   151   $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
   152 
   153 end;
   154 
   155 
   156 (* source *)
   157 
   158 fun source pos =
   159   Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
   160     Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
   161 
   162 
   163 (* compact representation -- with Symbol.DEL padding *)
   164 
   165 type text = string;
   166 
   167 fun pad [] = []
   168   | pad [(s, _)] = [s]
   169   | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
   170       let
   171         val end_pos1 = Position.advance s1 pos1;
   172         val d = Int.max (0, Position.distance_of end_pos1 pos2);
   173       in s1 :: replicate d Symbol.DEL @ pad rest end;
   174 
   175 val implode = implode o pad;
   176 
   177 fun range (syms as (_, pos) :: _) =
   178       let val pos' = List.last syms |-> Position.advance
   179       in Position.range pos pos' end
   180   | range [] = Position.no_range;
   181 
   182 fun implode_range pos1 pos2 syms =
   183   let val syms' = (("", pos1) :: syms @ [("", pos2)])
   184   in (implode syms', range syms') end;
   185 
   186 fun explode (str, pos) =
   187   let
   188     val (res, _) =
   189       fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
   190         (Symbol.explode str) ([], Position.reset_range pos);
   191   in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
   192 
   193 end;
   194 
   195 structure Basic_Symbol_Pos =   (*not open by default*)
   196 struct
   197   val $$$ = Symbol_Pos.$$$;
   198   val ~$$$ = Symbol_Pos.~$$$;
   199 end;
   200