summaryrefslogtreecommitdiff
path: root/support/texlab/crates/parser/src/util.rs
blob: cb147c493b839ed617871098f4025f838897c5c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::ops::{Index, Range};

use logos::{Logos, Source};

pub fn lex_command_name<'a, T>(lexer: &mut logos::Lexer<'a, T>) -> &'a str
where
    T: Logos<'a>,
    T::Source: Index<Range<usize>, Output = str>,
{
    let start = lexer.span().end;
    let input = &lexer.source()[start..lexer.source().len()];

    let mut chars = input.chars().peekable();
    let Some(c) = chars.next() else {
        return "";
    };

    if c.is_whitespace() {
        return "";
    }

    lexer.bump(c.len_utf8());

    if c.is_alphanumeric() || c == '@' {
        while let Some(c) = chars.next() {
            match c {
                '*' => {
                    lexer.bump(c.len_utf8());
                    break;
                }
                c if c.is_alphanumeric() => {
                    lexer.bump(c.len_utf8());
                }
                '_' => {
                    if !matches!(chars.peek(), Some(c) if c.is_alphanumeric()) {
                        break;
                    }

                    lexer.bump(c.len_utf8());
                }
                '@' | ':' => {
                    lexer.bump(c.len_utf8());
                }
                _ => {
                    break;
                }
            }
        }
    }

    &lexer.source()[start..lexer.span().end]
}