Skip to content

Commit

Permalink
(GRAM): Recognize assert!, assert_eq! and assert_ne! macros
Browse files Browse the repository at this point in the history
This improves navigation and code highlighting inside the following macros:
assert!(a == b);
debug_assert!(a == b);
assert_eq!(a, b);
debug_assert_eq!(a, b);
assert_ne!(a, b);
debug_assert_ne!(a, b);

Format arguments are supported:
assert!(a == b, "Text {} {} syntax", "with", "format");
assert!(a == b, "Some text");
assert_eq!(a, b, "Some text");
assert_ne!(a, b, "Some text");

Different parenthesis are supported:
assert!(a == b);
assert![a == b];
assert!{a == b};

Fixes intellij-rust#636

assert_ne! is stable since Rust 1.12.
rust-lang/rust#35074
  • Loading branch information
kumbayo committed Oct 7, 2016
1 parent 938f195 commit 90de7d6
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/main/kotlin/org/rust/lang/core/grammar/rust.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
extends (".*_attr") = attr

elementType ("expr_like_macro|item_like_macro") = macro
extends ("(try|format_like)_macro") = macro
extends ("(try|format_like|assert|assert_eq)_macro") = macro

This comment has been minimized.

Copy link
@kumbayo

kumbayo Oct 7, 2016

Author Owner

I am not sure if this part is necessary, i could not detect a difference with or without it :-/
Is there something i can test for?

This comment has been minimized.

Copy link
@matklad

matklad Oct 10, 2016

Is there something i can test for?

No, I don't think so, we don't handle macros specially anywhere yet. But it's better to extend macro anyway.

elementType (".*_macro_invocation|macro_rules_invocation") = macro_invocation

generateTokens=false
Expand Down Expand Up @@ -1004,6 +1004,8 @@ item_like_macro ::= macro_invocation item_macro_arg

try_macro ::= try_macro_invocation try_macro_args { pin = 1 }
format_like_macro ::= format_like_macro_invocation format_macro_args { pin = 1 }
assert_macro ::= assert_macro_invocation assert_macro_args { pin = 1 }
assert_eq_macro ::= assert_eq_macro_invocation assert_eq_macro_args { pin = 1 }

macro_definition ::= macro_rules_invocation IDENTIFIER item_macro_arg {
pin = 1
Expand All @@ -1012,6 +1014,8 @@ macro_definition ::= macro_rules_invocation IDENTIFIER item_macro_arg {

private build_in_macro ::= try_macro
| format_like_macro
| assert_macro
| assert_eq_macro

private zz_macro_call ::= build_in_macro | expr_like_macro { pin(".*") = macro_invocation }
private zz_macro_item ::= macro_definition | item_like_macro {
Expand Down Expand Up @@ -1039,6 +1043,14 @@ format_like_macro_invocation ::= ( "format"
| "trace"
| "warn" ) '!'

assert_macro_invocation ::= ( "assert"
| "debug_assert") '!'

assert_eq_macro_invocation ::= ( "assert_eq"
| "assert_ne"
| "debug_assert_eq"
| "debug_assert_ne") '!'

// Arguments
macro_arg ::= <<macro_args_delim token_trees>>

Expand All @@ -1051,6 +1063,12 @@ item_macro_arg ::= '(' token_trees ')' ';'

try_macro_args ::= <<macro_args_delim any_expr>>

private zz_assert_macro_args ::= any_expr [ ',' <<comma_separated_list format_macro_arg>> ]

This comment has been minimized.

Copy link
@kumbayo

kumbayo Oct 7, 2016

Author Owner

I added this intermediate rule because otherwise Grammar-Kit produces java code that does not compile.
I am not sure if this is an error in Grammar-Kit or expected?

This comment has been minimized.

Copy link
@matklad

matklad Oct 10, 2016

Looks like a bug in Grammar-Kit. I think I'll update it when the next release will be published.

assert_macro_args ::= <<macro_args_delim zz_assert_macro_args>>

private zz_assert_eq_macro_args ::= any_expr ',' any_expr [ ',' <<comma_separated_list format_macro_arg>> ]
assert_eq_macro_args ::= <<macro_args_delim zz_assert_eq_macro_args>>

format_macro_args ::= <<macro_args_delim [ <<comma_separated_list format_macro_arg>> ] >>
format_macro_arg ::= [ IDENTIFIER '=' ] any_expr

Expand Down

0 comments on commit 90de7d6

Please sign in to comment.