lex things

This commit is contained in:
nora 2022-06-21 13:15:26 +02:00
parent cc8eb57148
commit a5d063b944
7 changed files with 268 additions and 16 deletions

View file

@ -7,3 +7,6 @@ edition = "2021"
[dependencies]
peekmore = { version = "1.0.0", features = ["smallvec"] }
[dev-dependencies]
insta = "1.15.0"

View file

@ -1,3 +1,6 @@
#![allow(dead_code)]
mod lexer;
mod pre;
mod token;
pub type Span = std::ops::Range<usize>;

View file

@ -7,8 +7,9 @@ use std::ops::Not;
use peekmore::PeekMore;
type Span = std::ops::Range<usize>;
use crate::Span;
#[derive(Debug)]
pub enum PToken {
HeaderName(Vec<u8>),
Identifier(Vec<u8>),
@ -20,20 +21,7 @@ pub enum PToken {
Error,
}
pub enum Token {
Keyword(Keyword),
Identifier(),
Constant(),
StringLiteral(),
Punctuator(Punctuator),
}
pub enum Keyword {}
pub enum Constant {
Integer(i64),
}
#[derive(Debug)]
pub enum Punctuator {
/// [ <:
BracketOpen,
@ -363,3 +351,23 @@ pub fn preprocess_tokens(
};
lexer
}
#[cfg(test)]
mod tests {
fn lex_test(str: &str) {
let bytes = str.bytes().enumerate();
let tokens = super::preprocess_tokens(bytes);
let tokens = tokens.collect::<Vec<_>>();
insta::assert_debug_snapshot!(tokens);
}
#[test]
fn hello_world() {
let src = r#"\
int main() {
puts("Hello, World!");
}
"#;
lex_test(src);
}
}

3
parser/src/pre/mod.rs Normal file
View file

@ -0,0 +1,3 @@
mod lexer;
pub use lexer::{preprocess_tokens, PToken, Punctuator};

21
parser/src/token.rs Normal file
View file

@ -0,0 +1,21 @@
use crate::{
pre::{PToken, Punctuator},
Span,
};
pub enum Token {
Keyword(Keyword),
Identifier(),
Constant(),
StringLiteral(),
Punctuator(Punctuator),
Error,
}
pub struct Keyword;
fn from_pre_toks(
pre_toks: impl Iterator<Item = (PToken, Span)>,
) -> impl IntoIterator<Item = (Token, Span)> {
pre_toks.map(|token| todo!())
}