pretty colors

This commit is contained in:
nora 2022-06-25 22:56:58 +02:00
parent 79ab4bbb75
commit bf2ea3f7f4
10 changed files with 54 additions and 52 deletions

1
Cargo.lock generated
View file

@ -254,6 +254,7 @@ dependencies = [
name = "parser" name = "parser"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bitflags",
"dbg-pls", "dbg-pls",
"insta", "insta",
"peekmore", "peekmore",

View file

@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
bitflags = "1.3.2"
dbg-pls = { version = "0.3.2", features = ["derive", "colors"] } dbg-pls = { version = "0.3.2", features = ["derive", "colors"] }
peekmore = { version = "1.0.0", features = ["smallvec"] } peekmore = { version = "1.0.0", features = ["smallvec"] }

View file

@ -1,5 +1,6 @@
use std::fmt::{Debug, Formatter}; use std::fmt::Debug;
use bitflags::bitflags;
use dbg_pls::DebugPls; use dbg_pls::DebugPls;
use crate::Spanned; use crate::Spanned;
@ -26,42 +27,20 @@ pub enum TypeSpecifier {
pub type Ident = Spanned<String>; pub type Ident = Spanned<String>;
#[derive(Default)] bitflags! {
pub struct DeclAttr { pub struct DeclAttr: u8 {
pub is_extern: bool, const EXTERN = 0b00000001;
pub is_static: bool, const STATIC = 0b00000010;
pub is_thread_local: bool, const THREAD_LOCAL = 0b00000100;
}
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()
} }
} }
impl DebugPls for DeclAttr { impl DebugPls for DeclAttr {
fn fmt(&self, f: dbg_pls::Formatter<'_>) { fn fmt(&self, f: dbg_pls::Formatter<'_>) {
let mut d = f.debug_set(); use std::fmt::Write;
if self.is_extern { let mut string = String::new();
d = d.entry(&"extern"); write!(string, "{:?}", self).unwrap();
} DebugPls::fmt(&string, f);
if self.is_static {
d = d.entry(&"static");
}
if self.is_thread_local {
d = d.entry(&"thread_local");
}
d.finish();
} }
} }

View file

@ -3,6 +3,8 @@
use std::fmt::Debug; use std::fmt::Debug;
use crate::token::Token;
mod ast; mod ast;
mod parser; mod parser;
mod pre; mod pre;
@ -48,6 +50,18 @@ impl Debug for Span {
} }
} }
pub fn parse_file(src: &str) { fn lex_and_pre(src: &str) -> impl Iterator<Item = (Token<'_>, Span)> + '_ {
println!("{src}"); 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),
}
} }

View file

@ -11,7 +11,7 @@ use crate::{
}; };
#[derive(Debug)] #[derive(Debug)]
struct ParserError { pub struct ParserError {
span: Span, span: Span,
message: String, message: String,
} }
@ -214,7 +214,7 @@ where
/// function-specifier declaration-specifiers.opt /// function-specifier declaration-specifiers.opt
/// alignment-specifier declaration-specifiers.opt /// alignment-specifier declaration-specifiers.opt
fn decl_specifiers(&mut self) -> Result<Spanned<DeclSpec>> { fn decl_specifiers(&mut self) -> Result<Spanned<DeclSpec>> {
let mut decl_attr = DeclAttr::default(); let mut decl_attr = DeclAttr::empty();
let &(_, initial_span) = self.peek_t()?; let &(_, initial_span) = self.peek_t()?;
let (ty, span) = loop { let (ty, span) = loop {
match self.peek_t()?.0 { match self.peek_t()?.0 {
@ -224,15 +224,15 @@ where
} }
Tok::Kw(Kw::Extern) => { Tok::Kw(Kw::Extern) => {
self.next_t()?; self.next_t()?;
decl_attr.is_extern = true; decl_attr |= DeclAttr::EXTERN;
} }
Tok::Kw(Kw::Static) => { Tok::Kw(Kw::Static) => {
self.next_t()?; self.next_t()?;
decl_attr.is_static = true; decl_attr |= DeclAttr::STATIC;
} }
Tok::Kw(Kw::ThreadLocal) => { Tok::Kw(Kw::ThreadLocal) => {
self.next_t()?; self.next_t()?;
decl_attr.is_thread_local = true; decl_attr |= DeclAttr::THREAD_LOCAL;
} }
// (6.7.3) type-qualifier: // (6.7.3) type-qualifier:
Tok::Kw(Kw::Const | Kw::Restrict | Kw::Volatile | Kw::Atomic) => { Tok::Kw(Kw::Const | Kw::Restrict | Kw::Volatile | Kw::Atomic) => {
@ -406,5 +406,17 @@ where
} }
} }
pub fn parse_declarations<'src>(
src: impl Iterator<Item = (Tok<'src>, Span)>,
) -> Result<Vec<Spanned<ExternalDecl>>> {
use peekmore::PeekMore;
let mut parser = Parser {
lex: src.peekmore(),
};
parser.external_declarations()
}
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;

View file

@ -11,7 +11,7 @@ Ok(
NormalDecl { NormalDecl {
decl_spec: DeclSpec { decl_spec: DeclSpec {
ty: Int, ty: Int,
attrs: {}, attrs: (empty),
}, },
init_declarators: [ init_declarators: [
( (
@ -27,7 +27,7 @@ Ok(
decl_spec: ( decl_spec: (
DeclSpec { DeclSpec {
ty: Long, ty: Long,
attrs: {}, attrs: (empty),
}, },
9..13, 9..13,
), ),
@ -48,7 +48,7 @@ Ok(
decl_spec: ( decl_spec: (
DeclSpec { DeclSpec {
ty: Int, ty: Int,
attrs: {}, attrs: (empty),
}, },
19..22, 19..22,
), ),

View file

@ -11,10 +11,7 @@ Ok(
NormalDecl { NormalDecl {
decl_spec: DeclSpec { decl_spec: DeclSpec {
ty: Int, ty: Int,
attrs: { attrs: EXTERN | THREAD_LOCAL,
"extern",
"thread_local",
},
}, },
init_declarators: [ init_declarators: [
( (

View file

@ -11,7 +11,7 @@ Ok(
NormalDecl { NormalDecl {
decl_spec: DeclSpec { decl_spec: DeclSpec {
ty: Void, ty: Void,
attrs: {}, attrs: (empty),
}, },
init_declarators: [ init_declarators: [
( (

View file

@ -10,7 +10,7 @@ Ok(
NormalDecl { NormalDecl {
decl_spec: DeclSpec { decl_spec: DeclSpec {
ty: Int, ty: Int,
attrs: {}, attrs: (empty),
}, },
init_declarators: [ init_declarators: [
( (
@ -40,9 +40,7 @@ Ok(
NormalDecl { NormalDecl {
decl_spec: DeclSpec { decl_spec: DeclSpec {
ty: Double, ty: Double,
attrs: { attrs: THREAD_LOCAL,
"thread_local",
},
}, },
init_declarators: [ init_declarators: [
( (
@ -87,7 +85,7 @@ Ok(
NormalDecl { NormalDecl {
decl_spec: DeclSpec { decl_spec: DeclSpec {
ty: Int, ty: Int,
attrs: {}, attrs: (empty),
}, },
init_declarators: [ init_declarators: [
( (

View file