hedl-core 2.0.0

Core parser and data model for HEDL (Hierarchical Entity Data Language)
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
// Dweve HEDL - Hierarchical Entity Data Language
//
// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Unified error types for lexical analysis, CSV parsing, and tensor parsing.
//!
//! This module consolidates all parsing-related error types into a single hierarchy,
//! providing consistent error handling across the lexer subsystem.

use thiserror::Error;

// Re-export SourcePos from span module for backward compatibility
pub use crate::lex::span::SourcePos;

/// Unified error type for all lexical analysis operations.
///
/// This enum consolidates errors from:
/// - Token scanning and validation
/// - CSV/matrix row parsing
/// - Tensor literal parsing
/// - Resource limit enforcement
#[derive(Debug, Clone, Error, PartialEq)]
pub enum LexError {
    // ==================== Indentation errors ====================
    /// Invalid indentation (must be multiple of 2 spaces).
    #[error("line {}, column {}: invalid indentation: {} spaces (must be multiple of 2)", .pos.line(), .pos.column(), .spaces)]
    InvalidIndentation {
        /// Number of spaces found.
        spaces: usize,
        /// Source position of the error.
        pos: SourcePos,
    },

    /// Tab character in indentation (tabs not allowed).
    #[error("line {}, column {}: tab character not allowed for indentation", .pos.line(), .pos.column())]
    TabInIndentation {
        /// Source position of the tab.
        pos: SourcePos,
    },

    /// Indentation too deep.
    #[error("line {}, column {}: indent depth {} exceeds maximum {}", .pos.line(), .pos.column(), .depth, .max)]
    IndentTooDeep {
        /// Current indentation depth.
        depth: usize,
        /// Maximum allowed depth.
        max: usize,
        /// Source position of the error.
        pos: SourcePos,
    },

    // ==================== String/expression errors ====================
    /// Unclosed quoted string.
    #[error("line {}, column {}: unclosed quoted string", .pos.line(), .pos.column())]
    UnclosedQuote {
        /// Source position of the opening quote.
        pos: SourcePos,
    },

    /// Unclosed expression.
    #[error("line {}, column {}: unclosed expression", .pos.line(), .pos.column())]
    UnclosedExpression {
        /// Source position of the expression start.
        pos: SourcePos,
    },

    /// Invalid escape sequence.
    #[error("line {}, column {}: invalid escape sequence '{}'", .pos.line(), .pos.column(), .sequence)]
    InvalidEscape {
        /// The invalid escape sequence.
        sequence: String,
        /// Source position of the escape.
        pos: SourcePos,
    },

    // ==================== Token errors ====================
    /// Invalid reference format.
    #[error("line {}, column {}: invalid reference format: {}", .pos.line(), .pos.column(), .message)]
    InvalidReference {
        /// Description of the format error.
        message: String,
        /// Source position of the reference.
        pos: SourcePos,
    },

    /// Invalid token.
    #[error("line {}, column {}: invalid token: {}", .pos.line(), .pos.column(), .message)]
    InvalidToken {
        /// Description of the token error.
        message: String,
        /// Source position of the token.
        pos: SourcePos,
    },

    // ==================== Resource limit errors ====================
    /// String too long.
    #[error("line {}, column {}: string length {} exceeds maximum {}", .pos.line(), .pos.column(), .length, .max)]
    StringTooLong {
        /// Actual string length.
        length: usize,
        /// Maximum allowed length.
        max: usize,
        /// Source position of the string.
        pos: SourcePos,
    },

    /// Recursion too deep.
    #[error("line {}, column {}: recursion depth {} exceeds maximum {}", .pos.line(), .pos.column(), .depth, .max)]
    RecursionTooDeep {
        /// Current recursion depth.
        depth: usize,
        /// Maximum allowed depth.
        max: usize,
        /// Source position of the error.
        pos: SourcePos,
    },

    /// Too many fields.
    #[error("line {}, column {}: field count {} exceeds maximum {}", .pos.line(), .pos.column(), .count, .max)]
    TooManyFields {
        /// Actual field count.
        count: usize,
        /// Maximum allowed count.
        max: usize,
        /// Source position of the error.
        pos: SourcePos,
    },

    /// Parenthesis depth exceeded.
    #[error("line {}, column {}: parenthesis depth {} exceeds maximum {}", .pos.line(), .pos.column(), .depth, .max)]
    ParenthesisDepthExceeded {
        /// Current parenthesis depth.
        depth: usize,
        /// Maximum allowed depth.
        max: usize,
        /// Source position of the error.
        pos: SourcePos,
    },

