1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use pomelo_parse::{ast, AstToken};

use crate::lower::LoweringCtxt;
use crate::{Label, LongStrId, LongTyCon, LongVId, Name, StrId, TyCon, TyVar, VId};

impl VId {
    pub(super) fn from_token(ctx: &mut LoweringCtxt, opt_vid: Option<ast::VId>) -> Self {
        match opt_vid {
            Some(vid) => Self::try_builtin(vid.syntax.text(), ctx.interner_mut()),
            None => Self::missing(),
        }
    }
}

impl TyVar {
    pub(super) fn from_token(ctx: &mut LoweringCtxt, opt_tyvar: Option<ast::TyVar>) -> Self {
        match opt_tyvar {
            None => Self::missing(),
            Some(t) => Self::from_string(t.syntax().text(), ctx.interner_mut()),
        }
    }
}

impl LongVId {
    pub(super) fn from_node(ctx: &mut LoweringCtxt, node: &ast::LongVId) -> Self {
        let strids = node
            .strids()
            .map(|s| StrId::from_token(ctx, Some(s)))
            .collect();
        let vid = VId::from_token(ctx, node.vid());

        Self { strids, vid }
    }

    pub(super) fn from_opt_node(ctx: &mut LoweringCtxt, opt_node: Option<&ast::LongVId>) -> Self {
        match opt_node {
            Some(vid) => Self::from_node(ctx, vid),
            None => Self::missing(),
        }
    }
}

impl StrId {
    pub(super) fn from_token(ctx: &mut LoweringCtxt, opt_strid: Option<ast::StrId>) -> Self {
        match opt_strid {
            Some(strid) => Self::try_builtin(strid.syntax().text(), ctx.interner_mut()),
            None => Self::missing(),
        }
    }
}

impl LongStrId {
    pub(super) fn from_node(ctx: &mut LoweringCtxt, node: &ast::LongStrId) -> Self {
        let mut strids = node
            .strids()
            .map(|s| StrId::from_token(ctx, Some(s)))
            .collect::<Vec<_>>();
        let strid = strids.pop().unwrap_or_else(StrId::missing);

        Self {
            strid_path: strids.into_boxed_slice(),
            strid,
        }
    }
}

impl TyCon {
    pub(super) fn from_token(ctx: &mut LoweringCtxt, opt_tycon: Option<ast::TyCon>) -> Self {
        match opt_tycon {
            None => Self::missing(),
            Some(t) => Self::try_builtin(t.syntax().text(), ctx.interner_mut()),
        }
    }
}

impl LongTyCon {
    pub(super) fn from_node(ctx: &mut LoweringCtxt, node: &ast::LongTyCon) -> Self {
        let strids = node
            .strids()
            .map(|s| StrId::from_token(ctx, Some(s)))
            .collect();
        let tycon = TyCon::from_token(ctx, node.tycon());

        Self { strids, tycon }
    }

    pub(super) fn from_opt_node(ctx: &mut LoweringCtxt, opt_node: Option<&ast::LongTyCon>) -> Self {
        match opt_node {
            None => Self::missing(),
            Some(t) => Self::from_node(ctx, t),
        }
    }
}

impl Label {
    pub(super) fn from_token(ctx: &mut LoweringCtxt, opt_label: Option<ast::Label>) -> Self {
        if let Some(label) = opt_label {
            let s = label.syntax().text();

            if let Ok(n) = s.parse::<u32>() {
                Self::Numeric(n)
            } else {
                Self::Named(Name::from_string(s, ctx.interner_mut()))
            }
        } else {
            Self::Missing
        }
    }
}