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