• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ergoplatform / sigma-rust / 12359016920

16 Dec 2024 06:33PM UTC coverage: 78.448% (-0.4%) from 78.823%
12359016920

push

github

web-flow
Merge pull request #782 from ergoplatform/no_std

No_std support

125 of 217 new or added lines in 56 files covered. (57.6%)

67 existing lines in 33 files now uncovered.

11007 of 14031 relevant lines covered (78.45%)

2.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.34
/ergoscript-compiler/src/ast.rs
1
use crate::error::pretty_error_desc;
2

3
use super::syntax::{SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken};
4
use text_size::TextRange;
5

6
#[derive(Debug, PartialEq, Eq)]
7
pub struct AstError {
8
    pub msg: String,
9
    pub span: TextRange,
10
}
11

12
impl AstError {
13
    pub fn new(msg: String, span: TextRange) -> Self {
×
14
        AstError { msg, span }
15
    }
16

17
    pub fn pretty_desc(&self, source: &str) -> String {
×
18
        pretty_error_desc(source, self.span, &self.msg)
×
19
    }
20
}
21

22
#[derive(Debug)]
23
pub struct Root(SyntaxNode);
24

25
impl Root {
26
    pub fn cast(node: SyntaxNode) -> Option<Self> {
3✔
27
        if node.kind() == SyntaxKind::Root {
9✔
28
            Some(Self(node))
5✔
29
        } else {
30
            None
×
31
        }
32
    }
33

34
    pub fn children(&self) -> impl Iterator<Item = Expr> {
5✔
35
        self.0.children().filter_map(Expr::cast)
5✔
36
    }
37

38
    pub fn span(&self) -> TextRange {
×
39
        self.0.text_range()
×
40
    }
41
}
42

43
#[derive(Debug)]
44
pub struct Ident(SyntaxNode);
45

46
impl Ident {
47
    pub fn name(&self) -> Result<SyntaxToken, AstError> {
3✔
48
        self.0
6✔
49
            .children_with_tokens()
50
            .filter_map(SyntaxElement::into_token)
51
            .find(|token| token.kind() == SyntaxKind::Ident)
6✔
UNCOV
52
            .ok_or_else(|| AstError::new(format!("Empty Ident.name in: {:?}", self.0), self.span()))
×
53
    }
54

55
    pub fn span(&self) -> TextRange {
3✔
56
        self.0.text_range()
3✔
57
    }
58
}
59

60
#[allow(clippy::enum_variant_names)]
61
#[derive(Debug)]
62
pub enum Expr {
63
    Ident(Ident),
64
    BinaryExpr(BinaryExpr),
65
    Literal(Literal),
66
    // ParenExpr(ParenExpr),
67
    // UnaryExpr(UnaryExpr),
68
}
69

70
impl Expr {
71
    pub fn cast(node: SyntaxNode) -> Option<Self> {
5✔
72
        let result = match node.kind() {
7✔
73
            SyntaxKind::Ident => Self::Ident(Ident(node)),
3✔
74
            SyntaxKind::InfixExpr => Self::BinaryExpr(BinaryExpr(node)),
1✔
75
            SyntaxKind::IntNumber => Self::Literal(Literal(node)),
1✔
76
            SyntaxKind::LongNumber => Self::Literal(Literal(node)),
1✔
77
            // SyntaxKind::ParenExpr => Self::ParenExpr(ParenExpr(node)),
78
            // SyntaxKind::PrefixExpr => Self::UnaryExpr(UnaryExpr(node)),
79
            _ => return None,
×
80
        };
81

82
        Some(result)
4✔
83
    }
84

85
    // pub fn span(&self) -> TextRange {
86
    //     match self {
87
    //         Expr::Ident(node) => node.0.text_range(),
88
    //         _ => todo!(),
89
    //     }
90
    // }
91
}
92

93
#[derive(Debug)]
94
pub struct BinaryExpr(SyntaxNode);
95

96
impl BinaryExpr {
97
    pub fn lhs(&self) -> Result<Expr, AstError> {
1✔
98
        self.0.children().find_map(Expr::cast).ok_or_else(|| {
2✔
99
            AstError::new(
×
100
                format!("Cannot find lhs in {:?}", self.0.children()),
×
101
                self.0.text_range(),
×
102
            )
103
        })
104
    }
105

106
    pub fn rhs(&self) -> Result<Expr, AstError> {
1✔
107
        self.0
2✔
108
            .children()
109
            .filter_map(Expr::cast)
110
            .nth(1)
UNCOV
111
            .ok_or_else(|| {
×
112
                AstError::new(
×
113
                    format!("Cannot find rhs in {:?}", self.0.children()),
×
114
                    self.0.text_range(),
×
115
                )
116
            })
117
    }
118

119
    pub fn op(&self) -> Result<SyntaxToken, AstError> {
1✔
120
        self.0
2✔
121
            .children_with_tokens()
122
            .filter_map(SyntaxElement::into_token)
123
            .find(|token| {
1✔
124
                matches!(
1✔
125
                    token.kind(),
1✔
126
                    SyntaxKind::Plus
127
                        | SyntaxKind::Minus
128
                        | SyntaxKind::Star
129
                        | SyntaxKind::Slash
130
                        | SyntaxKind::And,
131
                )
132
            })
UNCOV
133
            .ok_or_else(|| {
×
134
                AstError::new(
×
135
                    format!("Cannot find bin op in {:?}", self.0),
×
136
                    self.0.text_range(),
×
137
                )
138
            })
139
    }
140

141
    pub fn span(&self) -> TextRange {
1✔
142
        self.0.text_range()
1✔
143
    }
144
}
145

146
#[derive(Debug)]
147
pub enum LiteralValue {
148
    Int(i32),
149
    Long(i64),
150
}
151

152
#[derive(Debug)]
153
pub struct Literal(SyntaxNode);
154

155
impl Literal {
156
    pub fn parse(&self) -> Result<LiteralValue, AstError> {
1✔
157
        let text = self.0.first_token().unwrap().text().to_string();
2✔
158
        if text.ends_with('L') {
2✔
159
            text.strip_suffix('L')
3✔
160
                .unwrap()
161
                .parse()
162
                .ok()
163
                .map(LiteralValue::Long)
164
        } else {
165
            text.parse().ok().map(LiteralValue::Int)
2✔
166
        }
UNCOV
167
        .ok_or_else(|| {
×
168
            AstError::new(
×
169
                format!("Failed to parse Literal from: {:?}", self.0),
×
170
                self.span(),
×
171
            )
172
        })
173
    }
174

175
    pub fn span(&self) -> TextRange {
1✔
176
        self.0.text_range()
1✔
177
    }
178
}
179

180
// #[derive(Debug)]
181
// pub struct ParenExpr(SyntaxNode);
182

183
// impl ParenExpr {
184
//     pub fn expr(&self) -> Option<Expr> {
185
//         self.0.children().find_map(Expr::cast)
186
//     }
187
// }
188

189
// #[derive(Debug)]
190
// pub struct UnaryExpr(SyntaxNode);
191

192
// impl UnaryExpr {
193
//     pub fn expr(&self) -> Option<Expr> {
194
//         self.0.children().find_map(Expr::cast)
195
//     }
196

197
//     pub fn op(&self) -> Option<SyntaxToken> {
198
//         self.0
199
//             .children_with_tokens()
200
//             .filter_map(SyntaxElement::into_token)
201
//             .find(|token| token.kind() == SyntaxKind::Minus)
202
//     }
203
// }
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc