start work on lexer
This commit is contained in:
commit
2c4dbe0971
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
target/
|
||||
.vscode/
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "regex-engine"
|
||||
version = "0.1.0"
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "regex-engine"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
69
src/lexer.rs
Normal file
69
src/lexer.rs
Normal file
|
@ -0,0 +1,69 @@
|
|||
use std::iter::Peekable;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Token {
|
||||
Character(char),
|
||||
CharacterRange(char, char)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Lexer<I>
|
||||
where
|
||||
I: Iterator<Item = char>
|
||||
{
|
||||
scanner: Peekable<I>
|
||||
}
|
||||
|
||||
impl<I> Lexer<I>
|
||||
where
|
||||
I: Iterator<Item = char>
|
||||
{
|
||||
pub fn new(scanner: I) -> Lexer<I> {
|
||||
Lexer {
|
||||
scanner: scanner.peekable()
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_character(&mut self) -> Option<Token> {
|
||||
let character = self.scanner.next()?;
|
||||
|
||||
if let Some(dash) = self.scanner.peek() {
|
||||
if *dash == '-' {
|
||||
return Some(Token::CharacterRange(character, self.scanner.nth(1)?));
|
||||
}
|
||||
}
|
||||
|
||||
Some(Token::Character(character))
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for Lexer<I>
|
||||
where
|
||||
I: Iterator<Item = char>
|
||||
{
|
||||
type Item = Token;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let symbol = self.scanner.peek()?;
|
||||
|
||||
match symbol {
|
||||
'-' => todo!(),
|
||||
'^' => todo!(),
|
||||
'$' => todo!(),
|
||||
'\\' => todo!(),
|
||||
'.' => todo!(),
|
||||
'*' => todo!(),
|
||||
'+' => todo!(),
|
||||
'?' => todo!(),
|
||||
'(' => todo!(),
|
||||
')' => todo!(),
|
||||
'[' => todo!(),
|
||||
']' => todo!(),
|
||||
'{' => todo!(),
|
||||
'}' => todo!(),
|
||||
'|' => todo!(),
|
||||
|
||||
_ => self.handle_character()
|
||||
}
|
||||
}
|
||||
}
|
11
src/main.rs
Normal file
11
src/main.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
mod parser;
|
||||
mod lexer;
|
||||
|
||||
fn main() {
|
||||
let output = match parser::parse_string("Hello A-Ztesting!") {
|
||||
Ok(val) => val,
|
||||
Err(e) => panic!("{e}")
|
||||
};
|
||||
|
||||
|
||||
}
|
35
src/parser.rs
Normal file
35
src/parser.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
use std::iter::Peekable;
|
||||
|
||||
use crate::lexer::Lexer;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Parser<I>
|
||||
where
|
||||
I: Iterator<Item = char>
|
||||
{
|
||||
lexer: Peekable<Lexer<I>>
|
||||
}
|
||||
|
||||
impl<I> Parser<I>
|
||||
where
|
||||
I: Iterator<Item = char>
|
||||
{
|
||||
fn new(scanner: I) -> Parser<I> {
|
||||
Parser {
|
||||
lexer: Lexer::new(scanner).peekable()
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(self) -> Result<(), String> {
|
||||
self.lexer.for_each(|token| println!("{token:?}",));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_string(input: &str) -> Result<(), String> {
|
||||
let scanner = input.chars();
|
||||
|
||||
Parser::new(scanner).parse()?;
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in a new issue