microcad_lang/parse/
attribute.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use crate::{parse::*, parser::*};
5
6impl Parse for AttributeCommand {
7    fn parse(pair: Pair) -> ParseResult<Self> {
8        Parser::ensure_rule(&pair, Rule::attribute_command);
9
10        for inner in pair.inner() {
11            match inner.as_rule() {
12                Rule::expression => return Ok(Self::Expression(Expression::parse(inner)?)),
13                Rule::identifier | Rule::argument_list => {
14                    return Ok(Self::Call(
15                        pair.find(Rule::identifier),
16                        pair.find(Rule::argument_list),
17                    ));
18                }
19                _ => {}
20            }
21        }
22
23        unreachable!()
24    }
25}
26
27impl Parse for Attribute {
28    fn parse(pair: Pair) -> ParseResult<Self> {
29        Parser::ensure_rules(&pair, &[Rule::attribute, Rule::inner_attribute]);
30
31        let mut commands = Vec::new();
32        for pair in pair.inner() {
33            if pair.as_rule() == Rule::attribute_command {
34                commands.push(AttributeCommand::parse(pair)?);
35            }
36        }
37
38        Ok(Self {
39            id: pair.find(Rule::identifier).expect("Id"),
40            commands,
41            is_inner: pair.as_rule() == Rule::inner_attribute,
42            src_ref: pair.src_ref(),
43        })
44    }
45}
46
47impl Parse for AttributeList {
48    fn parse(pair: Pair) -> ParseResult<Self> {
49        Parser::ensure_rule(&pair, Rule::attribute_list);
50        let mut attribute_list = AttributeList::default();
51
52        for pair in pair.inner() {
53            match pair.as_rule() {
54                Rule::attribute => {
55                    attribute_list.push(Attribute::parse(pair)?);
56                }
57                Rule::COMMENT | Rule::doc_comment => {}
58                rule => unreachable!("Unexpected element {rule:?}"),
59            }
60        }
61
62        Ok(attribute_list)
63    }
64}