Skip to content

Commit 8c6401f

Browse files
authored
Merge pull request #561 from ratmice/unquoted_glob_errs
Try to improve the error when we encounter an unquoted glob
2 parents ba40ec2 + 313ebe0 commit 8c6401f

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

cfgrammar/src/lib/header.rs

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub enum HeaderErrorKind {
5757
MissingGrmtoolsSection,
5858
IllegalName,
5959
ExpectedToken(char),
60+
UnexpectedToken(char, &'static str),
6061
DuplicateEntry,
6162
InvalidEntry(&'static str),
6263
ConversionError(&'static str, &'static str),
@@ -67,7 +68,10 @@ impl fmt::Display for HeaderErrorKind {
6768
let s = match self {
6869
HeaderErrorKind::MissingGrmtoolsSection => "Missing %grmtools section",
6970
HeaderErrorKind::IllegalName => "Illegal name",
70-
HeaderErrorKind::ExpectedToken(c) => &format!("Expected token: '{}", c),
71+
HeaderErrorKind::ExpectedToken(c) => &format!("Expected token: '{}'", c),
72+
HeaderErrorKind::UnexpectedToken(c, hint) => {
73+
&format!("Unxpected token: '{}', {} ", c, hint)
74+
}
7175
HeaderErrorKind::InvalidEntry(s) => &format!("Invalid entry: '{}'", s),
7276
HeaderErrorKind::DuplicateEntry => "Duplicate Entry",
7377
HeaderErrorKind::ConversionError(t, err_str) => {
@@ -430,7 +434,16 @@ impl<'input> GrmtoolsSectionParser<'input> {
430434
break;
431435
}
432436
}
433-
if let Some(i) = self.lookahead_is("}", i) {
437+
if let Some(j) = self.lookahead_is("*", i) {
438+
errs.push(HeaderError {
439+
kind: HeaderErrorKind::UnexpectedToken(
440+
'*',
441+
"perhaps this is a glob, in which case it requires string quoting.",
442+
),
443+
locations: vec![Span::new(i, j)],
444+
});
445+
Err(errs)
446+
} else if let Some(i) = self.lookahead_is("}", i) {
434447
if errs.is_empty() {
435448
Ok((ret, i))
436449
} else {
@@ -439,7 +452,7 @@ impl<'input> GrmtoolsSectionParser<'input> {
439452
} else {
440453
errs.push(HeaderError {
441454
kind: HeaderErrorKind::ExpectedToken('}'),
442-
locations: vec![Span::new(section_start_pos, self.src.len())],
455+
locations: vec![Span::new(section_start_pos, i)],
443456
});
444457
Err(errs)
445458
}
@@ -470,10 +483,22 @@ impl<'input> GrmtoolsSectionParser<'input> {
470483
i + m.end(),
471484
))
472485
}
473-
None => Err(HeaderError {
474-
kind: HeaderErrorKind::IllegalName,
475-
locations: vec![Span::new(i, i)],
476-
}),
486+
None => {
487+
if self.src[i..].starts_with("*") {
488+
Err(HeaderError {
489+
kind: HeaderErrorKind::UnexpectedToken(
490+
'*',
491+
"perhaps this is a glob, in which case it requires string quoting.",
492+
),
493+
locations: vec![Span::new(i, i)],
494+
})
495+
} else {
496+
Err(HeaderError {
497+
kind: HeaderErrorKind::IllegalName,
498+
locations: vec![Span::new(i, i)],
499+
})
500+
}
501+
}
477502
}
478503
}
479504

@@ -697,4 +722,25 @@ mod test {
697722
assert_eq!(errs[0].locations.len(), 3);
698723
}
699724
}
725+
726+
#[test]
727+
fn test_unquoted_globs() {
728+
let srcs = [
729+
"%grmtools {test_files: *.test,}",
730+
"%grmtools {test_files: foo*.test,}",
731+
];
732+
for src in srcs {
733+
let parser = GrmtoolsSectionParser::new(src, true);
734+
let res = parser.parse();
735+
let errs = res.unwrap_err();
736+
assert_eq!(errs.len(), 1);
737+
match errs[0] {
738+
HeaderError {
739+
kind: HeaderErrorKind::UnexpectedToken('*', _),
740+
locations: _,
741+
} => (),
742+
_ => panic!("Expected glob specific error"),
743+
}
744+
}
745+
}
700746
}

0 commit comments

Comments
 (0)