src/Pure/General/symbol_pos.ML
author wenzelm
Mon Jan 20 20:24:44 2014 +0100 (2014-01-20)
changeset 55106 080c0006e917
parent 55105 75815b3b38a1
child 55107 1a29ea173bf9
permissions -rw-r--r--
tuned error messages, more accurate position;
     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 * T list
    12   val $$$ : Symbol.symbol -> T list -> T list * T list
    13   val ~$$$ : Symbol.symbol -> T list -> T list * T list
    14   val content: T list -> string
    15   val range: T list -> Position.range
    16   val is_eof: T -> bool
    17   val stopper: T Scan.stopper
    18   val !!! : Scan.message -> (T list -> 'a) -> T list -> 'a
    19   val change_prompt: ('a -> 'b) -> 'a -> 'b
    20   val scan_pos: T list -> Position.T * T list
    21   val scan_string_q: string -> T list -> (Position.T * (T list * Position.T)) * T list
    22   val scan_string_qq: string -> T list -> (Position.T * (T list * Position.T)) * T list
    23   val scan_string_bq: string -> T list -> (Position.T * (T list * Position.T)) * T list
    24   val recover_string_q: T list -> T list * T list
    25   val recover_string_qq: T list -> T list * T list
    26   val recover_string_bq: T list -> T list * T list
    27   val quote_string_q: string -> string
    28   val quote_string_qq: string -> string
    29   val quote_string_bq: string -> string
    30   val scan_cartouche: string -> T list -> T list * T list
    31   val recover_cartouche: T list -> T list * T list
    32   val cartouche_content: T list -> T list
    33   val scan_comment: string -> T list -> T list * T list
    34   val scan_comment_body: string -> T list -> T list * T list
    35   val recover_comment: T list -> T list * T list
    36   val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
    37     (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
    38   type text = string
    39   val implode: T list -> text
    40   val implode_range: Position.T -> Position.T -> T list -> text * Position.range
    41   val explode: text * Position.T -> T list
    42   val scan_ident: T list -> T list * T list
    43   val is_identifier: string -> bool
    44 end;
    45 
    46 structure Symbol_Pos: SYMBOL_POS =
    47 struct
    48 
    49 (* type T *)
    50 
    51 type T = Symbol.symbol * Position.T;
    52 
    53 fun symbol ((s, _): T) = s;
    54 
    55 val content = implode o map symbol;
    56 
    57 fun range (syms as (_, pos) :: _) =
    58       let val pos' = List.last syms |-> Position.advance
    59       in Position.range pos pos' end
    60   | range [] = Position.no_range;
    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 [] = " (end-of-input)"
    79       | get_pos ((_, pos) :: _) = Position.here pos;
    80 
    81     fun err (syms, msg) = fn () =>
    82       text () ^ get_pos syms ^
    83       Markup.markup Markup.no_report (" at " ^ Symbol.beginning 10 (map symbol syms)) ^
    84       (case msg of NONE => "" | SOME m => "\n" ^ m ());
    85   in Scan.!! err scan end;
    86 
    87 fun change_prompt scan = Scan.prompt "# " scan;
    88 
    89 fun $$ s = Scan.one (fn x => symbol x = s);
    90 fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
    91 fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
    92 
    93 val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
    94 
    95 
    96 (* scan string literals *)
    97 
    98 local
    99 
   100 val char_code =
   101   Scan.one (Symbol.is_ascii_digit o symbol) --
   102   Scan.one (Symbol.is_ascii_digit o symbol) --
   103   Scan.one (Symbol.is_ascii_digit o symbol) :|--
   104   (fn (((a, pos), (b, _)), (c, _)) =>
   105     let val (n, _) = Library.read_int [a, b, c]
   106     in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
   107 
   108 fun scan_str q err_prefix =
   109   $$$ "\\" |-- !!! (fn () => err_prefix ^ "bad escape character in string")
   110     ($$$ q || $$$ "\\" || char_code) ||
   111   Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
   112 
   113 fun scan_strs q err_prefix =
   114   Scan.ahead ($$ q) |--
   115     !!! (fn () => err_prefix ^ "unclosed string literal")
   116       ((scan_pos --| $$$ q) --
   117         (change_prompt ((Scan.repeat (scan_str q err_prefix) >> flat) -- ($$$ q |-- scan_pos))));
   118 
   119 fun recover_strs q =
   120   $$$ q @@@ (Scan.repeat (Scan.permissive (scan_str q "")) >> flat);
   121 
   122 in
   123 
   124 val scan_string_q = scan_strs "'";
   125 val scan_string_qq = scan_strs "\"";
   126 val scan_string_bq = scan_strs "`";
   127 
   128 val recover_string_q = recover_strs "'";
   129 val recover_string_qq = recover_strs "\"";
   130 val recover_string_bq = recover_strs "`";
   131 
   132 end;
   133 
   134 
   135 (* quote string literals *)
   136 
   137 local
   138 
   139 fun char_code i =
   140   (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
   141 
   142 fun quote_str q s =
   143   if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
   144   else if s = q orelse s = "\\" then "\\" ^ s
   145   else s;
   146 
   147 fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
   148 
   149 in
   150 
   151 val quote_string_q = quote_string "'";
   152 val quote_string_qq = quote_string "\"";
   153 val quote_string_bq = quote_string "`";
   154 
   155 end;
   156 
   157 
   158 (* nested text cartouches *)
   159 
   160 val scan_cartouche_depth =
   161   Scan.repeat1 (Scan.depend (fn (d: int) =>
   162     $$ "\\<open>" >> pair (d + 1) ||
   163       (if d > 0 then
   164         Scan.one (fn (s, _) => s <> "\\<close>" andalso Symbol.is_regular s) >> pair d ||
   165         $$ "\\<close>" >> pair (d - 1)
   166       else Scan.fail)));
   167 
   168 fun scan_cartouche err_prefix =
   169   Scan.ahead ($$ "\\<open>") |--
   170     !!! (fn () => err_prefix ^ "unclosed text cartouche")
   171       (change_prompt (Scan.provide (fn d => d = 0) 0 scan_cartouche_depth));
   172 
   173 val recover_cartouche = Scan.pass 0 scan_cartouche_depth;
   174 
   175 fun cartouche_content syms =
   176   let
   177     fun err () =
   178       error ("Malformed text cartouche: " ^ quote (content syms) ^
   179         Position.here (Position.set_range (range syms)));
   180   in
   181     (case syms of
   182       ("\\<open>", _) :: rest =>
   183         (case rev rest of
   184           ("\\<close>", _) :: rrest => rev rrest
   185         | _ => err ())
   186     | _ => err ())
   187   end;
   188 
   189 
   190 (* ML-style comments *)
   191 
   192 local
   193 
   194 val scan_cmt =
   195   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
   196   Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
   197   Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
   198   Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
   199 
   200 val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
   201 
   202 val scan_body = change_prompt scan_cmts;
   203 
   204 in
   205 
   206 fun scan_comment err_prefix =
   207   Scan.ahead ($$ "(" -- $$ "*") |--
   208     !!! (fn () => err_prefix ^ "unclosed comment")
   209       ($$$ "(" @@@ $$$ "*" @@@ scan_body @@@ $$$ "*" @@@ $$$ ")");
   210 
   211 fun scan_comment_body err_prefix =
   212   Scan.ahead ($$ "(" -- $$ "*") |--
   213     !!! (fn () => err_prefix ^ "unclosed comment")
   214       ($$ "(" |-- $$ "*" |-- scan_body --| $$ "*" --| $$ ")");
   215 
   216 val recover_comment =
   217   $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
   218 
   219 end;
   220 
   221 
   222 (* source *)
   223 
   224 fun source pos =
   225   Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
   226     Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
   227 
   228 
   229 (* compact representation -- with Symbol.DEL padding *)
   230 
   231 type text = string;
   232 
   233 fun pad [] = []
   234   | pad [(s, _)] = [s]
   235   | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
   236       let
   237         val end_pos1 = Position.advance s1 pos1;
   238         val d = Int.max (0, Position.distance_of end_pos1 pos2);
   239       in s1 :: replicate d Symbol.DEL @ pad rest end;
   240 
   241 val implode = implode o pad;
   242 
   243 fun implode_range pos1 pos2 syms =
   244   let val syms' = (("", pos1) :: syms @ [("", pos2)])
   245   in (implode syms', range syms') end;
   246 
   247 fun explode (str, pos) =
   248   let
   249     val (res, _) =
   250       fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
   251         (Symbol.explode str) ([], Position.reset_range pos);
   252   in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
   253 
   254 
   255 (* identifiers *)
   256 
   257 local
   258 
   259 val letter = Scan.one (symbol #> Symbol.is_letter);
   260 val letdigs1 = Scan.many1 (symbol #> Symbol.is_letdig);
   261 
   262 val sub = Scan.one (symbol #> (fn s => s = "\\<^sub>"));
   263 
   264 in
   265 
   266 val scan_ident = letter ::: (Scan.repeat (letdigs1 || sub ::: letdigs1) >> flat);
   267 
   268 end;
   269 
   270 fun is_identifier s =
   271   Symbol.is_ascii_identifier s orelse
   272     (case try (Scan.finite stopper scan_ident) (explode (s, Position.none)) of
   273       SOME (_, []) => true
   274     | _ => false);
   275 
   276 end;
   277 
   278 structure Basic_Symbol_Pos =   (*not open by default*)
   279 struct
   280   val $$ = Symbol_Pos.$$;
   281   val $$$ = Symbol_Pos.$$$;
   282   val ~$$$ = Symbol_Pos.~$$$;
   283 end;
   284