fiasto 0.2.7

High-performance modern Wilkinson's formula parsing for statistical models. Parses R-style formulas into structured JSON metadata supporting linear models, mixed effects, and complex statistical specifications.
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
use crate::internal::{ast::Term, errors::ParseError, lexer::Token};

/// Parses a single term in a formula, which can be either a column name or a function call.
///
/// This function handles the core building blocks of formula terms. A term can be:
/// - A simple column name (e.g., "x", "age", "income")
/// - A function call with arguments (e.g., "poly(x, 2)", "log(price)")
///
/// # Arguments
/// * `tokens` - Reference to the vector of tokens
/// * `pos` - Mutable reference to the current position (will be advanced)
///
/// # Returns
/// * `Result<Term, ParseError>` - The parsed term, or an error
///
/// # Example
/// ```
/// use fiasto::internal::parse_term::parse_term;
/// use fiasto::internal::lexer::Token;
/// use fiasto::internal::ast::Term;
///
/// // Parse a simple column term
/// let tokens = vec![
///     (Token::ColumnName, "x")
/// ];
/// let mut pos = 0;
///
/// let result = parse_term(&tokens, &mut pos);
/// assert!(result.is_ok());
/// match result.unwrap() {
///     Term::Column(name) => assert_eq!(name, "x"),
///     _ => panic!("Expected column term")
/// }
///
/// // Parse a function term
/// let tokens = vec![
///     (Token::Poly, "poly"),
///     (Token::FunctionStart, "("),
///     (Token::ColumnName, "x"),
///     (Token::Comma, ","),
///     (Token::Integer, "2"),
///     (Token::FunctionEnd, ")")
/// ];
/// let mut pos = 0;
///
/// let result = parse_term(&tokens, &mut pos);
/// assert!(result.is_ok());
/// match result.unwrap() {
///     Term::Function { name, args } => {
///         assert_eq!(name, "poly");
///         assert_eq!(args.len(), 2);
///     },
///     _ => panic!("Expected function term")
/// }
/// ```
///
/// # How it works
/// 1. Expects either a Poly token or ColumnName token
/// 2. If followed by FunctionStart, parses as a function call
/// 3. If not followed by FunctionStart, returns as a column term
/// 4. For functions, parses argument list and expects closing parenthesis
///
/// # Grammar Rule
/// ```text
/// term = column_name | function_call
/// function_call = (poly | column_name) "(" arg_list ")"
/// arg_list = [argument ("," argument)*]
/// ```
///
/// # Use Cases
/// - Parsing individual predictor variables
/// - Handling polynomial and other transformations
/// - Supporting user-defined function calls
/// - Building the term structure for models
///
/// # Examples of Valid Inputs
/// - `"x"` → Term::Column("x")
/// - `"poly(x, 2)"` → Term::Function { name: "poly", args: [x, 2] }
/// - `"log(price)"` → Term::Function { name: "log", args: [price] }
pub fn parse_term<'a>(tokens: &'a [(Token, &'a str)], pos: &mut usize) -> Result<Term, ParseError> {
    // Check if this is a random effect (starts with opening parenthesis)
    if crate::internal::peek::peek(tokens, *pos)
        .map(|(t, _)| matches!(t, Token::FunctionStart))
        .unwrap_or(false)
    {
        // Check if the next token after the opening parenthesis suggests a random effect
        let saved_pos = *pos;
        *pos += 1; // Skip the opening parenthesis

        let is_random_effect = crate::internal::peek::peek(tokens, *pos)
            .map(|(t, _)| {
                matches!(
                    t,
                    Token::One | Token::Zero | Token::Minus | Token::ColumnName
                )
            })
            .unwrap_or(false);

        *pos = saved_pos; // Restore position

        if is_random_effect {
            // Parse as random effect
            let random_effect =
                crate::internal::parse_random_effect::parse_random_effect(tokens, pos)?;
            return Ok(Term::RandomEffect(random_effect));
        }
    }

    // Parse the leftmost atomic term (column, function, etc.)
    let atomic_term = {
        let (tok, name_slice) = crate::internal::expect::expect(
            tokens,
            pos,
            |t| {
                matches!(
                    t,
                    Token::Poly
                        | Token::ColumnName
                        | Token::Log
                        | Token::Offset
                        | Token::Factor
                        | Token::C
                        | Token::Scale
                        | Token::Standardize
                        | Token::Center
                        | Token::BSplines
                        | Token::GaussianProcess
                        | Token::Monotonic
                        | Token::MeasurementError
                        | Token::MissingValues
                        | Token::ForwardFill
                        | Token::BackwardFill
                        | Token::Diff
                        | Token::Lag
                        | Token::Lead
                        | Token::Trunc
                        | Token::Weights
                        | Token::Trials
                        | Token::Censored
                        | Token::Gr
                        | Token::Mm
                        | Token::Mmc
                        | Token::Cs
                        | Token::FunctionStart
                        | Token::One
                        | Token::Zero
                )
            },
            "Function, ColumnName, Intercept, or Zero",
        )?;
        if crate::internal::matches::matches(tokens, pos, |t| matches!(t, Token::FunctionStart)) {
            let fname = match tok {
                Token::Poly => "poly".to_string(),
                Token::Log => "log".to_string(),
                Token::Offset => "offset".to_string(),
                Token::Factor => "factor".to_string(),
                Token::C => "c".to_string(),
                Token::Scale => "scale".to_string(),
                Token::Standardize => "standardize".to_string(),
                Token::Center => "center".to_string(),
                Token::BSplines => "bs".to_string(),
                Token::GaussianProcess => "gp".to_string(),
                Token::Monotonic => "mono".to_string(),
                Token::MeasurementError => "me".to_string(),
                Token::MissingValues => "mi".to_string(),
                Token::ForwardFill => "forward_fill".to_string(),
                Token::BackwardFill => "backward_fill".to_string(),
                Token::Diff => "diff".to_string(),
                Token::Lag => "lag".to_string(),
                Token::Lead => "lead".to_string(),
                Token::Trunc => "trunc".to_string(),
                Token::Weights => "weights".to_string(),
                Token::Trials => "trials".to_string(),
                Token::Censored => "cens".to_string(),
                Token::ColumnName => name_slice.to_string(),
                _ => unreachable!(),
            };
            let args = crate::internal::parse_arg_list::parse_arg_list(tokens, pos)?;
            crate::internal::expect::expect(tokens, pos, |t| matches!(t, Token::FunctionEnd), ")")?;
            Term::Function { name: fname, args }
        } else {
            match tok {
                Token::ColumnName => {
                    // Return the atomic column name; interactions ('*' or ':') are
                    // handled by the loop after atomic term parsing to support
                    // chained interactions like `a*b*c`.
                    Term::Column(name_slice.to_string())
                }
                Token::One => {
                    // Return an intercept term for formulas like `y ~ 1`
                    Term::Intercept
                }
                Token::Zero => {
                    // Return a zero term for formulas like `y ~ 0` (no intercept)
                    Term::Zero
                }
                Token::Poly => return Err(ParseError::Syntax("expected '(' after 'poly'".into())),
                Token::Log => return Err(ParseError::Syntax("expected '(' after 'log'".into())),
                Token::Offset => {
                    return Err(ParseError::Syntax("expected '(' after 'offset'".into()))
                }
                Token::Factor => {
                    return Err(ParseError::Syntax("expected '(' after 'factor'".into()))
                }
                Token::Scale => {
                    return Err(ParseError::Syntax("expected '(' after 'scale'".into()))
                }
                Token::Standardize => {
                    return Err(ParseError::Syntax(
                        "expected '(' after 'standardize'".into(),
                    ))
                }
                Token::Center => {
                    return Err(ParseError::Syntax("expected '(' after 'center'".into()))
                }
                Token::BSplines => {
                    return Err(ParseError::Syntax("expected '(' after 'bs'".into()))
                }
                Token::GaussianProcess => {
                    return Err(ParseError::Syntax("expected '(' after 'gp'".into()))
                }
                Token::Monotonic => {
                    return Err(ParseError::Syntax("expected '(' after 'mono'".into()))
                }
                Token::MeasurementError => {
                    return Err(ParseError::Syntax("expected '(' after 'me'".into()))
                }
                Token::MissingValues => {
                    return Err(ParseError::Syntax("expected '(' after 'mi'".into()))
                }
                Token::ForwardFill => {
                    return Err(ParseError::Syntax(
                        "expected '(' after 'forward_fill'".into(),
                    ))
                }
                Token::BackwardFill => {
                    return Err(ParseError::Syntax(
                        "expected '(' after 'backward_fill'".into(),
                    ))
                }
                Token::Diff => return Err(ParseError::Syntax("expected '(' after 'diff'".into())),
                Token::Lag => return Err(ParseError::Syntax("expected '(' after 'lag'".into())),
                Token::Lead => return Err(ParseError::Syntax("expected '(' after 'lead'".into())),
                Token::Trunc => {
                    return Err(ParseError::Syntax("expected '(' after 'trunc'".into()))
                }
                Token::Weights => {
                    return Err(ParseError::Syntax("expected '(' after 'weights'".into()))
                }
                Token::Trials => {
                    return Err(ParseError::Syntax("expected '(' after 'trials'".into()))
                }
                Token::Censored => {
                    return Err(ParseError::Syntax("expected '(' after 'cens'".into()))
                }
                _ => {
                    return Err(ParseError::Unexpected {
                        expected: "term",
                        found: Some(tok),
                    })
                }
            }
        }
    };

    // Now check for multiplication (interaction) tokens and build up the interaction chain
    let mut term = atomic_term;
    loop {
        if crate::internal::matches::matches(tokens, pos, |t| {
            matches!(t, Token::InteractionAndEffect | Token::InteractionOnly)
        }) {
            // `matches` already consumed the interaction token, so parse the right-hand term now
            let right = parse_term(tokens, pos)?;
            term = Term::Interaction {
                left: Box::new(term),
                right: Box::new(right),
            };
        } else {
            break;
        }
    }
    Ok(term)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::internal::lexer::Token;

    #[test]
    fn test_parse_term_simple_column() {
        let tokens = vec![(Token::ColumnName, "x")];
        let mut pos = 0;

        let result = parse_term(&tokens, &mut pos);
        assert!(result.is_ok());
        match result.unwrap() {
            Term::Column(name) => assert_eq!(name, "x"),
            _ => panic!("Expected column term"),
        }
        assert_eq!(pos, 1);
    }

    #[test]
    fn test_parse_term_poly_function() {
        let tokens = vec![
            (Token::Poly, "poly"),
            (Token::FunctionStart, "("),
            (Token::ColumnName, "x"),
            (Token::Comma, ","),
            (Token::Integer, "2"),
            (Token::FunctionEnd, ")"),
        ];
        let mut pos = 0;

        let result = parse_term(&tokens, &mut pos);
        assert!(result.is_ok());
        match result.unwrap() {
            Term::Function { name, args } => {
                assert_eq!(name, "poly");
                assert_eq!(args.len(), 2);
            }
            _ => panic!("Expected function term"),
        }
        assert_eq!(pos, 6);
    }

    #[test]
    fn test_parse_term_custom_function() {
        let tokens = vec![
            (Token::ColumnName, "log"),
            (Token::FunctionStart, "("),
            (Token::ColumnName, "price"),
            (Token::FunctionEnd, ")"),
        ];
        let mut pos = 0;

        let result = parse_term(&tokens, &mut pos);
        assert!(result.is_ok());
        match result.unwrap() {
            Term::Function { name, args } => {
                assert_eq!(name, "log");
                assert_eq!(args.len(), 1);
            }
            _ => panic!("Expected function term"),
        }
        assert_eq!(pos, 4);
    }

    #[test]
    fn test_parse_term_poly_without_parentheses() {
        let tokens = vec![(Token::Poly, "poly")];
        let mut pos = 0;

        let result = parse_term(&tokens, &mut pos);
        assert!(result.is_err());
        assert_eq!(pos, 1); // Position advanced past poly
    }

    #[test]
    fn test_parse_term_function_with_multiple_args() {
        let tokens = vec![
            (Token::ColumnName, "custom_func"),
            (Token::FunctionStart, "("),
            (Token::ColumnName, "x"),
            (Token::Comma, ","),
            (Token::ColumnName, "y"),
            (Token::Comma, ","),
            (Token::Integer, "10"),
            (Token::FunctionEnd, ")"),
        ];
        let mut pos = 0;

        let result = parse_term(&tokens, &mut pos);
        assert!(result.is_ok());
        match result.unwrap() {
            Term::Function { name, args } => {
                assert_eq!(name, "custom_func");
                assert_eq!(args.len(), 3);
            }
            _ => panic!("Expected function term"),
        }
        assert_eq!(pos, 8);
    }

    #[test]
    fn test_parse_term_function_without_closing_paren() {
        let tokens = vec![
            (Token::ColumnName, "func"),
            (Token::FunctionStart, "("),
            (Token::ColumnName, "x"),
        ];
        let mut pos = 0;

        let result = parse_term(&tokens, &mut pos);
        assert!(result.is_err());
        assert_eq!(pos, 3); // Position at end
    }

    #[test]
    fn test_parse_term_long_column_name() {
        let tokens = vec![(Token::ColumnName, "very_long_column_name_with_underscores")];
        let mut pos = 0;

        let result = parse_term(&tokens, &mut pos);
        assert!(result.is_ok());
        match result.unwrap() {
            Term::Column(name) => assert_eq!(name, "very_long_column_name_with_underscores"),
            _ => panic!("Expected column term"),
        }
        assert_eq!(pos, 1);
    }

    #[test]
    fn test_parse_term_numeric_column_name() {
        let tokens = vec![(Token::ColumnName, "x1")];
        let mut pos = 0;

        let result = parse_term(&tokens, &mut pos);
        assert!(result.is_ok());
        match result.unwrap() {
            Term::Column(name) => assert_eq!(name, "x1"),
            _ => panic!("Expected column term"),
        }
        assert_eq!(pos, 1);
    }
}