Skip to content

Commit

Permalink
feat(formatter): Preserve comments in Formatter
Browse files Browse the repository at this point in the history
This is not finished work.

We need to support:
+ top-level comments
+ comments between components

And test it properly.

Closes #1086
  • Loading branch information
jubnzv committed Sep 21, 2022
1 parent a91f810 commit 8db2eaf
Show file tree
Hide file tree
Showing 5 changed files with 675 additions and 67 deletions.
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
26 changes: 17 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,19 @@ 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; 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
}
}
Loading

0 comments on commit 8db2eaf

Please sign in to comment.