microcad_lang/parse/
use.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use crate::{parse::*, parser::*, syntax::*};
5
6impl Parse for UseDeclaration {
7    fn parse(pair: Pair) -> ParseResult<Self> {
8        Parser::ensure_rule(&pair, Rule::use_declaration);
9
10        let mut inner = pair.inner();
11        let first = inner.next().expect("Expected use declaration element");
12
13        match first.as_rule() {
14            Rule::qualified_name => Ok(Self::Use(QualifiedName::parse(first)?)),
15            Rule::use_all => {
16                let inner = first.inner().next().expect("Expected qualified name");
17                Ok(Self::UseAll(QualifiedName::parse(inner)?))
18            }
19            Rule::use_alias => {
20                let mut inner = first.inner();
21                let name = QualifiedName::parse(inner.next().expect("Expected qualified name"))?;
22                let alias = Identifier::parse(inner.next().expect("Expected identifier"))?;
23                Ok(Self::UseAs(name, alias))
24            }
25            _ => unreachable!("Invalid use declaration"),
26        }
27    }
28}
29
30impl Parse for UseStatement {
31    fn parse(pair: Pair) -> ParseResult<Self> {
32        Parser::ensure_rule(&pair, Rule::use_statement);
33
34        Ok(Self {
35            visibility: crate::find_rule!(pair, visibility)?,
36            decl: crate::find_rule_exact!(pair, use_declaration)?,
37            src_ref: pair.into(),
38        })
39    }
40}
41
42impl Parse for Visibility {
43    fn parse(pair: Pair) -> ParseResult<Self> {
44        Parser::ensure_rule(&pair, Rule::visibility);
45
46        let s = pair.as_str();
47        match s {
48            "pub" => Ok(Self::Public),
49            "" => Ok(Self::Private),
50            _ => unreachable!("Invalid visibility"),
51        }
52    }
53}