microcad_lang/parse/
function.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use crate::{parse::*, parser::*, rc::*, syntax::*};
5
6impl Parse for Rc<FunctionDefinition> {
7    fn parse(pair: Pair) -> ParseResult<Self> {
8        Parser::ensure_rule(&pair, Rule::function_definition);
9
10        Ok(Rc::new(FunctionDefinition {
11            doc: crate::find_rule_opt!(pair, doc_block),
12            visibility: crate::find_rule!(pair, visibility)?,
13            id: crate::find_rule!(pair, identifier)?,
14            signature: pair
15                .find(Rule::function_signature)
16                .expect("Function signature"),
17            body: crate::find_rule!(pair, body)?,
18            src_ref: pair.clone().into(),
19        }))
20    }
21}
22
23impl Parse for FunctionSignature {
24    fn parse(pair: Pair) -> ParseResult<Self> {
25        let mut parameters = ParameterList::default();
26        let mut return_type = None;
27
28        for pair in pair.inner() {
29            match pair.as_rule() {
30                Rule::parameter_list => {
31                    parameters = ParameterList::parse(pair)?;
32                }
33                Rule::r#type => return_type = Some(TypeAnnotation::parse(pair)?),
34                rule => unreachable!("Unexpected token in function signature: {:?}", rule),
35            }
36        }
37
38        Ok(Self {
39            parameters,
40            return_type,
41            src_ref: pair.into(),
42        })
43    }
44}