From bdd183cc27036f7840db2c0d85a89a7d04c0dd0e Mon Sep 17 00:00:00 2001 From: Alex Povel Date: Wed, 6 Nov 2024 19:57:48 +0100 Subject: [PATCH] test(readme): Fix `alt` 21-tuple limitation Nesting `alt` works, which allows for grouping etc. This commit also sorts longest-to-shortest for more robustness. --- tests/readme.rs | 68 ++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/tests/readme.rs b/tests/readme.rs index 61966bf..70dde68 100644 --- a/tests/readme.rs +++ b/tests/readme.rs @@ -466,6 +466,7 @@ mod tests { } /// Parses a single, whole program invocation. + #[allow(clippy::too_many_lines)] // :( many hard-coded values fn parse_program(input: &str) -> IResult<&str, Program> { // Interior mutability is fine, as the different closures aliasing this run // sequentially, never at once (is using this and `map` of `nom` an @@ -491,34 +492,49 @@ mod tests { // *flag*, and `some_value` as a *positional argument*. tag("--"), alt(( - // Careful: all `--lang-query` options need to come - // first; otherwise, the `--lang` options eat them and - // results turn bad (complaining that `-query` is not a - // valid value). Parsing is brittle here :-( - tag("csharp-query"), - tag("go-query"), - tag("hcl-query"), - tag("python-query"), - tag("rust-query"), - tag("typescript-query"), + // ⚠️ Careful: all `---query` options need to come + // first; otherwise, the shorter `--` options eat + // them and results turn bad (complaining that `-query` + // is not a valid value). Generally, parsing is greedy, + // so shorter values will short-circuit, potentially + // incorrectly. That's why lines are generally sorted + // longest to shortest, to avoid confusion. // - tag("csharp"), - tag("glob"), - tag("go"), - tag("hcl"), - tag("python"), - tag("rust"), - tag("stdin-override-to"), - tag("threads"), - tag("typescript"), + // Parsing is brittle here :-( + alt(( + tag("typescript-query"), + tag("csharp-query"), + tag("python-query"), + tag("rust-query"), + tag("hcl-query"), + tag("go-query"), + tag("c-query"), + )), // - // Shorthands. NOTE: only a limited number of elements - // can go here. `nom` is generic, and this tuple inside - // `alt` is limited to 21 members. - tag("cs"), - tag("py"), - tag("rs"), - tag("ts"), + alt(( + tag("typescript"), + tag("csharp"), + tag("python"), + tag("rust"), + tag("hcl"), + tag("go"), + tag("c"), + )), + // Misc. flags used in the docs + alt((tag("glob"), tag("stdin-override-to"), tag("threads"))), + // Shorthands + alt(( + tag("tsx"), + tag("hcl"), + tag("cs"), + tag("py"), + tag("rs"), + tag("ts"), + tag("go"), + tag("tf"), + tag("c"), + // tag("h"), // Breaks `--help` and isn't used + )), )), ), cut(