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

[Feature Request] Bump Line/Terminator? #337

Open
juliusl opened this issue Sep 8, 2023 · 0 comments
Open

[Feature Request] Bump Line/Terminator? #337

juliusl opened this issue Sep 8, 2023 · 0 comments

Comments

@juliusl
Copy link

juliusl commented Sep 8, 2023

It would be useful if there was a built-in bump_line() fn that would bump the lexer to the next line. A more generic bump_terminator(pat: P) could be useful too. I know there are several ways to achieve this behavior, but I feel like it would be a nice ergonomic fn to have.

EDIT: Proof of Concept

pub trait LexerExtensions {
    fn bump_terminator(&mut self, pat: &[char]);
        
    //pub fn bump_terminator(&mut self, pat: P);
    fn bump_line(&mut self);
}

impl<'a, T> LexerExtensions for logos::Lexer<'a, T> 
where
    T: logos::Logos<'a, Source = str>
{
    fn bump_line(&mut self) {
        if let Some(next) = self.remainder().lines().next() {
            self.bump(next.len() + 1);
        }
    }

    fn bump_terminator(&mut self, pat: &[char]) {
        if let Some(next) = self.remainder().split_terminator(pat).next() {
            self.bump(next.len() + 1);
        }
    }
}

#[test]
fn test_lexer_extensions() {
    use logos::Logos;

    #[derive(Logos)]
    enum Test {
        #[token("A")]
        A,
        #[token("B")]
        B,
        #[token("C")]
        C
    }

    let mut lex = Test::lexer(r"
    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. 
    It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software 
    like Aldus PageMaker including versions of Lorem Ipsum.
    ");

    lex.bump_line();
    assert_eq!(lex.remainder(), r"    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. 
    It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software 
    like Aldus PageMaker including versions of Lorem Ipsum.
    ");

    lex.bump_terminator(&[',']);
    assert_eq!(lex.remainder(), r" when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. 
    It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software 
    like Aldus PageMaker including versions of Lorem Ipsum.
    ");
}
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

No branches or pull requests

1 participant