signature SYMBOL_POS = sig type T = Symbol.symbol * Position.T type'a scanner = T list -> 'a * T list val symbol: T -> Symbol.symbol val content: T list -> string val range: T list -> Position.range val is_eof: T -> bool val stopper: T Scan.stopper val !!! : Scan.message -> 'a scanner -> 'a scanner val $$ : Symbol.symbol -> T scanner val ~$$ : Symbol.symbol -> T scanner val $$$ : Symbol.symbol -> T list scanner val ~$$$ : Symbol.symbol -> T list scanner val scan_pos: Position.T scanner val scan_string_q: string -> (Position.T * (T list * Position.T)) scanner val scan_string_qq: string -> (Position.T * (T list * Position.T)) scanner val scan_string_bq: string -> (Position.T * (T list * Position.T)) scanner val recover_string_q: T list scanner val recover_string_qq: T list scanner val recover_string_bq: T list scanner val quote_string_q: string -> string val quote_string_qq: string -> string val quote_string_bq: string -> string val cartouche_content: T list -> T list val scan_cartouche: string -> T list scanner val scan_cartouche_content: string -> T list scanner val recover_cartouche: T list scanner val scan_comment: string -> T list scanner val scan_comment_body: string -> T list scanner val recover_comment: T list scanner val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
(T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source type text = string val implode: T list -> text val implode_range: Position.range -> T list -> text * Position.range val explode_deleted: string * Position.T -> Position.T list val explode: text * Position.T -> T list val explode0: string -> T list val scan_ident: T list scanner val is_identifier: string -> bool val scan_nat: T list scanner val scan_float: T list scanner end;
structure Symbol_Pos: SYMBOL_POS = struct
(* type T *)
type T = Symbol.symbol * Position.T; type'a scanner = T list -> 'a * T list;
fun symbol ((s, _): T) = s;
val content = implode o map symbol;
fun range (syms as (_, pos) :: _) = letval pos' = List.last syms |-> Position.symbol in Position.range (pos, pos') end
| range [] = Position.no_range;
(* stopper *)
fun mk_eof pos = (Symbol.eof, pos); val eof = mk_eof Position.none;
fun !!! text (scan: 'a scanner) = let fun get_pos [] = " (end-of-input)"
| get_pos ((_, pos) :: _) = Position.here pos;
fun err (syms, msg) = fn () =>
text () ^ get_pos syms ^
Markup.markup Markup.no_report (" at " ^ Symbol.beginning 10 (map symbol syms)) ^
(case msg of NONE => "" | SOME m => "\n" ^ m ()); in Scan.!! err scan end;
fun $$ s = Scan.one (fn x => symbol x = s); fun ~$$ s = Scan.one (fn x => symbol x <> s);
fun $$$ s = Scan.one (fn x => symbol x = s) >> single; fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
(* scan string literals *)
local
val char_code =
Scan.one (Symbol.is_ascii_digit o symbol) --
Scan.one (Symbol.is_ascii_digit o symbol) --
Scan.one (Symbol.is_ascii_digit o symbol) :|--
(fn (((a, pos), (b, _)), (c, _)) => letval (n, _) = Library.read_int [a, b, c] inif n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
fun scan_str q err_prefix =
$$$ "\\" |-- !!! (fn () => err_prefix ^ "bad escape character in string")
($$$ q || $$$ "\\" || char_code) ||
Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.not_eof s) >> single;
val recover_comment =
$$$ "(" @@@ $$$ "*" @@@ scan_cmts;
end;
(* source *)
fun source pos =
Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
Scan.one Symbol.not_eof >> (fn s => (Position.symbol s pos, (s, pos))))));
(* compact representation -- with Symbol.DEL padding *)
type text = string;
fun pad [] = []
| pad [(s, _)] = [s]
| pad ((s1, pos1) :: (rest as (_, pos2) :: _)) = let val end_pos1 = Position.symbol s1 pos1; val d = Int.max (0, the_default 0 (Position.distance_of (end_pos1, pos2))); in s1 :: replicate d Symbol.DEL @ pad rest end;
val implode = implode o pad;
fun implode_range (pos1, pos2) syms = letval syms' = (("", pos1) :: syms @ [("", pos2)]) in (implode syms', range syms') end;
local
fun rev_explode (str, pos) =
fold (fn s => fn (res, p) => ((s, p) :: res, Position.symbol s p))
(Symbol.explode str) ([], Position.no_range_position pos)
|> #1;
in
fun explode_deleted arg =
fold (fn (s, p) => s = Symbol.DEL ? cons p) (rev_explode arg) [];
fun explode arg =
fold (fn (s, p) => s <> Symbol.DEL ? cons (s, p)) (rev_explode arg) [];
fun explode0 str = explode (str, Position.none);
end;
(* identifiers *)
local
val letter = Scan.one (symbol #> Symbol.is_letter); val letdigs1 = Scan.many1 (symbol #> Symbol.is_letdig);
valsub = Scan.one (symbol #> (fn s => s = "\<^sub>"));
in
val scan_ident = letter ::: Scan.repeats (letdigs1 || sub ::: letdigs1);
end;
fun is_identifier s =
Symbol.is_ascii_identifier s orelse
(casetry (Scan.finite stopper scan_ident) (explode0 s) of
SOME (_, []) => true
| _ => false);
(* numerals *)
val scan_nat = Scan.many1 (Symbol.is_digit o symbol); val scan_float = scan_nat @@@ $$$ "." @@@ scan_nat;
end;
structure Basic_Symbol_Pos = (*not open by default*) struct val $$ = Symbol_Pos.$$; val ~$$ = Symbol_Pos.~$$; val $$$ = Symbol_Pos.$$$; val ~$$$ = Symbol_Pos.~$$$; end;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.