logos icon indicating copy to clipboard operation
logos copied to clipboard

[Feature Request] Bump Line/Terminator?

Open juliusl opened this issue 9 months ago • 0 comments

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.
    ");
}

juliusl avatar Sep 08 '23 22:09 juliusl