ironcalc_base 0.7.1

Open source spreadsheet engine
Documentation
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use super::lexer::{Compare, Lexer, Token};

pub struct Digit {
    pub kind: char, // '#' | '?' | '0'
    pub index: i32,
    pub number: NumberState, // 'i' | 'd' | 'e' (integer, decimal or exponent)
}

pub enum TextToken {
    Literal(char),
    Text(String),
    Ghost(char),
    Spacer(char),
    // Text
    Raw,
    Digit(Digit),
    Period,
    // Dates
    Day,
    DayPadded,
    DayNameShort,
    DayName,
    Month,
    MonthPadded,
    MonthNameShort,
    MonthName,
    MonthLetter,
    YearShort,
    Year,
    Hour,
    HourPadded,
    Minute,
    MinutePadded,
    Second,
    SecondPadded,
    ElapsedHour,
    ElapsedHourPadded,
    ElapsedMinute,
    ElapsedMinutePadded,
    ElapsedSecond,
    ElapsedSecondPadded,
    AMPM,
}
pub struct NumberPart {
    pub color: Option<i32>,
    pub condition: Option<(Compare, f64)>,
    pub use_thousands: bool,
    pub percent: i32, // multiply number by 100^percent
    pub comma: i32,   // divide number by 1000^comma
    pub tokens: Vec<TextToken>,
    pub digit_count: i32, // number of digit tokens (#, 0 or ?) to the left of the decimal point
    pub precision: i32,   // number of digits to the right of the decimal point
    pub is_scientific: bool,
    pub scientific_minus: bool,
    pub exponent_digit_count: i32,
    pub currency: Option<char>,
}

pub struct DatePart {
    pub color: Option<i32>,
    pub use_ampm: bool,
    pub tokens: Vec<TextToken>,
}

pub struct ErrorPart {}

pub struct GeneralPart {}

pub enum ParsePart {
    Number(NumberPart),
    Date(DatePart),
    Error(ErrorPart),
    General(GeneralPart),
}

pub struct Parser {
    pub parts: Vec<ParsePart>,
    lexer: Lexer,
}

#[derive(PartialEq, Copy, Clone)]
pub enum NumberState {
    Integer,
    Decimal,
    Exponent,
}

impl NumberState {
    pub fn is_integer(&self) -> bool {
        matches!(self, NumberState::Integer)
    }

    pub fn is_decimal(&self) -> bool {
        matches!(self, NumberState::Decimal)
    }

    pub fn is_exponent(&self) -> bool {
        matches!(self, NumberState::Exponent)
    }
}

impl ParsePart {
    pub fn is_error(&self) -> bool {
        match &self {
            ParsePart::Date(..) => false,
            ParsePart::Number(..) => false,
            ParsePart::Error(..) => true,
            ParsePart::General(..) => false,
        }
    }
    pub fn is_date(&self) -> bool {
        match &self {
            ParsePart::Date(..) => true,
            ParsePart::Number(..) => false,
            ParsePart::Error(..) => false,
            ParsePart::General(..) => false,
        }
    }
}

// Numbers:
// [integer section][decimal point][fractional section][optional exponent]
// So #,##0.00 is valid but 0.00#,## is not.

impl Parser {
    pub fn new(format: &str) -> Self {
        let lexer = Lexer::new(format);
        let parts = vec![];
        Parser { parts, lexer }
    }
    pub fn parse(&mut self) {
        while self.lexer.peek_token() != Token::EOF {
            let part = self.parse_part();
            self.parts.push(part);
        }
    }

