Skip to content
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

Merged
merged 6 commits into from
Jul 26, 2024

Conversation

koddsson
Copy link

How does this look? Is this correct? Should I peek and store that in a variable for performance? Or readability?

@koddsson koddsson marked this pull request as ready for review July 25, 2024 12:48
@koddsson
Copy link
Author

This feels like this might be wrong but just let me know.

@keithamus
Copy link
Owner

Want to try building a more complex macro using match arms? I’m sure we have something similar in the existing macros.

@koddsson
Copy link
Author

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.

@koddsson
Copy link
Author

koddsson commented Jul 25, 2024

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!()
} 

@keithamus
Copy link
Owner

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! macro which allows those two types. So I'd expect match_delim!(parser.next(), '-') to be the equivalent of matches!(parser.next(), Token::Delim('-')). But I'd expect the following two blocks to also be equivalent:

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 expect_ignore_case!, and match_ignore_case! (you can see this used in an if here and as a match block here). which represent this pattern. We have expect_delim!, so now we just need match_delim!.

match_ignore_case is defined here and it looks very complicated, but I think you could copy-paste this into a match_delim and tweak it. You won't need the Token::$tokenty bits as Delim is of course only one type.

Comment on lines 17 to 19
let next = parser.next();
Ok(match_delim! {parser.next() :
'=' => Comparison::Equal,
Copy link
Author

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?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting is okay here.

Comment on lines +286 to +298
#[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,)+
}
};
}
Copy link
Author

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?

Copy link
Owner

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]>
Copy link
Owner

@keithamus keithamus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@keithamus keithamus merged commit ad703d3 into keithamus:rewrite-lexer Jul 26, 2024
1 of 3 checks passed
@koddsson koddsson deleted the try-matching-delimiters branch July 27, 2024 17:47
keithamus added a commit that referenced this pull request Jul 29, 2024
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]>
keithamus added a commit that referenced this pull request Oct 23, 2024
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]>
keithamus added a commit that referenced this pull request Oct 27, 2024
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants