Skip to content

Commit

Permalink
feat: sort expected tokens lexicographically before printing them (#32)
Browse files Browse the repository at this point in the history
This MR sorts the "expected XXX, YYY, ZZZ" list printed by the
`expandable` proc macro so that its ordering does not depend on how the
grammar is defined. This will make the transition to the compiled parser
smoother (#29).

The sorting algorithm (lexicographic order) is far from perfect, but we
just need to have something vaguely deterministic for now.

---------

Signed-off-by: Sasha Pourcelot <[email protected]>
  • Loading branch information
scrabsha authored Apr 15, 2024
1 parent f36f28f commit 6e3e6c1
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ macro_rules! js_concat {

This emits the following error [^error-message]:
```none
error: Potentially invalid expansion. Expected an identifier, a literal, `if`, a `[`, a `{`, `break`, `return`.
error: Potentially invalid expansion. Expected `break`, `if`, `return`, a `[`, a `{`, a literal, an identifier.
--> tests/ui/fail/js_concat.rs:5:16
|
5 | $left ++ $right
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ fn mk_error_msg(error: expandable_impl::Error<Span>) -> syn::Error {
}

expandable_impl::Error::InvalidProducedAst { span, expected, .. } => {
let expected = expected.iter().map(describe).collect::<Vec<_>>().join(", ");
let mut expected = expected.iter().map(describe).collect::<Vec<_>>();
expected.sort_unstable();
let expected = expected.join(", ");
(
format!("Potentially invalid expansion. Expected {expected}."),
Some(span),
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/fail/fn_args.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: Potentially invalid expansion. Expected an identifier, a `)`.
error: Potentially invalid expansion. Expected a `)`, an identifier.
--> tests/ui/fail/fn_args.rs:5:17
|
5 | fn test(,) -> u8 {
| ^

error: Potentially invalid expansion. Expected an identifier, a `)`.
error: Potentially invalid expansion. Expected a `)`, an identifier.
--> tests/ui/fail/fn_args.rs:15:23
|
15 | fn test(a: u8,,) -> u8 {
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/fail/invalid_fn_calls.stderr
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error: Potentially invalid expansion. Expected an identifier, a literal, `if`, a `[`, a `{`, a `)`, `break`, `return`.
error: Potentially invalid expansion. Expected `break`, `if`, `return`, a `)`, a `[`, a `{`, a literal, an identifier.
--> tests/ui/fail/invalid_fn_calls.rs:6:18
|
6 | $fn_name(,)
| ^

error: Potentially invalid expansion. Expected an identifier, a literal, `if`, a `[`, a `{`, a `)`, `break`, `return`.
error: Potentially invalid expansion. Expected `break`, `if`, `return`, a `)`, a `[`, a `{`, a literal, an identifier.
--> tests/ui/fail/invalid_fn_calls.rs:14:18
|
14 | $fn_name(,,)
| ^

error: Potentially invalid expansion. Expected `+`, `-`, `*`, `/`, `%`, `&`, `|`, `^`, `<<`, `>>`, `==`, `>`, `>=`, `<`, `<=`, `!=`, a `(`, `,`, a `)`, `.`.
error: Potentially invalid expansion. Expected `!=`, `%`, `&`, `*`, `+`, `,`, `-`, `.`, `/`, `<<`, `<=`, `<`, `==`, `>=`, `>>`, `>`, `^`, `|`, a `(`, a `)`.
--> tests/ui/fail/invalid_fn_calls.rs:22:25
|
22 | $fn_name($arg1 $arg2)
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/fail/js_concat.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: Potentially invalid expansion. Expected an identifier, a literal, `if`, a `[`, a `{`, `break`, `return`.
error: Potentially invalid expansion. Expected `break`, `if`, `return`, a `[`, a `{`, a literal, an identifier.
--> tests/ui/fail/js_concat.rs:5:16
|
5 | $left ++ $right
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/fail/python_power_operator.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: Potentially invalid expansion. Expected an identifier, a literal, `if`, a `[`, a `{`, `break`, `return`.
error: Potentially invalid expansion. Expected `break`, `if`, `return`, a `[`, a `{`, a literal, an identifier.
--> tests/ui/fail/python_power_operator.rs:5:14
|
5 | $e * *$e
Expand Down

0 comments on commit 6e3e6c1

Please sign in to comment.