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