Skip to content

Commit bbe74aa

Browse files
authored
Merge pull request #256 from oli-obk/comment_symbols
Comment symbols
2 parents 3497d3d + c2beaf7 commit bbe74aa

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
target
2+
.jj

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
* examples and usage instructions
13+
* `Config::comment_start` field for changing the default comment symbols from `//`
1314

1415
### Fixed
1516

1617
### Changed
1718

19+
* default comment symbols for `Config::cargo` changed to `#`
20+
1821
### Removed
1922

2023
## [0.25.0] - 2024-07-24

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub struct Config {
5757
pub filter_exact: bool,
5858
/// The default settings settable via `@` comments
5959
pub comment_defaults: Comments,
60+
/// The symbol(s) that signify the start of a comment.
61+
pub comment_start: &'static str,
6062
/// Custom comment parsers
6163
pub custom_comments: BTreeMap<&'static str, CommandParserFunc>,
6264
/// Custom diagnostic extractor (invoked on the output of tests)
@@ -152,6 +154,7 @@ impl Config {
152154
run_only_ignored: false,
153155
filter_exact: false,
154156
comment_defaults,
157+
comment_start: "//",
155158
custom_comments: Default::default(),
156159
diagnostic_extractor: rustc_stderr::process,
157160
};
@@ -220,6 +223,7 @@ impl Config {
220223
program: CommandBuilder::cargo(),
221224
custom_comments: Default::default(),
222225
diagnostic_extractor: rustc_stderr::process_cargo,
226+
comment_start: "#",
223227
..Self::rustc(root_dir)
224228
};
225229
this.comment_defaults.base().custom.clear();

src/parser.rs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ pub struct CommentParser<T> {
202202
errors: Vec<Error>,
203203
/// The available commands and their parsing logic
204204
commands: HashMap<&'static str, CommandParserFunc>,
205+
/// The symbol(s) that signify the start of a comment.
206+
comment_start: &'static str,
205207
}
206208

207209
pub type CommandParserFunc =
@@ -312,6 +314,7 @@ impl CommentParser<Comments> {
312314
comments: config.comment_defaults.clone(),
313315
errors: vec![],
314316
commands: Self::commands(),
317+
comment_start: config.comment_start,
315318
};
316319
this.commands
317320
.extend(config.custom_comments.iter().map(|(&k, &v)| (k, v)));
@@ -468,15 +471,27 @@ impl CommentParser<Comments> {
468471
line: Spanned<&[u8]>,
469472
) -> std::result::Result<ParsePatternResult, Spanned<Utf8Error>> {
470473
let mut res = ParsePatternResult::Other;
471-
if let Some(command) = line.strip_prefix(b"//@") {
472-
self.parse_command(command.to_str()?.trim())
473-
} else if let Some((_, pattern)) = line.split_once_str("//~") {
474-
let (revisions, pattern) = self.parse_revisions(pattern.to_str()?);
475-
self.revisioned(revisions, |this| {
476-
res = this.parse_pattern(pattern, fallthrough_to, current_line);
477-
})
474+
475+
if let Some((_, comment)) =
476+
line.split_once_str(self.comment_start)
477+
.filter(|(pre, c)| match c[0] {
478+
b'@' => pre.is_empty(),
479+
b'~' => true,
480+
_ => false,
481+
})
482+
{
483+
if let Some(command) = comment.strip_prefix(b"@") {
484+
self.parse_command(command.to_str()?.trim())
485+
} else if let Some(pattern) = comment.strip_prefix(b"~") {
486+
let (revisions, pattern) = self.parse_revisions(pattern.to_str()?);
487+
self.revisioned(revisions, |this| {
488+
res = this.parse_pattern(pattern, fallthrough_to, current_line);
489+
})
490+
} else {
491+
unreachable!()
492+
}
478493
} else {
479-
for pos in line.clone().find_iter("//") {
494+
for pos in line.clone().find_iter(self.comment_start) {
480495
let (_, rest) = line.clone().to_str()?.split_at(pos + 2);
481496
for rest in std::iter::once(rest.clone()).chain(rest.strip_prefix(" ")) {
482497
let c = rest.chars().next();
@@ -489,25 +504,29 @@ impl CommentParser<Comments> {
489504
span,
490505
format!(
491506
"comment looks suspiciously like a test suite command: `{}`\n\
492-
All `//@` test suite commands must be at the start of the line.\n\
493-
The `//` must be directly followed by `@` or `~`.",
494-
*rest,
507+
All `{}@` test suite commands must be at the start of the line.\n\
508+
The `{}` must be directly followed by `@` or `~`.",
509+
*rest, self.comment_start, self.comment_start,
495510
),
496511
);
497512
} else {
498513
let mut parser = Self {
499514
errors: vec![],
500515
comments: Comments::default(),
501516
commands: std::mem::take(&mut self.commands),
517+
comment_start: self.comment_start,
502518
};
503519
let span = rest.span();
504520
parser.parse_command(rest);
505521
if parser.errors.is_empty() {
506522
self.error(
507523
span,
508-
"a compiletest-rs style comment was detected.\n\
524+
format!(
525+
"a compiletest-rs style comment was detected.\n\
509526
Please use text that could not also be interpreted as a command,\n\
510-
and prefix all actual commands with `//@`",
527+
and prefix all actual commands with `{}@`",
528+
self.comment_start
529+
),
511530
);
512531
}
513532
self.commands = parser.commands;
@@ -596,6 +615,7 @@ impl CommentParser<Comments> {
596615
span,
597616
} = revisions;
598617
let mut this = CommentParser {
618+
comment_start: self.comment_start,
599619
errors: std::mem::take(&mut self.errors),
600620
commands: std::mem::take(&mut self.commands),
601621
comments: self
@@ -909,7 +929,8 @@ impl CommentParser<&mut Revisioned> {
909929
}
910930
_ => {
911931
self.error(pattern.span(), format!(
912-
"//~^ pattern is trying to refer to {} lines above, but there are only {} lines above",
932+
"{}~^ pattern is trying to refer to {} lines above, but there are only {} lines above",
933+
self.comment_start,
913934
offset,
914935
current_line.get() - 1,
915936
));
@@ -940,7 +961,8 @@ impl CommentParser<&mut Revisioned> {
940961
// The line count of the file is not yet known so we can only check
941962
// if the resulting line is in the range of a usize.
942963
self.error(pattern.span(), format!(
943-
"//~v pattern is trying to refer to {} lines below, which is more than ui_test can count",
964+
"{}~v pattern is trying to refer to {} lines below, which is more than ui_test can count",
965+
self.comment_start,
944966
offset,
945967
));
946968
return ParsePatternResult::ErrorBelow {

0 commit comments

Comments
 (0)