src/Pure/General/symbol_pos.ML
author wenzelm
Fri Nov 30 15:36:51 2012 +0100 (2012-11-30)
changeset 50295 3d6a4135a54f
parent 50253 41fd9f68614b
child 50493 2bf3bfbb422d
permissions -rw-r--r--
eliminated redundant is_ident -- more official is_identifier;
     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 is_eof: T -> bool
    15   val stopper: T Scan.stopper
    16   val !!! : Scan.message -> (T list -> 'a) -> T list -> 'a
    17   val change_prompt: ('a -> 'b) -> 'a -> 'b
    18   val scan_pos: T list -> Position.T * T list
    19   val scan_string_q: string -> T list -> (Position.T * (T list * Position.T)) * T list
    20   val scan_string_qq: string -> T list -> (Position.T * (T list * Position.T)) * T list
    21   val scan_string_bq: string -> T list -> (Position.T * (T list * Position.T)) * T list
    22   val recover_string_q: T list -> T list * T list
    23   val recover_string_qq: T list -> T list * T list
    24   val recover_string_bq: T list -> T list * T list
    25   val quote_string_q: string -> string
    26   val quote_string_qq: string -> string
    27   val quote_string_bq: string -> string
    28   val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    29     T list -> T list * T list
    30   val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    31     T list -> T list * T list
    32   val recover_comment: T list -> T list * T list
    33   val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
    34     (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
    35   type text = string
    36   val implode: T list -> text
    37   val range: T list -> Position.range
    38   val implode_range: Position.T -> Position.T -> T list -> text * Position.range
    39   val explode: text * Position.T -> T list
    40   val scan_new_ident: T list -> T list * T list
    41   val scan_ident: T list -> T list * T list
    42   val is_identifier: string -> bool
    43 end;
    44 
    45 structure Symbol_Pos: SYMBOL_POS =
    46 struct
    47 
    48 (* type T *)
    49 
    50 type T = Symbol.symbol * Position.T;
    51 
    52 fun symbol ((s, _): T) = s;
    53 
    54 val content = implode o map symbol;
    55 
    56 
    57 (* stopper *)
    58 
    59 fun mk_eof pos = (Symbol.eof, pos);
    60 val eof = mk_eof Position.none;
    61 
    62 val is_eof = Symbol.is_eof o symbol;
    63 
    64 val stopper =
    65   Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
    66 
    67 
    68 (* basic scanners *)
    69 
    70 fun !!! text scan =
    71   let
    72     fun get_pos [] = " (end-of-input)"
    73       | get_pos ((_, pos) :: _) = Position.here pos;
    74 
    75     fun err (syms, msg) = fn () =>
    76       text () ^ get_pos syms ^
    77       Markup.markup Markup.no_report (" at " ^ Symbol.beginning 10 (map symbol syms)) ^
    78       (case msg of NONE => "" | SOME m => "\n" ^ m ());
    79   in Scan.!! err scan end;
    80 
    81 fun change_prompt scan = Scan.prompt "# " scan;
    82 
    83 fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
    84 fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
    85 
    86 val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
    87 
    88 
    89 (* scan string literals *)
    90 
    91 local
    92 
    93 val char_code =
    94   Scan.one (Symbol.is_ascii_digit o symbol) --
    95   Scan.one (Symbol.is_ascii_digit o symbol) --
    96   Scan.one (Symbol.is_ascii_digit o symbol) :|--
    97   (fn (((a, pos), (b, _)), (c, _)) =>
    98     let val (n, _) = Library.read_int [a, b, c]
    99     in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
   100 
   101 fun scan_str q err_prefix =
   102   $$$ "\\" |-- !!! (fn () => err_prefix ^ "bad escape character in string")
   103     ($$$ q || $$$ "\\" || char_code) ||
   104   Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
   105 
   106 fun scan_strs q err_prefix =
   107   (scan_pos --| $$$ q) -- !!! (fn () => err_prefix ^ "missing quote at end of string")
   108     (change_prompt ((Scan.repeat (scan_str q err_prefix) >> flat) -- ($$$ q |-- scan_pos)));
   109 
   110 fun recover_strs q =
   111   $$$ q @@@ (Scan.repeat (Scan.permissive (scan_str q "")) >> flat);
   112 
   113 in
   114 
   115 val scan_string_q = scan_strs "'";
   116 val scan_string_qq = scan_strs "\"";
   117 val scan_string_bq = scan_strs "`";
   118 
   119 val recover_string_q = recover_strs "'";
   120 val recover_string_qq = recover_strs "\"";
   121 val recover_string_bq = recover_strs "`";
   122 
   123 end;
   124 
   125 
   126 (* quote string literals *)
   127 
   128 local
   129 
   130 fun char_code i =
   131   (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
   132 
   133 fun quote_str q s =
   134   if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
   135   else if s = q orelse s = "\\" then "\\" ^ s
   136   else s;
   137 
   138 fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
   139 
   140 in
   141 
   142 val quote_string_q = quote_string "'";
   143 val quote_string_qq = quote_string "\"";
   144 val quote_string_bq = quote_string "`";
   145 
   146 end;
   147 
   148 
   149 (* ML-style comments *)
   150 
   151 local
   152 
   153 val scan_cmt =
   154   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
   155   Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
   156   Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
   157   Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
   158 
   159 val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat);
   160 
   161 val scan_body = change_prompt scan_cmts;
   162 
   163 in
   164 
   165 fun scan_comment cut =
   166   $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
   167 
   168 fun scan_comment_body cut =
   169   $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
   170 
   171 val recover_comment =
   172   $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
   173 
   174 end;
   175 
   176 
   177 (* source *)
   178 
   179 fun source pos =
   180   Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
   181     Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
   182 
   183 
   184 (* compact representation -- with Symbol.DEL padding *)
   185 
   186 type text = string;
   187 
   188 fun pad [] = []
   189   | pad [(s, _)] = [s]
   190   | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
   191       let
   192         val end_pos1 = Position.advance s1 pos1;
   193         val d = Int.max (0, Position.distance_of end_pos1 pos2);
   194       in s1 :: replicate d Symbol.DEL @ pad rest end;
   195 
   196 val implode = implode o pad;
   197 
   198 fun range (syms as (_, pos) :: _) =
   199       let val pos' = List.last syms |-> Position.advance
   200       in Position.range pos pos' end
   201   | range [] = Position.no_range;
   202 
   203 fun implode_range pos1 pos2 syms =
   204   let val syms' = (("", pos1) :: syms @ [("", pos2)])
   205   in (implode syms', range syms') end;
   206 
   207 fun explode (str, pos) =
   208   let
   209     val (res, _) =
   210       fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
   211         (Symbol.explode str) ([], Position.reset_range pos);
   212   in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
   213 
   214 
   215 (* identifiers *)
   216 
   217 local
   218 
   219 val latin = Symbol.is_ascii_letter;
   220 val digit = Symbol.is_ascii_digit;
   221 fun underscore s = s = "_";
   222 fun prime s = s = "'";
   223 fun script s = s = "\\<^sub>" orelse s = "\\<^isub>";
   224 fun special_letter s = Symbol.is_letter_symbol s andalso not (script s);
   225 
   226 val scan_plain = Scan.one ((latin orf digit orf prime) o symbol) >> single;
   227 val scan_digit = Scan.one (digit o symbol) >> single;
   228 val scan_prime = Scan.one (prime o symbol) >> single;
   229 
   230 val scan_script =
   231   Scan.one (script o symbol) -- Scan.one ((latin orf digit orf prime orf special_letter) o symbol)
   232   >> (fn (x, y) => [x, y]);
   233 
   234 val scan_ident_part1 =
   235   Scan.one (latin o symbol) ::: (Scan.repeat (scan_plain || scan_script) >> flat) ||
   236   Scan.one (special_letter o symbol) :::
   237     (Scan.repeat (scan_digit || scan_prime || scan_script) >> flat);
   238 
   239 val scan_ident_part2 =
   240   Scan.repeat1 (scan_plain || scan_script) >> flat ||
   241   scan_ident_part1;
   242 
   243 in
   244 
   245 val scan_new_ident =
   246   scan_ident_part1 @@@
   247     (Scan.repeat (Scan.many1 (underscore o symbol) @@@ scan_ident_part2) >> flat);
   248 
   249 end;
   250 
   251 val scan_ident =
   252   Scan.one (Symbol.is_letter o symbol) ::: Scan.many (Symbol.is_letdig o symbol);
   253 
   254 fun is_identifier s =
   255   Symbol.is_ascii_identifier s orelse
   256     (case try (Scan.finite stopper scan_ident) (explode (s, Position.none)) of
   257       SOME (_, []) => true
   258     | _ => false);
   259 
   260 end;
   261 
   262 structure Basic_Symbol_Pos =   (*not open by default*)
   263 struct
   264   val $$$ = Symbol_Pos.$$$;
   265   val ~$$$ = Symbol_Pos.~$$$;
   266 end;
   267