src/Pure/General/symbol_pos.ML
author wenzelm
Sat Mar 01 23:17:37 2014 +0100 (2014-03-01)
changeset 55829 b7bdea5336dd
parent 55828 42ac3cfb89f6
child 58047 9f3826352b52
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 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
    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.T -> Position.T -> T list -> text * Position.range
    42   val explode: text * Position.T -> T list
    43   type source = {delimited: bool, text: text, pos: Position.T}
    44   val source_content: source -> string * Position.T
    45   val scan_ident: T list -> T list * T list
    46   val is_identifier: string -> bool
    47 end;
    48 
    49 structure Symbol_Pos: SYMBOL_POS =
    50 struct
    51 
    52 (* type T *)
    53 
    54 type T = Symbol.symbol * Position.T;
    55 
    56 fun symbol ((s, _): T) = s;
    57 
    58 val content = implode o map symbol;
    59 
    60 fun range (syms as (_, pos) :: _) =
    61       let val pos' = List.last syms |-> Position.advance
    62       in Position.range pos pos' end
    63   | range [] = Position.no_range;
    64 
    65 
    66 (* stopper *)
    67 
    68 fun mk_eof pos = (Symbol.eof, pos);
    69 val eof = mk_eof Position.none;
    70 
    71 val is_eof = Symbol.is_eof o symbol;
    72 
    73 val stopper =
    74   Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
    75 
    76 
    77 (* basic scanners *)
    78 
    79 fun !!! text scan =
    80   let
    81     fun get_pos [] = " (end-of-input)"
    82       | get_pos ((_, pos) :: _) = Position.here pos;
    83 
    84     fun err (syms, msg) = fn () =>
    85       text () ^ get_pos syms ^
    86       Markup.markup Markup.no_report (" at " ^ Symbol.beginning 10 (map symbol syms)) ^
    87       (case msg of NONE => "" | SOME m => "\n" ^ m ());
    88   in Scan.!! err scan end;
    89 
    90 fun change_prompt scan = Scan.prompt "# " scan;
    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.is_regular 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) --
   122         (change_prompt ((Scan.repeat (scan_str q err_prefix) >> flat) -- ($$$ q |-- scan_pos))));
   123 
   124 fun recover_strs q =
   125   $$$ q @@@ (Scan.repeat (Scan.permissive (scan_str q "")) >> flat);
   126 
   127 in
   128 
   129 val scan_string_q = scan_strs "'";
   130 val scan_string_qq = scan_strs "\"";
   131 val scan_string_bq = scan_strs "`";
   132 
   133 val recover_string_q = recover_strs "'";
   134 val recover_string_qq = recover_strs "\"";
   135 val recover_string_bq = recover_strs "`";
   136 
   137 end;
   138 
   139 
   140 (* quote string literals *)
   141 
   142 local
   143 
   144 fun char_code i =
   145   (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
   146 
   147 fun quote_str q s =
   148   if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
   149   else if s = q orelse s = "\\" then "\\" ^ s
   150   else s;
   151 
   152 fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
   153 
   154 in
   155 
   156 val quote_string_q = quote_string "'";
   157 val quote_string_qq = quote_string "\"";
   158 val quote_string_bq = quote_string "`";
   159 
   160 end;
   161 
   162 
   163 (* nested text cartouches *)
   164 
   165 val scan_cartouche_depth =
   166   Scan.repeat1 (Scan.depend (fn (d: int) =>
   167     $$ "\\<open>" >> pair (d + 1) ||
   168       (if d > 0 then
   169         Scan.one (fn (s, _) => s <> "\\<close>" andalso Symbol.is_regular s) >> pair d ||
   170         $$ "\\<close>" >> pair (d - 1)
   171       else Scan.fail)));
   172 
   173 fun scan_cartouche err_prefix =
   174   Scan.ahead ($$ "\\<open>") |--
   175     !!! (fn () => err_prefix ^ "unclosed text cartouche")
   176       (change_prompt (Scan.provide (fn d => d = 0) 0 scan_cartouche_depth));
   177 
   178 val recover_cartouche = Scan.pass 0 scan_cartouche_depth;
   179 
   180 fun cartouche_content syms =
   181   let
   182     fun err () =
   183       error ("Malformed text cartouche: "
   184         ^ quote (content syms) ^ Position.here (#1 (range syms)));
   185   in
   186     (case syms of
   187       ("\\<open>", _) :: rest =>
   188         (case rev rest of
   189           ("\\<close>", _) :: rrest => rev rrest
   190         | _ => err ())
   191     | _ => err ())
   192   end;
   193 
   194 
   195 (* ML-style comments *)
   196 
   197 local
   198 
   199 val scan_cmt =
   200   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
   201   Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
   202   Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
   203   Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
   204 
   205 val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
   206 
   207 val scan_body = change_prompt scan_cmts;
   208 
   209 in
   210 
   211 fun scan_comment err_prefix =
   212   Scan.ahead ($$ "(" -- $$ "*") |--
   213     !!! (fn () => err_prefix ^ "unclosed comment")
   214       ($$$ "(" @@@ $$$ "*" @@@ scan_body @@@ $$$ "*" @@@ $$$ ")");
   215 
   216 fun scan_comment_body err_prefix =
   217   Scan.ahead ($$ "(" -- $$ "*") |--
   218     !!! (fn () => err_prefix ^ "unclosed comment")
   219       ($$ "(" |-- $$ "*" |-- scan_body --| $$ "*" --| $$ ")");
   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)))))) NONE;
   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 (* full source information *)
   261 
   262 type source = {delimited: bool, text: text, pos: Position.T};
   263 
   264 fun source_content {delimited = _, text, pos} =
   265   let val syms = explode (text, pos) in (content syms, pos) end;
   266 
   267 
   268 (* identifiers *)
   269 
   270 local
   271 
   272 val letter = Scan.one (symbol #> Symbol.is_letter);
   273 val letdigs1 = Scan.many1 (symbol #> Symbol.is_letdig);
   274 
   275 val sub = Scan.one (symbol #> (fn s => s = "\\<^sub>"));
   276 
   277 in
   278 
   279 val scan_ident = letter ::: (Scan.repeat (letdigs1 || sub ::: letdigs1) >> flat);
   280 
   281 end;
   282 
   283 fun is_identifier s =
   284   Symbol.is_ascii_identifier s orelse
   285     (case try (Scan.finite stopper scan_ident) (explode (s, Position.none)) of
   286       SOME (_, []) => true
   287     | _ => false);
   288 
   289 end;
   290 
   291 structure Basic_Symbol_Pos =   (*not open by default*)
   292 struct
   293   val $$ = Symbol_Pos.$$;
   294   val ~$$ = Symbol_Pos.~$$;
   295   val $$$ = Symbol_Pos.$$$;
   296   val ~$$$ = Symbol_Pos.~$$$;
   297 end;
   298