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