Skip to content

Commit 1fce0ea

Browse files
ejgallegoAlidra
andcommitted
[pure] [lsp] Try to recover unit testing of pure interface
PR #289 silently removed important unit tests in the Pure module. We fix this, in particular: - we add parsing offsets to LP's position type (this is very convenient for certain operations. - we add a new parsing API in pure to allow parsing resumption - we add new tests, including parsing resumption. - we fix `Command.equal` as to have equality functions ignore positions. Co-authored-by: Alidra <40537601+Alidra@users.noreply.github.com>
1 parent b5faa85 commit 1fce0ea

File tree

12 files changed

+355
-14
lines changed

12 files changed

+355
-14
lines changed

lambdapi.opam

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ depends: [
3838
"odoc" {with-doc}
3939
"lwt_ppx" {>= "1"}
4040
"uri" {>= "1.1"}
41+
# tests
42+
"ppx_inline_test" { with-test }
4143
]
4244
build: [
4345
["dune" "subst"] {dev}

src/common/pos.ml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ open Lplib open Base
55
(** Type of a position, corresponding to a continuous range of characters in a
66
(utf8-encoded) source. *)
77
type pos =
8-
{ fname : string option (** File name for the position. *)
9-
; start_line : int (** Line number of the starting point. *)
10-
; start_col : int (** Column number (utf8) of the starting point. *)
11-
; end_line : int (** Line number of the ending point. *)
12-
; end_col : int (** Column number (utf8) of the ending point. *) }
8+
{ fname : string option (** File name for the position. *)
9+
; start_line : int (** Line number of the starting point. *)
10+
; start_col : int (** Column number (utf8) of the starting point. *)
11+
; start_offset : int (** Number of chars since the beginning of the document for the starting point *)
12+
; end_line : int (** Line number of the ending point. *)
13+
; end_col : int (** Column number (utf8) of the ending point. *)
14+
; end_offset : int (** Number of chars since the beginning of the document for the ending point *)
15+
}
1316

1417
(** Total comparison function on pos. *)
1518
let cmp : pos cmp = fun p1 p2 ->
@@ -62,8 +65,11 @@ let cat : pos -> pos -> pos = fun p1 p2 ->
6265
if p1.fname <> p2.fname then invalid_arg __LOC__ else p1.fname*)
6366
; start_line = p1.start_line
6467
; start_col = p1.start_col
68+
; start_offset = p1.start_offset
6569
; end_line = p2.end_line
66-
; end_col = p2.end_col }
70+
; end_col = p2.end_col
71+
; end_offset = p2.end_offset
72+
}
6773

6874
let cat : popt -> popt -> popt = fun p1 p2 ->
6975
match p1, p2 with
@@ -131,9 +137,11 @@ let locate : ?fname:string -> Lexing.position * Lexing.position -> pos =
131137
let fname = if p1.pos_fname = "" then fname else Some(p1.pos_fname) in
132138
let start_line = p1.pos_lnum in
133139
let start_col = p1.pos_cnum - p1.pos_bol in
140+
let start_offset = p1.pos_cnum in
134141
let end_line = p2.pos_lnum in
135142
let end_col = p2.pos_cnum - p2.pos_bol in
136-
{fname; start_line; start_col; end_line; end_col}
143+
let end_offset = p2.pos_cnum in
144+
{fname; start_line; start_col; start_offset; end_line; end_col; end_offset }
137145

138146
(** [make_pos lps elt] creates a located element from the lexing positions
139147
[lps] and the element [elt]. *)

src/export/coq.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ let set_encoding : string -> unit = fun f ->
132132
(fun i b ->
133133
if not b then
134134
let pos =
135-
Some {fname=Some f;start_line=0;start_col=0;end_line=0;end_col=0}
135+
Some {fname=Some f;start_line=0;start_col=0;start_offset=0;end_line=0;end_col=0;end_offset=0}
136136
in fatal pos "Builtin %s undefined." (name_of_index i))
137137
found
138138

src/lsp/lp_doc.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ let new_doc ~uri ~version ~text =
138138
fname = Some(uri);
139139
start_line = 0;
140140
start_col = 0;
141+
start_offset = 0;
141142
end_line = 0;
142-
end_col = 0
143+
end_col = 0;
144+
end_offset = 0
143145
} in
144146
(None, [(1, msg), Some(loc)])
145147
in
@@ -161,8 +163,10 @@ let dummy_loc =
161163
Pos.{ fname = None
162164
; start_line = 1
163165
; start_col = 1
166+
; start_offset = -1
164167
; end_line = 2
165168
; end_col = 2
169+
; end_offset = -1
166170
}
167171

168172
let check_text ~doc =

src/lsp/lp_lsp.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ let do_check_text ofmt ~doc =
6464
fname = Some(doc.uri);
6565
start_line = 0;
6666
start_col = 0;
67+
start_offset = 0;
6768
end_line = 0;
68-
end_col = 0
69+
end_col = 0;
70+
end_offset = 0;
6971
} in
7072
(doc, Lp_doc.mk_error ~doc loc msg)
7173
in