    // ==================== CSV/Row parsing errors ====================
    /// Trailing comma in CSV row.
    #[error("trailing comma not allowed in matrix row")]
    TrailingComma,

    /// Expected comma after closing quote.
    #[error("expected comma after closing quote, got '{0}'")]
    ExpectedCommaAfterQuote(char),

    /// Quote character in unquoted field.
    #[error("quote character found in unquoted CSV field: '{0}'")]
    QuoteInUnquotedField(String),

    // ==================== Tensor parsing errors ====================
    /// Unbalanced brackets in tensor literal.
    #[error("unbalanced brackets in tensor literal")]
    UnbalancedBrackets,

    /// Invalid number in tensor.
    #[error("invalid number in tensor: {0}")]
    InvalidNumber(String),

    /// Empty tensor not allowed.
    #[error("empty tensor not allowed")]
    EmptyTensor,

    /// Mixed types in tensor.
    #[error("mixed types in tensor (expected number, got '{0}')")]
    MixedTypes(String),

    /// Inconsistent dimensions in tensor.
    #[error("inconsistent dimensions in tensor")]
    InconsistentDimensions,

    /// Unexpected character in tensor.
    #[error("unexpected character in tensor: '{0}'")]
    UnexpectedChar(char),

    /// Invalid tensor structure.
    #[error("invalid tensor structure: {0}")]
    InvalidStructure(String),
}

impl LexError {
    /// Get the position where this error occurred, if available.
    ///
    /// Returns `None` for errors that don't have position information
    /// (e.g., some CSV and tensor parsing errors).
    #[inline]
    pub fn position(&self) -> Option<SourcePos> {
        match self {
            LexError::InvalidIndentation { pos, .. } => Some(*pos),
            LexError::TabInIndentation { pos } => Some(*pos),
            LexError::IndentTooDeep { pos, .. } => Some(*pos),
            LexError::UnclosedQuote { pos } => Some(*pos),
            LexError::UnclosedExpression { pos } => Some(*pos),
            LexError::InvalidEscape { pos, .. } => Some(*pos),
            LexError::InvalidReference { pos, .. } => Some(*pos),
            LexError::InvalidToken { pos, .. } => Some(*pos),
            LexError::StringTooLong { pos, .. } => Some(*pos),
            LexError::RecursionTooDeep { pos, .. } => Some(*pos),
            LexError::TooManyFields { pos, .. } => Some(*pos),
            LexError::ParenthesisDepthExceeded { pos, .. } => Some(*pos),
            // Errors without position information
            LexError::TrailingComma
            | LexError::ExpectedCommaAfterQuote(_)
            | LexError::QuoteInUnquotedField(_)
            | LexError::UnbalancedBrackets
            | LexError::InvalidNumber(_)
            | LexError::EmptyTensor
            | LexError::MixedTypes(_)
            | LexError::InconsistentDimensions
            | LexError::UnexpectedChar(_)
            | LexError::InvalidStructure(_) => None,
        }
    }

    /// Returns `true` if this is a resource limit error.
    #[inline]
    pub fn is_resource_limit(&self) -> bool {
        matches!(
            self,
            LexError::StringTooLong { .. }
                | LexError::RecursionTooDeep { .. }
                | LexError::TooManyFields { .. }
                | LexError::ParenthesisDepthExceeded { .. }
                | LexError::IndentTooDeep { .. }
        )
    }

    /// Returns `true` if this is a tensor parsing error.
    #[inline]
    pub fn is_tensor_error(&self) -> bool {
        matches!(
            self,
            LexError::UnbalancedBrackets
                | LexError::InvalidNumber(_)
                | LexError::EmptyTensor
                | LexError::MixedTypes(_)
                | LexError::InconsistentDimensions
                | LexError::UnexpectedChar(_)
                | LexError::InvalidStructure(_)
        )
    }

    /// Returns `true` if this is a CSV/row parsing error.
    #[inline]
    pub fn is_csv_error(&self) -> bool {
        matches!(
            self,
            LexError::TrailingComma
                | LexError::ExpectedCommaAfterQuote(_)
                | LexError::QuoteInUnquotedField(_)
                | LexError::UnclosedQuote { .. }
                | LexError::UnclosedExpression { .. }
                | LexError::InvalidEscape { .. }
        )
    }
}

