Simon Génier pushed to branch location-inside-source at Stefan / Typer
Commits:
4fb0ccd3 by Simon Génier at 2021-06-09T12:40:18-04:00
Shorten the names of a few methods on source objects.
These names were redundants. For instance in `current_offset`, the `current`
part is already hinted at by the fact that source objects are a kind of cursor.
Overall I think the new names a clearer since they are more concise.
- - - - -
aff91a51 by Simon Génier at 2021-09-28T17:16:11-04:00
Merge branch 'shorten-source-methods'
- - - - -
e8a59f86 by Simon Génier at 2021-09-28T17:25:19-04:00
Replace raw offsets with points which also track line and column.
- - - - -
0b3b0b5b by Simon Génier at 2021-09-28T17:28:19-04:00
Compute location inside the source from the given point.
- - - - -
3 changed files:
- src/lexer.ml
- src/prelexer.ml
- src/source.ml
Changes:
=====================================
src/lexer.ml
=====================================
@@ -37,35 +37,35 @@ let unescape str =
let lex_number
(source : #Source.t)
- (start_location : Source.Location.t)
- (start_offset : int)
+ (start_point : Source.Point.t)
+ (doc : string)
: sexp =
let rec lex_exponent () =
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9')
-> source#advance;
lex_exponent ()
| _
- -> let source = source#slice_from_offset start_offset in
- Float (start_location, float_of_string source)
+ -> let source, location = source#slice start_point doc in
+ Float (location, float_of_string source)
in
let lex_exponent_sign () =
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9' | '+' | '-')
-> source#advance;
lex_exponent ()
(* TODO: error if there are no signs or digits after the 'e'? *)
| _
- -> let source = source#slice_from_offset start_offset in
- Float (start_location, float_of_string source)
+ -> let source, location = source#slice start_point doc in
+ Float (location, float_of_string source)
in
let rec lex_fractional () =
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9')
-> source#advance;
lex_fractional ()
@@ -75,12 +75,12 @@ let lex_number
lex_exponent_sign ()
| _
- -> let source = source#slice_from_offset start_offset in
- Float (start_location, float_of_string source)
+ -> let source, location = source#slice start_point doc in
+ Float (location, float_of_string source)
in
let rec lex_integer () =
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9')
-> source#advance;
lex_integer ()
@@ -94,8 +94,8 @@ let lex_number
lex_exponent_sign ()
| _
- -> let source = source#slice_from_offset start_offset in
- Integer (start_location, Z.of_string source)
+ -> let source, location = source#slice start_point doc in
+ Integer (location, Z.of_string source)
in
lex_integer ()
@@ -105,41 +105,40 @@ let lex_symbol
(token_env : token_env)
(source : #Source.t)
(doc : string)
- (start_location : Source.Location.t)
- (start_offset : int)
+ (start_point : Source.Point.t)
: sexp =
- let mksym start_location start_offset escaped =
- if Int.equal start_offset source#current_offset
- then epsilon start_location
+ let mksym start_point escaped =
+ if Source.Point.equal start_point source#point
+ then epsilon (Source.Location.of_point source#file_name start_point doc)
else
- let raw_name = source#slice_from_offset start_offset in
+ let raw_name, location = source#slice start_point doc in
let name = if escaped then unescape raw_name else raw_name in
- hSymbol (start_location, name)
+ hSymbol (location, name)
in
- let rec loop start_location start_offset prec lf (escaped : bool) =
- match source#peek_char with
+ let rec loop start_point prec lf (escaped : bool) =
+ match source#peek with
| None
- -> lf (mksym start_location start_offset escaped)
+ -> lf (mksym start_point escaped)
| Some c
-> (match token_env.(Char.code c) with
| CKseparate
- -> lf (mksym start_location start_offset escaped)
+ -> lf (mksym start_point escaped)
| CKinner prec'
- -> let left = mksym start_location start_offset escaped in
- let op_location = source#current_location doc in
+ -> let left = mksym start_point escaped in
+ let op_location = source#location doc in
(* To be considered `inner`, an operator char needs to have
something on its LHS or its RHS, otherwise, treat it as a
normal char. `.` can be the normal function composition
operator as well. *)
- let lhs_p = start_offset != source#current_offset in
+ let lhs_p = not (Source.Point.equal start_point source#point) in
source#advance;
let rhs_p =
- (match source#peek_char with
+ (match source#peek with
| None -> false
| Some c' -> CKseparate != token_env.(Char.code c'))
in
@@ -151,13 +150,13 @@ let lex_symbol
then fun s -> lf (Node (op, [left; s]))
else fun s -> Node (op, [lf left; s])
in
- loop (source#current_location doc) source#current_offset prec' lf' false
+ loop source#point prec' lf' false
else
- loop start_location start_offset prec lf escaped
+ loop start_point prec lf escaped
| CKnormal
-> (source#advance;
- let is_last = Option.is_none source#peek_char in
+ let is_last = Option.is_none source#peek in
match c with
(* Skip next char, in case it's a special token. For utf-8,
simply advancing is risky but actually works: '\' counts as 1
@@ -165,11 +164,11 @@ let lex_symbol
leading byte, so it has to count as 1 as well ;-) *)
| '\\' when not is_last
-> source#advance;
- loop start_location start_offset prec lf true
+ loop start_point prec lf true
- | _ -> loop start_location start_offset prec lf escaped))
+ | _ -> loop start_point prec lf escaped))
in
- loop start_location start_offset 0 (fun s -> s) false
+ loop start_point 0 (fun s -> s) false
let split_presymbol
(token_env : token_env)
@@ -178,33 +177,32 @@ let split_presymbol
: sexp list =
let rec loop (acc : sexp list) : sexp list =
- match source#peek_char with
+ match source#peek with
| None -> List.rev acc
| Some (c)
- -> let start_offset = source#current_offset in
- let start_location = source#current_location doc in
+ -> let start_point = source#point in
let token =
match c with
(* A negative number literal, or a symbol starting with '-'. *)
| '-'
-> (source#advance;
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9')
- -> lex_number source start_location start_offset
+ -> lex_number source start_point doc
| _
- -> lex_symbol token_env source doc start_location start_offset)
+ -> lex_symbol token_env source doc start_point)
| '0' .. '9'
-> (source#advance;
- lex_number source start_location start_offset)
+ lex_number source start_point doc)
| _ when token_env.(Char.code c) = CKseparate
-> source#advance;
- let name = source#slice_from_offset start_offset in
- hSymbol (start_location, name)
+ let name, location = source#slice start_point doc in
+ hSymbol (location, name)
| _
- -> lex_symbol token_env source doc start_location start_offset
+ -> lex_symbol token_env source doc start_point
in
loop (token :: acc)
in
=====================================
src/prelexer.ml
=====================================
@@ -74,7 +74,7 @@ let prelex (source : #Source.t) : pretoken list =
(doc : string)
: pretoken list =
- match source#peek_char with
+ match source#peek with
| None
-> (match ctx with
| [] -> List.rev acc
@@ -94,10 +94,10 @@ let prelex (source : #Source.t) : pretoken list =
(* line's bounds seems ok: String.sub line 1 0 == "" *)
| Some ('@')
-> source#advance;
- let start_offset = source#current_offset in
+ let start_point = source#point in
consume_until_newline source;
- let new_doc = String.trim (source#slice_from_offset start_offset) in
- loop ctx acc (doc ^ "\n" ^ new_doc)
+ let new_doc, _ = source#slice start_point "" in
+ loop ctx acc (doc ^ "\n" ^ String.trim new_doc)
(* A string. *)
| Some ('"')
@@ -105,26 +105,26 @@ let prelex (source : #Source.t) : pretoken list =
let rec prestring chars =
match source#next_char with
| None | Some ('\n')
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
prelexer_error location "Unterminated string";
loop ctx (Prestring (location, "") :: acc) ""
| Some ('"')
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
let pretoken = Prestring (location, string_implode (List.rev chars)) in
loop ctx (pretoken :: acc) ""
| Some ('\\')
-> (match source#next_char with
| None | Some ('\n')
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
prelexer_error location "Unterminated escape sequence";
loop ctx (Prestring (location, "") :: acc) ""
| Some ('t') -> prestring ('\t' :: chars)
| Some ('n') -> prestring ('\n' :: chars)
| Some ('r') -> prestring ('\r' :: chars)
| Some ('u')
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
prelexer_error location "Unimplemented unicode escape";
prestring chars
| Some (c) -> prestring (c :: chars))
@@ -134,12 +134,12 @@ let prelex (source : #Source.t) : pretoken list =
| Some ('{')
-> source#advance;
- let ctx' = (source#current_location doc, acc) :: ctx in
+ let ctx' = (source#location doc, acc) :: ctx in
loop ctx' [] doc
| Some ('}')
-> source#advance;
- let location = source#current_location doc in
+ let location = source#location doc in
(match ctx with
| ((slocation, sacc) :: ctx)
-> let preblock = Preblock (slocation, List.rev acc, location) in
@@ -150,18 +150,16 @@ let prelex (source : #Source.t) : pretoken list =
(* A pretoken. *)
| Some _
- -> let start_offset = source#current_offset in
+ -> let start_point = source#point in
source#advance;
let rec pretok () =
- match source#peek_char with
+ match source#peek with
| None | Some ('%' | '"' | '{' | '}')
- -> let location = source#current_location doc in
- let text = source#slice_from_offset start_offset in
+ -> let text, location = source#slice start_point doc in
loop ctx (Pretoken (location, text) :: acc) ""
| Some (c) when c <= ' '
- -> let location = source#current_location doc in
- let text = source#slice_from_offset start_offset in
+ -> let text, location = source#slice start_point doc in
source#advance;
loop ctx (Pretoken (location, text) :: acc) ""
@@ -170,10 +168,12 @@ let prelex (source : #Source.t) : pretoken list =
(match source#next_char with
| Some _ -> pretok ()
| None
- -> let location = source#current_location doc in
+ -> let error_location = source#location doc in
source#advance;
- let text = source#slice_from_offset start_offset in
- prelexer_error location ("Unterminated escape sequence in: " ^ text);
+ let text, location = source#slice start_point doc in
+ prelexer_error
+ error_location
+ ("Unterminated escape sequence in: " ^ text);
loop ctx (Pretoken (location, text) :: acc) "")
| Some _
=====================================
src/source.ml
=====================================
@@ -18,11 +18,29 @@
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>. *)
+module Point = struct
+ (* offset * line * column *)
+ type t = int * int * int
+
+ let equal (l, _, _ : t) (r, _, _ : t) : bool =
+ (* The line and column are metadata, only the offset is necessary for
+ determining equality. *)
+ Int.equal l r
+end
+
module Location = struct
type t = {file : string; line : int; column : int; docstr : string}
let dummy = {file = ""; line = 0; column = 0; docstr = ""}
+ let of_point
+ (file : string)
+ (_, line, column : Point.t)
+ (docstr : string)
+ : t =
+
+ {file; line; column; docstr}
+
let to_string ({file; line; column; _} : t) : string =
Printf.sprintf "%s:%d:%d" file line column
@@ -43,37 +61,43 @@ let first_column_of_line = 1
(* A source object is text paired with a cursor. The text can be lazily loaded
as it is accessed byte by byte, but it must be retained for future reference
by error messages. *)
-class virtual t (base_line : int) (base_column : int) (file_name : string) =
+class virtual t (base_line : int) (base_column : int) (file : string) =
object (self)
val mutable line = base_line
val mutable column = base_column
(* A path if the text comes from a file, otherwise a meaningful identifier. *)
- method file_name : string = file_name
+ method file : string = file
(* Return the byte at the cursor, or None if the cursor is at the end of
the text. *)
- method virtual peek_char : char option
+ method virtual peek : char option
(* The current location of the cursor. *)
- method current_location (docstr : string) : Location.t =
- {file = file_name; line; column; docstr}
+ method location (docstr : string) : Location.t =
+ {file; line; column; docstr}
+
+ method point : Point.t =
+ (self#offset, line, column)
(* The current offset of the cursor in the file, in bytes. *)
- method virtual current_offset : int
+ method virtual private offset : int
- (* Slices the text, from (and including) a starting offset and to (and
+ (* Slices the text, from (and including) a starting point and to (and
excluding) the curent cursor offset.
Note that the source is required only to buffer the last line read and may
raise an Invalid_argument if the slice extends before the start of the
line. *)
- method virtual slice_from_offset : int -> string
+ method slice (offset, line, column : Point.t) (docstr : string) : string * Location.t =
+ (self#slice_impl offset, {file = file_name; line; column; docstr})
+
+ method virtual private slice_impl : int -> string
(* Moves the cursor forward one byte *)
method advance : unit =
let is_utf8_head c = Char.code c < 128 || Char.code c >= 192 in
- let c = self#peek_char in
+ let c = self#peek in
self#advance_impl;
match c with
| Some '\n'
@@ -87,7 +111,7 @@ object (self)
(* Returns the char at the cursor, then advances it forward. *)
method next_char : char option =
- let c = self#peek_char in
+ let c = self#peek in
self#advance;
c
end
@@ -103,29 +127,29 @@ class source_file file_name = object (self)
val mutable line_offset = 0
val mutable offset = 0
- method private peek_char_unchecked =
+ method private peek_unchecked =
if offset < String.length source_line
then source_line.[offset]
else '\n'
- method peek_char =
+ method peek =
if offset <= String.length source_line
- then Some self#peek_char_unchecked
+ then Some self#peek_unchecked
else
try
line_offset <- line_offset + 1 + String.length source_line;
source_line <- input_line in_channel;
offset <- 0;
- Some self#peek_char_unchecked
+ Some self#peek_unchecked
with
| End_of_file -> None
- method advance_impl =
+ method private advance_impl =
offset <- offset + 1
- method current_offset = line_offset + offset
+ method private offset = line_offset + offset
- method slice_from_offset start_offset =
+ method private slice_impl start_offset =
let relative_start_offset = start_offset - line_offset in
String.sub source_line relative_start_offset (offset - relative_start_offset)
end
@@ -141,16 +165,16 @@ object
val mutable offset = 0
- method peek_char =
+ method peek =
if offset < String.length source
then Some (source.[offset])
else None
- method advance_impl =
+ method private advance_impl =
offset <- offset + 1
- method current_offset = offset
+ method private offset = offset
- method slice_from_offset start_offset =
+ method private slice_impl start_offset =
String.sub source start_offset (offset - start_offset)
end
View it on GitLab: https://gitlab.com/monnier/typer/-/compare/81e7e20ee757b578f0a3017e5f4a8c48…
--
View it on GitLab: https://gitlab.com/monnier/typer/-/compare/81e7e20ee757b578f0a3017e5f4a8c48…
You're receiving this email because of your account on gitlab.com.
Simon Génier pushed to branch offsets-to-points at Stefan / Typer
Commits:
4fb0ccd3 by Simon Génier at 2021-06-09T12:40:18-04:00
Shorten the names of a few methods on source objects.
These names were redundants. For instance in `current_offset`, the `current`
part is already hinted at by the fact that source objects are a kind of cursor.
Overall I think the new names a clearer since they are more concise.
- - - - -
aff91a51 by Simon Génier at 2021-09-28T17:16:11-04:00
Merge branch 'shorten-source-methods'
- - - - -
e8a59f86 by Simon Génier at 2021-09-28T17:25:19-04:00
Replace raw offsets with points which also track line and column.
- - - - -
3 changed files:
- src/lexer.ml
- src/prelexer.ml
- src/source.ml
Changes:
=====================================
src/lexer.ml
=====================================
@@ -38,34 +38,34 @@ let unescape str =
let lex_number
(source : #Source.t)
(start_location : Source.Location.t)
- (start_offset : int)
+ (start_point : Source.Point.t)
: sexp =
let rec lex_exponent () =
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9')
-> source#advance;
lex_exponent ()
| _
- -> let source = source#slice_from_offset start_offset in
+ -> let source = source#slice start_point in
Float (start_location, float_of_string source)
in
let lex_exponent_sign () =
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9' | '+' | '-')
-> source#advance;
lex_exponent ()
(* TODO: error if there are no signs or digits after the 'e'? *)
| _
- -> let source = source#slice_from_offset start_offset in
+ -> let source = source#slice start_point in
Float (start_location, float_of_string source)
in
let rec lex_fractional () =
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9')
-> source#advance;
lex_fractional ()
@@ -75,12 +75,12 @@ let lex_number
lex_exponent_sign ()
| _
- -> let source = source#slice_from_offset start_offset in
+ -> let source = source#slice start_point in
Float (start_location, float_of_string source)
in
let rec lex_integer () =
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9')
-> source#advance;
lex_integer ()
@@ -94,7 +94,7 @@ let lex_number
lex_exponent_sign ()
| _
- -> let source = source#slice_from_offset start_offset in
+ -> let source = source#slice start_point in
Integer (start_location, Z.of_string source)
in
@@ -106,40 +106,40 @@ let lex_symbol
(source : #Source.t)
(doc : string)
(start_location : Source.Location.t)
- (start_offset : int)
+ (start_point : Source.Point.t)
: sexp =
- let mksym start_location start_offset escaped =
- if Int.equal start_offset source#current_offset
+ let mksym start_location start_point escaped =
+ if Source.Point.equal start_point source#point
then epsilon start_location
else
- let raw_name = source#slice_from_offset start_offset in
+ let raw_name = source#slice start_point in
let name = if escaped then unescape raw_name else raw_name in
hSymbol (start_location, name)
in
- let rec loop start_location start_offset prec lf (escaped : bool) =
- match source#peek_char with
+ let rec loop start_location start_point prec lf (escaped : bool) =
+ match source#peek with
| None
- -> lf (mksym start_location start_offset escaped)
+ -> lf (mksym start_location start_point escaped)
| Some c
-> (match token_env.(Char.code c) with
| CKseparate
- -> lf (mksym start_location start_offset escaped)
+ -> lf (mksym start_location start_point escaped)
| CKinner prec'
- -> let left = mksym start_location start_offset escaped in
- let op_location = source#current_location doc in
+ -> let left = mksym start_location start_point escaped in
+ let op_location = source#location doc in
(* To be considered `inner`, an operator char needs to have
something on its LHS or its RHS, otherwise, treat it as a
normal char. `.` can be the normal function composition
operator as well. *)
- let lhs_p = start_offset != source#current_offset in
+ let lhs_p = not (Source.Point.equal start_point source#point) in
source#advance;
let rhs_p =
- (match source#peek_char with
+ (match source#peek with
| None -> false
| Some c' -> CKseparate != token_env.(Char.code c'))
in
@@ -151,13 +151,13 @@ let lex_symbol
then fun s -> lf (Node (op, [left; s]))
else fun s -> Node (op, [lf left; s])
in
- loop (source#current_location doc) source#current_offset prec' lf' false
+ loop (source#location doc) source#point prec' lf' false
else
- loop start_location start_offset prec lf escaped
+ loop start_location start_point prec lf escaped
| CKnormal
-> (source#advance;
- let is_last = Option.is_none source#peek_char in
+ let is_last = Option.is_none source#peek in
match c with
(* Skip next char, in case it's a special token. For utf-8,
simply advancing is risky but actually works: '\' counts as 1
@@ -165,11 +165,11 @@ let lex_symbol
leading byte, so it has to count as 1 as well ;-) *)
| '\\' when not is_last
-> source#advance;
- loop start_location start_offset prec lf true
+ loop start_location start_point prec lf true
- | _ -> loop start_location start_offset prec lf escaped))
+ | _ -> loop start_location start_point prec lf escaped))
in
- loop start_location start_offset 0 (fun s -> s) false
+ loop start_location start_point 0 (fun s -> s) false
let split_presymbol
(token_env : token_env)
@@ -178,33 +178,33 @@ let split_presymbol
: sexp list =
let rec loop (acc : sexp list) : sexp list =
- match source#peek_char with
+ match source#peek with
| None -> List.rev acc
| Some (c)
- -> let start_offset = source#current_offset in
- let start_location = source#current_location doc in
+ -> let start_point = source#point in
+ let start_location = source#location doc in
let token =
match c with
(* A negative number literal, or a symbol starting with '-'. *)
| '-'
-> (source#advance;
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9')
- -> lex_number source start_location start_offset
+ -> lex_number source start_location start_point
| _
- -> lex_symbol token_env source doc start_location start_offset)
+ -> lex_symbol token_env source doc start_location start_point)
| '0' .. '9'
-> (source#advance;
- lex_number source start_location start_offset)
+ lex_number source start_location start_point)
| _ when token_env.(Char.code c) = CKseparate
-> source#advance;
- let name = source#slice_from_offset start_offset in
+ let name = source#slice start_point in
hSymbol (start_location, name)
| _
- -> lex_symbol token_env source doc start_location start_offset
+ -> lex_symbol token_env source doc start_location start_point
in
loop (token :: acc)
in
=====================================
src/prelexer.ml
=====================================
@@ -74,7 +74,7 @@ let prelex (source : #Source.t) : pretoken list =
(doc : string)
: pretoken list =
- match source#peek_char with
+ match source#peek with
| None
-> (match ctx with
| [] -> List.rev acc
@@ -94,9 +94,9 @@ let prelex (source : #Source.t) : pretoken list =
(* line's bounds seems ok: String.sub line 1 0 == "" *)
| Some ('@')
-> source#advance;
- let start_offset = source#current_offset in
+ let start_point = source#point in
consume_until_newline source;
- let new_doc = String.trim (source#slice_from_offset start_offset) in
+ let new_doc = String.trim (source#slice start_point) in
loop ctx acc (doc ^ "\n" ^ new_doc)
(* A string. *)
@@ -105,26 +105,26 @@ let prelex (source : #Source.t) : pretoken list =
let rec prestring chars =
match source#next_char with
| None | Some ('\n')
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
prelexer_error location "Unterminated string";
loop ctx (Prestring (location, "") :: acc) ""
| Some ('"')
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
let pretoken = Prestring (location, string_implode (List.rev chars)) in
loop ctx (pretoken :: acc) ""
| Some ('\\')
-> (match source#next_char with
| None | Some ('\n')
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
prelexer_error location "Unterminated escape sequence";
loop ctx (Prestring (location, "") :: acc) ""
| Some ('t') -> prestring ('\t' :: chars)
| Some ('n') -> prestring ('\n' :: chars)
| Some ('r') -> prestring ('\r' :: chars)
| Some ('u')
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
prelexer_error location "Unimplemented unicode escape";
prestring chars
| Some (c) -> prestring (c :: chars))
@@ -134,12 +134,12 @@ let prelex (source : #Source.t) : pretoken list =
| Some ('{')
-> source#advance;
- let ctx' = (source#current_location doc, acc) :: ctx in
+ let ctx' = (source#location doc, acc) :: ctx in
loop ctx' [] doc
| Some ('}')
-> source#advance;
- let location = source#current_location doc in
+ let location = source#location doc in
(match ctx with
| ((slocation, sacc) :: ctx)
-> let preblock = Preblock (slocation, List.rev acc, location) in
@@ -150,18 +150,18 @@ let prelex (source : #Source.t) : pretoken list =
(* A pretoken. *)
| Some _
- -> let start_offset = source#current_offset in
+ -> let start_point = source#point in
source#advance;
let rec pretok () =
- match source#peek_char with
+ match source#peek with
| None | Some ('%' | '"' | '{' | '}')
- -> let location = source#current_location doc in
- let text = source#slice_from_offset start_offset in
+ -> let location = source#location doc in
+ let text = source#slice start_point in
loop ctx (Pretoken (location, text) :: acc) ""
| Some (c) when c <= ' '
- -> let location = source#current_location doc in
- let text = source#slice_from_offset start_offset in
+ -> let location = source#location doc in
+ let text = source#slice start_point in
source#advance;
loop ctx (Pretoken (location, text) :: acc) ""
@@ -170,9 +170,9 @@ let prelex (source : #Source.t) : pretoken list =
(match source#next_char with
| Some _ -> pretok ()
| None
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
source#advance;
- let text = source#slice_from_offset start_offset in
+ let text = source#slice start_point in
prelexer_error location ("Unterminated escape sequence in: " ^ text);
loop ctx (Pretoken (location, text) :: acc) "")
=====================================
src/source.ml
=====================================
@@ -18,6 +18,16 @@
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>. *)
+module Point = struct
+ (* offset * line * column *)
+ type t = int * int * int
+
+ let equal (l, _, _ : t) (r, _, _ : t) : bool =
+ (* The line and column are metadata, only the offset is necessary for
+ determining equality. *)
+ Int.equal l r
+end
+
module Location = struct
type t = {file : string; line : int; column : int; docstr : string}
@@ -43,37 +53,40 @@ let first_column_of_line = 1
(* A source object is text paired with a cursor. The text can be lazily loaded
as it is accessed byte by byte, but it must be retained for future reference
by error messages. *)
-class virtual t (base_line : int) (base_column : int) (file_name : string) =
+class virtual t (base_line : int) (base_column : int) (file : string) =
object (self)
val mutable line = base_line
val mutable column = base_column
(* A path if the text comes from a file, otherwise a meaningful identifier. *)
- method file_name : string = file_name
+ method file : string = file
(* Return the byte at the cursor, or None if the cursor is at the end of
the text. *)
- method virtual peek_char : char option
+ method virtual peek : char option
(* The current location of the cursor. *)
- method current_location (docstr : string) : Location.t =
- {file = file_name; line; column; docstr}
+ method location (docstr : string) : Location.t =
+ {file; line; column; docstr}
+
+ method point : Point.t =
+ (self#offset, line, column)
(* The current offset of the cursor in the file, in bytes. *)
- method virtual current_offset : int
+ method virtual private offset : int
- (* Slices the text, from (and including) a starting offset and to (and
+ (* Slices the text, from (and including) a starting point and to (and
excluding) the curent cursor offset.
Note that the source is required only to buffer the last line read and may
raise an Invalid_argument if the slice extends before the start of the
line. *)
- method virtual slice_from_offset : int -> string
+ method virtual slice : Point.t -> string
(* Moves the cursor forward one byte *)
method advance : unit =
let is_utf8_head c = Char.code c < 128 || Char.code c >= 192 in
- let c = self#peek_char in
+ let c = self#peek in
self#advance_impl;
match c with
| Some '\n'
@@ -87,7 +100,7 @@ object (self)
(* Returns the char at the cursor, then advances it forward. *)
method next_char : char option =
- let c = self#peek_char in
+ let c = self#peek in
self#advance;
c
end
@@ -103,29 +116,29 @@ class source_file file_name = object (self)
val mutable line_offset = 0
val mutable offset = 0
- method private peek_char_unchecked =
+ method private peek_unchecked =
if offset < String.length source_line
then source_line.[offset]
else '\n'
- method peek_char =
+ method peek =
if offset <= String.length source_line
- then Some self#peek_char_unchecked
+ then Some self#peek_unchecked
else
try
line_offset <- line_offset + 1 + String.length source_line;
source_line <- input_line in_channel;
offset <- 0;
- Some self#peek_char_unchecked
+ Some self#peek_unchecked
with
| End_of_file -> None
method advance_impl =
offset <- offset + 1
- method current_offset = line_offset + offset
+ method private offset = line_offset + offset
- method slice_from_offset start_offset =
+ method slice (start_offset, _, _) =
let relative_start_offset = start_offset - line_offset in
String.sub source_line relative_start_offset (offset - relative_start_offset)
end
@@ -141,7 +154,7 @@ object
val mutable offset = 0
- method peek_char =
+ method peek =
if offset < String.length source
then Some (source.[offset])
else None
@@ -149,8 +162,8 @@ object
method advance_impl =
offset <- offset + 1
- method current_offset = offset
+ method private offset = offset
- method slice_from_offset start_offset =
+ method slice (start_offset, _, _) =
String.sub source start_offset (offset - start_offset)
end
View it on GitLab: https://gitlab.com/monnier/typer/-/compare/b6f70b8a97488267c3dc6ebc32f6e6a5…
--
View it on GitLab: https://gitlab.com/monnier/typer/-/compare/b6f70b8a97488267c3dc6ebc32f6e6a5…
You're receiving this email because of your account on gitlab.com.
Simon Génier pushed to branch master at Stefan / Typer
Commits:
4fb0ccd3 by Simon Génier at 2021-06-09T12:40:18-04:00
Shorten the names of a few methods on source objects.
These names were redundants. For instance in `current_offset`, the `current`
part is already hinted at by the fact that source objects are a kind of cursor.
Overall I think the new names a clearer since they are more concise.
- - - - -
aff91a51 by Simon Génier at 2021-09-28T17:16:11-04:00
Merge branch 'shorten-source-methods'
- - - - -
3 changed files:
- src/lexer.ml
- src/prelexer.ml
- src/source.ml
Changes:
=====================================
src/lexer.ml
=====================================
@@ -42,30 +42,30 @@ let lex_number
: sexp =
let rec lex_exponent () =
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9')
-> source#advance;
lex_exponent ()
| _
- -> let source = source#slice_from_offset start_offset in
+ -> let source = source#slice start_offset in
Float (start_location, float_of_string source)
in
let lex_exponent_sign () =
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9' | '+' | '-')
-> source#advance;
lex_exponent ()
(* TODO: error if there are no signs or digits after the 'e'? *)
| _
- -> let source = source#slice_from_offset start_offset in
+ -> let source = source#slice start_offset in
Float (start_location, float_of_string source)
in
let rec lex_fractional () =
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9')
-> source#advance;
lex_fractional ()
@@ -75,12 +75,12 @@ let lex_number
lex_exponent_sign ()
| _
- -> let source = source#slice_from_offset start_offset in
+ -> let source = source#slice start_offset in
Float (start_location, float_of_string source)
in
let rec lex_integer () =
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9')
-> source#advance;
lex_integer ()
@@ -94,7 +94,7 @@ let lex_number
lex_exponent_sign ()
| _
- -> let source = source#slice_from_offset start_offset in
+ -> let source = source#slice start_offset in
Integer (start_location, Z.of_string source)
in
@@ -110,16 +110,16 @@ let lex_symbol
: sexp =
let mksym start_location start_offset escaped =
- if Int.equal start_offset source#current_offset
+ if Int.equal start_offset source#offset
then epsilon start_location
else
- let raw_name = source#slice_from_offset start_offset in
+ let raw_name = source#slice start_offset in
let name = if escaped then unescape raw_name else raw_name in
hSymbol (start_location, name)
in
let rec loop start_location start_offset prec lf (escaped : bool) =
- match source#peek_char with
+ match source#peek with
| None
-> lf (mksym start_location start_offset escaped)
@@ -130,16 +130,16 @@ let lex_symbol
| CKinner prec'
-> let left = mksym start_location start_offset escaped in
- let op_location = source#current_location doc in
+ let op_location = source#location doc in
(* To be considered `inner`, an operator char needs to have
something on its LHS or its RHS, otherwise, treat it as a
normal char. `.` can be the normal function composition
operator as well. *)
- let lhs_p = start_offset != source#current_offset in
+ let lhs_p = start_offset != source#offset in
source#advance;
let rhs_p =
- (match source#peek_char with
+ (match source#peek with
| None -> false
| Some c' -> CKseparate != token_env.(Char.code c'))
in
@@ -151,13 +151,13 @@ let lex_symbol
then fun s -> lf (Node (op, [left; s]))
else fun s -> Node (op, [lf left; s])
in
- loop (source#current_location doc) source#current_offset prec' lf' false
+ loop (source#location doc) source#offset prec' lf' false
else
loop start_location start_offset prec lf escaped
| CKnormal
-> (source#advance;
- let is_last = Option.is_none source#peek_char in
+ let is_last = Option.is_none source#peek in
match c with
(* Skip next char, in case it's a special token. For utf-8,
simply advancing is risky but actually works: '\' counts as 1
@@ -178,17 +178,17 @@ let split_presymbol
: sexp list =
let rec loop (acc : sexp list) : sexp list =
- match source#peek_char with
+ match source#peek with
| None -> List.rev acc
| Some (c)
- -> let start_offset = source#current_offset in
- let start_location = source#current_location doc in
+ -> let start_offset = source#offset in
+ let start_location = source#location doc in
let token =
match c with
(* A negative number literal, or a symbol starting with '-'. *)
| '-'
-> (source#advance;
- match source#peek_char with
+ match source#peek with
| Some ('0' .. '9')
-> lex_number source start_location start_offset
| _
@@ -200,7 +200,7 @@ let split_presymbol
| _ when token_env.(Char.code c) = CKseparate
-> source#advance;
- let name = source#slice_from_offset start_offset in
+ let name = source#slice start_offset in
hSymbol (start_location, name)
| _
=====================================
src/prelexer.ml
=====================================
@@ -74,7 +74,7 @@ let prelex (source : #Source.t) : pretoken list =
(doc : string)
: pretoken list =
- match source#peek_char with
+ match source#peek with
| None
-> (match ctx with
| [] -> List.rev acc
@@ -94,9 +94,9 @@ let prelex (source : #Source.t) : pretoken list =
(* line's bounds seems ok: String.sub line 1 0 == "" *)
| Some ('@')
-> source#advance;
- let start_offset = source#current_offset in
+ let start_offset = source#offset in
consume_until_newline source;
- let new_doc = String.trim (source#slice_from_offset start_offset) in
+ let new_doc = String.trim (source#slice start_offset) in
loop ctx acc (doc ^ "\n" ^ new_doc)
(* A string. *)
@@ -105,26 +105,26 @@ let prelex (source : #Source.t) : pretoken list =
let rec prestring chars =
match source#next_char with
| None | Some ('\n')
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
prelexer_error location "Unterminated string";
loop ctx (Prestring (location, "") :: acc) ""
| Some ('"')
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
let pretoken = Prestring (location, string_implode (List.rev chars)) in
loop ctx (pretoken :: acc) ""
| Some ('\\')
-> (match source#next_char with
| None | Some ('\n')
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
prelexer_error location "Unterminated escape sequence";
loop ctx (Prestring (location, "") :: acc) ""
| Some ('t') -> prestring ('\t' :: chars)
| Some ('n') -> prestring ('\n' :: chars)
| Some ('r') -> prestring ('\r' :: chars)
| Some ('u')
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
prelexer_error location "Unimplemented unicode escape";
prestring chars
| Some (c) -> prestring (c :: chars))
@@ -134,12 +134,12 @@ let prelex (source : #Source.t) : pretoken list =
| Some ('{')
-> source#advance;
- let ctx' = (source#current_location doc, acc) :: ctx in
+ let ctx' = (source#location doc, acc) :: ctx in
loop ctx' [] doc
| Some ('}')
-> source#advance;
- let location = source#current_location doc in
+ let location = source#location doc in
(match ctx with
| ((slocation, sacc) :: ctx)
-> let preblock = Preblock (slocation, List.rev acc, location) in
@@ -150,18 +150,18 @@ let prelex (source : #Source.t) : pretoken list =
(* A pretoken. *)
| Some _
- -> let start_offset = source#current_offset in
+ -> let start_offset = source#offset in
source#advance;
let rec pretok () =
- match source#peek_char with
+ match source#peek with
| None | Some ('%' | '"' | '{' | '}')
- -> let location = source#current_location doc in
- let text = source#slice_from_offset start_offset in
+ -> let location = source#location doc in
+ let text = source#slice start_offset in
loop ctx (Pretoken (location, text) :: acc) ""
| Some (c) when c <= ' '
- -> let location = source#current_location doc in
- let text = source#slice_from_offset start_offset in
+ -> let location = source#location doc in
+ let text = source#slice start_offset in
source#advance;
loop ctx (Pretoken (location, text) :: acc) ""
@@ -170,9 +170,9 @@ let prelex (source : #Source.t) : pretoken list =
(match source#next_char with
| Some _ -> pretok ()
| None
- -> let location = source#current_location doc in
+ -> let location = source#location doc in
source#advance;
- let text = source#slice_from_offset start_offset in
+ let text = source#slice start_offset in
prelexer_error location ("Unterminated escape sequence in: " ^ text);
loop ctx (Pretoken (location, text) :: acc) "")
=====================================
src/source.ml
=====================================
@@ -43,24 +43,24 @@ let first_column_of_line = 1
(* A source object is text paired with a cursor. The text can be lazily loaded
as it is accessed byte by byte, but it must be retained for future reference
by error messages. *)
-class virtual t (base_line : int) (base_column : int) (file_name : string) =
+class virtual t (base_line : int) (base_column : int) (file : string) =
object (self)
val mutable line = base_line
val mutable column = base_column
(* A path if the text comes from a file, otherwise a meaningful identifier. *)
- method file_name : string = file_name
+ method file : string = file
(* Return the byte at the cursor, or None if the cursor is at the end of
the text. *)
- method virtual peek_char : char option
+ method virtual peek : char option
(* The current location of the cursor. *)
- method current_location (docstr : string) : Location.t =
- {file = file_name; line; column; docstr}
+ method location (docstr : string) : Location.t =
+ {file; line; column; docstr}
(* The current offset of the cursor in the file, in bytes. *)
- method virtual current_offset : int
+ method virtual offset : int
(* Slices the text, from (and including) a starting offset and to (and
excluding) the curent cursor offset.
@@ -68,12 +68,12 @@ object (self)
Note that the source is required only to buffer the last line read and may
raise an Invalid_argument if the slice extends before the start of the
line. *)
- method virtual slice_from_offset : int -> string
+ method virtual slice : int -> string
(* Moves the cursor forward one byte *)
method advance : unit =
let is_utf8_head c = Char.code c < 128 || Char.code c >= 192 in
- let c = self#peek_char in
+ let c = self#peek in
self#advance_impl;
match c with
| Some '\n'
@@ -87,7 +87,7 @@ object (self)
(* Returns the char at the cursor, then advances it forward. *)
method next_char : char option =
- let c = self#peek_char in
+ let c = self#peek in
self#advance;
c
end
@@ -103,29 +103,29 @@ class source_file file_name = object (self)
val mutable line_offset = 0
val mutable offset = 0
- method private peek_char_unchecked =
+ method private peek_unchecked =
if offset < String.length source_line
then source_line.[offset]
else '\n'
- method peek_char =
+ method peek =
if offset <= String.length source_line
- then Some self#peek_char_unchecked
+ then Some self#peek_unchecked
else
try
line_offset <- line_offset + 1 + String.length source_line;
source_line <- input_line in_channel;
offset <- 0;
- Some self#peek_char_unchecked
+ Some self#peek_unchecked
with
| End_of_file -> None
method advance_impl =
offset <- offset + 1
- method current_offset = line_offset + offset
+ method offset = line_offset + offset
- method slice_from_offset start_offset =
+ method slice start_offset =
let relative_start_offset = start_offset - line_offset in
String.sub source_line relative_start_offset (offset - relative_start_offset)
end
@@ -141,7 +141,7 @@ object
val mutable offset = 0
- method peek_char =
+ method peek =
if offset < String.length source
then Some (source.[offset])
else None
@@ -149,8 +149,8 @@ object
method advance_impl =
offset <- offset + 1
- method current_offset = offset
+ method offset = offset
- method slice_from_offset start_offset =
+ method slice start_offset =
String.sub source start_offset (offset - start_offset)
end
View it on GitLab: https://gitlab.com/monnier/typer/-/compare/a7ac505a8249a889aa1235f6823b1c70…
--
View it on GitLab: https://gitlab.com/monnier/typer/-/compare/a7ac505a8249a889aa1235f6823b1c70…
You're receiving this email because of your account on gitlab.com.
Soilihi BEN SOILIHI BOINA pushed to branch soilih at Stefan / Typer
Commits:
9d9ea020 by Soilih BEN SOILIH at 2021-09-27T15:05:21-06:00
considerate vname loc and ltype
- - - - -
2 changed files:
- src/REPL.ml
- src/typer_lsp_server.ml
Changes:
=====================================
src/REPL.ml
=====================================
@@ -150,6 +150,8 @@ let eval_interactive
List.iter interactive#process_decls ldecls;
print_and_clear_log ();
+
+ List.iter Lexp.lexp_print lexprs;
let values = List.map interactive#eval_expr lexprs in
List.iter (Eval.print_eval_result i) values;
=====================================
src/typer_lsp_server.ml
=====================================
@@ -125,13 +125,12 @@ let rec nearest_lexp_of_the_list (liste : (vname * Lexp.lexp * Lexp.ltype) list)
match liste with
| [] -> failwith "No Lexp found!!!"
- | [n] -> let (_,lxp,_) = n in
- lxp
+ | [n] -> n
| [hd;tl] -> let (_,lxp_hd,_) = hd in
let (_,lxp_tl,_) = tl in
if (dist (Lexp.lexp_location lxp_hd) cursor < dist (Lexp.lexp_location lxp_tl) cursor )
- then lxp_hd
- else lxp_tl
+ then hd
+ else tl
| hd::ml::tl -> let (_,lxp_hd,_) = hd in
let (_,lxp_ml,_) = ml in
if (dist (Lexp.lexp_location lxp_hd) cursor < dist (Lexp.lexp_location lxp_ml) cursor )
@@ -376,16 +375,17 @@ let update_the_context (ctx: Debruijn.lexp_context) (liste : (vname * Lexp.lexp
let lstr = update_the_context ctx lst in
- let rec foo (ls: (Debruijn.lexp_context * (vname * Lexp.lexp * Lexp.ltype) list) list) (cursor:Source.Location.t) : Debruijn.lexp_context * Lexp.lexp =
+ let rec foo (ls: (Debruijn.lexp_context * (vname * Lexp.lexp * Lexp.ltype) list) list) (cursor:Source.Location.t) : (Debruijn.lexp_context * (vname * Lexp.lexp * Lexp.ltype)) =
match ls with
| [] -> failwith "No Lexp found!"
| hd::tl -> let (lctx_hd, hdl) = hd in
- let lxp = nearest_lexp_of_the_list hdl cursor in
+ let v = nearest_lexp_of_the_list hdl cursor in
+ let (_,lxp,_) = v in
let location = Lexp.lexp_location lxp in
if ((location.start_line <= cursor.start_line && location.start_column <= cursor.start_column)
&&
(location.end_line >= cursor.end_line && location.end_column >= cursor.end_column))
- then (lctx_hd,lxp)
+ then (lctx_hd, v)
else foo tl cursor
in foo lstr cursor
@@ -394,18 +394,18 @@ let update_the_context (ctx: Debruijn.lexp_context) (liste : (vname * Lexp.lexp
let lstr = update_the_context ctx lst in
- let rec foo (ls: (Debruijn.lexp_context * (vname * Lexp.lexp * Lexp.ltype) list) list) (cursor:Source.Location.t) : Debruijn.lexp_context * Lexp.lexp =
+ let rec foo (ls: (Debruijn.lexp_context * (vname * Lexp.lexp * Lexp.ltype) list) list) (cursor:Source.Location.t) : (Debruijn.lexp_context * (vname * Lexp.lexp * Lexp.ltype)) =
match ls with
| [] -> failwith "No Lexp found!"
| [n] -> let (lctx,l) = n in
- let lxp = nearest_lexp_of_the_list l cursor in
- (lctx,lxp)
+ let v = nearest_lexp_of_the_list l cursor in
+ (lctx,v)
| [hd;tl] -> let (lctx_hd,hdl) = hd in
let (lctx_tl,tll) = tl in
- let lxp = nearest_lexp_of_the_list hdl cursor in
- let lxp' = nearest_lexp_of_the_list tll cursor in
- if (dist (Lexp.lexp_location lxp) cursor < dist (Lexp.lexp_location lxp') cursor )
+ let (_,x,_) as lxp = nearest_lexp_of_the_list hdl cursor in
+ let (_,y,_) as lxp' = nearest_lexp_of_the_list tll cursor in
+ if (dist (Lexp.lexp_location x) cursor < dist (Lexp.lexp_location y) cursor )
then (lctx_hd,lxp)
else
(
@@ -413,9 +413,9 @@ let update_the_context (ctx: Debruijn.lexp_context) (liste : (vname * Lexp.lexp
)
| hd::ml::tl -> let (lctx_hd,hdl) = hd in
let (lctx_ml,mll) = ml in
- let lxp = nearest_lexp_of_the_list hdl cursor in
- let lxp' = nearest_lexp_of_the_list mll cursor in
- if (dist (Lexp.lexp_location lxp) cursor < dist (Lexp.lexp_location lxp') cursor )
+ let (_,x,_) as lxp = nearest_lexp_of_the_list hdl cursor in
+ let (_,y,_) as lxp' = nearest_lexp_of_the_list mll cursor in
+ if (dist (Lexp.lexp_location x) cursor < dist (Lexp.lexp_location y) cursor )
then (foo (hd::tl) cursor)
else foo (ml::tl) cursor
@@ -502,9 +502,10 @@ let update_the_context (ctx: Debruijn.lexp_context) (liste : (vname * Lexp.lexp
| Lambda (ak, ((l,_) as v), t, e) -> let ctx' = Debruijn.lctx_extend ctx v Variable t in
let a = browse_lexp ctx' e cursor in
let b = browse_lexp ctx t cursor in
- let etp = Opslexp.get_type ctx' e in
- let c = (ctx,None, Lexp.mkArrow (ak,v,t,l,etp),l) in
- browse_list_lexp ([a;b;c]) cursor
+ (*let etp = Opslexp.get_type ctx' e in
+ let c = (ctx,None, Lexp.mkArrow (ak,v,t,l,etp),l) in*)
+ let d = (ctx,None, t,l) in
+ browse_list_lexp ([a;b;d]) cursor
| Call (f, args)
-> (
@@ -789,7 +790,7 @@ let lsp_compl_from_var (ret : Debruijn.lexp_context * (((Source.Location.t * str
let lst = update_the_context ctx list_list in
let nlst = List.map ( fun x ->
let (lctx,liste) = x in
- let lxp = nearest_lexp_of_the_list liste cursor in
+ let (_,lxp,_) = nearest_lexp_of_the_list liste cursor in
browse_lexp lctx lxp cursor
) lst in
List.filter (fun x ->
@@ -983,7 +984,8 @@ class lsp_server =
let (list_list, _) = ast in
let typer_loc = typer_pos_of_lsp_pos pos in
let ctx = Debruijn.ectx_to_lctx (Elab.default_ectx) in
- let (lctx,lexp) = lexp_search ctx list_list typer_loc in
+ let (lctx,vx) = lexp_search ctx list_list typer_loc in
+ let (_,lexp,_) = vx in
let (l_ctx, idx,_ ,_ ) as ret = browse_lexp lctx lexp typer_loc in
let loc = find_def_location l_ctx ret in
let l = Lsp.Types.Location.create
@@ -1000,8 +1002,11 @@ class lsp_server =
let (list_list, _) = ast in
let typer_loc = typer_pos_of_lsp_pos pos in
let ctx = Debruijn.ectx_to_lctx (Elab.default_ectx) in
- let (lctx,lexp) = lexp_search ctx list_list typer_loc in
- let (l_ctx,_,l_type,location) = browse_lexp lctx lexp typer_loc in
+ let (lctx,vx) = lexp_search ctx list_list typer_loc in
+ let ((lvn,_),lexp,ltyp) = vx in
+ let (l_ctx_r,_,_,_) as ret = browse_lexp lctx lexp typer_loc in
+ let ret' = (l_ctx_r,None,ltyp,lvn) in
+ let (l_ctx,_,l_type,location) = browse_list_lexp [ret;ret'] typer_loc in
let range = lsp_range_of_loc location in
let s = Lexp.lexp_string_for_lsp l_type in
let r = Lsp.Types.Hover.create
@@ -1045,7 +1050,8 @@ class lsp_server =
let (list_list, _) = ast in
let typer_loc = typer_pos_of_lsp_pos pos in
let ctx = Debruijn.ectx_to_lctx (Elab.default_ectx) in
- let (lctx,lexp) = lexp_search ctx list_list typer_loc in
+ let (lctx,vx) = lexp_search ctx list_list typer_loc in
+ let (_,lexp,_) = vx in
let (l_ctx,_,_,_) = browse_lexp lctx lexp typer_loc in
(*
@@ -1083,15 +1089,17 @@ let hoverTest (ctx:Debruijn.lexp_context) (name:string) (content:string) (posit
let (list_list, _) = ast in
let (l,c) = position in
let pos = pos_of_tuple position in
- let (lctx,lexp) = lexp_search ctx list_list pos in
+ let (lctx,vx) = lexp_search ctx list_list pos in
+ let (_,lexp,_) = vx in
let (_,_, l_type,_) = browse_lexp lctx lexp pos in
+ let excpect = "excpected : " ^ expected ^ "\n" in
let output = "output : " ^ (Lexp.lexp_string_for_lsp l_type) ^ "\n\n" in
let input = "input : " ^ content ^ "\n" in
let res = "result : " ^ (comp expected (Lexp.lexp_string_for_lsp l_type)) in
let pos = "position : line " ^ (string_of_int l) ^ " character " ^ (string_of_int c) ^ "\n" in
let sep = "################################################\n" in
let oc = open_out_gen [Open_creat; Open_text; Open_append] 0o640 name in
- output_string oc (sep ^ input ^ pos ^ output ^ res ^ sep );
+ output_string oc (sep ^ input ^ pos ^ excpect ^ output ^ res ^ sep );
close_out oc
let sep name s =
@@ -1114,21 +1122,27 @@ let test () =
hoverTest ctx name "f = 12.3 ;" (1,1) "##Float";
hoverTest ctx name "f = 12.3 ;" (1,6) "##Float";
sep name "LAMBDA";
- hoverTest ctx name "l = (lambda x -> _+_ x 2) ;" (1,1) "##Type";
- hoverTest ctx name "l = (lambda x -> _+_ x 2) ;" (1,8) "##Int";
+ hoverTest ctx name "l = (lambda x -> _+_ x 2) ;" (1,1) "(x : ##Int) -> Int";
+ hoverTest ctx name "l = (lambda x -> _+_ x 2) ;" (1,8) "(x : ##Int) -> Int";
hoverTest ctx name "l = (lambda x -> _+_ x 2) ;" (1,13) "##Int";
hoverTest ctx name "l = (lambda x -> _+_ x 2) ;" (1,19) "(Int -> (Int -> Int))";
hoverTest ctx name "l = (lambda x -> _+_ x 2) ;" (1,21) "##Int";
sep name "FUNCTION(LAMBDA)";
- hoverTest ctx name "multiply x y z = _*_ x y ;" (1,1) "##TypeLevel";
+ hoverTest ctx name "multiply x y z = _*_ x y ;" (1,1) "(ℓ : ##TypeLevel) ≡> (τ : (##Type_ ℓ)) ≡> (x : ##Int) -> (y : ##Int) -> (z : τ) -> Int";
hoverTest ctx name "multiply x y z = _*_ x y ;" (1,10) "##Int";
hoverTest ctx name "multiply x y z = _*_ x y ;" (1,12) "##Int";
hoverTest ctx name "multiply x y z = _*_ x y ;" (1,14) "τ";
- hoverTest ctx name "multiply x y z = _*_ x y ;" (1,19) "(Int -> (Int -> Int))";
- hoverTest ctx name "multiply x y z = x + y + z ;" (1,19) "##Int";
- hoverTest ctx name "multiply x y z = x + y + z ;" (1,23) "##Int";
- hoverTest ctx name "multiply x y z = x + y + z ;" (1,28) "##Int";
- hoverTest ctx name "multiply x y z = _*_ x y ;" (1,26) "##Int";
+ hoverTest ctx name "multiply x y z = _*_ x y ;" (1,18) "(Int -> (Int -> Int))";
+ hoverTest ctx name "multiply x y z = _*_ x y ;" (1,21) "##Int";
+ hoverTest ctx name "multiply x y z = _*_ x y ;" (1,23) "##Int";
+ hoverTest ctx name "multiply x y z = x + y + z ;" (1,1) "(x : ##Int) -> (y : ##Int) -> (z : ##Int) -> Int";
+ hoverTest ctx name "multiply x y z = x + y + z ;" (1,10) "##Int";
+ hoverTest ctx name "multiply x y z = x + y + z ;" (1,12) "##Int";
+ hoverTest ctx name "multiply x y z = x + y + z ;" (1,14) "##Int";
+ hoverTest ctx name "multiply x y z = x + y + z ;" (1,18) "##Int";
+ hoverTest ctx name "multiply x y z = x + y + z ;" (1,20) "(Int -> (Int -> Int))";
+ hoverTest ctx name "multiply x y z = x + y + z ;" (1,22) "##Int";
+ hoverTest ctx name "multiply x y z = x + y + z ;" (1,26) "##Int";
sep name "CALL";
hoverTest ctx name "multiply x y z = _*_ x y ; \n\n\ne = multiply 2 3 \"h\" ;" (4,1) "(ℓ : ##TypeLevel) ≡> (τ : (##Type_ ℓ)) ≡> (x : ##Int) -> (y : ##Int) -> (z : τ) -> Int";
hoverTest ctx name "multiply x y z = _*_ x y ; \n\n\ne = multiply 2 3 \"h\" ;" (4,7) "(ℓ : ##TypeLevel) ≡> (τ : (##Type_ ℓ)) ≡> (x : ##Int) -> (y : ##Int) -> (z : τ) -> Int";
@@ -1141,6 +1155,8 @@ let test () =
sep name "SUSP";
hoverTest ctx name "foo = ##Int ; \n\n\nv = foo ;" (4,1) "(##Type_ ##TypeLevel.z)";
hoverTest ctx name "foo = ##Int ; \n\n\nv = foo ;" (4,6) "(##Type_ ##TypeLevel.z)";
+ hoverTest ctx name "foo = ##String ; \n\n\nv = foo ;" (4,1) "(##Type_ (##TypeLevel.succ ##TypeLevel.z))";
+ hoverTest ctx name "foo = ##Type ; \n\n\nv = foo ;" (4,1) "(##Type_ ##TypeLevel.z)";
sep name "LIST";
hoverTest ctx name "lst = cons 2 ( (cons 1 ) nil ) ;" (1,1) "##Type";
hoverTest ctx name "lst = cons 2 ( (cons 1 ) nil ) ;" (1,8) "(ℓ : TypeLevel) ≡> (a : (##Type_ ℓ)) ≡> (a -> ((List ℓ a) -> (List ℓ a)))";
@@ -1148,6 +1164,10 @@ let test () =
hoverTest ctx name "lst = cons 2 ( (cons 1 ) nil ) ;" (1,27) "(ℓ : TypeLevel) ≡> (a : (##Type_ ℓ)) ≡> (List ℓ a)";
sep name "ARROW";
hoverTest ctx name "type Floo = bar Int Int ;" (1,1) "";
+ hoverTest ctx name "floo = typecons (floo) (bar Int Int) ;\n\n\nbar = datacons floo bar ;" (4,17) "";
+ hoverTest ctx name "floo = typecons (floo) (bar Int Int) ;\n\n\nbar = datacons floo bar ;" (4,29) "";
+ hoverTest ctx name "v = Int -> Int ;" (1,1) "";
+ sep name "INDUCTIVE";
hoverTest ctx name "type Floo = bar Int Int ;" (1,7) "";
hoverTest ctx name "type Floo = bar Int Int ;" (1,18) "";
hoverTest ctx name "type Floo = bar Int Int ;" (1,22) "";
@@ -1156,10 +1176,6 @@ let test () =
hoverTest ctx name "floo = typecons (floo) (bar Int Int) ;\n\n\nbar = datacons floo bar ;" (1,26) "";
hoverTest ctx name "floo = typecons (floo) (bar Int Int) ;\n\n\nbar = datacons floo bar ;" (4,1) "";
hoverTest ctx name "floo = typecons (floo) (bar Int Int) ;\n\n\nbar = datacons floo bar ;" (4,10) "";
- hoverTest ctx name "floo = typecons (floo) (bar Int Int) ;\n\n\nbar = datacons floo bar ;" (4,17) "";
- hoverTest ctx name "floo = typecons (floo) (bar Int Int) ;\n\n\nbar = datacons floo bar ;" (4,29) "";
- hoverTest ctx name "v = Int -> Int ;" (1,1) "";
- sep name "INDUCTIVE";
hoverTest ctx name "Bool = typecons (Bool) (true) (false);" (1,1) "";
hoverTest ctx name "Bool = typecons (Bool) (true) (false);" (1,10) "";
hoverTest ctx name "Bool = typecons (Bool) (true) (false);" (1,18) "";
@@ -1195,6 +1211,9 @@ let test () =
hoverTest ctx name "foo x = case x\n\n| true => \"h\"\n\n| false => \"jj\" ;" (3,13) "";
hoverTest ctx name "foo x = case x\n\n| true => \"h\"\n\n| false => \"jj\" ;" (5,4) "";
hoverTest ctx name "foo x = case x\n\n| true => \"h\"\n\n| false => \"jj\" ;" (5,14) ""
+
+
+
(*=======================================Test======================================*)
@@ -1206,7 +1225,7 @@ let test () =
let run () =
ignore (Arg.Set Log.lsp_enabled);
Logs.set_level ~all:true (Some Logs.Debug);
- (*test ();*)
+ test ();
let s = new lsp_server in
let server = Linol_lwt.Jsonrpc2.create_stdio s in
let task = Linol_lwt.Jsonrpc2.run server in
View it on GitLab: https://gitlab.com/monnier/typer/-/commit/9d9ea020a8374265e9c72b83c66b6a59c…
--
View it on GitLab: https://gitlab.com/monnier/typer/-/commit/9d9ea020a8374265e9c72b83c66b6a59c…
You're receiving this email because of your account on gitlab.com.