src/Pure/General/symbol_pos.ML
author wenzelm
Sat Mar 01 22:46:31 2014 +0100 (2014-03-01)
changeset 55828 42ac3cfb89f6
parent 55709 4e5a83a46ded
child 55829 b7bdea5336dd
permissions -rw-r--r--
clarified language markup: added "delimited" property;
type Symbol_Pos.source preserves information about delimited outer tokens (e.g string, cartouche);
observe Completion.Language_Context only for delimited languages, which is important to complete keywords after undelimited inner tokens, e.g. "lemma A pro";
     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   type source = {delimited: bool, text: text, pos: Position.T}
    41   val implode: T list -> text
    42   val implode_range: Position.T -> Position.T -> T list -> text * Position.range
    43   val explode: text * Position.T -> T list
    44   val scan_ident: T list -> T list * T list
    45   val is_identifier: string -> bool
    46 end;
    47 
    48 structure Symbol_Pos: SYMBOL_POS =
    49 struct
    50 
    51 (* type T *)
    52 
    53 type T = Symbol.symbol * Position.T;
    54 
    55 fun symbol ((s, _): T) = s;
    56 
    57 val content = implode o map symbol;
    58 
    59 fun range (syms as (_, pos) :: _) =
    60       let val pos' = List.last syms |-> Position.advance
    61       in Position.range pos pos' end
    62   | range [] = Position.no_range;
    63 
    64 
    65 (* stopper *)
    66 
    67 fun mk_eof pos = (Symbol.eof, pos);
    68 val eof = mk_eof Position.none;
    69 
    70 val is_eof = Symbol.is_eof o symbol;
    71 
    72 val stopper =
    73   Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
    74 
    75 
    76 (* basic scanners *)
    77 
    78 fun !!! text scan =
    79   let
    80     fun get_pos [] = " (end-of-input)"
    81       | get_pos ((_, pos) :: _) = Position.here pos;
    82 
    83     fun err (syms, msg) = fn () =>
    84       text () ^ get_pos syms ^
    85       Markup.markup Markup.no_report (" at " ^ Symbol.beginning 10 (map symbol syms)) ^
    86       (case msg of NONE => "" | SOME m => "\n" ^ m ());
    87   in Scan.!! err scan end;
    88 
    89 fun change_prompt scan = Scan.prompt "# " scan;
    90 
    91 fun $$ s = Scan.one (fn x => symbol x = s);
    92 fun ~$$ s = Scan.one (fn x => symbol x <> s);
    93 
    94 fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
    95 fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
    96 
    97 val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
    98 
    99 
   100 (* scan string literals *)
   101 
   102 local
   103 
   104 val char_code =
   105   Scan.one (Symbol.is_ascii_digit o symbol) --
   106   Scan.one (Symbol.is_ascii_digit o symbol) --
   107   Scan.one (Symbol.is_ascii_digit o symbol) :|--
   108   (fn (((a, pos), (b, _)), (c, _)) =>
   109     let val (n, _) = Library.read_int [a, b, c]
   110     in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
   111 
   112 fun scan_str q err_prefix =
   113   $$$ "\\" |-- !!! (fn () => err_prefix ^ "bad escape character in string")
   114     ($$$ q || $$$ "\\" || char_code) ||
   115   Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
   116 
   117 fun scan_strs q err_prefix =
   118   Scan.ahead ($$ q) |--
   119     !!! (fn () => err_prefix ^ "unclosed string literal")
   120       ((scan_pos --| $$$ q) --
   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 val scan_cartouche_depth =
   165   Scan.repeat1 (Scan.depend (fn (d: int) =>
   166     $$ "\\<open>" >> pair (d + 1) ||
   167       (if d > 0 then
   168         Scan.one (fn (s, _) => s <> "\\<close>" andalso Symbol.is_regular s) >> pair d ||
   169         $$ "\\<close>" >> pair (d - 1)
   170       else Scan.fail)));
   171 
   172 fun scan_cartouche err_prefix =
   173   Scan.ahead ($$ "\\<open>") |--
   174     !!! (fn () => err_prefix ^ "unclosed text cartouche")
   175       (change_prompt (Scan.provide (fn d => d = 0) 0 scan_cartouche_depth));
   176 
   177 val recover_cartouche = Scan.pass 0 scan_cartouche_depth;
   178 
   179 fun cartouche_content syms =
   180   let
   181     fun err () =
   182       error ("Malformed text cartouche: "
   183         ^ quote (content syms) ^ Position.here (#1 (range syms)));
   184   in
   185     (case syms of
   186       ("\\<open>", _) :: rest =>
   187         (case rev rest of
   188           ("\\<close>", _) :: rrest => rev rrest
   189         | _ => err ())
   190     | _ => err ())
   191   end;
   192 
   193 
   194 (* ML-style comments *)
   195 
   196 local
   197 
   198 val scan_cmt =
   199   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
   200   Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
   201   Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
   202   Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
   203 
   204 val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
   205 
   206 val scan_body = change_prompt scan_cmts;
   207 
   208 in
   209 
   210 fun scan_comment err_prefix =
   211   Scan.ahead ($$ "(" -- $$ "*") |--
   212     !!! (fn () => err_prefix ^ "unclosed comment")
   213       ($$$ "(" @@@ $$$ "*" @@@ scan_body @@@ $$$ "*" @@@ $$$ ")");
   214 
   215 fun scan_comment_body err_prefix =
   216   Scan.ahead ($$ "(" -- $$ "*") |--
   217     !!! (fn () => err_prefix ^ "unclosed comment")
   218       ($$ "(" |-- $$ "*" |-- scan_body --| $$ "*" --| $$ ")");
   219 
   220 val recover_comment =
   221   $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
   222 
   223 end;
   224 
   225 
   226 (* source *)
   227 
   228 fun source pos =
   229   Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
   230     Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
   231 
   232 
   233 (* compact representation -- with Symbol.DEL padding *)
   234 
   235 type text = string;
   236 type source = {delimited: bool, text: text, pos: Position.T}
   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.repeat (letdigs1 || sub ::: letdigs1) >> flat);
   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