    fn parse_part(&mut self) -> ParsePart {
        let mut token = self.lexer.next_token();
        let mut digit_count = 0;
        let mut precision = 0;
        let mut is_date = false;
        let mut use_ampm = false;
        let mut is_number = false;
        let mut found_decimal_dot = false;
        let mut use_thousands = false;
        let mut comma = 0;
        let mut percent = 0;
        let mut last_token_is_digit = false;
        let mut color = None;
        let mut condition = None;
        let mut tokens = vec![];
        let mut is_scientific = false;
        let mut scientific_minus = false;
        let mut exponent_digit_count = 0;
        let mut number = NumberState::Integer;
        let mut index = 0;
        let mut currency = None;
        let mut is_time = false;

        while token != Token::EOF && token != Token::Separator {
            let next_token = self.lexer.next_token();
            let token_is_digit = token.is_digit();
            is_number = is_number || token_is_digit;
            let next_token_is_digit = next_token.is_digit();
            if token_is_digit {
                if is_scientific {
                    exponent_digit_count += 1;
                } else if found_decimal_dot {
                    precision += 1;
                } else {
                    digit_count += 1;
                }
            }
            match token {
                Token::General => {
                    if tokens.is_empty() {
                        return ParsePart::General(GeneralPart {});
                    } else {
                        return ParsePart::Error(ErrorPart {});
                    }
                }
                Token::Comma => {
                    // If it is in between digit tokens then we use the thousand separator
                    if last_token_is_digit && next_token_is_digit {
                        use_thousands = true;
                    } else if digit_count > 0 {
                        comma += 1;
                    } else {
                        // Before the number is just a literal.
                        tokens.push(TextToken::Literal(','));
                    }
                }
                Token::Percent => {
                    tokens.push(TextToken::Literal('%'));
                    percent += 1;
                }
                Token::Period => {
                    if is_number && !found_decimal_dot {
                        tokens.push(TextToken::Period);
                        found_decimal_dot = true;
                        if number.is_integer() {
                            number = NumberState::Decimal;
                            index = 0;
                        }
                    } else {
                        tokens.push(TextToken::Literal('.'));
                    }
                }
                Token::Color(index) => {
                    color = Some(index);
                }
                Token::Condition(cmp, value) => {
                    condition = Some((cmp, value));
                }
                Token::Currency(c) => {
                    currency = Some(c);
                }
                Token::QuestionMark => {
                    tokens.push(TextToken::Digit(Digit {
                        kind: '?',
                        index,
                        number,
                    }));
                    index += 1;
                }
                Token::Sharp => {
                    tokens.push(TextToken::Digit(Digit {
                        kind: '#',
                        index,
                        number,
                    }));
                    index += 1;
                }
                Token::Zero => {
                    tokens.push(TextToken::Digit(Digit {
                        kind: '0',
                        index,
                        number,
                    }));
                    index += 1;
                }
                Token::Literal(value) => {
                    if value == ':' {
                        is_time = true;
                    }
                    tokens.push(TextToken::Literal(value));
                }
                Token::Text(value) => {
                    tokens.push(TextToken::Text(value));
                }
                Token::Ghost(value) => {
                    tokens.push(TextToken::Ghost(value));
                }
                Token::Spacer(value) => {
                    tokens.push(TextToken::Spacer(value));
                }
                Token::Day => {
                    is_date = true;
                    tokens.push(TextToken::Day);
                }
                Token::DayPadded => {
                    is_date = true;
                    tokens.push(TextToken::DayPadded);
                }
                Token::DayNameShort => {
                    is_date = true;
                    tokens.push(TextToken::DayNameShort);
                }
                Token::DayName => {
                    is_date = true;
                    tokens.push(TextToken::DayName);
                }
                Token::MonthNameShort => {
                    is_date = true;
                    tokens.push(TextToken::MonthNameShort);
                }
                Token::MonthName => {
                    is_date = true;
                    tokens.push(TextToken::MonthName);
                }
                Token::Month => {
                    if is_time {
                        // minute
                        tokens.push(TextToken::Minute);
                    } else {
                        is_date = true;
                        tokens.push(TextToken::Month);
                    }
                }
                Token::MonthPadded => {
                    if is_time {
                        // minute padded
                        tokens.push(TextToken::MinutePadded);
                    } else {
                        is_date = true;
                        tokens.push(TextToken::MonthPadded);
                    }
                }
                Token::MonthLetter => {
                    is_date = true;
                    tokens.push(TextToken::MonthLetter);
                }
                Token::YearShort => {
                    is_date = true;
                    tokens.push(TextToken::YearShort);
                }
                Token::Year => {
                    is_date = true;
                    tokens.push(TextToken::Year);
                }
                Token::Hour => {
                    is_date = true;
                    is_time = true;
                    tokens.push(TextToken::Hour);
                }
                Token::HourPadded => {
                    is_date = true;
                    is_time = true;
                    tokens.push(TextToken::HourPadded);
                }
                Token::Second => {
                    is_date = true;
                    is_time = true;
                    tokens.push(TextToken::Second);
                }
                Token::SecondPadded => {
                    is_date = true;
                    is_time = true;
                    tokens.push(TextToken::SecondPadded);
                }
                Token::AMPM => {
                    is_date = true;
                    use_ampm = true;
                    tokens.push(TextToken::AMPM);
                }
                Token::Scientific => {
                    if !is_scientific {
                        index = 0;
                        number = NumberState::Exponent;
                    }
                    is_scientific = true;
                }
                Token::ScientificMinus => {
                    if !is_scientific {
                        index = 0;
                        number = NumberState::Exponent;
                    }
                    is_scientific = true;
                    scientific_minus = true;
                }
                Token::Separator => {}
                Token::Raw => {
                    tokens.push(TextToken::Raw);
                }
                Token::ILLEGAL => {
                    return ParsePart::Error(ErrorPart {});
                }
                Token::EOF => {}
                Token::ElapsedHour => {
                    is_date = true;
                    is_time = true;
                    tokens.push(TextToken::ElapsedHour);
                }
                Token::ElapsedMinute => {
                    is_date = true;
                    is_time = true;
                    tokens.push(TextToken::ElapsedMinute);
                }
                Token::ElapsedSecond => {
                    is_date = true;
                    is_time = true;
                    tokens.push(TextToken::ElapsedSecond);
                }
                Token::ElapsedHourPadded => {
                    is_date = true;
                    is_time = true;
                    tokens.push(TextToken::ElapsedHourPadded);
                }
                Token::ElapsedMinutePadded => {
                    is_date = true;
                    is_time = true;
                    tokens.push(TextToken::ElapsedMinutePadded);
                }
                Token::ElapsedSecondPadded => {
                    is_date = true;
                    is_time = true;
                    tokens.push(TextToken::ElapsedSecondPadded);
                }
            }
            last_token_is_digit = token_is_digit;
            token = next_token;
        }
        if is_date {
            if is_number {
                return ParsePart::Error(ErrorPart {});
            }
            ParsePart::Date(DatePart {
                color,
                use_ampm,
                tokens,
            })
        } else {
            ParsePart::Number(NumberPart {
                color,
                condition,
                use_thousands,
                percent,
                comma,
                tokens,
                digit_count,
                precision,
                is_scientific,
                scientific_minus,
                exponent_digit_count,
                currency,
            })
        }
    }
}