Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter: Support comments #1172

Merged
merged 27 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d7eb966
fix(parser): `Let` location
jubnzv Sep 20, 2022
193f240
fix(parser): `Fun` location
jubnzv Sep 20, 2022
a91f810
chore(meta): Move `Formatter` to the separate library
jubnzv Sep 21, 2022
8db2eaf
feat(formatter): Preserve comments in `Formatter`
jubnzv Sep 21, 2022
c6a6b3c
feat(formatter): Parse top-level comments
jubnzv Sep 27, 2022
7726398
feat(formatter): Support top-level comments
jubnzv Sep 29, 2022
eba9666
fix(formatter): Allow multiple file/lib/contract comments
jubnzv Sep 30, 2022
7a68f6d
fix(formatter): Contract comments
jubnzv Sep 30, 2022
b4961cc
feat(formatter): Comment tests
jubnzv Sep 30, 2022
7c7f43a
feat(formatter): Component comments
jubnzv Oct 1, 2022
46c9f41
feat(formatter): Comments for library entries
jubnzv Oct 1, 2022
20f4bab
feat(formatter): Multiline comments
jubnzv Oct 1, 2022
5f8a564
feat(formatter): Field comments
jubnzv Oct 1, 2022
69dc9bf
feat(formatter): Typedef comments
jubnzv Oct 1, 2022
0c2fdab
chore(formatter): Refactor. NFC.
jubnzv Oct 1, 2022
005fdea
feat(formatter): Extra cases for file comment
jubnzv Oct 2, 2022
5863dc1
feat(formatter): Extract cases when no imports/library
jubnzv Oct 2, 2022
881990e
feat(formatter): Comments on the right of library components
jubnzv Oct 2, 2022
2daa886
feat(formatter): Comments in pm arms
jubnzv Oct 2, 2022
f602e13
fix(formatter): Extra whitespaces in pm arms
jubnzv Oct 2, 2022
211ca77
chore(formatter): Promote tests
jubnzv Oct 2, 2022
83fe262
chore(fmt): fmt for `dune` files
jubnzv Oct 2, 2022
c12ebd1
Merge branch 'master' into 1086_comments
jjcnn Oct 5, 2022
a0af1ab
chore(formatter): Improve comments; NFC
jubnzv Oct 4, 2022
d2065aa
fix(formatter): Update the test
jubnzv Oct 4, 2022
837e41f
fix(fmt): Quickfix for test
jubnzv Oct 6, 2022
67b22e3
chore(fmt): Spaced in the comment. NFC.
jubnzv Oct 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/base/ErrorUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type loc = {
(* line number *)
cnum : int; (* column number *)
}
[@@deriving sexp, equal]
[@@deriving sexp, equal, compare]

let toLoc (p : Lexing.position) : loc =
{
Expand Down
2 changes: 1 addition & 1 deletion src/base/ErrorUtils.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type loc = {
(* line number *)
cnum : int; (* column number *)
}
[@@deriving sexp, equal]
[@@deriving sexp, equal, compare]

val toLoc : Lexing.position -> loc
val dummy_loc : loc
Expand Down
1 change: 1 addition & 0 deletions src/base/FrontEndParser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ module ScillaFrontEndParser (Literal : ScillaLiteral) = struct
let parse_expr_from_stdin () = parse_stdin Parser.Incremental.exp_term
let parse_lmodule filename = parse_file Parser.Incremental.lmodule filename
let parse_cmodule filename = parse_file Parser.Incremental.cmodule filename
let get_comments () = Lexer.get_comments ()
end
28 changes: 19 additions & 9 deletions src/base/ScillaLexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ module MkLexer (S : ParserUtil.Syn) = struct

exception Error of string

let comments = ref []
let add_comment start_p s =
let loc = ErrorUtils.toLoc start_p in
comments := (loc, s) :: !comments
let get_comments () = List.rev !comments
}

let digit = ['0'-'9']
Expand All @@ -55,7 +60,7 @@ rule read =

