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

Multi-line comments should be #664

Merged
merged 12 commits into from
Apr 11, 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
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ set(TRIESTE_BUILD_SAMPLES OFF)
FetchContent_Declare(
trieste
GIT_REPOSITORY https://github.com/microsoft/trieste
GIT_TAG fac9e4c84de192ee53051be32c5426256cd1086e
GIT_SHALLOW TRUE
GIT_TAG 1e1448ca9e4ac158492bf8dbbd86b407c452fc0c
GIT_SHALLOW FALSE
)

FetchContent_MakeAvailable(trieste)
Expand Down
20 changes: 14 additions & 6 deletions src/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace verona
auto depth = std::make_shared<size_t>(0);
auto str = std::make_shared<Str>();
auto indent = std::make_shared<std::vector<size_t>>();
auto start_location = std::make_shared<Location>();
indent->push_back(restart);

p.prefile([](auto&, auto& path) { return path.extension() == ".verona"; });
Expand Down Expand Up @@ -202,10 +203,11 @@ namespace verona

// Unescaped string.
"([']+)\"([^\"]*)" >>
[str](auto& m) {
[str, start_location](auto& m) {
str->start = m.match(1).len;
str->end = 0;
m.add(String, 2);
*start_location = m.match(1);
m.mode("string");
},

Expand All @@ -220,8 +222,10 @@ namespace verona

// Nested comment.
"/\\*" >>
[depth](auto& m) {
[depth, start_location](auto& m) {
assert(*depth == 0);
++(*depth);
*start_location = m.match();
m.mode("comment");
},

Expand Down Expand Up @@ -320,6 +324,7 @@ namespace verona

p("comment",
{
"[^/\\*]+" >> [](auto&) {},
"/\\*" >> [depth](auto&) { ++(*depth); },

"\\*/" >>
Expand All @@ -328,12 +333,15 @@ namespace verona
m.mode("start");
},

"." >> [](auto&) {},
"[/\\*]" >> [](auto&) {},
});

p.done([](auto& m) {
if (m.mode() != "start")
m.error("unterminated comment at end of file");
p.done([start_location](auto& m) {
if (m.mode() == "comment")
m.error("Unterminated comment starting at ", *start_location);

if (m.mode() == "string")
m.error("Unterminated string starting at ", *start_location);

m.term(terminators);
});
Expand Down
2 changes: 1 addition & 1 deletion testsuite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ foreach(TOOL ${TOOL_FOLDERS})
# Check if there are any files to compare for this test.
list(LENGTH results res_length)
if(res_length EQUAL 0)
message(WARNING "Test does not have results directory: ${golden_dir}")
message(WARNING "Test does not have results directory: ${golden_dir}\nRun `update-dump` to generate golden files.")
# Add to generate golden output target
add_custom_command(OUTPUT ${test_path}
COMMAND
Expand Down
38 changes: 38 additions & 0 deletions testsuite/verona_nostdlib/spec/comments.verona
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Comment

// Comment /* with nested comment */

// Comment with "quotes"

// Comment with "quotes /* and nested comment */"

// Comment with "quotes /* and nested comment */" and more text

/* Single-line comment */
/**/
/* Single-line comment with /*Nested comment*/ */

/* Multi-line
comment */
/*

*/
/* Multi-line
comment /* with nested comment */
*/


// /* Comment opening inside comment ignored

/* // line comment inside comment ignored */

// Interaction with strings:
let a = "// not a comment!"

let b = "/* not a comment start"

let c = "not a comment end */"

/* Quotes don't affect the nesting. "/*" */ */

// Comment doesn't need a new line to terminate it
1 change: 1 addition & 0 deletions testsuite/verona_nostdlib/spec/comments_broken_nest.verona
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Nested comments /* like this need well bracketed termination */
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Verona
parse
(top
{}
(file
(group
(error
(errormsg 33:Unterminated comment starting at )
(errorast)))))
9 changes: 9 additions & 0 deletions testsuite/verona_nostdlib/spec/comments_broken_nest_out/ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Verona
parse
(top
{}
(file
(group
(error
(errormsg 33:Unterminated comment starting at )
(errorast)))))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Errors:
Unterminated comment starting at
-- comments_broken_nest.verona:1:1
/* Nested comments /* like this need well bracketed termination */
~~

Pass parse failed with 1 error!

23 changes: 23 additions & 0 deletions testsuite/verona_nostdlib/spec/comments_out/00_parse.trieste
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Verona
parse
(top
{}
(file
(equals
(group
(let)
(ident 1:a))
(group
(escaped 17:// not a comment!)))
(equals
(group
(let)
(ident 1:b))
(group
(escaped 22:/* not a comment start)))
(equals
(group
(let)
(ident 1:c))
(group
(escaped 20:not a comment end */)))))
1 change: 1 addition & 0 deletions testsuite/verona_nostdlib/spec/comments_out/exit_code.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Empty file.
Empty file.
Loading