src/Pure/General/symbol_pos.ML
author wenzelm
Sat Jan 18 19:15:12 2014 +0100 (2014-01-18)
changeset 55033 8e8243975860
parent 54732 b01bb3d09928
child 55035 68afbb5ce4ff
permissions -rw-r--r--
support for nested text cartouches;
clarified Symbol.is_symbolic: exclude \<open> and \<close>;
     1 (*  Title:      Pure/General/symbol_pos.ML
     2     Author:     Makarius
     3 
     4 Symbols with explicit position information.
     5 *)
     6 
     7 val legacy_isub_isup = Unsynchronized.ref false;
     8 
     9 signature SYMBOL_POS =
    10 sig
    11   type T = Symbol.symbol * Position.T
    12   val symbol: T -> Symbol.symbol
    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 is_eof: T -> bool
    18   val stopper: T Scan.stopper
    19   val !!! : Scan.message -> (T list -> 'a) -> T list -> 'a
    20   val change_prompt: ('a -> 'b) -> 'a -> 'b
    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) -> T list -> T list * T list) ->
    32     T list -> T list * T list
    33   val scan_cartouche_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    34     T list -> T list * T list
    35   val recover_cartouche: T list -> T list * T list
    36   val cartouche_content: T list -> T list
    37   val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    38     T list -> T list * T list
    39   val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    40     T list -> T list * T list
    41   val recover_comment: T list -> T list * T list
    42   val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
    43     (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
    44   type text = string
    45   val implode: T list -> text
    46   val implode_range: Position.T -> Position.T -> T list -> text * Position.range
    47   val explode: text * Position.T -> T list
    48   val scan_ident: T list -> T list * T list
    49   val is_identifier: string -> bool
    50 end;
    51 
    52 structure Symbol_Pos: SYMBOL_POS =
    53 struct
    54 
    55 (* type T *)
    56 
    57 type T = Symbol.symbol * Position.T;
    58 
    59 fun symbol ((s, _): T) = s;
    60 
    61 val content = implode o map symbol;
    62 
    63 fun range (syms as (_, pos) :: _) =
    64       let val pos' = List.last syms |-> Position.advance
    65       in Position.range pos pos' end
    66   | range [] = Position.no_range;
    67 
    68 
    69 (* stopper *)
    70 
    71 fun mk_eof pos = (Symbol.eof, pos);
    72 val eof = mk_eof Position.none;
    73 
    74 val is_eof = Symbol.is_eof o symbol;
    75 
    76 val stopper =
    77   Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
    78 
    79 
    80 (* basic scanners *)
    81 
    82 fun !!! text scan =
    83   let
    84     fun get_pos [] = " (end-of-input)"
    85       | get_pos ((_, pos) :: _) = Position.here pos;
    86 
    87     fun err (syms, msg) = fn () =>
    88       text () ^ get_pos syms ^
    89       Markup.markup Markup.no_report (" at " ^ Symbol.beginning 10 (map symbol syms)) ^
    90       (case msg of NONE => "" | SOME m => "\n" ^ m ());
    91   in Scan.!! err scan end;
    92 
    93 fun change_prompt scan = Scan.prompt "# " scan;
    94 
    95 fun $$ s = Scan.one (fn x => symbol x = s);
    96 fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
    97 fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
    98 
    99 val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
   100 
   101 
   102 (* scan string literals *)
   103 
   104 local
   105 
   106 val char_code =
   107   Scan.one (Symbol.is_ascii_digit o symbol) --
   108   Scan.one (Symbol.is_ascii_digit o symbol) --
   109   Scan.one (Symbol.is_ascii_digit o symbol) :|--
   110   (fn (((a, pos), (b, _)), (c, _)) =>
   111     let val (n, _) = Library.read_int [a, b, c]
   112     in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
   113 
   114 fun scan_str q err_prefix =
   115   $$$ "\\" |-- !!! (fn () => err_prefix ^ "bad escape character in string")
   116     ($$$ q || $$$ "\\" || char_code) ||
   117   Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
   118 
   119 fun scan_strs q err_prefix =
   120   (scan_pos --| $$$ q) -- !!! (fn () => err_prefix ^ "missing quote at end of string")
   121     (change_prompt ((Scan.repeat (scan_str q err_prefix) >> flat) -- ($$$ q |-- scan_pos)));
   122 
   123 fun recover_strs q =
   124   $$$ q @@@ (Scan.repeat (Scan.permissive (scan_str q "")) >> flat);
   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 local
   165 
   166 val scan_cart =
   167   Scan.depend (fn (d: int) => $$ "\\<open>" >> pair (d + 1)) ||
   168   Scan.depend (fn 0 => Scan.fail | d => $$ "\\<close>" >> pair (d - 1)) ||
   169   Scan.lift (Scan.one (fn (s, _) => s <> "\\<close>" andalso Symbol.is_regular s));
   170 
   171 val scan_carts = Scan.pass 0 (Scan.repeat scan_cart);
   172 
   173 val scan_body = change_prompt scan_carts;
   174 
   175 in
   176 
   177 fun scan_cartouche cut =
   178   $$ "\\<open>" ::: cut "unclosed text cartouche" (scan_body @@@ $$$ "\\<close>");
   179 
   180 fun scan_cartouche_body cut =
   181   $$ "\\<open>" |-- cut "unclosed text cartouche" (scan_body --| $$ "\\<close>");
   182 
   183 val recover_cartouche =
   184   $$$ "\\<open>" @@@ scan_carts;
   185 
   186 end;
   187 
   188 fun cartouche_content syms =
   189   let
   190     fun err () =
   191       error ("Malformed text cartouche: " ^ quote (content syms) ^
   192         Position.here (Position.set_range (range syms)));
   193   in
   194     (case syms of
   195       ("\\<open>", _) :: rest =>
   196         (case rev rest of
   197           ("\\<close>", _) :: rrest => rev rrest
   198         | _ => err ())
   199     | _ => err ())
   200   end;
   201 
   202 
   203 (* ML-style comments *)
   204 
   205 local
   206 
   207 val scan_cmt =
   208   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
   209   Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
   210   Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
   211   Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
   212 
   213 val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
   214 
   215 val scan_body = change_prompt scan_cmts;
   216 
   217 in
   218 
   219 fun scan_comment cut =
   220   $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
   221 
   222 fun scan_comment_body cut =
   223   $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
   224 
   225 val recover_comment =
   226   $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
   227 
   228 end;
   229 
   230 
   231 (* source *)
   232 
   233 fun source pos =
   234   Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
   235     Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
   236 
   237 
   238 (* compact representation -- with Symbol.DEL padding *)
   239 
   240 type text = string;
   241 
   242 fun pad [] = []
   243   | pad [(s, _)] = [s]
   244   | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
   245       let
   246         val end_pos1 = Position.advance s1 pos1;
   247         val d = Int.max (0, Position.distance_of end_pos1 pos2);
   248       in s1 :: replicate d Symbol.DEL @ pad rest end;
   249 
   250 val implode = implode o pad;
   251 
   252 fun implode_range pos1 pos2 syms =
   253   let val syms' = (("", pos1) :: syms @ [("", pos2)])
   254   in (implode syms', range syms') end;
   255 
   256 fun explode (str, pos) =
   257   let
   258     val (res, _) =
   259       fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
   260         (Symbol.explode str) ([], Position.reset_range pos);
   261   in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
   262 
   263 
   264 (* identifiers *)
   265 
   266 local
   267 
   268 val letter = Scan.one (symbol #> Symbol.is_letter);
   269 val letdigs1 = Scan.many1 (symbol #> Symbol.is_letdig);
   270 
   271 val sub = Scan.one (symbol #> (fn s => s = "\\<^sub>"));
   272 
   273 in
   274 
   275 val scan_ident = letter ::: (Scan.repeat (letdigs1 || sub ::: letdigs1) >> flat);
   276 
   277 end;
   278 
   279 fun is_identifier s =
   280   Symbol.is_ascii_identifier s orelse
   281     (case try (Scan.finite stopper scan_ident) (explode (s, Position.none)) of
   282       SOME (_, []) => true
   283     | _ => false);
   284 
   285 end;
   286 
   287 structure Basic_Symbol_Pos =   (*not open by default*)
   288 struct
   289   val $$$ = Symbol_Pos.$$$;
   290   val ~$$$ = Symbol_Pos.~$$$;
   291 end;
   292