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