Skip to content

Commit 283f1ef

Browse files
committed
Finished simple support for parsing C function definitions
1 parent 378fe1b commit 283f1ef

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

Diff for: src/c/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl<'a> Parser<'a> {
322322

323323
if self.eat_punctuator(Punctuator::Arrow) {
324324
let field = self.eat_identifier().ok_or_else(|| {
325-
ParseErrorKind::Misc("Expected field name after '.'").at(source)
325+
ParseErrorKind::Misc("Expected field name after '->'").at(source)
326326
})?;
327327

328328
base = ExprKind::Field(Box::new(Field {

Diff for: src/c/parser/mod.rs

+53-18
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,12 @@ pub struct CommonDeclaration {
504504
}
505505

506506
#[derive(Clone, Debug)]
507-
pub struct FunctionDefinition {}
507+
pub struct FunctionDefinition {
508+
pub attributes: Vec<Attribute>,
509+
pub declaration_specifiers: DeclarationSpecifiers,
510+
pub declarator: Declarator,
511+
pub body: CompoundStatement,
512+
}
508513

509514
#[derive(Clone, Debug)]
510515
pub struct ConstExpr {
@@ -639,10 +644,10 @@ impl<'a> Parser<'a> {
639644
}
640645
}
641646
}
642-
Declaration::StaticAssert(_) => todo!(),
643-
Declaration::Attribute(_) => todo!(),
647+
Declaration::StaticAssert(_) => todo!("c static assert"),
648+
Declaration::Attribute(_) => todo!("c attribute declaration"),
644649
},
645-
ExternalDeclaration::FunctionDefinition(_) => todo!(),
650+
ExternalDeclaration::FunctionDefinition(_) => todo!("define c function"),
646651
}
647652
}
648653

@@ -658,11 +663,17 @@ impl<'a> Parser<'a> {
658663
}
659664

660665
fn parse_function_definition(&mut self) -> Result<FunctionDefinition, ParseError> {
661-
self.parse_attribute_specifier_sequence()?;
662-
self.parse_declaration_specifiers()?;
663-
self.parse_declarator()?;
664-
self.parse_function_body()?;
665-
Ok(todo!("parse_function_definition"))
666+
let attributes = self.parse_attribute_specifier_sequence()?;
667+
let declaration_specifiers = self.parse_declaration_specifiers()?;
668+
let declarator = self.parse_declarator()?;
669+
let body = self.parse_function_body()?;
670+
671+
Ok(FunctionDefinition {
672+
attributes,
673+
declaration_specifiers,
674+
declarator,
675+
body,
676+
})
666677
}
667678

668679
fn parse_attribute_specifier_sequence(&mut self) -> Result<Vec<Attribute>, ParseError> {
@@ -1469,7 +1480,7 @@ impl<'a> Parser<'a> {
14691480

14701481
let attribute_specifiers = self.parse_attribute_specifier_sequence()?;
14711482

1472-
if self.eat_punctuator(Punctuator::Semicolon) {
1483+
if !attribute_specifiers.is_empty() && self.eat_punctuator(Punctuator::Semicolon) {
14731484
// attribute-declaration
14741485
todo!("parse attribute declaration");
14751486
return Ok(todo!());
@@ -1629,9 +1640,8 @@ impl<'a> Parser<'a> {
16291640
Ok(Designation { path })
16301641
}
16311642

1632-
fn parse_function_body(&mut self) -> Result<(), ParseError> {
1633-
let _statements = self.parse_compound_statement()?;
1634-
todo!("parse_function_body");
1643+
fn parse_function_body(&mut self) -> Result<CompoundStatement, ParseError> {
1644+
self.parse_compound_statement()
16351645
}
16361646

16371647
fn parse_compound_statement(&mut self) -> Result<CompoundStatement, ParseError> {
@@ -1649,17 +1659,19 @@ impl<'a> Parser<'a> {
16491659
break;
16501660
}
16511661

1652-
if let Ok(declaration) = self.parse_declaration() {
1662+
if let Ok(declaration) = speculate!(self.input, self.parse_declaration()) {
16531663
statements.push(declaration.into());
16541664
continue;
16551665
}
16561666

1657-
if let Ok(unlabeled_statement) = self.parse_unlabeled_statement() {
1667+
if let Ok(unlabeled_statement) =
1668+
speculate!(self.input, self.parse_unlabeled_statement())
1669+
{
16581670
statements.push(unlabeled_statement.into());
16591671
continue;
16601672
}
16611673

1662-
if let Ok(label) = self.parse_label() {
1674+
if let Ok(label) = speculate!(self.input, self.parse_label()) {
16631675
statements.push(label.into());
16641676
continue;
16651677
}
@@ -1674,11 +1686,34 @@ impl<'a> Parser<'a> {
16741686
}
16751687

16761688
fn parse_unlabeled_statement(&mut self) -> Result<UnlabeledStatement, ParseError> {
1677-
if let Ok(expr_statement) = self.parse_expr_statement() {
1689+
if let Ok(expr_statement) = speculate!(self.input, self.parse_expr_statement()) {
16781690
return Ok(expr_statement.into());
16791691
}
16801692

1681-
todo!("parse_unlabeled_statement")
1693+
let _attribute_specifier_sequence = self.parse_attribute_specifier_sequence()?;
1694+
1695+
if let Ok(_primary_block) = speculate!(self.input, self.parse_primary_block()) {
1696+
return todo!("handle parsed primary block");
1697+
}
1698+
1699+
if let Ok(_primary_block) = speculate!(self.input, self.parse_jump_block()) {
1700+
return todo!("handle parsed jump block");
1701+
}
1702+
1703+
todo!("parse_unlabeled_statement");
1704+
1705+
return Err(ParseError::message(
1706+
"Expected unlabeled statement",
1707+
self.input.peek().source,
1708+
));
1709+
}
1710+
1711+
fn parse_primary_block(&mut self) -> Result<(), ParseError> {
1712+
todo!("parse_primary_block")
1713+
}
1714+
1715+
fn parse_jump_block(&mut self) -> Result<(), ParseError> {
1716+
todo!("parse_jump_block")
16821717
}
16831718

16841719
fn parse_expr_statement(&mut self) -> Result<ExprStatement, ParseError> {

0 commit comments

Comments
 (0)