From bf2ea3f7f4dca5e521efc3dcef1c17bef62ad038 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 25 Jun 2022 22:56:58 +0200 Subject: [PATCH] pretty colors --- Cargo.lock | 1 + parser/Cargo.toml | 1 + parser/src/ast.rs | 43 +++++-------------- parser/src/lib.rs | 18 +++++++- parser/src/parser.rs | 22 +++++++--- ...er__tests__empty_function_with_params.snap | 6 +-- ...y_funky_attributes_no_params_function.snap | 5 +-- ...r__parser__tests__empty_void_function.snap | 2 +- ...__tests__global_variable_declarations.snap | 8 ++-- src/lib.rs | 0 10 files changed, 54 insertions(+), 52 deletions(-) delete mode 100644 src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 441a531..b5ee882 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -254,6 +254,7 @@ dependencies = [ name = "parser" version = "0.1.0" dependencies = [ + "bitflags", "dbg-pls", "insta", "peekmore", diff --git a/parser/Cargo.toml b/parser/Cargo.toml index becd3e5..0da5041 100644 --- a/parser/Cargo.toml +++ b/parser/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +bitflags = "1.3.2" dbg-pls = { version = "0.3.2", features = ["derive", "colors"] } peekmore = { version = "1.0.0", features = ["smallvec"] } diff --git a/parser/src/ast.rs b/parser/src/ast.rs index a003af4..76864e8 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -1,5 +1,6 @@ -use std::fmt::{Debug, Formatter}; +use std::fmt::Debug; +use bitflags::bitflags; use dbg_pls::DebugPls; use crate::Spanned; @@ -26,42 +27,20 @@ pub enum TypeSpecifier { pub type Ident = Spanned; -#[derive(Default)] -pub struct DeclAttr { - pub is_extern: bool, - pub is_static: bool, - pub is_thread_local: bool, -} - -impl Debug for DeclAttr { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - let mut d = f.debug_set(); - if self.is_extern { - d.entry(&"extern"); - } - if self.is_static { - d.entry(&"static"); - } - if self.is_thread_local { - d.entry(&"thread_local"); - } - d.finish() +bitflags! { + pub struct DeclAttr: u8 { + const EXTERN = 0b00000001; + const STATIC = 0b00000010; + const THREAD_LOCAL = 0b00000100; } } impl DebugPls for DeclAttr { fn fmt(&self, f: dbg_pls::Formatter<'_>) { - let mut d = f.debug_set(); - if self.is_extern { - d = d.entry(&"extern"); - } - if self.is_static { - d = d.entry(&"static"); - } - if self.is_thread_local { - d = d.entry(&"thread_local"); - } - d.finish(); + use std::fmt::Write; + let mut string = String::new(); + write!(string, "{:?}", self).unwrap(); + DebugPls::fmt(&string, f); } } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 44dd2a9..62284c2 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -3,6 +3,8 @@ use std::fmt::Debug; +use crate::token::Token; + mod ast; mod parser; mod pre; @@ -48,6 +50,18 @@ impl Debug for Span { } } -pub fn parse_file(src: &str) { - println!("{src}"); +fn lex_and_pre(src: &str) -> impl Iterator, Span)> + '_ { + let pre_tokens = pre::preprocess_tokens(src); + token::pre_tokens_to_tokens(pre_tokens) +} + +pub fn parse_file(src: &str) { + let lexer = lex_and_pre(src); + let declarations = parser::parse_declarations(lexer); + match declarations { + Ok(declarations) => { + dbg_pls::color!(declarations); + } + Err(err) => eprintln!("error :(\n{:#?}", err), + } } diff --git a/parser/src/parser.rs b/parser/src/parser.rs index e874d18..ef38ccd 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -11,7 +11,7 @@ use crate::{ }; #[derive(Debug)] -struct ParserError { +pub struct ParserError { span: Span, message: String, } @@ -214,7 +214,7 @@ where /// function-specifier declaration-specifiers.opt /// alignment-specifier declaration-specifiers.opt fn decl_specifiers(&mut self) -> Result> { - let mut decl_attr = DeclAttr::default(); + let mut decl_attr = DeclAttr::empty(); let &(_, initial_span) = self.peek_t()?; let (ty, span) = loop { match self.peek_t()?.0 { @@ -224,15 +224,15 @@ where } Tok::Kw(Kw::Extern) => { self.next_t()?; - decl_attr.is_extern = true; + decl_attr |= DeclAttr::EXTERN; } Tok::Kw(Kw::Static) => { self.next_t()?; - decl_attr.is_static = true; + decl_attr |= DeclAttr::STATIC; } Tok::Kw(Kw::ThreadLocal) => { self.next_t()?; - decl_attr.is_thread_local = true; + decl_attr |= DeclAttr::THREAD_LOCAL; } // (6.7.3) type-qualifier: Tok::Kw(Kw::Const | Kw::Restrict | Kw::Volatile | Kw::Atomic) => { @@ -406,5 +406,17 @@ where } } +pub fn parse_declarations<'src>( + src: impl Iterator, Span)>, +) -> Result>> { + use peekmore::PeekMore; + + let mut parser = Parser { + lex: src.peekmore(), + }; + + parser.external_declarations() +} + #[cfg(test)] mod tests; diff --git a/parser/src/parser/snapshots/parser__parser__tests__empty_function_with_params.snap b/parser/src/parser/snapshots/parser__parser__tests__empty_function_with_params.snap index c62ab7e..3476762 100644 --- a/parser/src/parser/snapshots/parser__parser__tests__empty_function_with_params.snap +++ b/parser/src/parser/snapshots/parser__parser__tests__empty_function_with_params.snap @@ -11,7 +11,7 @@ Ok( NormalDecl { decl_spec: DeclSpec { ty: Int, - attrs: {}, + attrs: (empty), }, init_declarators: [ ( @@ -27,7 +27,7 @@ Ok( decl_spec: ( DeclSpec { ty: Long, - attrs: {}, + attrs: (empty), }, 9..13, ), @@ -48,7 +48,7 @@ Ok( decl_spec: ( DeclSpec { ty: Int, - attrs: {}, + attrs: (empty), }, 19..22, ), diff --git a/parser/src/parser/snapshots/parser__parser__tests__empty_funky_attributes_no_params_function.snap b/parser/src/parser/snapshots/parser__parser__tests__empty_funky_attributes_no_params_function.snap index f413ca1..d5da061 100644 --- a/parser/src/parser/snapshots/parser__parser__tests__empty_funky_attributes_no_params_function.snap +++ b/parser/src/parser/snapshots/parser__parser__tests__empty_funky_attributes_no_params_function.snap @@ -11,10 +11,7 @@ Ok( NormalDecl { decl_spec: DeclSpec { ty: Int, - attrs: { - "extern", - "thread_local", - }, + attrs: EXTERN | THREAD_LOCAL, }, init_declarators: [ ( diff --git a/parser/src/parser/snapshots/parser__parser__tests__empty_void_function.snap b/parser/src/parser/snapshots/parser__parser__tests__empty_void_function.snap index d062102..d9c6c4a 100644 --- a/parser/src/parser/snapshots/parser__parser__tests__empty_void_function.snap +++ b/parser/src/parser/snapshots/parser__parser__tests__empty_void_function.snap @@ -11,7 +11,7 @@ Ok( NormalDecl { decl_spec: DeclSpec { ty: Void, - attrs: {}, + attrs: (empty), }, init_declarators: [ ( diff --git a/parser/src/parser/snapshots/parser__parser__tests__global_variable_declarations.snap b/parser/src/parser/snapshots/parser__parser__tests__global_variable_declarations.snap index d3c59e6..9365231 100644 --- a/parser/src/parser/snapshots/parser__parser__tests__global_variable_declarations.snap +++ b/parser/src/parser/snapshots/parser__parser__tests__global_variable_declarations.snap @@ -10,7 +10,7 @@ Ok( NormalDecl { decl_spec: DeclSpec { ty: Int, - attrs: {}, + attrs: (empty), }, init_declarators: [ ( @@ -40,9 +40,7 @@ Ok( NormalDecl { decl_spec: DeclSpec { ty: Double, - attrs: { - "thread_local", - }, + attrs: THREAD_LOCAL, }, init_declarators: [ ( @@ -87,7 +85,7 @@ Ok( NormalDecl { decl_spec: DeclSpec { ty: Int, - attrs: {}, + attrs: (empty), }, init_declarators: [ ( diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index e69de29..0000000