Skip to content

Commit 57717e3

Browse files
committed
Change nimbleparse test_files to use an array
1 parent 9830859 commit 57717e3

File tree

3 files changed

+90
-106
lines changed

3 files changed

+90
-106
lines changed

.buildbot.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,35 @@ echo "2 + 3 * 4" | cargo run | grep "Result: 14"
5656
touch src/main.rs && CACHE_EXPECTED=y cargo build
5757
cd $root/lrpar/examples/calc_actions
5858
echo "2 + 3 * 4" | cargo run --package nimbleparse -- src/calc.l src/calc.y -
59+
# Invoke `%grmtools{test_files}`
60+
cargo run --package nimbleparse -- src/calc.l src/calc.y
5961
echo "2 + 3 * 4" | cargo run | grep "Result: 14"
6062
touch src/main.rs && CACHE_EXPECTED=y cargo build
6163
cd $root/lrpar/examples/calc_ast
6264
echo "2 + 3 * 4" | cargo run --package nimbleparse -- src/calc.l src/calc.y -
65+
# Invoke `%grmtools{test_files}`
66+
cargo run --package nimbleparse -- src/calc.l src/calc.y
6367
echo "2 + 3 * 4" | cargo run | grep "Result: 14"
6468
cd $root/lrpar/examples/calc_ast_arena
6569
echo "2 + 3 * 4" | cargo run --package nimbleparse -- src/calc.l src/calc.y -
70+
# Invoke `%grmtools{test_files}`
71+
cargo run --package nimbleparse -- src/calc.l src/calc.y
6672
echo "2 + 3 * 4" | cargo run | grep "Result: 14"
6773
touch src/main.rs && CACHE_EXPECTED=y cargo build
6874
cd $root/lrpar/examples/calc_parsetree
6975
echo "2 + 3 * 4" | cargo run --package nimbleparse -- src/calc.l src/calc.y -
76+
# Invoke `%grmtools{test_files}`
77+
cargo run --package nimbleparse -- src/calc.l src/calc.y
7078
echo "2 + 3 * 4" | cargo run | grep "Result: 14"
7179
touch src/main.rs && CACHE_EXPECTED=y cargo build
7280
cd $root/lrpar/examples/clone_param
7381
echo "1+++" | cargo run --package nimbleparse -- src/param.l src/param.y -
82+
# Invoke `%grmtools{test_files}`
83+
cargo run --package nimbleparse -- src/param.l src/param.y
7484
cd $root/lrpar/examples/start_states
7585
echo "/* /* commented out */ */ uncommented text /* */" | cargo run --package nimbleparse -- src/comment.l src/comment.y -
86+
# Invoke `%grmtools{test_files}`
87+
cargo run --package nimbleparse -- src/comment.l src/comment.y
7688
cd $root
7789

7890
RUSTDOCFLAGS="-Dwarnings" cargo doc --no-deps

cfgrammar/src/lib/header.rs

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -210,56 +210,6 @@ impl From<Value<Span>> for Value<Location> {
210210
}
211211
}
212212

