rust bing bong

This commit is contained in:
SimonFJ20 2023-01-18 20:43:54 +01:00
parent cfdfc42918
commit 91b2f4b43f
5 changed files with 116 additions and 5 deletions

View File

@ -103,15 +103,25 @@ MultiLineWhitespace -> [ \t\n\r\f\v]+
SingleLineComment -> \/\/[^\n]+ SingleLineComment -> \/\/[^\n]+
MultiLineComment -> \/\*.*?\*\/ MultiLineComment -> \/\*.*?\*\/
Name -> [a-zA-Z_][a-zA-Z0-9_]*
Id -> #[a-zA-Z0-9_]+
Class \.[a-zA-Z0-9_]+
Int -> [0-9]+
Float -> [0-9]+\.[0-9]* Float -> [0-9]+\.[0-9]*
Int -> [0-9]+
String -> "<sequence of optionally escaped characters \ String -> "<sequence of optionally escaped characters \
and escaped special character placeholders \ and escaped special character placeholders \
terminated by an unescaped '"'> terminated by an unescaped '"'>
Null -> "null" Null -> "null"
False -> "false" False -> "false"
True -> "true" True -> "true"
Name -> [a-zA-Z_][a-zA-Z0-9_]*
Id -> #[a-zA-Z0-9_]+
Class \.[a-zA-Z0-9_]+
LBrace -> "("
RBrace -> ")"
LBracket -> "["
RBracket -> "]"
Equal -> "="
Colon -> ":"
SemiColon -> ";"
Comma -> ","

68
src/bong/lexer.rs Normal file
View File

@ -0,0 +1,68 @@
use std::{iter::Peekable, str::Chars};
pub enum TokenType {
SinglelineWhitespace,
MultilineWhitespace,
SinglelineComment,
MultilineComment,
Name,
Id,
Class,
Int,
Float,
String,
Null,
False,
True,
LBrace,
RBrace,
LBracket,
RBracket,
Equal,
Colon,
SemiColon,
Comma,
}
pub struct Token<'a> {
token_type: TokenType,
value: &'a str,
line: u32,
col: u32,
}
pub struct LexerError {
line: u32,
col: u32,
message: String,
}
impl LexerError {
pub fn new(line: u32, col: u32, message: String) -> Self {
Self { line, col, message }
}
}
pub struct Lexer<'a> {
phantom: std::marker::PhantomData<&'a u32>,
}
impl<'a> Lexer<'a> {
pub fn new(text: &'a str) -> Self {
Self {
phantom: std::marker::PhantomData {},
}
}
}
impl<'a> Iterator for Lexer<'a> {
type Item = Result<Token<'a>, String>;
fn next(&mut self) -> Option<Self::Item> {
todo!()
}
}

2
src/bong/mod.rs Normal file
View File

@ -0,0 +1,2 @@
mod lexer;
mod parser;

29
src/bong/parser.rs Normal file
View File

@ -0,0 +1,29 @@
use std::iter::Map;
use crate::bong::lexer::Lexer;
pub enum Node {
Element {
name: String,
ids: Vec<String>,
classes: Vec<String>,
properties: Map<String, Box<Node>>,
values: Vec<Node>,
},
Object(Map<String, Box<Node>>),
Array(Vec<Node>),
Int(i64),
Float(f64),
String(String),
Bool(bool),
}
pub struct Parser<'a> {
lexer: Lexer<'a>,
}
impl<'a> Parser<'a> {
pub fn new(lexer: Lexer<'a>) -> Self {
Self { lexer }
}
}

View File

@ -1,3 +1,5 @@
mod bong;
fn main() { fn main() {
println!("Hello, world!"); println!("Hello, world!");
} }