formualizer-parse 2.0.0

High-performance Excel/OpenFormula tokenizer + parser with a stable AST surface
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! Recursive-descent parser for Excel structured (table) references.
//!
//! Implements the bracket-content grammar described in MS-XLSX
//! \u00a718.17.6.2 and used throughout `Table[Column]` style references:
//!
//! ```text
//! specifier       := "[" content "]"
//! content         := "" | special | column | column_range | combination
//! special         := "#All" | "#Headers" | "#Data" | "#Totals" | "#This Row" | "@"
//! column          := "[" name "]"   ; outer bracket optional in simple form
//! column_range    := column ":" column
//! combination     := item ("," item)*
//! item            := "[" special "]" | column | column_range
//! ```
//!
//! Naming is case-insensitive for specials. Column names support an
//! OOXML-style per-character escape `'X` that lets `[`, `]`, `'`, and `#`
//! appear inside a column identifier.

use crate::parser::{SpecialItem, TableSpecifier};
use crate::types::ParsingError;

/// Top-level entry point: parse a complete bracketed specifier and reject
/// trailing garbage.
pub(crate) fn parse_full_specifier(input: &str) -> Result<Option<TableSpecifier>, ParsingError> {
    if input.is_empty() {
        return Ok(None);
    }
    let mut p = SpecifierParser::new(input);
    let spec = p.parse_specifier()?;
    if p.pos != p.src.len() {
        return Err(ParsingError::InvalidReference(format!(
            "Trailing content after structured reference at offset {}: {:?}",
            p.pos,
            &p.src[p.pos..]
        )));
    }
    Ok(Some(spec))
}

struct SpecifierParser<'a> {
    src: &'a str,
    bytes: &'a [u8],
    pos: usize,
}

impl<'a> SpecifierParser<'a> {
    fn new(src: &'a str) -> Self {
        Self {
            src,
            bytes: src.as_bytes(),
            pos: 0,
        }
    }

    fn peek(&self) -> Option<u8> {
        self.bytes.get(self.pos).copied()
    }

    fn bump(&mut self, expected: u8) -> Result<(), ParsingError> {
        match self.peek() {
            Some(b) if b == expected => {
                self.pos += 1;
                Ok(())
            }
            Some(b) => Err(ParsingError::InvalidReference(format!(
                "Expected {:?} at offset {}, found {:?}",
                expected as char, self.pos, b as char
            ))),
            None => Err(ParsingError::InvalidReference(format!(
                "Expected {:?} at offset {}, found end of input",
                expected as char, self.pos
            ))),
        }
    }

    /// Parse the outermost `[ content ]` block.
    fn parse_specifier(&mut self) -> Result<TableSpecifier, ParsingError> {
        self.bump(b'[')?;
        let inner_start = self.pos;
        // Find the matching `]` that terminates this specifier (respecting
        // nesting and `'`-escapes). The substring [inner_start..close) is
        // the "content".
        let close = self.find_matching_close(inner_start - 1)?;
        let content = &self.src[inner_start..close];
        // Advance past the closing bracket so callers see what comes next.
        self.pos = close + 1;
        parse_content(content)
    }

    /// Given the position of an opening `[`, return the byte index of the
    /// matching `]`.
    fn find_matching_close(&self, open_pos: usize) -> Result<usize, ParsingError> {
        debug_assert_eq!(self.bytes[open_pos], b'[');
        let mut depth: u32 = 1;
        let mut i = open_pos + 1;
        while i < self.bytes.len() {
            match self.bytes[i] {
                b'\'' => {
                    // Per-character escape; skip the next byte unconditionally.
                    if i + 1 < self.bytes.len() {
                        i += 2;
                        continue;
                    } else {
                        return Err(ParsingError::InvalidReference(
                            "Trailing '\\'' inside structured reference".to_string(),
                        ));
                    }
                }
                b'[' => depth += 1,
                b']' => {
                    depth -= 1;
                    if depth == 0 {
                        return Ok(i);
                    }
                }
                _ => {}
            }
            i += 1;
        }
        Err(ParsingError::InvalidReference(format!(
            "Unbalanced '[' at offset {open_pos} in structured reference"
        )))
    }
}

