lexer: parse comments

This commit is contained in:
Michael Sippel 2024-05-15 23:51:20 +02:00
parent 4b879a5516
commit 9ad58620d6
Signed by: senvas
GPG key ID: F96CF119C34B64A6
2 changed files with 58 additions and 2 deletions

View file

@ -1,5 +1,6 @@
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum LTIRToken {
Comment(String),
Symbol(String),
Char(char),
Num(i64),
@ -29,6 +30,7 @@ pub enum LexError {
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum LexerState {
Any,
Comment(String),
TypeTerm(String),
Sym(String),
Num(i64),
@ -39,6 +41,7 @@ impl LexerState {
fn into_token(self) -> Option<LTIRToken> {
match self {
LexerState::Any => None,
LexerState::Comment(s) => Some(LTIRToken::Comment(s)),
LexerState::TypeTerm(s) => Some(LTIRToken::AssignType(s)),
LexerState::Sym(s) => Some(LTIRToken::Symbol(s)),
LexerState::Num(n) => Some(LTIRToken::Num(n)),
@ -169,6 +172,22 @@ where
region.end += 1;
state = LexerState::Char(None);
}
'/' => {
self.chars.next();
self.position += 1;
region.end += 1;
match self.chars.next() {
Some('*') => {
self.position += 1;
region.end += 1;
state = LexerState::Comment(String::new());
}
_ => {
return Some((region, Err(LexError::InvalidChar)));
}
}
}
c => {
if c.is_whitespace() {
self.chars.next();
@ -183,6 +202,38 @@ where
}
},
LexerState::Comment(s) => {
match self.chars.next() {
Some('*') => {
match self.chars.peek() {
Some('/') => {
self.chars.next();
self.position += 2;
region.end += 2;
if let Some(token) = state.clone().into_token() {
return Some((region, Ok(token)));
}
}
_ => {
s.push('*');
self.position += 1;
region.end += 1;
}
}
}
Some(c) => {
s.push(c);
self.position += 1;
region.end += 1;
}
None => {
return Some((region, Err(LexError::InvalidChar)));
}
}
}
LexerState::Char(val) => {
self.position += 2;
region.end += 2;
@ -288,6 +339,7 @@ mod tests {
fn test_lexer() {
let mut lexer = crate::lexer::LTIRLexer::from(
"let var1:=123;
/* comment */
let square =λx.* x x;
let sqrt = λx:~machine::Float64~machine::Word.(f64-sqrt x);

View file

@ -26,7 +26,12 @@ fn compile(
.compile(
&parser::parse_expr(
&scope.read().unwrap().typectx,
&mut lexer::LTIRLexer::from(source.chars().peekable()).peekable(),
&mut lexer::LTIRLexer::from(source.chars().peekable())
.filter(|tok| match tok {
(_, Ok(lexer::LTIRToken::Comment(_))) => false,
_ => true
})
.peekable(),
)
.expect("syntax error"),
)
@ -35,7 +40,6 @@ fn compile(
/* TODO:
* - Parse Comments
* - write to address resulting from expression
* - `::` -> '.' and allow only
* - Parser error reporting