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

View file

@ -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<String>;
#[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);
}
}

View file

@ -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<Item = (Token<'_>, 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),
}
}

View file

@ -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<Spanned<DeclSpec>> {
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<Item = (Tok<'src>, Span)>,
) -> Result<Vec<Spanned<ExternalDecl>>> {
use peekmore::PeekMore;
let mut parser = Parser {
lex: src.peekmore(),
};
parser.external_declarations()
}
#[cfg(test)]
mod tests;

View file

@ -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,
),

View file

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

View file

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

View file

@ -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: [
(