diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index 88b5c9c..3f7a7f6 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -200,7 +200,6 @@ impl<'de> Iterator for Lexer<'de> { "dec" => Ok((TokenKind::DecimalType, TokenValue::None)), "str" => Ok((TokenKind::StringType, TokenValue::None)), "char" => Ok((TokenKind::CharacterType, TokenValue::None)), - "()" => Ok((TokenKind::UnitType, TokenValue::None)), ident => Ok(( TokenKind::Identifier, TokenValue::Identifier(ident.to_string().into()), diff --git a/src/lexer/token.rs b/src/lexer/token.rs index 970ccca..fe6bbc3 100644 --- a/src/lexer/token.rs +++ b/src/lexer/token.rs @@ -176,8 +176,6 @@ pub enum TokenKind { StringType, /// The character type; `char`. CharacterType, - /// The unit type; `()`. - UnitType, } impl Display for TokenKind { @@ -239,7 +237,6 @@ impl Display for TokenKind { TokenKind::DecimalType => write!(f, "a decimal type"), TokenKind::StringType => write!(f, "a string type"), TokenKind::CharacterType => write!(f, "a character type"), - TokenKind::UnitType => write!(f, "a unit type"), } } } diff --git a/src/main.rs b/src/main.rs index c36b56e..944343c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,14 @@ use lexer::{Lexer, TokenKind}; use owo_colors::{Style, Styled}; use parser::Parser; +use passer::Passer; use std::vec; pub mod lexer; pub mod parser; +pub mod passer; fn main() { - let input = " - fn main() { - fib(9999); - } - - fn fib(n ~ int) ~ int { - return if n < 2; - fib(n - 1) + fib(n - 20) - } - "; - miette::set_hook(Box::new(|_| { Box::new( miette::MietteHandlerOpts::new() @@ -30,6 +21,17 @@ fn main() { })) .unwrap(); + let input = " + fn main() { + fib(9999); + } + + fn fib(n ~ int) ~ int { + return n if n < 2; + fib(n - 1) + fib(n - 20) + } + "; + let mut parser = Parser::new(input); let symbol = match parser.parse() { Ok(symbol) => symbol, @@ -39,7 +41,15 @@ fn main() { } }; - println!("{:?}", symbol); + let typing_pass = passer::typing::TypingPasser::pass(&symbol).unwrap(); + + for note in typing_pass.non_critical { + println!("{:?}", note.with_source_code(input.to_string())); + } + + for note in typing_pass.critical { + println!("{:?}", note.with_source_code(input.to_string())); + } } struct SomHighlighter {} @@ -80,8 +90,9 @@ impl miette::highlighters::HighlighterState for SomHighlighterState { | TokenKind::DecimalType | TokenKind::BooleanType | TokenKind::StringType - | TokenKind::CharacterType - | TokenKind::UnitType => Style::new().fg_rgb::<86, 156, 214>().italic(), + | TokenKind::CharacterType => { + Style::new().fg_rgb::<86, 156, 214>().italic() + } TokenKind::Equal | TokenKind::LessThan | TokenKind::GreaterThan diff --git a/src/parser/lookup.rs b/src/parser/lookup.rs index e214595..3977343 100644 --- a/src/parser/lookup.rs +++ b/src/parser/lookup.rs @@ -196,7 +196,7 @@ impl Default for Lookup<'_> { .add_statement_handler(TokenKind::Function, statement::function_) .add_statement_handler(TokenKind::Trait, statement::trait_) .add_type_handler(TokenKind::Identifier, typing::identifier) - .add_type_handler(TokenKind::UnitType, typing::unit) + .add_type_handler(TokenKind::ParenOpen, typing::unit) .add_type_handler(TokenKind::CharacterType, typing::character) .add_type_handler(TokenKind::BooleanType, typing::boolean) .add_type_handler(TokenKind::IntegerType, typing::integer) diff --git a/src/parser/typing.rs b/src/parser/typing.rs index 10e673b..7c44fa9 100644 --- a/src/parser/typing.rs +++ b/src/parser/typing.rs @@ -63,7 +63,11 @@ pub fn parse<'de>(parser: &mut Parser<'de>, binding_power: BindingPower) -> Resu pub fn unit<'de>(parser: &mut Parser<'de>) -> Result> { parser .lexer - .expect(TokenKind::UnitType, "expected an unit type")?; + .expect(TokenKind::ParenOpen, "expected an opening parenthesis")?; + + parser + .lexer + .expect(TokenKind::ParenClose, "expected a closing parenthesis")?; Ok(Type::Unit) } diff --git a/src/passer/mod.rs b/src/passer/mod.rs new file mode 100644 index 0000000..5626037 --- /dev/null +++ b/src/passer/mod.rs @@ -0,0 +1,13 @@ +use crate::parser::ast::Symbol; +use miette::{Report, Result}; + +pub mod typing; + +pub trait Passer { + fn pass(ast: &Symbol<'_>) -> Result; +} + +pub struct PasserResult { + pub non_critical: Vec, + pub critical: Vec, +} diff --git a/src/passer/typing/mod.rs b/src/passer/typing/mod.rs new file mode 100644 index 0000000..e126b41 --- /dev/null +++ b/src/passer/typing/mod.rs @@ -0,0 +1,23 @@ +use super::{Passer, PasserResult}; +use crate::parser::ast::Symbol; +use miette::{Result, Severity}; + +pub struct TypingPasser; + +impl Passer for TypingPasser { + fn pass(ast: &Symbol<'_>) -> Result { + let mut critical = vec![]; + let mut non_critical = vec![]; + + non_critical.push(miette::miette! { + severity = Severity::Warning, + labels = vec![miette::LabeledSpan::at(100, "uwu")], + "Typing is not yet implemented" + }); + + Ok(PasserResult { + critical, + non_critical, + }) + } +}