Skip to content

Commit e91fa58

Browse files
committed
Change nimbleparse test_files to using an array
1 parent daf9449 commit e91fa58

File tree

5 files changed

+118
-104
lines changed

5 files changed

+118
-104
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(|| {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Test string value type instead of array in %grmtools{test_files}
2+
grammar: |
3+
%grmtools {
4+
yacckind: Original(YaccOriginalActionKind::UserAction),
5+
recoverer: RecoveryKind::None,
6+
test_files: "should_be_an_array"
7+
}
8+
%start Expr
9+
%actiontype ()
10+
%%
11+
Expr: '(' ')' { () } ;
12+
lexer: |
13+
%%
14+
\( "("
15+
\) ")"
16+
[\t\n ]+ ;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Test non-string in array of %grmtools{test_files}
2+
grammar: |
3+
%grmtools {
4+
yacckind: Original(YaccOriginalActionKind::UserAction),
5+
recoverer: RecoveryKind::None,
6+
test_files: [ShouldBeAString]
7+
}
8+
%start Expr
9+
%actiontype ()
10+
%%
11+
Expr: '(' ')' { () } ;
12+
lexer: |
13+
%%
14+
\( "("
15+
\) ")"
16+
[\t\n ]+ ;

nimbleparse/src/main.rs

Lines changed: 74 additions & 54 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
};
@@ -543,77 +543,97 @@ 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!("'test_files' glob '{}' matched no paths", s)
569+
.to_string()
570+
.into(),
571+
));
572+
}
573+
for path in glob_paths {
574+
let path = path?;
575+
if let Some(ext) = path.extension() {
576+
if let Some(ext) = ext.to_str() {
577+
if ext.starts_with("grm") {
578+
Err(NimbleparseError::Other(
579+
"test_files extensions beginning with `grm` are reserved."
580+
.into(),
581+
))?
582+
}
583+
}
584+
}
585+
paths.push(path);
572586
}
587+
} else {
588+
return Err(NimbleparseError::Other(
589+
format!(
590+
"Unable to convert joined path to str {} with glob '{}'",
591+
self.yacc_y_path.display(),
592+
s
593+
)
594+
.into(),
595+
));
573596
}
597+
} else {
598+
return Err(NimbleparseError::Other(
599+
format!(
600+
"Unable to find parent path for {}",
601+
self.yacc_y_path.display()
602+
)
603+
.into(),
604+
));
574605
}
575-
input_paths.push(path);
576606
}
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-
));
607+
608+
_ => {
609+
return Err(NimbleparseError::Other(
610+
"Expected string values in `test_files`".into(),
611+
));
612+
}
587613
}
588-
} else {
589-
return Err(NimbleparseError::Other(
590-
format!(
591-
"Unable to find parent path for {}",
592-
self.yacc_y_path.display()
593-
)
594-
.into(),
595-
));
596614
}
597-
} else {
615+
}
616+
Some(_) => {
617+
return Err(NimbleparseError::Other(
618+
"Expected Array of string values in `test_files`".into(),
619+
));
620+
621+
}
622+
None => {
598623
return Err(NimbleparseError::Other(
599624
"Missing <input file> argument".into(),
600625
));
601626
}
602-
} else {
603-
// Just convert the given arguments to paths.
604-
input_paths
605-
.iter()
606-
.map(PathBuf::from)
607-
.collect::<Vec<PathBuf>>()
627+
}
608628
};
609-
if input_paths.is_empty() {
629+
if paths.is_empty() {
610630
return Err(NimbleparseError::Other(
611631
"Missing <input file> argument".into(),
612632
));
613633
}
614634
let pb = RTParserBuilder::new(&self.grm, &self.stable).recoverer(self.recoverykind);
615635
// Actually parse the given arguments or the `test_files` specified in the grammar.
616-
for input_path in input_paths {
636+
for input_path in paths {
617637
let input = read_file(&input_path);
618638
let lexer = self.lexerdef.lexer(&input);
619639
let (pt, errs) =

0 commit comments

Comments
 (0)