src/Pure/General/symbol_pos.ML
author wenzelm
Sat Jan 18 19:31:32 2014 +0100 (2014-01-18)
changeset 55035 68afbb5ce4ff
parent 55033 8e8243975860
child 55103 57d87ec3da4c
permissions -rw-r--r--
unused;
     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 list * T list
    12   val ~$$$ : Symbol.symbol -> T list -> T list * T list
    13   val content: T list -> string
    14   val range: T list -> Position.range
    15   val is_eof: T -> bool
    16   val stopper: T Scan.stopper
    17   val !!! : Scan.message -> (T list -> 'a) -> T list -> 'a
    18   val change_prompt: ('a -> 'b) -> 'a -> 'b
    19   val scan_pos: T list -> Position.T * T list
    20   val scan_string_q: string -> T list -> (Position.T * (T list * Position.T)) * T list
    21   val scan_string_qq: string -> T list -> (Position.T * (T list * Position.T)) * T list
    22   val scan_string_bq: string -> T list -> (Position.T * (T list * Position.T)) * T list
    23   val recover_string_q: T list -> T list * T list
    24   val recover_string_qq: T list -> T list * T list
    25   val recover_string_bq: T list -> T list * T list
    26   val quote_string_q: string -> string
    27   val quote_string_qq: string -> string
    28   val quote_string_bq: string -> string
    29   val scan_cartouche: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    30     T list -> T list * T list
    31   val scan_cartouche_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    32     T list -> T list * T list
    33   val recover_cartouche: T list -> T list * T list
    34   val cartouche_content: T list -> T list
    35   val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    36     T list -> T list * T list
    37   val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    38     T list -> T list * T list
    39   val recover_comment: T list -> T list * T list
    40   val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
    41     (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
    42   type text = string
    43   val implode: T list -> text
    44   val implode_range: Position.T -> Position.T -> T list -> text * Position.range
    45   val explode: text * Position.T -> T list
    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) >> 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_pos --| $$$ q) -- !!! (fn () => err_prefix ^ "missing quote at end of string")
   119     (change_prompt ((Scan.repeat (scan_str q err_prefix) >> flat) -- ($$$ q |-- scan_pos)));
   120 
   121 fun recover_strs q =
   122   $$$ q @@@ (Scan.repeat (Scan.permissive (scan_str q "")) >> flat);
   123 
   124 in
   125 
   126 val scan_string_q = scan_strs "'";
   127 val scan_string_qq = scan_strs "\"";
   128 val scan_string_bq = scan_strs "`";
   129 
   130 val recover_string_q = recover_strs "'";
   131 val recover_string_qq = recover_strs "\"";
   132 val recover_string_bq = recover_strs "`";
   133 
   134 end;
   135 
   136 
   137 (* quote string literals *)
   138 
   139 local
   140 
   141 fun char_code i =
   142   (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
   143 
   144 fun quote_str q s =
   145   if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
   146   else if s = q orelse s = "\\" then "\\" ^ s
   147   else s;
   148 
   149 fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
   150 
   151 in
   152 
   153 val quote_string_q = quote_string "'";
   154 val quote_string_qq = quote_string "\"";
   155 val quote_string_bq = quote_string "`";
   156 
   157 end;
   158 
   159 
   160 (* nested text cartouches *)
   161 
   162 local
   163 
   164 val scan_cart =
   165   Scan.depend (fn (d: int) => $$ "\\<open>" >> pair (d + 1)) ||
   166   Scan.depend (fn 0 => Scan.fail | d => $$ "\\<close>" >> pair (d - 1)) ||
   167   Scan.lift (Scan.one (fn (s, _) => s <> "\\<close>" andalso Symbol.is_regular s));
   168 
   169 val scan_carts = Scan.pass 0 (Scan.repeat scan_cart);
   170 
   171 val scan_body = change_prompt scan_carts;
   172 
   173 in
   174 
   175 fun scan_cartouche cut =
   176   $$ "\\<open>" ::: cut "unclosed text cartouche" (scan_body @@@ $$$ "\\<close>");
   177 
   178 fun scan_cartouche_body cut =
   179   $$ "\\<open>" |-- cut "unclosed text cartouche" (scan_body --| $$ "\\<close>");
   180 
   181 val recover_cartouche =
   182   $$$ "\\<open>" @@@ scan_carts;
   183 
   184 end;
   185 
   186 fun cartouche_content syms =
   187   let
   188     fun err () =
   189       error ("Malformed text cartouche: " ^ quote (content syms) ^
   190         Position.here (Position.set_range (range syms)));
   191   in
   192     (case syms of
   193       ("\\<open>", _) :: rest =>
   194         (case rev rest of
   195           ("\\<close>", _) :: rrest => rev rrest
   196         | _ => err ())
   197     | _ => err ())
   198   end;
   199 
   200 
   201 (* ML-style comments *)
   202 
   203 local
   204 
   205 val scan_cmt =
   206   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
   207   Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
   208   Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
   209   Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
   210 
   211 val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
   212 
   213 val scan_body = change_prompt scan_cmts;
   214 
   215 in
   216 
   217 fun scan_comment cut =
   218   $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
   219 
   220 fun scan_comment_body cut =
   221   $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
   222 
   223 val recover_comment =
   224   $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
   225 
   226 end;
   227 
   228 
   229 (* source *)
   230 
   231 fun source pos =
   232   Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
   233     Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
   234 
   235 
   236 (* compact representation -- with Symbol.DEL padding *)
   237 
   238 type text = string;
   239 
   240 fun pad [] = []
   241   | pad [(s, _)] = [s]
   242   | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
   243       let
   244         val end_pos1 = Position.advance s1 pos1;
   245         val d = Int.max (0, Position.distance_of end_pos1 pos2);
   246       in s1 :: replicate d Symbol.DEL @ pad rest end;
   247 
   248 val implode = implode o pad;
   249 
   250 fun implode_range pos1 pos2 syms =
   251   let val syms' = (("", pos1) :: syms @ [("", pos2)])
   252   in (implode syms', range syms') end;
   253 
   254 fun explode (str, pos) =
   255   let
   256     val (res, _) =
   257       fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
   258         (Symbol.explode str) ([], Position.reset_range pos);
   259   in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
   260 
   261 
   262 (* identifiers *)
   263 
   264 local
   265 
   266 val letter = Scan.one (symbol #> Symbol.is_letter);
   267 val letdigs1 = Scan.many1 (symbol #> Symbol.is_letdig);
   268 
   269 val sub = Scan.one (symbol #> (fn s => s = "\\<^sub>"));
   270 
   271 in
   272 
   273 val scan_ident = letter ::: (Scan.repeat (letdigs1 || sub ::: letdigs1) >> flat);
   274 
   275 end;
   276 
   277 fun is_identifier s =
   278   Symbol.is_ascii_identifier s orelse
   279     (case try (Scan.finite stopper scan_ident) (explode (s, Position.none)) of
   280       SOME (_, []) => true
   281     | _ => false);
   282 
   283 end;
   284 
   285 structure Basic_Symbol_Pos =   (*not open by default*)
   286 struct
   287   val $$$ = Symbol_Pos.$$$;
   288   val ~$$$ = Symbol_Pos.~$$$;
   289 end;
   290