/// Parse the content between the outermost brackets of a specifier.
fn parse_content(content: &str) -> Result<TableSpecifier, ParsingError> {
    let trimmed = content.trim();
    if trimmed.is_empty() {
        // `Table1[]` is canonically the whole table.
        return Ok(TableSpecifier::All);
    }

    // Decide between three top-level shapes based on a structural scan that
    // respects `'`-escapes and `[]` nesting:
    //   * a single bracketed item or a comma-separated combination of items
    //     (any `[` at depth 0)
    //   * a column-range using bare names: `name : name`
    //   * a special starting with `#` or `@`
    //   * a single bare column name
    // `@`-prefixed shorthands cannot be confused with combinations because
    // the `@` always precedes either nothing (`@`) or a single column (a
    // bracketed `[Col]` or bare `Col`). Handle this form first so the
    // ordinary combination parser doesn't choke on the leading `@`.
    if let Some(rest) = trimmed.strip_prefix('@') {
        return parse_at_shorthand(rest.trim());
    }

    let scan = scan_top_level(trimmed)?;

    if scan.has_top_level_bracket {
        return parse_combination_or_item(trimmed);
    }

    if scan.has_top_level_comma {
        return Err(ParsingError::InvalidReference(format!(
            "Unexpected ',' in structured reference content: {trimmed:?}"
        )));
    }

    if let Some(colon_idx) = scan.top_level_colon {
        let start = trimmed[..colon_idx].trim();
        let end = trimmed[colon_idx + 1..].trim();
        return build_column_range(start, end);
    }

    if trimmed.starts_with('#') {
        return parse_special_token(trimmed).map(TableSpecifier::SpecialItem);
    }

    Ok(TableSpecifier::Column(unescape_name(trimmed)))
}

struct TopLevelScan {
    has_top_level_bracket: bool,
    has_top_level_comma: bool,
    top_level_colon: Option<usize>,
}

/// Walk `s` once, ignoring escaped bytes and bracket-enclosed regions, and
/// report top-level structural punctuation.
fn scan_top_level(s: &str) -> Result<TopLevelScan, ParsingError> {
    let bytes = s.as_bytes();
    let mut depth: u32 = 0;
    let mut top_bracket = false;
    let mut top_comma = false;
    let mut top_colon: Option<usize> = None;
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'\'' => {
                if i + 1 < bytes.len() {
                    i += 2;
                    continue;
                } else {
                    return Err(ParsingError::InvalidReference(
                        "Dangling '\\'' escape in structured reference".to_string(),
                    ));
                }
            }
            b'[' => {
                if depth == 0 {
                    top_bracket = true;
                }
                depth += 1;
            }
            b']' => {
                if depth == 0 {
                    return Err(ParsingError::InvalidReference(
                        "Stray ']' in structured reference content".to_string(),
                    ));
                }
                depth -= 1;
            }
            b',' if depth == 0 => top_comma = true,
            b':' if depth == 0 && top_colon.is_none() => top_colon = Some(i),
            _ => {}
        }
        i += 1;
    }
    if depth != 0 {
        return Err(ParsingError::InvalidReference(
            "Unbalanced '[' in structured reference content".to_string(),
        ));
    }
    Ok(TopLevelScan {
        has_top_level_bracket: top_bracket,
        has_top_level_comma: top_comma,
        top_level_colon: top_colon,
    })
}

/// Parse content that contains at least one bracketed segment: either a
/// single bracketed item, a `[col]:[col]` range, or a combination.
fn parse_combination_or_item(content: &str) -> Result<TableSpecifier, ParsingError> {
    let mut items: Vec<TableSpecifier> = Vec::new();
    let mut p = ItemParser::new(content);
    p.skip_ws();
    if p.eof() {
        return Ok(TableSpecifier::All);
    }
    loop {
        let item = p.parse_item()?;
        items.push(item);
        p.skip_ws();
        if p.eof() {
            break;
        }
        if p.peek() == Some(b',') {
            p.advance(1);
            p.skip_ws();
            continue;
        }
        return Err(ParsingError::InvalidReference(format!(
            "Expected ',' between structured-reference items at offset {} of {:?}",
            p.pos, content
        )));
    }

    if items.len() == 1 {
        Ok(items.pop().unwrap())
    } else {
        Ok(TableSpecifier::Combination(
            items.into_iter().map(Box::new).collect(),
        ))
    }
}

struct ItemParser<'a> {
    src: &'a str,
    bytes: &'a [u8],
    pos: usize,
}

