From 2c4dbe0971c9e562960a1fee7f440ef316e189da Mon Sep 17 00:00:00 2001 From: Robert Date: Sun, 23 Apr 2023 14:58:07 +0200 Subject: [PATCH] start work on lexer --- .gitignore | 2 ++ Cargo.lock | 7 ++++++ Cargo.toml | 8 ++++++ src/lexer.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 11 ++++++++ src/parser.rs | 35 ++++++++++++++++++++++++++ 6 files changed, 132 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/lexer.rs create mode 100644 src/main.rs create mode 100644 src/parser.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..64ee209 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target/ +.vscode/ \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..fefa038 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..94b03d3 --- /dev/null +++ b/Cargo.toml @@ -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] diff --git a/src/lexer.rs b/src/lexer.rs new file mode 100644 index 0000000..e4e39aa --- /dev/null +++ b/src/lexer.rs @@ -0,0 +1,69 @@ +use std::iter::Peekable; + +#[derive(Debug)] +pub enum Token { + Character(char), + CharacterRange(char, char) +} + +#[derive(Debug)] +pub struct Lexer +where + I: Iterator +{ + scanner: Peekable +} + +impl Lexer +where + I: Iterator +{ + pub fn new(scanner: I) -> Lexer { + Lexer { + scanner: scanner.peekable() + } + } + + fn handle_character(&mut self) -> Option { + 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 Iterator for Lexer +where + I: Iterator +{ + type Item = Token; + + fn next(&mut self) -> Option { + 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() + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f227f7a --- /dev/null +++ b/src/main.rs @@ -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}") + }; + + +} diff --git a/src/parser.rs b/src/parser.rs new file mode 100644 index 0000000..1568f0a --- /dev/null +++ b/src/parser.rs @@ -0,0 +1,35 @@ +use std::iter::Peekable; + +use crate::lexer::Lexer; + +#[derive(Debug)] +pub struct Parser +where + I: Iterator +{ + lexer: Peekable> +} + +impl Parser +where + I: Iterator +{ + fn new(scanner: I) -> Parser { + 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(()) +} \ No newline at end of file