src/Pure/General/symbol_pos.ML
author wenzelm
Sat, 30 Apr 2011 18:16:40 +0200
changeset 43374 27514b6fbe93
parent 41666 a2208d3e2bd6
child 44580 717e96cf9527
permissions -rw-r--r--
more uniform variations of scan_string;
     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 untabify_content: T list -> string
    15   val is_eof: T -> bool
    16   val stopper: T Scan.stopper
    17   val !!! : string -> (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: T list -> (Position.T * (T list * Position.T)) * T list
    21   val scan_string_qq: T list -> (Position.T * (T list * Position.T)) * T list
    22   val scan_string_bq: T list -> (Position.T * (T list * Position.T)) * T list
    23   val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    24     T list -> T list * T list
    25   val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
    26     T list -> T list * T list
    27   val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
    28     (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
    29   type text = string
    30   val implode: T list -> text
    31   val range: T list -> Position.range
    32   val implode_range: Position.T -> Position.T -> T list -> text * Position.range
    33   val explode: text * Position.T -> T list
    34 end;
    35 
    36 structure Symbol_Pos: SYMBOL_POS =
    37 struct
    38 
    39 (* type T *)
    40 
    41 type T = Symbol.symbol * Position.T;
    42 
    43 fun symbol ((s, _): T) = s;
    44 
    45 
    46 (* content *)
    47 
    48 val content = implode o map symbol;
    49 
    50 
    51 val tab_width = (8: int);
    52 
    53 fun untabify ("\t", pos) =
    54       (case Position.column_of pos of
    55         SOME n => Symbol.spaces (tab_width - ((n - 1) mod tab_width))
    56       | NONE => Symbol.space)
    57   | untabify (s, _) = s;
    58 
    59 val untabify_content = implode o map untabify;
    60 
    61 
    62 (* stopper *)
    63 
    64 fun mk_eof pos = (Symbol.eof, pos);
    65 val eof = mk_eof Position.none;
    66 
    67 val is_eof = Symbol.is_eof o symbol;
    68 
    69 val stopper =
    70   Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
    71 
    72 
    73 (* basic scanners *)
    74 
    75 fun !!! text scan =
    76   let
    77     fun get_pos [] = " (past end-of-text!)"
    78       | get_pos ((_, pos) :: _) = Position.str_of pos;
    79 
    80     fun err (syms, msg) =
    81       text ^ get_pos syms ^ " at " ^ Symbol.beginning 10 (map symbol syms) ^
    82       (case msg of NONE => "" | SOME s => "\n" ^ s);
    83   in Scan.!! err scan end;
    84 
    85 fun change_prompt scan = Scan.prompt "# " scan;
    86 
    87 fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
    88 fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
    89 
    90 val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
    91 
    92 
    93 (* Isabelle strings *)
    94 
    95 local
    96 
    97 val char_code =
    98   Scan.one (Symbol.is_ascii_digit o symbol) --
    99   Scan.one (Symbol.is_ascii_digit o symbol) --
   100   Scan.one (Symbol.is_ascii_digit o symbol) :|--
   101   (fn (((a, pos), (b, _)), (c, _)) =>
   102     let val (n, _) = Library.read_int [a, b, c]
   103     in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
   104 
   105 fun scan_str q =
   106   $$$ "\\" |-- !!! "bad escape character in string" ($$$ q || $$$ "\\" || char_code) ||
   107   Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
   108 
   109 fun scan_strs q =
   110   (scan_pos --| $$$ q) -- !!! "missing quote at end of string"
   111     (change_prompt ((Scan.repeat (scan_str q) >> flat) -- ($$$ q |-- scan_pos)));
   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 end;
   120 
   121 
   122 (* ML-style comments *)
   123 
   124 local
   125 
   126 val scan_cmt =
   127   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
   128   Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
   129   Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
   130   Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
   131 
   132 val scan_body = change_prompt (Scan.pass 0 (Scan.repeat scan_cmt >> flat));
   133 
   134 in
   135 
   136 fun scan_comment cut =
   137   $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
   138 
   139 fun scan_comment_body cut =
   140   $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
   141 
   142 end;
   143 
   144 
   145 (* source *)
   146 
   147 fun source pos =
   148   Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
   149     Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
   150 
   151 
   152 (* compact representation -- with Symbol.DEL padding *)
   153 
   154 type text = string;
   155 
   156 fun pad [] = []
   157   | pad [(s, _)] = [s]
   158   | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
   159       let
   160         val end_pos1 = Position.advance s1 pos1;
   161         val d = Int.max (0, Position.distance_of end_pos1 pos2);
   162       in s1 :: replicate d Symbol.DEL @ pad rest end;
   163 
   164 val implode = implode o pad;
   165 
   166 fun range (syms as (_, pos) :: _) =
   167       let val pos' = List.last syms |-> Position.advance
   168       in Position.range pos pos' end
   169   | range [] = Position.no_range;
   170 
   171 fun implode_range pos1 pos2 syms =
   172   let val syms' = (("", pos1) :: syms @ [("", pos2)])
   173   in (implode syms', range syms') end;
   174 
   175 fun explode (str, pos) =
   176   let
   177     val (res, _) =
   178       fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
   179         (Symbol.explode str) ([], Position.reset_range pos);
   180   in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
   181 
   182 end;
   183 
   184 structure Basic_Symbol_Pos =   (*not open by default*)
   185 struct
   186   val $$$ = Symbol_Pos.$$$;
   187   val ~$$$ = Symbol_Pos.~$$$;
   188 end;
   189