openjd-expr 0.1.2

Open Job Description expression language — types, evaluation, and path mapping
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Copyright by contributors to this project.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

//! Expression language error types.

use std::fmt;

/// Structured error kinds for expression evaluation.
///
/// Callers can match on specific variants to handle errors programmatically
/// without parsing error message strings.
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
pub enum ExpressionErrorKind {
    /// A referenced variable was not found in any symbol table.
    #[error("Undefined variable: '{name}'.{suggestion}")]
    UndefinedVariable { name: String, suggestion: String },

    /// A referenced function was not found in the function library.
    #[error("Unknown function: '{name}'")]
    UnknownFunction { name: String },

    /// Argument types did not match any overload signature.
    #[error("{message}")]
    TypeError { message: String },

    /// An integer operation overflowed the 64-bit signed range.
    #[error("Integer overflow: result is outside the 64-bit signed range")]
    IntegerOverflow,

    /// Division or modulo by zero.
    #[error("{op} by zero")]
    DivisionByZero { op: &'static str },

    /// A float operation produced infinity or NaN.
    #[error("{message}")]
    FloatError { message: String },

    /// An index was out of bounds for a list, string, or range expression.
    #[error("{message}")]
    IndexOutOfBounds { message: String },

    /// Expression memory usage exceeded the configured limit.
    #[error("Expression memory usage ({used} bytes) exceeded limit ({limit} bytes)")]
    MemoryLimitExceeded { used: usize, limit: usize },

    /// Expression operation count exceeded the configured limit.
    #[error("Expression operation count ({count}) exceeded limit ({limit})")]
    OperationLimitExceeded { count: usize, limit: usize },

    /// Expression AST nesting depth exceeded the configured limit.
    ///
    /// Raised by the parser's structural validator and by the evaluator to
    /// prevent stack exhaustion on pathological inputs such as
    /// `((((...1...))))` or a ~1000-term left-associative binop chain.
    /// The limit is [`MAX_EXPRESSION_DEPTH`](crate::MAX_EXPRESSION_DEPTH).
    #[error("Expression nesting depth ({depth}) exceeded limit ({limit})")]
    ExpressionTooDeep { depth: usize, limit: usize },

    /// A Python syntax feature that is not supported in the expression language.
    #[error("{feature}")]
    UnsupportedSyntax { feature: String },

    /// The `fail()` function was called explicitly.
    #[error("{0}")]
    ExplicitFail(String),

    /// A parse error from the underlying Python parser.
    #[error("{0}")]
    ParseError(String),

    /// Any other error that doesn't fit a structured variant.
    #[error("{0}")]
    Other(String),
}

/// Base error for expression evaluation.
///
/// Wraps an [`ExpressionErrorKind`] with optional source location context
/// for caret-style error formatting.
///
/// Internally boxed (8 bytes on stack) to keep `Result<ExprValue, ExpressionError>`
/// compact. Same pattern as `serde_json::Error`.
#[derive(Debug, Clone)]
pub struct ExpressionError {
    inner: Box<ExpressionErrorInner>,
}

#[derive(Debug, Clone)]
struct ExpressionErrorInner {
    kind: ExpressionErrorKind,
    expr: Option<String>,
    col_offset: Option<usize>,
    end_col_offset: Option<usize>,
    caret_offset: Option<usize>,
    sub_errors: Option<Vec<ExpressionError>>,
}

impl ExpressionError {
    /// Create an error from a structured kind.
    pub fn from_kind(kind: ExpressionErrorKind) -> Self {
        Self {
            inner: Box::new(ExpressionErrorInner {
                kind,
                expr: None,
                col_offset: None,
                end_col_offset: None,
                caret_offset: None,
                sub_errors: None,
            }),
        }
    }

    /// Create an error with a plain message string.
    ///
    /// This is a convenience for call sites that don't need a structured kind.
    /// Prefer [`ExpressionError::from_kind`] with a specific variant when the
    /// error category is known.
    pub fn new(message: impl Into<String>) -> Self {
        Self::from_kind(ExpressionErrorKind::Other(message.into()))
    }

    // ── Convenience constructors for common error kinds ──

    /// Integer overflow error.
    pub fn integer_overflow() -> Self {
        Self::from_kind(ExpressionErrorKind::IntegerOverflow)
    }

    /// Division or modulo by zero.
    pub fn division_by_zero(op: &'static str) -> Self {
        Self::from_kind(ExpressionErrorKind::DivisionByZero { op })
    }

    /// Float produced infinity or NaN.
    pub fn float_error(message: impl Into<String>) -> Self {
        Self::from_kind(ExpressionErrorKind::FloatError {
            message: message.into(),
        })
    }

    /// Type mismatch error.
    pub fn type_error(message: impl Into<String>) -> Self {
        Self::from_kind(ExpressionErrorKind::TypeError {
            message: message.into(),
        })
    }

