-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
try a method for matching delimiters #36
try a method for matching delimiters #36
Conversation
This feels like this might be wrong but just let me know. |
Want to try building a more complex macro using match arms? I’m sure we have something similar in the existing macros. |
Sure! I'm not actually sure what that entails here. |
Is it creating a macro that can be matched? Something that would enable this? match token_kind_and_char!(token) {
(Kind::Delim, '=') => todo!()
} |
Generally I'm trying to work towards a set of macros that take the shape of this: expect_some_type!(parser.next());
expect_some_type!(parser.next(), some, details, about, the, type);
if match_some_type!(parser.next(), some, details, about, the, type) {
// ...
}
match_some_type!{ parser, some, details, :
foo => { /* ... */ }
bar => { /* ... */ }
baz => { /* ... */ }
} In this way, we should write a match_delim!{ parser.next() :
'=' => true,
'>' => true,
'<' => true,
_ => false,
}
let token = parser.next();
match token.kind():
Kind::Delim => match token.char() {
'=' => true,
'>' => true,
'<' => true,
_ => false,
},
_ => false,
} You can see I have
|
crates/hdx_parser/src/comparison.rs
Outdated
let next = parser.next(); | ||
Ok(match_delim! {parser.next() : | ||
'=' => Comparison::Equal, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like the formatting is all messed up here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting is okay here.
#[macro_export] | ||
macro_rules! match_delim { | ||
( $parser: ident.$method: ident($($args: tt)*): | ||
$( | ||
$pattern:pat $(if $guard:expr)? => $then: expr | ||
),+ | ||
$(,)? | ||
) => { | ||
match $parser.$method($($args)*).char() { | ||
$(Some($pattern) $( if $guard )? => $then,)+ | ||
} | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@keithamus: something like this? I'm noticing now that I don't have the .kind()
check in here now. I still need that right? Should I do the match here for a tuple like I was doing earlier and always make sure that .kind() == Kind::Delim
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally it should check .kind()
because .char()
returns values for the other single character tokens like RightParen
. In this particular case it's okay because we have a fallthrough to unexpected!
but we should refine this later.
Co-authored-by: Keith Cirkel <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
How does this look? Is this correct? Should I peek and store that in a variable for performance? Or readability? --------- Co-authored-by: Keith Cirkel <[email protected]>
How does this look? Is this correct? Should I peek and store that in a variable for performance? Or readability? --------- Co-authored-by: Keith Cirkel <[email protected]>
How does this look? Is this correct? Should I peek and store that in a variable for performance? Or readability? --------- Co-authored-by: Keith Cirkel <[email protected]>
How does this look? Is this correct? Should I peek and store that in a variable for performance? Or readability?