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