impl<'a> ItemParser<'a> {
    fn new(src: &'a str) -> Self {
        Self {
            src,
            bytes: src.as_bytes(),
            pos: 0,
        }
    }
    fn eof(&self) -> bool {
        self.pos >= self.bytes.len()
    }
    fn peek(&self) -> Option<u8> {
        self.bytes.get(self.pos).copied()
    }
    fn advance(&mut self, n: usize) {
        self.pos += n;
    }
    fn skip_ws(&mut self) {
        while let Some(b) = self.peek() {
            if b == b' ' || b == b'\t' || b == b'\n' {
                self.pos += 1;
            } else {
                break;
            }
        }
    }

    /// Parse a single item: a bracketed item (`[...]`), or - in a combination -
    /// a bare column or column range. Bracketed items themselves may be
    /// either a special, a column name, or part of a `[col]:[col]` range.
    fn parse_item(&mut self) -> Result<TableSpecifier, ParsingError> {
        self.skip_ws();
        if self.peek() != Some(b'[') {
            return Err(ParsingError::InvalidReference(format!(
                "Expected '[' to start structured-reference item at offset {} of {:?}",
                self.pos, self.src
            )));
        }
        let first = self.parse_bracketed_token()?;
        // Look-ahead for `:` (column range continuation).
        let save = self.pos;
        self.skip_ws();
        if self.peek() == Some(b':') {
            self.advance(1);
            self.skip_ws();
            if self.peek() != Some(b'[') {
                return Err(ParsingError::InvalidReference(format!(
                    "Expected '[' after ':' in column range at offset {} of {:?}",
                    self.pos, self.src
                )));
            }
            let second = self.parse_bracketed_token()?;
            return build_column_range_from_tokens(first, second);
        }
        // No `:` follow-on; rewind whitespace skip.
        self.pos = save;
        bracketed_token_to_specifier(first)
    }

    fn parse_bracketed_token(&mut self) -> Result<BracketedToken, ParsingError> {
        debug_assert_eq!(self.peek(), Some(b'['));
        let open = self.pos;
        self.pos += 1;
        let inner_start = self.pos;
        let mut depth: u32 = 1;
        while !self.eof() {
            match self.bytes[self.pos] {
                b'\'' => {
                    if self.pos + 1 < self.bytes.len() {
                        self.pos += 2;
                        continue;
                    } else {
                        return Err(ParsingError::InvalidReference(format!(
                            "Trailing '\\'' inside item at offset {}",
                            self.pos
                        )));
                    }
                }
                b'[' => {
                    depth += 1;
                    self.pos += 1;
                }
                b']' => {
                    depth -= 1;
                    if depth == 0 {
                        let inner = &self.src[inner_start..self.pos];
                        self.pos += 1;
                        return Ok(BracketedToken {
                            inner: inner.to_string(),
                        });
                    }
                    self.pos += 1;
                }
                _ => self.pos += 1,
            }
        }
        Err(ParsingError::InvalidReference(format!(
            "Unbalanced '[' starting at offset {open} in {:?}",
            self.src
        )))
    }
}

#[derive(Debug, Clone)]
struct BracketedToken {
    inner: String,
}

fn bracketed_token_to_specifier(tok: BracketedToken) -> Result<TableSpecifier, ParsingError> {
    let trimmed = tok.inner.trim();
    if trimmed.is_empty() {
        return Err(ParsingError::InvalidReference(
            "Empty '[]' inside structured-reference combination".to_string(),
        ));
    }
    if trimmed.starts_with('#') {
        return parse_special_token(trimmed).map(TableSpecifier::SpecialItem);
    }
    if let Some(rest) = trimmed.strip_prefix('@') {
        let rest = rest.trim();
        if rest.is_empty() {
            // Bare `[@]` inside a combination is the ThisRow special item; the
            // combination form distinguishes itself from `Table1[@]` (which
            // alone resolves to `Row(Current)`).
            return Ok(TableSpecifier::SpecialItem(SpecialItem::ThisRow));
        }
        // `[@Column]` inside a combination/item still resolves to ThisRow + Column.
        return parse_at_shorthand(rest);
    }
    Ok(TableSpecifier::Column(unescape_name(trimmed)))
}

fn build_column_range_from_tokens(
    a: BracketedToken,
    b: BracketedToken,
) -> Result<TableSpecifier, ParsingError> {
    let lhs = a.inner.trim();
    let rhs = b.inner.trim();
    if lhs.is_empty() || rhs.is_empty() {
        return Err(ParsingError::InvalidReference(
            "Empty column name in structured-reference range".to_string(),
        ));
    }
    if lhs.starts_with('#') || lhs.starts_with('@') || rhs.starts_with('#') || rhs.starts_with('@')
    {
        return Err(ParsingError::InvalidReference(format!(
            "Special items cannot appear in column range: [{lhs}]:[{rhs}]"
        )));
    }
    Ok(TableSpecifier::ColumnRange(
        unescape_name(lhs),
        unescape_name(rhs),
    ))
}