(* Whitespaces *)
| newline { new_line lexbuf; read lexbuf }
| "(*" { comment [lexbuf.lex_curr_p] lexbuf }
| "(*" { comment (Buffer.create 50) [lexbuf.lex_start_p] lexbuf }
| white { read lexbuf }

(* Numbers and hashes *)
Expand Down Expand Up @@ -148,16 +153,21 @@ and read_string buf =
| eof { raise (Error ("String is not terminated")) }

(* Nested comments, keeping a list of where comments open *)
and comment braces =
and comment buf braces =
parse
| "(*" { comment (lexbuf.lex_curr_p::braces) lexbuf}
| "(*" { comment buf (lexbuf.lex_curr_p::braces) lexbuf }
| "*)" { match braces with
_::[] -> read lexbuf
| _ -> comment (List.tl_exn braces) lexbuf }
| newline { new_line lexbuf; comment braces lexbuf}
| _ { comment braces lexbuf}
| eof { lexbuf.lex_curr_p <- List.hd_exn braces; raise (Error ("Comment unfinished"))}
p::[] -> add_comment p (Buffer.contents buf);
read lexbuf
| _ -> comment buf (List.tl_exn braces) lexbuf }
| newline { new_line lexbuf;
Buffer.add_char buf '\n';
comment buf braces lexbuf }
| _ { Buffer.add_string buf (Lexing.lexeme lexbuf);
comment buf braces lexbuf }
| eof { lexbuf.lex_curr_p <- List.hd_exn braces;
raise (Error ("Comment unfinished")) }

{
end
}
}
4 changes: 2 additions & 2 deletions src/base/ScillaParser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ simple_exp :
| LET; x = ID;
t = ioption(type_annot)
EQ; f = simple_exp; IN; e = exp
{(Let ( to_loc_id x (toLoc $startpos(x)), t, f, e), toLoc $startpos(f)) }
{(Let ( to_loc_id x (toLoc $startpos(x)), t, f, e), toLoc $startpos) }
(* Function *)
| FUN; LPAREN; iwt = id_with_typ; RPAREN; ARROW; e = exp
{ match iwt with
| (i, t) -> (Fun (i, t, e), toLoc $startpos(e) ) }
| (i, t) -> (Fun (i, t, e), toLoc $startpos ) }
(* Application *)
| f = sid;
args = nonempty_list(sident)
Expand Down
10 changes: 6 additions & 4 deletions src/base/TypeChecker.ml
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,18 @@ module ScillaTypechecker (SR : Rep) (ER : Rep) = struct
pure
@@ ( TypedSyntax.Builtin ((fst b, q_ret_tag), ts, typed_actuals),
(q_ret_typ, rep) )
| Let (i, topt, lhs, rhs) ->
| Let (i, topt, (lhs, lhs_rep), rhs) ->
(* Poor man's error reporting *)
let%bind ((_, (ityp, _)) as checked_lhs) = type_expr lhs tenv in
let%bind ((_, (ityp, _)) as checked_lhs) =
type_expr (lhs, lhs_rep) tenv
in
let%bind actual_typ =
match topt with
| Some tannot ->
let%bind () =
fromR_TE
@@ assert_type_assignable ~lc:(ER.get_loc rep) ~expected:tannot
~actual:ityp.tp
@@ assert_type_assignable ~lc:(ER.get_loc lhs_rep)
~expected:tannot ~actual:ityp.tp
in
pure (mk_qual_tp tannot)
| None -> pure ityp
Expand Down
6 changes: 3 additions & 3 deletions src/base/dune
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
(wrapped true)
(libraries core core_unix core_unix.sys_unix num hex stdint angstrom
polynomials cryptokit secp256k1 bitstring yojson fileutils scilla_crypto
menhirLib ocamlgraph pprint)
menhirLib ocamlgraph)
(preprocess
(pps ppx_sexp_conv ppx_let ppx_deriving.show ppx_compare
ppx_string_interpolation bisect_ppx --conditional))
(pps ppx_sexp_conv ppx_let ppx_deriving.show ppx_compare bisect_ppx
--conditional))
(synopsis "Scilla workbench implementation."))
Loading