2020-01-04 22:55:04 -05:00
|
|
|
use std::iter::Peekable;
|
|
|
|
use std::str::Chars;
|
|
|
|
|
2020-03-29 13:28:17 -04:00
|
|
|
use crate::common::Pos;
|
|
|
|
use crate::Token;
|
2020-01-26 16:50:08 -05:00
|
|
|
|
2020-03-29 13:28:17 -04:00
|
|
|
pub const FORM_FEED: char = '\x0C';
|
2020-02-28 18:27:32 -05:00
|
|
|
|
2020-01-04 22:55:04 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2020-01-20 13:15:47 -05:00
|
|
|
pub(crate) struct Lexer<'a> {
|
2020-01-04 22:55:04 -05:00
|
|
|
buf: Peekable<Chars<'a>>,
|
|
|
|
pos: Pos,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for Lexer<'a> {
|
|
|
|
type Item = Token;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2020-03-29 13:28:17 -04:00
|
|
|
let kind = match self.buf.next()? {
|
|
|
|
'\n' | FORM_FEED => {
|
2020-01-07 18:37:28 -05:00
|
|
|
self.pos.newline();
|
2020-03-29 13:28:17 -04:00
|
|
|
'\n'
|
2020-01-07 18:37:28 -05:00
|
|
|
}
|
|
|
|
'\r' => {
|
2020-03-29 13:28:17 -04:00
|
|
|
if self.buf.peek() == Some(&'\n') {
|
|
|
|
self.buf.next();
|
|
|
|
'\n'
|
|
|
|
} else {
|
|
|
|
'\n'
|
|
|
|
}
|
2020-01-07 18:37:28 -05:00
|
|
|
}
|
2020-01-04 22:55:04 -05:00
|
|
|
'\0' => return None,
|
2020-03-29 13:28:17 -04:00
|
|
|
c => c,
|
2020-01-04 22:55:04 -05:00
|
|
|
};
|
|
|
|
self.pos.next_char();
|
|
|
|
Some(Token {
|
|
|
|
kind,
|
|
|
|
pos: self.pos,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Lexer<'a> {
|
|
|
|
pub fn new(buf: &'a str) -> Lexer<'a> {
|
|
|
|
Lexer {
|
|
|
|
buf: buf.chars().peekable(),
|
|
|
|
pos: Pos::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|