defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
1 (* Title: Pure/General/symbol_pos.ML
4 Symbols with explicit position information.
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
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: 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 quote_string_q: string -> string
23 val quote_string_qq: string -> string
24 val quote_string_bq: string -> string
25 val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
26 T list -> T list * T list
27 val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) ->
28 T list -> T list * T list
29 val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
30 (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
32 val implode: T list -> text
33 val range: T list -> Position.range
34 val implode_range: Position.T -> Position.T -> T list -> text * Position.range
35 val explode: text * Position.T -> T list
38 structure Symbol_Pos: SYMBOL_POS =
43 type T = Symbol.symbol * Position.T;
45 fun symbol ((s, _): T) = s;
47 val content = implode o map symbol;
52 fun mk_eof pos = (Symbol.eof, pos);
53 val eof = mk_eof Position.none;
55 val is_eof = Symbol.is_eof o symbol;
58 Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
65 fun get_pos [] = " (past end-of-text!)"
66 | get_pos ((_, pos) :: _) = Position.str_of pos;
68 fun err (syms, msg) = fn () =>
69 text () ^ get_pos syms ^ " at " ^ Symbol.beginning 10 (map symbol syms) ^
70 (case msg of NONE => "" | SOME m => "\n" ^ m ());
71 in Scan.!! err scan end;
73 fun change_prompt scan = Scan.prompt "# " scan;
75 fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
76 fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
78 val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
81 (* scan string literals *)
86 Scan.one (Symbol.is_ascii_digit o symbol) --
87 Scan.one (Symbol.is_ascii_digit o symbol) --
88 Scan.one (Symbol.is_ascii_digit o symbol) :|--
89 (fn (((a, pos), (b, _)), (c, _)) =>
90 let val (n, _) = Library.read_int [a, b, c]
91 in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
94 $$$ "\\" |-- !!! (fn () => "bad escape character in string") ($$$ q || $$$ "\\" || char_code) ||
95 Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single;
98 (scan_pos --| $$$ q) -- !!! (fn () => "missing quote at end of string")
99 (change_prompt ((Scan.repeat (scan_str q) >> flat) -- ($$$ q |-- scan_pos)));
103 val scan_string_q = scan_strs "'";
104 val scan_string_qq = scan_strs "\"";
105 val scan_string_bq = scan_strs "`";
110 (* quote string literals *)
115 (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
118 if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
119 else if s = q orelse s = "\\" then "\\" ^ s
122 fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
126 val quote_string_q = quote_string "'";
127 val quote_string_qq = quote_string "\"";
128 val quote_string_bq = quote_string "`";
133 (* ML-style comments *)
138 Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
139 Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
140 Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
141 Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single;
143 val scan_body = change_prompt (Scan.pass 0 (Scan.repeat scan_cmt >> flat));
147 fun scan_comment cut =
148 $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
150 fun scan_comment_body cut =
151 $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
159 Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
160 Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE;
163 (* compact representation -- with Symbol.DEL padding *)
169 | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
171 val end_pos1 = Position.advance s1 pos1;
172 val d = Int.max (0, Position.distance_of end_pos1 pos2);
173 in s1 :: replicate d Symbol.DEL @ pad rest end;
175 val implode = implode o pad;
177 fun range (syms as (_, pos) :: _) =
178 let val pos' = List.last syms |-> Position.advance
179 in Position.range pos pos' end
180 | range [] = Position.no_range;
182 fun implode_range pos1 pos2 syms =
183 let val syms' = (("", pos1) :: syms @ [("", pos2)])
184 in (implode syms', range syms') end;
186 fun explode (str, pos) =
189 fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
190 (Symbol.explode str) ([], Position.reset_range pos);
191 in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
195 structure Basic_Symbol_Pos = (*not open by default*)
197 val $$$ = Symbol_Pos.$$$;
198 val ~$$$ = Symbol_Pos.~$$$;