Skip to content

Commit 6679897

Browse files
authored
Multi-line comments should be (#664)
* Add a broken nesting example * Add some comment examples * Multi-line comments should be RE2 `.` does not match newlines by default. This allows newlines inside multi-line comments.
1 parent 6dd1ec8 commit 6679897

File tree

14 files changed

+107
-9
lines changed

14 files changed

+107
-9
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ set(TRIESTE_BUILD_SAMPLES OFF)
1111
FetchContent_Declare(
1212
trieste
1313
GIT_REPOSITORY https://github.com/microsoft/trieste
14-
GIT_TAG fac9e4c84de192ee53051be32c5426256cd1086e
15-
GIT_SHALLOW TRUE
14+
GIT_TAG 1e1448ca9e4ac158492bf8dbbd86b407c452fc0c
15+
GIT_SHALLOW FALSE
1616
)
1717

1818
FetchContent_MakeAvailable(trieste)

src/parse.cc

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ namespace verona
4949
auto depth = std::make_shared<size_t>(0);
5050
auto str = std::make_shared<Str>();
5151
auto indent = std::make_shared<std::vector<size_t>>();
52+
auto start_location = std::make_shared<Location>();
5253
indent->push_back(restart);
5354

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

203204
// Unescaped string.
204205
"([']+)\"([^\"]*)" >>
205-
[str](auto& m) {
206+
[str, start_location](auto& m) {
206207
str->start = m.match(1).len;
207208
str->end = 0;
208209
m.add(String, 2);
210+
*start_location = m.match(1);
209211
m.mode("string");
210212
},
211213

@@ -220,8 +222,10 @@ namespace verona
220222

221223
// Nested comment.
222224
"/\\*" >>
223-
[depth](auto& m) {
225+
[depth, start_location](auto& m) {
226+
assert(*depth == 0);
224227
++(*depth);
228+
*start_location = m.match();
225229
m.mode("comment");
226230
},
227231

@@ -320,6 +324,7 @@ namespace verona
320324

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

325330
"\\*/" >>
@@ -328,12 +333,15 @@ namespace verona
328333
m.mode("start");
329334
},
330335

331-
"." >> [](auto&) {},
336+
"[/\\*]" >> [](auto&) {},
332337
});
333338

334-
p.done([](auto& m) {
335-
if (m.mode() != "start")
336-
m.error("unterminated comment at end of file");
339+
p.done([start_location](auto& m) {
340+
if (m.mode() == "comment")
341+
m.error("Unterminated comment starting at ", *start_location);
342+
343+
if (m.mode() == "string")
344+
m.error("Unterminated string starting at ", *start_location);
337345

338346
m.term(terminators);
339347
});

testsuite/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ foreach(TOOL ${TOOL_FOLDERS})
9191
# Check if there are any files to compare for this test.
9292
list(LENGTH results res_length)
9393
if(res_length EQUAL 0)
94-
message(WARNING "Test does not have results directory: ${golden_dir}")
94+
message(WARNING "Test does not have results directory: ${golden_dir}\nRun `update-dump` to generate golden files.")
9595
# Add to generate golden output target
9696
add_custom_command(OUTPUT ${test_path}
9797
COMMAND
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Comment
2+
3+
// Comment /* with nested comment */
4+
5+
// Comment with "quotes"
6+
7+
// Comment with "quotes /* and nested comment */"
8+
9+
// Comment with "quotes /* and nested comment */" and more text
10+
11+
/* Single-line comment */
12+
/**/
13+
/* Single-line comment with /*Nested comment*/ */
14+
15+
/* Multi-line
16+
comment */
17+
/*
18+
19+
*/
20+
/* Multi-line
21+
comment /* with nested comment */
22+
*/
23+
24+
25+
// /* Comment opening inside comment ignored
26+
27+
/* // line comment inside comment ignored */
28+
29+
// Interaction with strings:
30+
let a = "// not a comment!"
31+
32+
let b = "/* not a comment start"
33+
34+
let c = "not a comment end */"
35+
36+
/* Quotes don't affect the nesting. "/*" */ */
37+
38+
// Comment doesn't need a new line to terminate it
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* Nested comments /* like this need well bracketed termination */
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Verona
2+
parse
3+
(top
4+
{}
5+
(file
6+
(group
7+
(error
8+
(errormsg 33:Unterminated comment starting at )
9+
(errorast)))))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Verona
2+
parse
3+
(top
4+
{}
5+
(file
6+
(group
7+
(error
8+
(errormsg 33:Unterminated comment starting at )
9+
(errorast)))))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

testsuite/verona_nostdlib/spec/comments_broken_nest_out/stderr.txt

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Errors:
2+
Unterminated comment starting at
3+
-- comments_broken_nest.verona:1:1
4+
/* Nested comments /* like this need well bracketed termination */
5+
~~
6+
7+
Pass parse failed with 1 error!
8+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Verona
2+
parse
3+
(top
4+
{}
5+
(file
6+
(equals
7+
(group
8+
(let)
9+
(ident 1:a))
10+
(group
11+
(escaped 17:// not a comment!)))
12+
(equals
13+
(group
14+
(let)
15+
(ident 1:b))
16+
(group
17+
(escaped 22:/* not a comment start)))
18+
(equals
19+
(group
20+
(let)
21+
(ident 1:c))
22+
(group
23+
(escaped 20:not a comment end */)))))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

testsuite/verona_nostdlib/spec/comments_out/stderr.txt

Whitespace-only changes.

testsuite/verona_nostdlib/spec/comments_out/stdout.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)