src/parsing/parser.ml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ let parser_fatal : Pos.pos -> ('a,'b) koutfmt -> 'a = fun pos fmt ->
1515

1616
(** Module type of a parser. *)
1717
module type PARSER = sig
18+
type lexbuf
19+
1820
val parse : in_channel -> Syntax.ast
1921
(** [parse inchan] returns a stream of commands parsed from
2022
channel [inchan]. Commands are parsed lazily and the channel is
@@ -27,11 +29,16 @@ module type PARSER = sig
2729
val parse_string : string -> string -> Syntax.ast
2830
(** [parse_string f s] returns a stream of parsed commands from string [s]
2931
which comes from file [f] ([f] can be anything). *)
32+
33+
val parse_from_lexbuf : lexbuf -> Syntax.ast
34+
(** [parse_from_lexbuf lexbuf] is the same than [parse_string] but with an
35+
already created lexbuf *)
36+
3037
end
3138

3239
module Lp :
3340
sig
34-
include PARSER
41+
include PARSER with type lexbuf := Sedlexing.lexbuf
3542

3643
val parse_term : in_channel -> Syntax.p_term Stream.t
3744
(** [parse inchan] returns a stream of terms parsed from
@@ -57,6 +64,10 @@ sig
5764
string [s] which comes from file [f] ([f] can be anything). *)
5865

5966
val parse_qid : string -> Core.Term.qident
67+
68+
val parse_from_lexbuf : Sedlexing.lexbuf -> Syntax.ast
69+
(** [parse_from_lexbuf lexbuf] Same than [parse_string] but with an already created lexbuf *)
70+
6071
end
6172
= struct
6273

@@ -97,6 +108,9 @@ sig
97108
let parse_string ~grammar_entry fname s =
98109
stream_of_lexbuf ~grammar_entry ~fname (Sedlexing.Utf8.from_string s)
99110

111+
let parse_from_lexbuf ~grammar_entry lexbuf =
112+
stream_of_lexbuf ~grammar_entry ?fname:None lexbuf
113+
100114
let parse_term = parse ~grammar_entry:LpParser.term_alone
101115
let parse_term_string = parse_string ~grammar_entry:LpParser.term_alone
102116
let parse_rwpatt_string =
@@ -113,10 +127,12 @@ sig
113127
let parse_string = parse_string ~grammar_entry:LpParser.command
114128
let parse_file = parse_file ~grammar_entry:LpParser.command
115129

130+
let parse_from_lexbuf = parse_from_lexbuf ~grammar_entry:LpParser.command
131+
116132
end
117133

118134
(** Parsing dk syntax. *)
119-
module Dk : PARSER = struct
135+
module Dk : PARSER with type lexbuf := Lexing.lexbuf = struct
120136

121137
let token : Lexing.lexbuf -> DkTokens.token =
122138
let r = ref DkTokens.EOF in fun lb ->
@@ -156,6 +172,8 @@ module Dk : PARSER = struct
156172
stream_of_lexbuf ~inchan ~fname (Lexing.from_channel inchan)
157173

158174
let parse_string fname s = stream_of_lexbuf ~fname (Lexing.from_string s)
175+
176+
let parse_from_lexbuf lexbuf = stream_of_lexbuf ?fname:None lexbuf
159177
end
160178

161179
(* Include parser of new syntax so that functions are directly available.*)

src/parsing/syntax.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ type p_modifier_aux =
296296

297297
type p_modifier = p_modifier_aux loc
298298

299+
(* ppx ? *)
300+
let eq_p_modifier { elt = m1; _} {elt = m2; _} =
301+
m1 = m2
302+
299303
let is_prop {elt; _} = match elt with P_prop _ -> true | _ -> false
300304
let is_opaq {elt; _} = match elt with P_opaq -> true | _ -> false
301305
let is_expo {elt; _} = match elt with P_expo _ -> true | _ -> false
@@ -462,7 +466,7 @@ let eq_p_symbol : p_symbol eq = fun
462466
{ p_sym_mod=p_sym_mod2; p_sym_nam=p_sym_nam2; p_sym_arg=p_sym_arg2;
463467
p_sym_typ=p_sym_typ2; p_sym_trm=p_sym_trm2; p_sym_prf=p_sym_prf2;
464468
p_sym_def=p_sym_def2} ->
465-
p_sym_mod1 = p_sym_mod2
469+
List.eq eq_p_modifier p_sym_mod1 p_sym_mod2
466470
&& eq_p_ident p_sym_nam1 p_sym_nam2
467471
&& List.eq eq_p_params p_sym_arg1 p_sym_arg2
468472
&& Option.eq eq_p_term p_sym_typ1 p_sym_typ2

src/pure/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
(name pure)
33
(public_name lambdapi.pure)
44
(modules :standard)
5+
(inline_tests (deps tests/foo.lp tests/lambdapi.pkg))
6+
(preprocess (pps ppx_inline_test))
57
(libraries camlp-streams lambdapi.handle lambdapi.core)
68
(flags -w +3))

0 commit comments

Comments
 (0)