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