src/Pure/General/symbol_pos.ML
author wenzelm
Wed Aug 27 12:32:42 2014 +0200 (2014-08-27)
changeset 58047 9f3826352b52
parent 55829 b7bdea5336dd
child 58850 1bb0ad7827b4
permissions -rw-r--r--
tuned signature -- prefer quasi-abstract Symbol_Pos.source;
     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_explode: source -> T list
    45   val source_content: source -> string * Position.T
    46   val scan_ident: T list -> T list * T list
    47   val is_identifier: string -> bool
    48 end;
    49 
    50 structure Symbol_Pos: SYMBOL_POS =
    51 struct
    52 
    53 (* type T *)
    54 
    55 type T = Symbol.symbol * Position.T;
    56 
    57 fun symbol ((s, _): T) = s;
    58 
    59 val content = implode o map symbol;
    60 
    61 fun range (syms as (_, pos) :: _) =
    62       let val pos' = List.last syms |-> Position.advance
    63       in Position.range pos pos' end
    64   | range [] = Position.no_range;
    65 
    66 
    67 (* stopper *)
    68 
    69 fun mk_eof pos = (Symbol.eof, pos);
    70 val eof = mk_eof Position.none;
    71 
    72 val is_eof = Symbol.is_eof o symbol;
    73 
    74 val stopper =
    75   Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
    76 
    77 
    78 (* basic scanners *)
    79 
    80 fun !!! text scan =
    81   let
    82     fun get_pos [] = " (end-of-input)"
    83       | get_pos ((_, pos) :: _) = Position.here pos;
    84 
    85     fun err (syms, msg) = fn () =>
    86       text () ^ get_pos syms ^
    87       Markup.markup Markup.no_report (" at " ^ Symbol.beginning 10 (map symbol syms)) ^
    88       (case msg of NONE => "" | SOME m => "\n" ^ m ());
    89   in Scan.!! err scan end;
    90 
    91 fun change_prompt scan = Scan.prompt "# " scan;
    92 
    93 fun $$ s = Scan.one (fn x => symbol x = s);
    94 fun ~$$ s = Scan.one (fn x => symbol x <> s);
    95 
    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.ahead ($$ q) |--
   121     !!! (fn () => err_prefix ^ "unclosed string literal")
   122       ((scan_pos --| $$$ q) --
   123         (change_prompt ((Scan.repeat (scan_str q err_prefix) >> flat) -- ($$$ q |-- scan_pos))));
   124 
   125 fun recover_strs q =
   126   $$$ q @@@ (Scan.repeat (Scan.permissive (scan_str q "")) >> flat);
   127 
   128 in
   129 
   130 val scan_string_q = scan_strs "'";
   131 val scan_string_qq = scan_strs "\"";
   132 val scan_string_bq = scan_strs "`";
   133 
   134 val recover_string_q = recover_strs "'";
   135 val recover_string_qq = recover_strs "\"";
   136 val recover_string_bq = recover_strs "`";
   137 
   138 end;
   139 
   140 
   141 (* quote string literals *)
   142 
   143 local
   144 
   145 fun char_code i =
   146   (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
   147 
   148 fun quote_str q s =
   149   if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
   150   else if s = q orelse s = "\\" then "\\" ^ s
   151   else s;
   152 
   153 fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
   154 
   155 in
   156 
   157 val quote_string_q = quote_string "'";
   158 val quote_string_qq = quote_string "\"";
   159 val quote_string_bq = quote_string "`";
   160 
   161 end;
   162 
   163 
   164 (* nested text cartouches *)
   165 
   166 val scan_cartouche_depth =
   167   Scan.repeat1 (Scan.depend (fn (d: int) =>
   168     $$ "\\<open>" >> pair (d + 1) ||
   169       (if d > 0 then
   170         Scan.one (fn (s, _) => s <> "\\<close>" andalso Symbol.is_regular s) >> pair d ||
   171         $$ "\\<close>" >> pair (d - 1)
   172       else Scan.fail)));
   173 
   174 fun scan_cartouche err_prefix =
   175   Scan.ahead ($$ "\\<open>") |--
   176     !!! (fn () => err_prefix ^ "unclosed text cartouche")
   177       (change_prompt (Scan.provide (fn d => d = 0) 0 scan_cartouche_depth));
   178 
   179 val recover_cartouche = Scan.pass 0 scan_cartouche_depth;
   180 
   181 fun cartouche_content syms =
   182   let
   183     fun err () =
   184       error ("Malformed text cartouche: "
   185         ^ quote (content syms) ^ Position.here (#1 (range syms)));
   186   in
   187     (case syms of
   188       ("\\<open>", _) :: rest =>
   189         (case rev rest of
   190           ("\\<close>", _) :: rrest => rev rrest
   191         | _ => err ())
   192     | _ => err ())
   193   end;
   194 
   195 
   196 (* ML-style comments *)
   197 
   198 local
   199 
   200 val scan_cmt =
   201   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
   202   Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
   203   Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
   204   Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
   205 
   206 val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
   207 
   208 val scan_body = change_prompt scan_cmts;
   209 
   210 in
   211 
   212 fun scan_comment err_prefix =
   213   Scan.ahead ($$ "(" -- $$ "*") |--
   214     !!! (fn () => err_prefix ^ "unclosed comment")
   215       ($$$ "(" @@@ $$$ "*" @@@ scan_body @@@ $$$ "*" @@@ $$$ ")");
   216 
   217 fun scan_comment_body err_prefix =
   218   Scan.ahead ($$ "(" -- $$ "*") |--
   219     !!! (fn () => err_prefix ^ "unclosed comment")
   220       ($$ "(" |-- $$ "*" |-- scan_body --| $$ "*" --| $$ ")");
   221 
   222 val recover_comment =
   223   $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
   224 
   225 end;
   226 
   227 
   228 (* source *)
   229 
   230 fun source pos =
   231   Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
   232     Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
   233 
   234 
   235 (* compact representation -- with Symbol.DEL padding *)
   236 
   237 type text = string;
   238 
   239 fun pad [] = []
   240   | pad [(s, _)] = [s]
   241   | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
   242       let
   243         val end_pos1 = Position.advance s1 pos1;
   244         val d = Int.max (0, Position.distance_of end_pos1 pos2);
   245       in s1 :: replicate d Symbol.DEL @ pad rest end;
   246 
   247 val implode = implode o pad;
   248 
   249 fun implode_range pos1 pos2 syms =
   250   let val syms' = (("", pos1) :: syms @ [("", pos2)])
   251   in (implode syms', range syms') end;
   252 
   253 fun explode (str, pos) =
   254   let
   255     val (res, _) =
   256       fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
   257         (Symbol.explode str) ([], Position.reset_range pos);
   258   in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
   259 
   260 
   261 (* full source information *)
   262 
   263 type source = {delimited: bool, text: text, pos: Position.T};
   264 
   265 fun source_explode {delimited = _, text, pos} = explode (text, pos);
   266 
   267 fun source_content {delimited = _, text, pos} =
   268   let val syms = explode (text, pos) in (content syms, pos) end;
   269 
   270 
   271 (* identifiers *)
   272 
   273 local
   274 
   275 val letter = Scan.one (symbol #> Symbol.is_letter);
   276 val letdigs1 = Scan.many1 (symbol #> Symbol.is_letdig);
   277 
   278 val sub = Scan.one (symbol #> (fn s => s = "\\<^sub>"));
   279 
   280 in
   281 
   282 val scan_ident = letter ::: (Scan.repeat (letdigs1 || sub ::: letdigs1) >> flat);
   283 
   284 end;
   285 
   286 fun is_identifier s =
   287   Symbol.is_ascii_identifier s orelse
   288     (case try (Scan.finite stopper scan_ident) (explode (s, Position.none)) of
   289       SOME (_, []) => true
   290     | _ => false);
   291 
   292 end;
   293 
   294 structure Basic_Symbol_Pos =   (*not open by default*)
   295 struct
   296   val $$ = Symbol_Pos.$$;
   297   val ~$$ = Symbol_Pos.~$$;
   298   val $$$ = Symbol_Pos.$$$;
   299   val ~$$$ = Symbol_Pos.~$$$;
   300 end;
   301