errors.go RawHistoryFolder
1// SPDX-License-Identifier: MIT
2
3package expr
4
5import (
6 "fmt"
7)
8
9// Limits of the reference implementation.
10const (
11 // MaxParserDepth is the deepest nesting of groupings, function arguments,
12 // index accesses, negations and logical operators the parser accepts.
13 MaxParserDepth = 50
14 // MaxExpressionLength is the longest expression accepted, counted in
15 // UTF-16 code units like the JavaScript string length it mirrors.
16 MaxExpressionLength = 21000
17)
18
19// ErrorType classifies an [Error].
20type ErrorType int
21
22// The error types of the reference implementation, in its order.
23const (
24 ErrorUnexpectedSymbol ErrorType = iota
25 // ErrorUnrecognizedNamedValue is declared by the reference but never
26 // produced; ErrorUnrecognizedContext carries the same text.
27 ErrorUnrecognizedNamedValue
28 ErrorUnexpectedEndOfExpression
29 ErrorExceededMaxDepth
30 ErrorExceededMaxLength
31 ErrorTooFewParameters
32 ErrorTooManyParameters
33 ErrorEvenParameters
34 ErrorUnrecognizedContext
35 ErrorUnrecognizedFunction
36)
37
38func (t ErrorType) description() string {
39 switch t {
40 case ErrorUnexpectedEndOfExpression:
41 return "Unexpected end of expression"
42 case ErrorUnexpectedSymbol:
43 return "Unexpected symbol"
44 case ErrorUnrecognizedNamedValue:
45 return "Unrecognized named-value"
46 case ErrorExceededMaxDepth:
47 return fmt.Sprintf("Exceeded max expression depth %d", MaxParserDepth)
48 case ErrorExceededMaxLength:
49 return fmt.Sprintf("Exceeded max expression length %d", MaxExpressionLength)
50 case ErrorTooFewParameters:
51 return "Too few parameters supplied"
52 case ErrorTooManyParameters:
53 return "Too many parameters supplied"
54 case ErrorEvenParameters:
55 return "Even number of parameters supplied, requires an odd number of parameters"
56 case ErrorUnrecognizedContext:
57 return "Unrecognized named-value"
58 case ErrorUnrecognizedFunction:
59 return "Unrecognized function"
60 }
61 return "Unknown error"
62}
63
64// Position is a zero-based line and column in an expression. Columns count
65// UTF-16 code units, as in the reference implementation.
66type Position struct {
67 Line int
68 Column int
69}
70
71// Error is a lexing or parsing error. Its message is exactly the one the
72// reference implementation produces.
73type Error struct {
74 Type ErrorType
75 // Message is the full text, e.g. "Unexpected symbol: ')'".
76 Message string
77 // Pos is where the offending token starts.
78 Pos Position
79 // Token is the text of the offending token.
80 Token string
81}
82
83func (e *Error) Error() string { return e.Message }
84
85// EvalError is an error raised while evaluating an expression: a failing
86// function, such as fromJSON on invalid input, or an invalid format string.
87type EvalError struct {
88 Message string
89 // Err is the underlying error, if any.
90 Err error
91}
92
93func (e *EvalError) Error() string { return e.Message }
94func (e *EvalError) Unwrap() error { return e.Err }
95
96func evalErrorf(format string, a ...any) *EvalError {
97 return &EvalError{Message: fmt.Sprintf(format, a...)}
98}