src/Pure/General/symbol_pos.ML
author wenzelm
Tue Aug 13 17:26:22 2013 +0200 (2013-08-13)
changeset 53016 fa9c38891cf2
parent 52920 4539e4a06339
child 54732 b01bb3d09928
permissions -rw-r--r--
disable old identifier syntax by default, legacy_isub_isup := true may be used temporarily as fall-back;
     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 is_eof: T -> bool
    17   val stopper: T Scan.stopper
    18   val !!! : Scan.message -> (T list -> 'a) -> T list -> 'a
    19   val change_prompt: ('a -> 'b) -> 'a -> 'b
    20   val scan_pos: T list -> Position.T * T list
    21   val scan_string_q: string -> T list -> (Position.T * (T list * Position.T)) * T list
    22   val scan_string_qq: string -> T list -> (Position.T * (T list * Position.T)) * T list
    23   val scan_string_bq: string -> T list -> (Position.T * (T list * Position.T)) * T list
    24   val recover_string_q: T list -> T list * T list
    25   val recover_string_qq: T list -> T list * T list
    26   val recover_string_bq: T list -> T list * T list
    27   val quote_string_q: string -> string
    28   val quote_string_qq: string -> string
    29   val quote_string_bq: string -> string
    30   val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    31     T list -> T list * T list
    32   val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    33     T list -> T list * T list
    34   val recover_comment: T list -> T list * T list
    35   val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
    36     (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
    37   type text = string
    38   val implode: T list -> text
    39   val range: T list -> Position.range
    40   val implode_range: Position.T -> Position.T -> T list -> text * Position.range
    41   val explode: text * Position.T -> T list
    42   val scan_ident: T list -> T list * T list
    43   val is_identifier: string -> bool
    44 end;
    45 
    46 structure Symbol_Pos: SYMBOL_POS =
    47 struct
    48 
    49 (* type T *)
    50 
    51 type T = Symbol.symbol * Position.T;
    52 
    53 fun symbol ((s, _): T) = s;
    54 
    55 val content = implode o map symbol;
    56 
    57 
    58 (* stopper *)
    59 
    60 fun mk_eof pos = (Symbol.eof, pos);
    61 val eof = mk_eof Position.none;
    62 
    63 val is_eof = Symbol.is_eof o symbol;
    64 
    65 val stopper =
    66   Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
    67 
    68 
    69 (* basic scanners *)
    70 
    71 fun !!! text scan =
    72   let
    73     fun get_pos [] = " (end-of-input)"
    74       | get_pos ((_, pos) :: _) = Position.here pos;
    75 
    76     fun err (syms, msg) = fn () =>
    77       text () ^ get_pos syms ^
    78       Markup.markup Markup.no_report (" at " ^ Symbol.beginning 10 (map symbol syms)) ^
    79       (case msg of NONE => "" | SOME m => "\n" ^ m ());
    80   in Scan.!! err scan end;
    81 
    82 fun change_prompt scan = Scan.prompt "# " scan;
    83 
    84 fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
    85 fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
    86 
    87 val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
    88 
    89 
    90 (* scan string literals *)
    91 
    92 local
    93 
    94 val char_code =
    95   Scan.one (Symbol.is_ascii_digit o symbol) --
    96   Scan.one (Symbol.is_ascii_digit o symbol) --
    97   Scan.one (Symbol.is_ascii_digit o symbol) :|--
    98   (fn (((a, pos), (b, _)), (c, _)) =>
    99     let val (n, _) = Library.read_int [a, b, c]
   100     in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
   101 
   102 fun scan_str q err_prefix =
   103   $$$ "\\" |-- !!! (fn () => err_prefix ^ "bad escape character in string")
   104     ($$$ q || $$$ "\\" || char_code) ||
   105   Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
   106 
   107 fun scan_strs q err_prefix =
   108   (scan_pos --| $$$ q) -- !!! (fn () => err_prefix ^ "missing quote at end of string")
   109     (change_prompt ((Scan.repeat (scan_str q err_prefix) >> flat) -- ($$$ q |-- scan_pos)));
   110 
   111 fun recover_strs q =
   112   $$$ q @@@ (Scan.repeat (Scan.permissive (scan_str q "")) >> flat);
   113 
   114 in
   115 
   116 val scan_string_q = scan_strs "'";
   117 val scan_string_qq = scan_strs "\"";
   118 val scan_string_bq = scan_strs "`";
   119 
   120 val recover_string_q = recover_strs "'";
   121 val recover_string_qq = recover_strs "\"";
   122 val recover_string_bq = recover_strs "`";
   123 
   124 end;
   125 
   126 
   127 (* quote string literals *)
   128 
   129 local
   130 
   131 fun char_code i =
   132   (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
   133 
   134 fun quote_str q s =
   135   if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
   136   else if s = q orelse s = "\\" then "\\" ^ s
   137   else s;
   138 
   139 fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
   140 
   141 in
   142 
   143 val quote_string_q = quote_string "'";
   144 val quote_string_qq = quote_string "\"";
   145 val quote_string_bq = quote_string "`";
   146 
   147 end;
   148 
   149 
   150 (* ML-style comments *)
   151 
   152 local
   153 
   154 val scan_cmt =
   155   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
   156   Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
   157   Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
   158   Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
   159 
   160 val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
   161 
   162 val scan_body = change_prompt scan_cmts;
   163 
   164 in
   165 
   166 fun scan_comment cut =
   167   $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
   168 
   169 fun scan_comment_body cut =
   170   $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
   171 
   172 val recover_comment =
   173   $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
   174 
   175 end;
   176 
   177 
   178 (* source *)
   179 
   180 fun source pos =
   181   Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
   182     Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
   183 
   184 
   185 (* compact representation -- with Symbol.DEL padding *)
   186 
   187 type text = string;
   188 
   189 fun pad [] = []
   190   | pad [(s, _)] = [s]
   191   | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
   192       let
   193         val end_pos1 = Position.advance s1 pos1;
   194         val d = Int.max (0, Position.distance_of end_pos1 pos2);
   195       in s1 :: replicate d Symbol.DEL @ pad rest end;
   196 
   197 val implode = implode o pad;
   198 
   199 fun range (syms as (_, pos) :: _) =
   200       let val pos' = List.last syms |-> Position.advance
   201       in Position.range pos pos' end
   202   | range [] = Position.no_range;
   203 
   204 fun implode_range pos1 pos2 syms =
   205   let val syms' = (("", pos1) :: syms @ [("", pos2)])
   206   in (implode syms', range syms') end;
   207 
   208 fun explode (str, pos) =
   209   let
   210     val (res, _) =
   211       fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
   212         (Symbol.explode str) ([], Position.reset_range pos);
   213   in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
   214 
   215 
   216 (* identifiers *)
   217 
   218 local
   219 
   220 val letter = Scan.one (symbol #> Symbol.is_letter);
   221 val letdigs1 = Scan.many1 (symbol #> Symbol.is_letdig);
   222 
   223 val sub =
   224   Scan.one (symbol #> (fn s =>
   225     if ! legacy_isub_isup then s = "\\<^sub>" orelse s = "\\<^isub>" orelse s = "\\<^isup>"
   226     else s = "\\<^sub>"));
   227 
   228 in
   229 
   230 val scan_ident = letter ::: (Scan.repeat (letdigs1 || sub ::: letdigs1) >> flat);
   231 
   232 end;
   233 
   234 fun is_identifier s =
   235   Symbol.is_ascii_identifier s orelse
   236     (case try (Scan.finite stopper scan_ident) (explode (s, Position.none)) of
   237       SOME (_, []) => true
   238     | _ => false);
   239 
   240 end;
   241 
   242 structure Basic_Symbol_Pos =   (*not open by default*)
   243 struct
   244   val $$$ = Symbol_Pos.$$$;
   245   val ~$$$ = Symbol_Pos.~$$$;
   246 end;
   247