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