fn build_column_range(lhs: &str, rhs: &str) -> Result<TableSpecifier, ParsingError> {
    if lhs.is_empty() || rhs.is_empty() {
        return Err(ParsingError::InvalidReference(
            "Empty column name in structured-reference range".to_string(),
        ));
    }
    Ok(TableSpecifier::ColumnRange(
        unescape_name(lhs),
        unescape_name(rhs),
    ))
}

/// Parse a `#`-prefixed special item, case-insensitively. Returns the bare
/// `SpecialItem` enum (`@` is handled by the caller, not here).
fn parse_special_token(token: &str) -> Result<SpecialItem, ParsingError> {
    debug_assert!(token.starts_with('#'));
    // Normalise internal whitespace runs to a single ASCII space so
    // `#This  Row` and `#This\tRow` still match.
    let normalized: String = token
        .chars()
        .map(|c| if c == '\t' { ' ' } else { c })
        .collect();
    let mut compact = String::with_capacity(normalized.len());
    let mut prev_space = false;
    for c in normalized.chars() {
        if c == ' ' {
            if !prev_space {
                compact.push(' ');
            }
            prev_space = true;
        } else {
            compact.push(c);
            prev_space = false;
        }
    }
    match compact.to_ascii_lowercase().as_str() {
        "#all" => Ok(SpecialItem::All),
        "#headers" => Ok(SpecialItem::Headers),
        "#data" => Ok(SpecialItem::Data),
        "#totals" => Ok(SpecialItem::Totals),
        "#this row" => Ok(SpecialItem::ThisRow),
        _ => Err(ParsingError::InvalidReference(format!(
            "Unknown special item: {token}"
        ))),
    }
}

/// Parse `@...` shorthand: `@`, `@Col`, `@[Col Name]` ...
fn parse_at_shorthand(rest: &str) -> Result<TableSpecifier, ParsingError> {
    if rest.is_empty() {
        return Ok(TableSpecifier::Row(
            crate::parser::TableRowSpecifier::Current,
        ));
    }
    // Strip an optional surrounding `[ ... ]` to support `@[Col Name]`.
    let trimmed = rest.trim();
    let column = if let Some(stripped) = strip_outer_brackets(trimmed) {
        stripped
    } else {
        trimmed.to_string()
    };
    if column.is_empty() {
        return Err(ParsingError::InvalidReference(
            "Empty column after '@' in structured reference".to_string(),
        ));
    }
    Ok(TableSpecifier::Combination(vec![
        Box::new(TableSpecifier::SpecialItem(SpecialItem::ThisRow)),
        Box::new(TableSpecifier::Column(unescape_name(&column))),
    ]))
}

fn strip_outer_brackets(s: &str) -> Option<String> {
    let bytes = s.as_bytes();
    if bytes.len() < 2 || bytes[0] != b'[' || bytes[bytes.len() - 1] != b']' {
        return None;
    }
    // Validate balanced (with `'`-escape) and that the matching close is the
    // final byte; if the first `[` matches an interior `]` then this isn't
    // a single outer-bracketed token.
    let mut depth: u32 = 1;
    let mut i = 1;
    while i < bytes.len() - 1 {
        match bytes[i] {
            b'\'' => {
                i += 2;
                continue;
            }
            b'[' => depth += 1,
            b']' => {
                depth -= 1;
                if depth == 0 {
                    return None;
                }
            }
            _ => {}
        }
        i += 1;
    }
    if depth != 1 {
        return None;
    }
    Some(s[1..s.len() - 1].to_string())
}

/// Apply the OOXML `'X` per-character escape: a single apostrophe makes the
/// next character literal. A trailing apostrophe is left as-is (the caller
/// will already have caught structural truncation in earlier phases).
fn unescape_name(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut chars = s.chars();
    while let Some(c) = chars.next() {
        if c == '\'' {
            if let Some(next) = chars.next() {
                out.push(next);
            }
        } else {
            out.push(c);
        }
    }
    // Trim outer ASCII whitespace; column names rarely have leading/trailing
    // spaces and Excel itself strips them on round-trip.
    out.trim().to_string()
}