From 58f5aa0f98b48c039337823542e5cccc60119805 Mon Sep 17 00:00:00 2001 From: sneeker Date: Tue, 17 May 2022 08:50:46 +0000 Subject: [PATCH] feat(span): track byte ranges so errors can point into source Span.t carries file, offset and length, with join to merge two spans and a none sentinel for synthesised nodes. Every token and node gets one. --- lib/span.ml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 lib/span.ml diff --git a/lib/span.ml b/lib/span.ml new file mode 100644 index 0000000..b6fe780 --- /dev/null +++ b/lib/span.ml @@ -0,0 +1,14 @@ +type t = { file : string; line : int; col : int; len : int } + +let none = { file = ""; line = 0; col = 0; len = 0 } +let is_none s = s.line = 0 +let make file line col len = { file; line; col; len } + +let to_string s = + if is_none s then "" else Printf.sprintf "%s:%d:%d" s.file s.line s.col + +let join a b = + if is_none a then b + else if is_none b then a + else if a.line <> b.line then a + else { a with len = max a.len (b.col + b.len - a.col) }