1use core::ops::Range;2use std::convert::TryFrom;34use logos::Logos;5use rowan::{TextRange, TextSize};67use crate::SyntaxKind;89pub struct Lexer<'a> {10 inner: logos::Lexer<'a, SyntaxKind>,11}1213impl<'a> Lexer<'a> {14 pub fn new(input: &'a str) -> Self {15 Self {16 inner: SyntaxKind::lexer(input),17 }18 }19}2021impl<'a> Iterator for Lexer<'a> {22 type Item = Lexeme<'a>;2324 fn next(&mut self) -> Option<Self::Item> {25 let kind = self.inner.next()?;26 let text = self.inner.slice();2728 Some(Self::Item {29 kind,30 text,31 range: {32 let Range { start, end } = self.inner.span();3334 TextRange::new(35 TextSize::try_from(start).unwrap(),36 TextSize::try_from(end).unwrap(),37 )38 },39 })40 }41}4243#[derive(Clone, Copy, Debug)]44pub struct Lexeme<'i> {45 pub kind: SyntaxKind,46 pub text: &'i str,47 pub range: TextRange,48}4950pub fn lex(input: &str) -> Vec<Lexeme<'_>> {51 Lexer::new(input).collect()52}