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