File ‹SourcePos.ML›
signature SOURCE_POS =
sig
type t
val bogus: t
val column: t -> int
val compare: t * t -> order
val equals: t * t -> bool
val is_bogus: t -> bool
val file: t -> string
val line: t -> int
val make: {column: int, file: string, line: int} -> t
val toString: t -> string
val posToString : t -> string
val show_c_parser_positions : string
end
structure SourcePos : SOURCE_POS =
struct
datatype t = T of {column: int, file: string, line: int}
local
fun f g (T r) = g r
in
val column = f #column
val line = f #line
end
fun compare (T {column = c, file = f, line = l},
T {column = c', file = f', line = l'}) =
case string_ord (f, f') of
EQUAL =>
(case int_ord (l, l') of
EQUAL => int_ord (c, c')
| r => r)
| r => r
fun equals (T r, T r') = r = r'
fun make {column, file, line} =
T {column = column,
file = file,
line = line}
fun file (T {file, ...}) = file
val bogus = T {column = ~1,
file = "<bogus>",
line = ~1}
fun is_bogus t = equals (t, bogus)
fun prettyPos (T {column, line, file}) =
Pretty.strs [Int.toString line ^ "." ^ Int.toString column, "in " ^ quote (file) ]
fun toString (p as T {column, line, file}) =
String.concat [file, " ", Int.toString line, ".", Int.toString column]
fun posToString (T {column,line,...}) =
String.concat [Int.toString line, ".", Int.toString column]
val show_c_parser_positions = "show_c_parser_positions"
val _ =
ML_system_pp (fn depth => fn pretty => fn (pos:t) =>
if print_mode_active show_c_parser_positions
then Pretty.to_ML (prettyPos pos)
else ML_Pretty.str "<pos>");
end
val _ = tracing ("position hidden: " ^ @{make_string} SourcePos.bogus)
val _ = Print_Mode.with_modes [SourcePos.show_c_parser_positions]
(fn _ => tracing ("position pretty: " ^ @{make_string} SourcePos.bogus)) ()