From 5e0dc6b60a4991d7574e611bcd13a0511482673a Mon Sep 17 00:00:00 2001 From: favonia Date: Sun, 27 Oct 2024 16:39:49 -0500 Subject: [PATCH] feat(SourceMarker): display EOL and EOF points as "EOL" and "EOF" --- src/MarkedSource.ml | 9 ++++++++- src/MarkedSourceData.ml | 7 ++++++- src/SourceMarker.ml | 13 +++++++++++-- src/tty/Tty.ml | 10 +++++++--- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/MarkedSource.ml b/src/MarkedSource.ml index d52d4fa..044e5f4 100644 --- a/src/MarkedSource.ml +++ b/src/MarkedSource.ml @@ -6,10 +6,17 @@ let dump_marker dump_tag fmt = | RangeEnd tag -> Format.fprintf fmt {|@[<2>RangeEnd@ @[%a@]@]|} dump_tag tag | Point tag -> Format.fprintf fmt {|@[<2>Point@ @[%a@]@]|} dump_tag tag +let dump_special_position fmt = + function + | End_of_line -> Format.fprintf fmt {|End_of_line|} + | End_of_file -> Format.fprintf fmt {|End_of_file|} + let dump_token dump_tag fmt = function | String str -> Format.fprintf fmt {|@[<2>String@ "%s"@]|} (String.escaped str) - | Marker m -> Format.fprintf fmt {|@[<2>Marker@ @[<1>(%a)@]@]|} (dump_marker dump_tag) m + | Marker (p, m) -> + Format.fprintf fmt {|@[<2>Marker@ @[<1>(@[%a@],@ @[%a@])@]@]|} + (Utils.dump_option dump_special_position) p (dump_marker dump_tag) m let dump_line dump_tag fmt {markers; tokens} = Format.fprintf fmt {|@[<1>{@[<2>markers=@,@[%a@]@];@ @[<2>tokens=@ @[%a@]@]}@]|} diff --git a/src/MarkedSourceData.ml b/src/MarkedSourceData.ml index 98f47e6..a807f66 100644 --- a/src/MarkedSourceData.ml +++ b/src/MarkedSourceData.ml @@ -1,3 +1,8 @@ +(** Special positions. *) +type special_position = + | End_of_line + | End_of_file + (** A marker is a delimiter of a range or a specific point. *) type 'tag marker = | RangeBegin of 'tag @@ -7,7 +12,7 @@ type 'tag marker = (** A token is either a string or a marker. *) type 'tag token = | String of string - | Marker of 'tag marker + | Marker of special_position option * 'tag marker (** A line is a list of {!type:segment}s along with tags. *) type 'tag line = diff --git a/src/SourceMarker.ml b/src/SourceMarker.ml index 7ea8254..f8e75fc 100644 --- a/src/SourceMarker.ml +++ b/src/SourceMarker.ml @@ -83,11 +83,20 @@ module Make (Tag : Tag) = struct | (loc, marker) :: markers when state.cursor.line_num = loc.line_num (* on the same line *) -> if loc.offset > eof then invalid_arg "Asai.SourceMarker.mark: position beyond EOF; use the debug mode"; if loc.offset > state.eol then invalid_arg "Asai.SourceMarker.mark: unexpected newline; use the debug mode"; + let special_position = + if loc.offset = state.eol then + if loc.offset = eof then + Some End_of_file + else + Some End_of_line + else + None + in let tokens = if loc.offset = state.cursor.offset then - state.tokens <: Marker marker + state.tokens <: Marker (special_position, marker) else - state.tokens <: String (read_between ~source state.cursor.offset loc.offset) <: Marker marker + state.tokens <: String (read_between ~source state.cursor.offset loc.offset) <: Marker (special_position, marker) in go { state with tokens; cursor = loc } markers | markers -> diff --git a/src/tty/Tty.ml b/src/tty/Tty.ml index 614e706..b3b3b2c 100644 --- a/src/tty/Tty.ml +++ b/src/tty/Tty.ml @@ -111,11 +111,15 @@ struct function | MarkedSource.String s -> render_styled_segment ~param fmt (TtyTagSet.prioritized set) s; set - | MarkedSource.Marker RangeEnd t -> + | MarkedSource.Marker (_, RangeEnd t) -> TtyTagSet.remove t set - | MarkedSource.Marker Point t -> + | MarkedSource.Marker (Some End_of_file, Point t) -> + render_styled_segment ~param fmt (Some t) "‹EOF›"; set + | MarkedSource.Marker (Some End_of_line, Point t) -> + render_styled_segment ~param fmt (Some t) "‹EOL›"; set + | MarkedSource.Marker (None, Point t) -> render_styled_segment ~param fmt (Some t) "‹POS›"; set - | MarkedSource.Marker RangeBegin t -> + | MarkedSource.Marker (_, RangeBegin t) -> TtyTagSet.add t set in Format.fprintf fmt (" " ^^ highlight "%*d |" ^^ " ")