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
// 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.
//! Lexical analysis subsystem for HEDL.
//!
//! This module provides the complete lexical analysis infrastructure for HEDL,
//! consolidating functionality from `hedl-lex`, `hedl-row`, and `hedl-tensor`
//! into a unified, DRY, and modular architecture.
//!
//! # Module Structure
//!
//! - [`error`] - Unified error types for all lexer operations
//! - [`span`] - Source position and span tracking for error reporting
//! - [`tokens`] - Token validation and parsing (keys, types, IDs, references)
//! - [`row`] - CSV/matrix row parsing with expression and tensor support
//! - [`tensor`] - Multi-dimensional tensor literal parsing
//! - [`incremental`] - Incremental parsing for IDE integration
//!
//! # Examples
//!
//! ## Token Validation
//!
//! ```
//! use hedl_core::lex::{is_valid_key_token, is_valid_type_name, is_valid_id_token};
//!
//! assert!(is_valid_key_token("user_name")); // snake_case
//! assert!(is_valid_type_name("UserProfile")); // PascalCase
//! assert!(is_valid_id_token("SKU-4020")); // IDs with hyphens
//! ```
//!
//! ## Reference Parsing
//!
//! ```
//! use hedl_core::lex::{parse_reference, Reference};
//!
//! let local = parse_reference("@user_1").unwrap();
//! assert!(local.is_local());
//!
//! let qualified = parse_reference("@User:user_1").unwrap();
//! assert!(qualified.is_qualified());
//! assert_eq!(qualified.type_name, Some("User".to_string()));
//! ```
//!
//! ## CSV Row Parsing
//!
//! ```
//! use hedl_core::lex::parse_csv_row;
//!
//! let fields = parse_csv_row("id, name, [1, 2, 3]").unwrap();
//! assert_eq!(fields.len(), 3);
//! assert_eq!(fields[2].value, "[1, 2, 3]");
//! ```
//!
//! ## Tensor Parsing
//!
//! ```
//! use hedl_core::lex::{parse_tensor, is_tensor_literal, Tensor};
//!
//! assert!(is_tensor_literal("[1, 2, 3]"));
//!
//! let tensor = parse_tensor("[[1, 2], [3, 4]]").unwrap();
//! assert_eq!(tensor.shape(), vec![2, 2]);
//! assert_eq!(tensor.flatten(), vec![1.0, 2.0, 3.0, 4.0]);
//! ```
//!
//! # Security
//!
//! All parsing operations enforce resource limits to prevent DoS attacks:
//! - Maximum string lengths
//! - Maximum recursion depths
//! - Maximum element counts
//! - Rejection of NaN/Infinity values
//!
//! # Migration from Separate Crates
//!
//! This module consolidates the following crates:
//! - `hedl-lex` -> `hedl_core::lex` (tokens, spans, config, regions, etc.)
//! - `hedl-row` -> `hedl_core::lex::row` (CSV parsing)
//! - `hedl-tensor` -> `hedl_core::lex::tensor` (tensor parsing)
//!
//! The consolidation unified tokens, spans, config, regions, and format-specific
//! parsing into a single module hierarchy.
// Core modules
/// Unified error types for lexical analysis, CSV parsing, and tensor parsing.
/// Source position and span tracking for error reporting.
/// Token validation and parsing for keys, types, IDs, and references.
// Extended modules from hedl-lex
/// Arena allocation for HEDL parsing.
/// Configuration for lexical analysis with resource limits.
/// Parsing utilities for parenthesized count hints.
/// CSV parsing for HEDL matrix rows.
/// Directive parsing for HEDL header section.
/// Expression AST for HEDL `$(...)` expressions.
/// Incremental parsing support for efficient IDE integration.
/// Indentation handling for HEDL.
/// Value inference for HEDL scalars.
/// Protected region scanning for HEDL.
/// String manipulation utilities for HEDL format conversion.
// Data modules from hedl-row and hedl-tensor
/// CSV row parsing state machine for HEDL matrix rows.
/// Tensor literal parsing for HEDL format.
// Re-export error types
pub use ;
// Re-export span types
pub use ;
// Re-export token types and functions
pub use ;
// Re-export expression types and functions
pub use ;
// Re-export configuration
pub use LexConfig;
// Re-export string utilities
pub use singularize_and_capitalize;
// Re-export indent handling
pub use ;
// Re-export directive parsing
pub use ;
// Re-export region scanning
pub use ;
// Re-export value inference
pub use ;
// Re-export CSV parsing (from row module which handles tensors correctly)
pub use ;
// Re-export incremental parsing
pub use ;
// Re-export tensor types and functions
pub use ;
// Re-export arena allocation
pub use ;
// Re-export count hint parsing for header module