213-
impl<T> Value<T> {
214-
pub fn expect_string_with_context(&self, ctxt: &str) -> Result<&str, Box<dyn Error>> {
215-
let found = match self {
216-
Value::Flag(_, _) => "bool".to_string(),
217-
Value::Setting(Setting::String(s, _)) => {
218-
return Ok(s);
219-
}
220-
Value::Setting(Setting::Num(_, _)) => "numeric".to_string(),
221-
Value::Setting(Setting::Unitary(Namespaced {
222-
namespace,
223-
member: (member, _),
224-
})) => {
225-
if let Some((ns, _)) = namespace {
226-
format!("'{ns}::{member}'")
227-
} else {
228-
format!("'{member}'")
229-
}
230-
}
231-
Value::Setting(Setting::Array(_, _, _)) => "array".to_string(),
232-
Value::Setting(Setting::Constructor {
233-
ctor:
234-
Namespaced {
235-
namespace: ctor_ns,
236-
member: (ctor_memb, _),
237-
},
238-
arg:
239-
Namespaced {
240-
namespace: arg_ns,
241-
member: (arg_memb, _),
242-
},
243-
}) => {
244-
format!(
245-
"'{}({})'",
246-
if let Some((ns, _)) = ctor_ns {
247-
format!("{ns}::{ctor_memb}")
248-
} else {
249-
arg_memb.to_string()
250-
},
251-
if let Some((ns, _)) = arg_ns {
252-
format!("{ns}::{arg_memb}")
253-
} else {
254-
arg_memb.to_string()
255-
}
256-
)
257-
}
258-
};
259-
Err(format!("Expected 'String' value, found {}, at {ctxt}", found).into())
260-
}
261-
}
262-
263213
static RE_LEADING_WS: LazyLock<Regex> =
264214
LazyLock::new(|| Regex::new(r"^[\p{Pattern_White_Space}]*").unwrap());
265215
static RE_NAME: LazyLock<Regex> = LazyLock::new(|| {

nimbleparse/src/main.rs

Lines changed: 78 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cfgrammar::{
22
Location, RIdx, Span, TIdx,
3-
header::{GrmtoolsSectionParser, Header, HeaderError, HeaderValue, Value},
3+
header::{GrmtoolsSectionParser, Header, HeaderError, HeaderValue, Setting, Value},
44
markmap::Entry,
55
yacc::{YaccGrammar, YaccKind, YaccOriginalActionKind, ast::ASTWithValidityInfo},
66
};
@@ -495,7 +495,7 @@ impl fmt::Display for NimbleparseError {
495495
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
496496
match self {
497497
Self::Source { src_path, errs } => {
498-
writeln!(f, "While parsing: {}", src_path.display())?;
498+
writeln!(f, "While parsing {}:", src_path.display())?;
499499
for e in errs {
500500
writeln!(f, "{}", e)?
501501
}
@@ -543,77 +543,99 @@ where
543543
}
544544

545545
fn parse_many(self, input_paths: &[String]) -> Result<(), NimbleparseError> {
546-
let input_paths = if input_paths.is_empty() {
546+
let mut paths = Vec::new();
547+
if !input_paths.is_empty() {
548+
paths.extend(
549+
input_paths
550+
.iter()
551+
.map(PathBuf::from)
552+
.collect::<Vec<PathBuf>>(),
553+
);
554+
} else {
547555
// If given no input paths, try to find some with `test_files` in the header.
548-
if let Some(HeaderValue(_, val)) = self.header.get("test_files") {
549-
let s = val.expect_string_with_context("'test_files' in %grmtools")?;
550-
if let Some(yacc_y_path_dir) = self.yacc_y_path.parent() {
551-
let joined = yacc_y_path_dir.join(s);
552-
let joined = joined.as_os_str().to_str();
553-
if let Some(s) = joined {
554-
let mut paths = glob::glob(s)?.peekable();
555-
if paths.peek().is_none() {
556-
return Err(NimbleparseError::Other(
557-
format!("'test_files' glob '{}' matched no paths", s)
558-
.to_string()
559-
.into(),
560-
));
561-
}
562-
let mut input_paths = Vec::new();
563-
for path in paths {
564-
let path = path?;
565-
if let Some(ext) = path.extension() {
566-
if let Some(ext) = ext.to_str() {
567-
if ext.starts_with("grm") {
568-
Err(NimbleparseError::Other(
569-
"test_files extensions beginning with `grm` are reserved."
570-
.into(),
571-
))?
556+
match self.header.get("test_files") {
557+
Some(HeaderValue(_, Value::Setting(Setting::Array(test_globs, _, _)))) => {
558+
for setting in test_globs {
559+
match setting {
560+
Setting::String(s, _) => {
561+
if let Some(yacc_y_path_dir) = self.yacc_y_path.parent() {
562+
let joined = yacc_y_path_dir.join(s);
563+
let joined = joined.as_os_str().to_str();
564+
if let Some(s) = joined {
565+
let mut glob_paths = glob::glob(s)?.peekable();
566+
if glob_paths.peek().is_none() {
567+
return Err(NimbleparseError::Other(
568+
format!(
569+
"'test_files' glob '{}' matched no paths",
570+
s
571+
)
572+
.to_string()
573+
.into(),
574+
));
575+
}
576+
for path in glob_paths {
577+
let path = path?;
578+
if let Some(ext) = path.extension() {
579+
if let Some(ext) = ext.to_str() {
580+
if ext.starts_with("grm") {
581+
Err(NimbleparseError::Other(
582+
"test_files extensions beginning with `grm` are reserved."
583+
.into(),
584+
))?
585+
}
586+
}
587+
}
588+
paths.push(path);
589+
}
590+
} else {
591+
return Err(NimbleparseError::Other(
592+
format!(
593+
"Unable to convert joined path to str {} with glob '{}'",
594+
self.yacc_y_path.display(),
595+
s
596+
)
597+
.into(),
598+
));
572599
}
600+
} else {
601+
return Err(NimbleparseError::Other(
602+
format!(
603+
"Unable to find parent path for {}",
604+
self.yacc_y_path.display()
605+
)
606+
.into(),
607+
));
573608
}
574609
}
575-
input_paths.push(path);
610+
611+
_ => {
612+
return Err(NimbleparseError::Other(
613+
"Expected string values in `test_files`".into(),
614+
));
615+
}
576616
}
577-
input_paths
578-
} else {
579-
return Err(NimbleparseError::Other(
580-
format!(
581-
"Unable to convert joined path to str {} with glob '{}'",
582-
self.yacc_y_path.display(),
583-
s
584-
)
585-
.into(),
586-
));
587617
}
588-
} else {
618+
}
619+
Some(_) => {
589620
return Err(NimbleparseError::Other(
590-
format!(
591-
"Unable to find parent path for {}",
592-
self.yacc_y_path.display()
593-
)
594-
.into(),
621+
"Expected Array of string values in `test_files`".into(),
622+
));
623+
}
624+
None => {
625+
return Err(NimbleparseError::Other(
626+
"Missing <input file> argument".into(),
595627
));
596628
}
597-
} else {
598-
return Err(NimbleparseError::Other(
599-
"Missing <input file> argument".into(),
600-
));
601629
}
602-
} else {
603-
// Just convert the given arguments to paths.
604-
input_paths
605-
.iter()
606-
.map(PathBuf::from)
607-
.collect::<Vec<PathBuf>>()
608630
};
609-
if input_paths.is_empty() {
631+
if paths.is_empty() {
610632
return Err(NimbleparseError::Other(
611633
"Missing <input file> argument".into(),
612634
));
613635
}
614636
let pb = RTParserBuilder::new(&self.grm, &self.stable).recoverer(self.recoverykind);
615637
// Actually parse the given arguments or the `test_files` specified in the grammar.
616-
for input_path in input_paths {
638+
for input_path in paths {
617639
let input = read_file(&input_path);
618640
let lexer = self.lexerdef.lexer(&input);
619641
let (pt, errs) =

0 commit comments

Comments
 (0)