    /// Index out of bounds.
    pub fn index_out_of_bounds(message: impl Into<String>) -> Self {
        Self::from_kind(ExpressionErrorKind::IndexOutOfBounds {
            message: message.into(),
        })
    }

    /// Unsupported Python syntax feature.
    pub fn unsupported(feature: impl Into<String>) -> Self {
        Self::from_kind(ExpressionErrorKind::UnsupportedSyntax {
            feature: feature.into(),
        })
    }

    /// Explicit fail() call.
    pub fn explicit_fail(message: impl Into<String>) -> Self {
        Self::from_kind(ExpressionErrorKind::ExplicitFail(message.into()))
    }

    /// A parse-stage error (underlying parser, range expression parser, etc.).
    pub fn parse_error(message: impl Into<String>) -> Self {
        Self::from_kind(ExpressionErrorKind::ParseError(message.into()))
    }

    /// AST nesting depth exceeded the configured limit.
    pub fn expression_too_deep(depth: usize, limit: usize) -> Self {
        Self::from_kind(ExpressionErrorKind::ExpressionTooDeep { depth, limit })
    }

    /// The structured error kind.
    pub fn kind(&self) -> &ExpressionErrorKind {
        &self.inner.kind
    }

    /// The human-readable error message (the Display output of the kind).
    pub fn message(&self) -> String {
        self.inner.kind.to_string()
    }

    /// Sub-errors for compound failures (e.g., both branches of an if/else).
    pub fn sub_errors(&self) -> &[ExpressionError] {
        match &self.inner.sub_errors {
            Some(v) => v.as_slice(),
            None => &[],
        }
    }

    /// Attach sub-errors (consumes and returns self for chaining).
    pub fn with_sub_errors(mut self, sub_errors: Vec<ExpressionError>) -> Self {
        if !sub_errors.is_empty() {
            self.inner.sub_errors = Some(sub_errors);
        }
        self
    }

    /// The expression source text, if attached.
    pub fn expr(&self) -> Option<&str> {
        self.inner.expr.as_deref()
    }

    /// The start column offset within the expression, if attached.
    pub fn col_offset(&self) -> Option<usize> {
        self.inner.col_offset
    }

    /// The end column offset within the expression, if attached.
    pub fn end_col_offset(&self) -> Option<usize> {
        self.inner.end_col_offset
    }

    /// The caret offset relative to col_offset, if attached.
    pub fn caret_offset(&self) -> Option<usize> {
        self.inner.caret_offset
    }

    /// Attach expression source and AST node span for caret formatting.
    #[must_use]
    pub fn with_node(mut self, expr_source: &str, node: &ruff_python_ast::Expr) -> Self {
        use ruff_text_size::Ranged;
        if self.inner.expr.is_some() {
            return self;
        }
        self.inner.expr = Some(expr_source.to_string());
        let range = node.range();
        self.inner.col_offset = Some(range.start().to_usize());
        self.inner.end_col_offset = Some(range.end().to_usize());
        self.inner.caret_offset = Some(compute_caret_offset(expr_source, node));
        self
    }

    /// Attach expression source with explicit span (no AST node).
    #[must_use]
    pub fn with_span(mut self, expr_source: &str, col: usize, end_col: usize) -> Self {
        if self.inner.expr.is_some() {
            return self;
        }
        self.inner.expr = Some(expr_source.to_string());
        self.inner.col_offset = Some(col);
        self.inner.end_col_offset = Some(end_col);
        self
    }

    /// Set the expression source and span directly (for cases where `with_node`
    /// cannot be used because the span is computed manually).
    pub fn set_source_span(
        &mut self,
        expr_source: &str,
        col: usize,
        end_col: usize,
        caret_offset: usize,
    ) {
        self.inner.expr = Some(expr_source.to_string());
        self.inner.col_offset = Some(col);
        self.inner.end_col_offset = Some(end_col);
        self.inner.caret_offset = Some(caret_offset);
    }

