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: crate::find_rule_exact!(pair, function_signature)?,
15            body: crate::find_rule!(pair, body)?,
16        }))
17    }
18}
19
20impl Parse for FunctionSignature {
21    fn parse(pair: Pair) -> ParseResult<Self> {
22        let mut parameters = ParameterList::default();
23        let mut return_type = None;
24
25        for pair in pair.inner() {
26            match pair.as_rule() {
27                Rule::parameter_list => {
28                    parameters = ParameterList::parse(pair)?;
29                }
30                Rule::r#type => return_type = Some(TypeAnnotation::parse(pair)?),
31                rule => unreachable!("Unexpected token in function signature: {:?}", rule),
32            }
33        }
34
35        Ok(Self {
36            parameters,
37            return_type,
38            src_ref: pair.into(),
39        })
40    }
41}