Skip to content

Commit

Permalink
test(readme): Fix alt 21-tuple limitation
Browse files Browse the repository at this point in the history
Nesting `alt` works, which allows for grouping
etc.

This commit also sorts longest-to-shortest for
more robustness.
  • Loading branch information
alexpovel committed Nov 6, 2024
1 parent 3af3f52 commit bdd183c
Showing 1 changed file with 42 additions and 26 deletions.
68 changes: 42 additions & 26 deletions tests/readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 `--<lang>-query` options need to come
// first; otherwise, the shorter `--<lang>` 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(
Expand Down

0 comments on commit bdd183c

Please sign in to comment.