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 yypos: t -> int
val make: {column: int, file: string, line: int, yypos: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, yypos:int}
local
fun f g (T r) = g r
in
val column = f #column
val line = f #line
val yypos = f #yypos
end
fun compare (T {column = c, file = f, line = l, ...},
T {column = c', file = f', line = l', ...}) =
prod_ord string_ord (prod_ord int_ord int_ord) ((f, (l, c)), (f', (l', c')))
val equals = is_equal o compare
fun make {column, file, line, yypos} =
T {column = column,
file = file,
line = line,
yypos = yypos}
fun file (T {file, ...}) = file
val bogus = T {column = ~1,
file = "<bogus>",
line = ~1, yypos = ~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)) ()