/// Result type for lexer operations.
pub type LexResult<T> = Result<T, LexError>;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_source_pos_new() {
        let pos = SourcePos::new(10, 5);
        assert_eq!(pos.line(), 10);
        assert_eq!(pos.column(), 5);
    }

    #[test]
    fn test_source_pos_display() {
        let pos = SourcePos::new(42, 15);
        assert_eq!(format!("{}", pos), "line 42, column 15");
    }

    #[test]
    fn test_source_pos_default() {
        let pos = SourcePos::default();
        assert_eq!(pos.line(), 0);
        assert_eq!(pos.column(), 0);
    }

    #[test]
    fn test_error_position_extraction() {
        let pos = SourcePos::new(10, 20);

        assert_eq!(
            LexError::InvalidIndentation { spaces: 3, pos }.position(),
            Some(pos)
        );
        assert_eq!(LexError::TabInIndentation { pos }.position(), Some(pos));
        assert_eq!(LexError::UnclosedQuote { pos }.position(), Some(pos));

        // Errors without position
        assert_eq!(LexError::TrailingComma.position(), None);
        assert_eq!(LexError::EmptyTensor.position(), None);
        assert_eq!(LexError::UnbalancedBrackets.position(), None);
    }

    #[test]
    fn test_is_resource_limit() {
        let pos = SourcePos::new(1, 1);

        assert!(LexError::StringTooLong {
            length: 100,
            max: 50,
            pos
        }
        .is_resource_limit());
        assert!(LexError::RecursionTooDeep {
            depth: 10,
            max: 5,
            pos
        }
        .is_resource_limit());
        assert!(LexError::TooManyFields {
            count: 100,
            max: 50,
            pos
        }
        .is_resource_limit());
        assert!(LexError::IndentTooDeep {
            depth: 10,
            max: 5,
            pos
        }
        .is_resource_limit());

        assert!(!LexError::TrailingComma.is_resource_limit());
        assert!(!LexError::EmptyTensor.is_resource_limit());
    }

    #[test]
    fn test_is_tensor_error() {
        assert!(LexError::UnbalancedBrackets.is_tensor_error());
        assert!(LexError::EmptyTensor.is_tensor_error());
        assert!(LexError::InconsistentDimensions.is_tensor_error());
        assert!(LexError::InvalidNumber("abc".to_string()).is_tensor_error());
        assert!(LexError::UnexpectedChar('x').is_tensor_error());

        assert!(!LexError::TrailingComma.is_tensor_error());
        assert!(!LexError::UnclosedQuote {
            pos: SourcePos::new(1, 1)
        }
        .is_tensor_error());
    }

    #[test]
    fn test_is_csv_error() {
        assert!(LexError::TrailingComma.is_csv_error());
        assert!(LexError::ExpectedCommaAfterQuote('x').is_csv_error());
        assert!(LexError::QuoteInUnquotedField("test".to_string()).is_csv_error());
        assert!(LexError::UnclosedQuote {
            pos: SourcePos::new(1, 1)
        }
        .is_csv_error());
        assert!(LexError::UnclosedExpression {
            pos: SourcePos::new(1, 1)
        }
        .is_csv_error());

        assert!(!LexError::EmptyTensor.is_csv_error());
        assert!(!LexError::UnbalancedBrackets.is_csv_error());
    }

    #[test]
    fn test_error_display() {
        let pos = SourcePos::new(5, 10);

        let err = LexError::InvalidIndentation { spaces: 3, pos };
        let msg = format!("{}", err);
        assert!(msg.contains("line 5"));
        assert!(msg.contains("column 10"));
        assert!(msg.contains("3 spaces"));

        let err = LexError::TrailingComma;
        assert_eq!(
            format!("{}", err),
            "trailing comma not allowed in matrix row"
        );

        let err = LexError::EmptyTensor;
        assert_eq!(format!("{}", err), "empty tensor not allowed");
    }

    #[test]
    fn test_error_equality() {
        let pos = SourcePos::new(1, 1);

        assert_eq!(
            LexError::UnclosedQuote { pos },
            LexError::UnclosedQuote { pos }
        );
        assert_ne!(
            LexError::UnclosedQuote { pos },
            LexError::UnclosedExpression { pos }
        );
        assert_eq!(LexError::TrailingComma, LexError::TrailingComma);
        assert_eq!(LexError::EmptyTensor, LexError::EmptyTensor);
    }

    #[test]
    fn test_error_clone() {
        let pos = SourcePos::new(5, 10);
        let original = LexError::InvalidToken {
            message: "test".to_string(),
            pos,
        };
        let cloned = original.clone();
        assert_eq!(original, cloned);
    }

    #[test]
    fn test_error_is_std_error() {
        fn accepts_error<E: std::error::Error>(_: E) {}
        accepts_error(LexError::TrailingComma);
        accepts_error(LexError::EmptyTensor);
        accepts_error(LexError::UnclosedQuote {
            pos: SourcePos::new(1, 1),
        });
    }
}