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

New implementation of block commets #168

Merged
merged 3 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 examples/Modules/Main.fram
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Relative imports refer to the module which is the closest to the importing
module, working upwards through the hierarchy. In this example the module
`/Main/B/C/D` imports the relative path `A`, and the nearest matching
module is `/Main/B/A`. #}
module is `/Main/B/A`. ##}

import List

Expand Down
7 changes: 5 additions & 2 deletions src/DblParser/Error.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ let unexpected_token pos tok =
let invalid_character pos ch =
(Some pos, Printf.sprintf "Invalid character `%s'" (Char.escaped ch))

let eof_in_comment pos =
(Some pos, "Unexpected end of file inside a block comment")
let eof_in_comment pos name =
(Some pos,
Printf.sprintf
"Unexpected end of file inside a block comment (`%s#}' was expected)"
name)

let invalid_number pos str =
(Some pos, Printf.sprintf "Invalid integer literal `%s'" str)
Expand Down
2 changes: 1 addition & 1 deletion src/DblParser/Error.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ val module_dependency_cycle : string -> t
val unexpected_token : Position.t -> string -> t
val invalid_character : Position.t -> char -> t

val eof_in_comment : Position.t -> t
val eof_in_comment : Position.t -> string -> t

val invalid_number : Position.t -> string -> t
val number_out_of_bounds : Position.t -> string -> t
Expand Down
28 changes: 12 additions & 16 deletions src/DblParser/Lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ let op_char = [
'~' '*' '%' ';' ',' '=' '|' ':' '.' '/'
]

let comment_name = [^'\000'-' ' '\x7f' '{' '}']*

rule token = parse
whitespace+ { token lexbuf }
| '\n' { Lexing.new_line lexbuf; token lexbuf }
| "{#" { block_comment 1 lexbuf }
| "{#" (comment_name as name) { block_comment name lexbuf }
| "#" { skip_line lexbuf; token lexbuf }
| '(' { YaccParser.BR_OPN }
| ')' { YaccParser.BR_CLS }
Expand Down Expand Up @@ -205,25 +207,19 @@ and string_token pos buf = parse
(Position.of_lexing 0 lexbuf.Lexing.lex_start_p))
}

and block_comment depth = parse
'\n' { Lexing.new_line lexbuf; block_comment depth lexbuf }
| "{#" { block_comment (depth+1) lexbuf }
| "#}" {
if depth = 1 then token lexbuf
else block_comment (depth-1) lexbuf
and block_comment name = parse
'\n' { Lexing.new_line lexbuf; block_comment name lexbuf }
| (comment_name as name') "#}" {
if String.ends_with ~suffix:name name' then token lexbuf
else block_comment name lexbuf
}
| '"' {
let buf = Buffer.create 32 in
let _ : YaccParser.token =
string_token lexbuf.Lexing.lex_start_p buf lexbuf in
block_comment depth lexbuf
}
| "#" { skip_line lexbuf; block_comment depth lexbuf }
| comment_name { block_comment name lexbuf }
| eof {
Error.fatal (Error.eof_in_comment
(Position.of_lexing 0 lexbuf.Lexing.lex_start_p))
(Position.of_lexing 0 lexbuf.Lexing.lex_start_p)
name)
}
| _ { block_comment depth lexbuf }
| _ { block_comment name lexbuf }

and skip_line = parse
'\n' { Lexing.new_line lexbuf }
Expand Down
3 changes: 3 additions & 0 deletions test/err/lexer_0002_eofInComment.fram
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @stderr:!!#}
{#!!
{# #}
39 changes: 39 additions & 0 deletions test/ok/ok0117_comments.fram
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This is a single-line comment.
{# This is a block comment. #}
{# Block comments
may span multiple lines.
#}
let id x = x # A single-line comment may appear at the end of a line.

let n {# A block comment may span a part of a single line. #} = 42
{#aaa
Comments cannot be nested,
{# but the programmer may choose the comment delimiters. #}
aaa#}

{#!a! Comment names may contain operators. !a!#}

{#abc
This comment is ended by `abc` immediately followed by `#}`,
even if the closing sequence is preceded by other characters.
zzabc#}

let {#
# This is not a single-line comment,
# because comments are not nested.
# This comment can be ended #} here = 13

## This is a documentation comment.
let foo x = x

{## This is another documentation comment. ##}
let bar = foo

{###
Documentation comments can contain some code
```
{## with another documentation comment (with a different name). ##}
let some_code = 42
```
###}
let baz = bar
Loading