src/Pure/General/symbol_pos.ML
author wenzelm
Mon Jan 20 19:47:31 2014 +0100 (2014-01-20)
changeset 55104 8284c0d5bf52
parent 55103 57d87ec3da4c
child 55105 75815b3b38a1
permissions -rw-r--r--
clarified scan_cartouche_depth, according to Scala version;
more accurate error position;
     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 list * T list
    13   val ~$$$ : Symbol.symbol -> T list -> T list * T list
    14   val content: T list -> string
    15   val range: T list -> Position.range
    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_cartouche: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    31     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) -> T list -> T list * T list) ->
    35     T list -> T list * T list
    36   val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    37     T list -> T list * T list
    38   val recover_comment: T list -> T list * T list
    39   val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
    40     (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
    41   type text = string
    42   val implode: T list -> text
    43   val implode_range: Position.T -> Position.T -> T list -> text * Position.range
    44   val explode: text * Position.T -> T list
    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) >> single;
    94 fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
    95 
    96 val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
    97 
    98 
    99 (* scan string literals *)
   100 
   101 local
   102 
   103 val char_code =
   104   Scan.one (Symbol.is_ascii_digit o symbol) --
   105   Scan.one (Symbol.is_ascii_digit o symbol) --
   106   Scan.one (Symbol.is_ascii_digit o symbol) :|--
   107   (fn (((a, pos), (b, _)), (c, _)) =>
   108     let val (n, _) = Library.read_int [a, b, c]
   109     in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
   110 
   111 fun scan_str q err_prefix =
   112   $$$ "\\" |-- !!! (fn () => err_prefix ^ "bad escape character in string")
   113     ($$$ q || $$$ "\\" || char_code) ||
   114   Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
   115 
   116 fun scan_strs q err_prefix =
   117   Scan.ahead ($$ q) |--
   118     !!! (fn () => err_prefix ^ "unclosed string literal")
   119       ((scan_pos --| $$$ q) --
   120         (change_prompt ((Scan.repeat (scan_str q err_prefix) >> flat) -- ($$$ q |-- scan_pos))));
   121 
   122 fun recover_strs q =
   123   $$$ q @@@ (Scan.repeat (Scan.permissive (scan_str q "")) >> flat);
   124 
   125 in
   126 
   127 val scan_string_q = scan_strs "'";
   128 val scan_string_qq = scan_strs "\"";
   129 val scan_string_bq = scan_strs "`";
   130 
   131 val recover_string_q = recover_strs "'";
   132 val recover_string_qq = recover_strs "\"";
   133 val recover_string_bq = recover_strs "`";
   134 
   135 end;
   136 
   137 
   138 (* quote string literals *)
   139 
   140 local
   141 
   142 fun char_code i =
   143   (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
   144 
   145 fun quote_str q s =
   146   if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
   147   else if s = q orelse s = "\\" then "\\" ^ s
   148   else s;
   149 
   150 fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
   151 
   152 in
   153 
   154 val quote_string_q = quote_string "'";
   155 val quote_string_qq = quote_string "\"";
   156 val quote_string_bq = quote_string "`";
   157 
   158 end;
   159 
   160 
   161 (* nested text cartouches *)
   162 
   163 val scan_cartouche_depth =
   164   Scan.repeat1 (Scan.depend (fn (d: int) =>
   165     $$ "\\<open>" >> pair (d + 1) ||
   166       (if d > 0 then
   167         Scan.one (fn (s, _) => s <> "\\<close>" andalso Symbol.is_regular s) >> pair d ||
   168         $$ "\\<close>" >> pair (d - 1)
   169       else Scan.fail)));
   170 
   171 fun scan_cartouche cut =
   172   Scan.ahead ($$ "\\<open>") |--
   173     cut "unclosed text cartouche"
   174       (change_prompt (Scan.provide (fn d => d = 0) 0 scan_cartouche_depth));
   175 
   176 val recover_cartouche = Scan.pass 0 scan_cartouche_depth;
   177 
   178 fun cartouche_content syms =
   179   let
   180     fun err () =
   181       error ("Malformed text cartouche: " ^ quote (content syms) ^
   182         Position.here (Position.set_range (range syms)));
   183   in
   184     (case syms of
   185       ("\\<open>", _) :: rest =>
   186         (case rev rest of
   187           ("\\<close>", _) :: rrest => rev rrest
   188         | _ => err ())
   189     | _ => err ())
   190   end;
   191 
   192 
   193 (* ML-style comments *)
   194 
   195 local
   196 
   197 val scan_cmt =
   198   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
   199   Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
   200   Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
   201   Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
   202 
   203 val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
   204 
   205 val scan_body = change_prompt scan_cmts;
   206 
   207 in
   208 
   209 fun scan_comment cut =
   210   $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
   211 
   212 fun scan_comment_body cut =
   213   $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
   214 
   215 val recover_comment =
   216   $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
   217 
   218 end;
   219 
   220 
   221 (* source *)
   222 
   223 fun source pos =
   224   Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
   225     Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
   226 
   227 
   228 (* compact representation -- with Symbol.DEL padding *)
   229 
   230 type text = string;
   231 
   232 fun pad [] = []
   233   | pad [(s, _)] = [s]
   234   | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
   235       let
   236         val end_pos1 = Position.advance s1 pos1;
   237         val d = Int.max (0, Position.distance_of end_pos1 pos2);
   238       in s1 :: replicate d Symbol.DEL @ pad rest end;
   239 
   240 val implode = implode o pad;
   241 
   242 fun implode_range pos1 pos2 syms =
   243   let val syms' = (("", pos1) :: syms @ [("", pos2)])
   244   in (implode syms', range syms') end;
   245 
   246 fun explode (str, pos) =
   247   let
   248     val (res, _) =
   249       fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
   250         (Symbol.explode str) ([], Position.reset_range pos);
   251   in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
   252 
   253 
   254 (* identifiers *)
   255 
   256 local
   257 
   258 val letter = Scan.one (symbol #> Symbol.is_letter);
   259 val letdigs1 = Scan.many1 (symbol #> Symbol.is_letdig);
   260 
   261 val sub = Scan.one (symbol #> (fn s => s = "\\<^sub>"));
   262 
   263 in
   264 
   265 val scan_ident = letter ::: (Scan.repeat (letdigs1 || sub ::: letdigs1) >> flat);
   266 
   267 end;
   268 
   269 fun is_identifier s =
   270   Symbol.is_ascii_identifier s orelse
   271     (case try (Scan.finite stopper scan_ident) (explode (s, Position.none)) of
   272       SOME (_, []) => true
   273     | _ => false);
   274 
   275 end;
   276 
   277 structure Basic_Symbol_Pos =   (*not open by default*)
   278 struct
   279   val $$ = Symbol_Pos.$$;
   280   val $$$ = Symbol_Pos.$$$;
   281   val ~$$$ = Symbol_Pos.~$$$;
   282 end;
   283