From 2ce2edfc647f9cc34d661c25d2114d791b296429 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Tue, 21 Jun 2022 22:09:02 +0200 Subject: [PATCH] more parsing --- parser/src/ast.rs | 8 ++++++ parser/src/lib.rs | 4 +++ parser/src/parser.rs | 59 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index bc7db38..2edee35 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -36,3 +36,11 @@ pub struct DeclSpec { pub ty: TypeSpecifier, pub attrs: DeclAttr, } + +#[derive(Debug, DebugPls)] +pub struct FunctionDefinition { + pub decl_spec: Spanned, + pub declarator: Spanned, + pub declaration_list: Spanned>, + pub body: Vec<()>, +} \ No newline at end of file diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 8d55b85..0837772 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -22,6 +22,10 @@ impl Span { } } +impl dbg_pls::DebugPls for Span { + fn fmt(&self, f: dbg_pls::Formatter<'_>) {} +} + pub fn parse_file(src: &str) { println!("{src}"); } diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 0ca4b01..bdddd32 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -1,10 +1,11 @@ use peekmore::PeekMoreIterator; use crate::{ - ast::{DeclAttr, DeclSpec, Spanned, TypeSpecifier}, + ast::{DeclAttr, DeclSpec, FunctionDefinition, Spanned, TypeSpecifier}, token::{Keyword as Kw, Token as Tok}, Span, }; +use crate::pre::Punctuator; struct ParserError { span: Span, @@ -34,6 +35,20 @@ where lex: PeekMoreIterator, } +macro_rules! expect { + ($self:ident, $pat:pat) => { + match $self.next_t()? { + ($pat, span) => span, + (token, span) => { + return Err(ParserError::new( + span, + format!(concat!("expected `", stringify!($pat), "`, found {}"), token), + )) + } + } + }; +} + impl<'src, I> Parser<'src, I> where I: Iterator, Span)>, @@ -50,6 +65,16 @@ where self.lex.peek().ok_or_else(ParserError::eof) } + fn ident(&mut self) -> Result> { + match self.next_t()? { + (Tok::Identifier(ident), span) => Ok((ident.to_string(), span)), + (tok, span) => Err(ParserError::new( + span, + format!("expected identifier, found `{tok}`"), + )), + } + } + // ----------------------- // Declarations // ----------------------- @@ -142,11 +167,16 @@ where Ok((ty, span)) } + /// (6.7.6) declarator: + /// pointer.opt direct-declarator + fn declarator(&mut self) -> Result> { + todo!() + } + // ----------------------- - // External definitions + // External definitions // ----------------------- - /// (6.9) external-declaration: /// function-definition /// declaration @@ -167,7 +197,26 @@ where /// (6.9.1) function-definition: /// declaration-specifiers declarator declaration-list.opt compound-statement - fn function_definition(&mut self, specifiers: Spanned) -> Result> { - todo!() + fn function_definition( + &mut self, + decl_spec: Spanned, + ) -> Result> { + let declarator = self.ident()?; + + let decl_spec_span = decl_spec.1; + + let def = FunctionDefinition { + decl_spec, + declarator, + declaration_list: (Vec::new(), Span::default()), + body: Vec::new(), + }; + + expect!(self, Tok::Punctuator(Punctuator::ParenOpen)); + expect!(self, Tok::Punctuator(Punctuator::ParenClose)); + expect!(self, Tok::Punctuator(Punctuator::BraceOpen)); + let last = expect!(self, Tok::Punctuator(Punctuator::BraceClose)); + + Ok((def, decl_spec_span.extend(last))) } }