    /// Format the error with a prefix prepended to the expression line.
    ///
    /// The caret position is shifted right by `prefix.len()` so it still
    /// points at the correct column in the combined `prefix + expr` string.
    /// Used for let-binding errors where the expression is part of a larger
    /// construct like `"x = Param.Frame + \"oops\""`.
    ///
    /// Only applies to single-line expressions with caret indicators.
    /// Falls back to the normal `Display` output for multi-line or
    /// context-free errors.
    pub fn message_with_expr_prefix(&self, prefix: &str) -> String {
        let (Some(expr), Some(col), Some(end_col)) = (
            &self.inner.expr,
            self.inner.col_offset,
            self.inner.end_col_offset,
        ) else {
            return self.to_string();
        };
        if expr.contains('\n') {
            return self.to_string();
        }
        let msg = self.message();
        let mut out = msg;
        out.push_str("\n  ");
        out.push_str(prefix);
        out.push_str(expr);
        out.push_str("\n  ");
        let _ = write_caret_line(
            &mut out,
            col + prefix.len(),
            end_col + prefix.len(),
            self.inner.caret_offset.unwrap_or(0),
        );
        out
    }
}

impl fmt::Display for ExpressionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.inner.kind)?;
        if let (Some(expr), Some(col), Some(end_col)) = (
            &self.inner.expr,
            self.inner.col_offset,
            self.inner.end_col_offset,
        ) {
            let is_multiline = expr.contains('\n');
            // For multi-line, the parser wraps in parens shifting offsets by 1
            let (col, end_col) = if is_multiline {
                (col.saturating_sub(1), end_col.saturating_sub(1))
            } else {
                (col, end_col)
            };

            // Find the line containing col_offset and adjust to line-relative offsets
            let (expr_line, line_col, line_end_col) = if is_multiline {
                let mut pos = 0;
                let mut found_line = expr.as_str();
                let mut line_start = 0;
                for line in expr.split('\n') {
                    if pos + line.len() >= col {
                        found_line = line;
                        line_start = pos;
                        break;
                    }
                    pos += line.len() + 1; // +1 for \n
                }
                let lc = col - line_start;
                let lec = if end_col > line_start {
                    (end_col - line_start).min(found_line.len())
                } else {
                    lc + 1
                };
                (found_line, lc, lec)
            } else {
                (expr.as_str(), col, end_col)
            };

            write!(f, "\n  {expr_line}\n  ")?;
            write_caret_line(
                f,
                line_col,
                line_end_col,
                self.inner.caret_offset.unwrap_or(0),
            )?;
        }
        Ok(())
    }
}

impl std::error::Error for ExpressionError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(&self.inner.kind)
    }
}

/// Render a caret-annotation line matching the format used by
/// [`ExpressionError`]'s `Display` impl: `col` leading spaces, then
/// `caret_idx` tildes, then `^`, then trailing tildes to cover
/// `end_col - col`.
///
/// `col`, `end_col`, and `caret_idx` are all zero-based column offsets
/// measured within the displayed line (so callers that add indentation
/// should add it into `col` before calling).
///
/// If the span is zero- or one-wide, only `^` is drawn (no tildes).
///
/// This is the single source of truth for caret rendering across the
/// crate — `Display for ExpressionError`, `message_with_expr_prefix`,
/// and the if/else both-branches-fail renderer in `eval_ifexp` all call
/// it to guarantee identical output.
pub(crate) fn write_caret_line(
    w: &mut dyn std::fmt::Write,
    col: usize,
    end_col: usize,
    caret_offset: usize,
) -> std::fmt::Result {
    let span_len = end_col.saturating_sub(col);
    for _ in 0..col {
        w.write_char(' ')?;
    }
    if span_len > 1 {
        let caret_idx = caret_offset.min(span_len.saturating_sub(1));
        for _ in 0..caret_idx {
            w.write_char('~')?;
        }
        w.write_char('^')?;
        for _ in 0..span_len.saturating_sub(caret_idx + 1) {
            w.write_char('~')?;
        }
    } else {
        w.write_char('^')?;
    }
    Ok(())
}

/// Compute the caret offset within a node's span based on node type.
fn compute_caret_offset(expr: &str, node: &ruff_python_ast::Expr) -> usize {
    use ruff_python_ast as ast;
    use ruff_text_size::Ranged;
    match node {
        ast::Expr::BinOp(b) => {
            let left_end = b.left.range().end().to_usize();
            let right_start = b.right.range().start().to_usize();
            let node_start = node.range().start().to_usize();
            // Scan backwards from right operand to find operator
            let bytes = expr.as_bytes();
            let mut i = right_start.saturating_sub(1);
            while i > left_end
                && i < bytes.len()
                && (bytes[i] == b' ' || bytes[i] == b'\t' || bytes[i] == b'(')
            {
                i -= 1;
            }
            // Check for two-char operators (**, //)
            if i > left_end
                && i < bytes.len()
                && i >= 1
                && (bytes[i - 1..=i] == *b"**" || bytes[i - 1..=i] == *b"//")
            {
                return (i - 1) - node_start;
            }
            if i >= left_end && i < bytes.len() {
                i - node_start
            } else {
                0
            }
        }
        ast::Expr::Attribute(a) => {
            let value_end = a.value.range().end().to_usize();
            let node_start = node.range().start().to_usize();
            (value_end + 1).saturating_sub(node_start) // +1 for the dot
        }
        ast::Expr::Call(c) => {
            if let ast::Expr::Attribute(a) = &*c.func {
                let value_end = a.value.range().end().to_usize();
                let node_start = node.range().start().to_usize();
                (value_end + 1).saturating_sub(node_start)
            } else {
                0
            }
        }
        ast::Expr::Subscript(s) => {
            let value_end = s.value.range().end().to_usize();
            let node_start = node.range().start().to_usize();
            value_end.saturating_sub(node_start)
        }
        _ => 0,
    }
}