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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Core parsing and schema types for COBOL copybooks
//!
//! This crate provides the fundamental types and parsing logic for COBOL copybook
//! processing, including AST construction, layout resolution, and schema validation.
//!
// Allow missing inline for public methods in this library - too many methods to inline individually
//! ## Key Features
//!
//! ### Enhanced COBOL Support
//! - **Level-88 Condition Values**: Full support for condition name definitions with VALUE clauses
//! - **Binary Width Syntax**: Support for explicit `BINARY(n)` width specifications
//! - **Comprehensive Numeric Types**: Full parsing of zoned, packed, and binary field definitions
//! - **Recursion Limits**: Parser protection against deeply nested or malformed copybooks
//! - **Error Context**: Detailed error reporting with field paths and source locations
//!
//! ### Schema Generation
//! - **Field Hierarchy**: Complete representation of COBOL data structures with Level-88 conditions
//! - **Layout Resolution**: Accurate byte offset and size calculations
//! - **Structural Validation**: Comprehensive checks for ODO positioning, Level-88 placement, REDEFINES compatibility
//!
//! ### Parser Improvements
//! - **Robustness**: Enhanced handling of edge cases and malformed input
//! - **Performance**: Efficient parsing with minimal memory allocation
//! - **Standards Compliance**: Adherence to COBOL copybook syntax standards
//! - **Safety**: Zero unsafe code with comprehensive validation
//!
//! ## Usage Example
//!
//! ```rust
//! use copybook_core::{parse_copybook, parse_copybook_with_options, ParseOptions, FieldKind};
//!
//! // Parse a copybook with Level-88 condition values
//! let copybook_text = r#"
//! 01 CUSTOMER-RECORD.
//! 05 CUSTOMER-STATUS PIC X(1).
//! 88 STATUS-ACTIVE VALUE 'A'.
//! 88 STATUS-INACTIVE VALUE 'I'.
//! 88 STATUS-PENDING VALUE 'P'.
//! 05 CUSTOMER-ID PIC 9(6).
//! 05 ORDER-COUNT PIC 9(3).
//! 05 ORDERS OCCURS 1 TO 100 TIMES
//! DEPENDING ON ORDER-COUNT.
//! 10 ORDER-ID PIC 9(8).
//! 88 RUSH-ORDER VALUE 99999999.
//! "#;
//!
//! // Parse with default options and handle any errors gracefully
//! let schema_default = match parse_copybook(copybook_text) {
//! Ok(schema) => schema,
//! Err(error) => {
//! eprintln!("Failed to parse copybook: {error}");
//! return;
//! }
//! };
//! println!(
//! "Default parsing produced {} top-level fields",
//! schema_default.fields.len()
//! );
//!
//! // Parse with custom options for enhanced validation
//! let options = ParseOptions {
//! allow_inline_comments: true,
//! strict: true,
//! ..Default::default()
//! };
//! let schema_strict = match parse_copybook_with_options(copybook_text, &options) {
//! Ok(schema) => schema,
//! Err(error) => {
//! eprintln!("Strict parsing failed: {error}");
//! return;
//! }
//! };
//!
//! // Examine the parsed schema including Level-88 conditions
//! for field in &schema_strict.fields {
//! match &field.kind {
//! FieldKind::Condition { values } => {
//! println!("Level-88 condition '{}' with values: {:?}", field.name, values);
//! }
//! FieldKind::Group => {
//! println!("Group field: {}", field.name);
//! }
//! _ => {
//! println!(
//! "Data field: {} (offset: {}, size: {})",
//! field.name, field.offset, field.len
//! );
//! }
//! }
//! }
//! ```
/// Dialect contract for ODO `min_count` semantics (Normative, ZeroTolerant, OneTolerant).
/// Re-export of all error types from [`copybook_error`].
/// Structured error reporting with severity levels and summary statistics.
/// Compile-time and runtime feature flag governance.
/// Layout resolution: byte offsets, REDEFINES, and OCCURS DEPENDING ON.
/// Lexical analysis of COBOL copybook source text.
/// Recursive-descent parser producing the [`Schema`] AST from tokens.
/// PICTURE clause analysis and field-size computation.
/// Field projection for selective decode/encode (`--select`).
/// Core schema types: [`Schema`], [`Field`], [`FieldKind`], and related structures.
/// COBOL feature support matrix and status registry.
/// Shared utility functions and extension traits.
pub use ;
pub use Dialect;
pub use ;
pub use ;
pub use ;
pub use ParseOptions;
pub use project_schema;
pub use ;
pub use *;
// Performance-optimized audit stubs when audit feature is disabled
/// Parse a COBOL copybook into a structured schema
///
/// # Examples
///
/// ```
/// use copybook_core::parse_copybook;
///
/// let schema = parse_copybook("01 REC.\n 05 FLD PIC X(10).").unwrap();
/// assert_eq!(schema.fields.len(), 1);
/// assert_eq!(schema.fields[0].name, "REC");
/// assert_eq!(schema.lrecl_fixed, Some(10));
/// ```
///
/// # Errors
/// Returns an error if the copybook contains syntax errors or unsupported features.
/// Parse a COBOL copybook with specific options
///
/// # Examples
///
/// ```
/// use copybook_core::{parse_copybook_with_options, ParseOptions, Dialect};
///
/// let options = ParseOptions {
/// dialect: Dialect::ZeroTolerant,
/// ..ParseOptions::default()
/// };
/// let schema = parse_copybook_with_options(
/// "01 REC.\n 05 CNT PIC 9.\n 05 ARR OCCURS 0 TO 5 DEPENDING ON CNT PIC X(3).",
/// &options,
/// ).unwrap();
/// assert_eq!(schema.lrecl_fixed, Some(16)); // 1 + 5*3
/// ```
///
/// # Errors
/// Returns an error if the copybook contains syntax errors or unsupported features.
/// Parse a COBOL copybook with specific options and feature flags
///
/// # Examples
///
/// ```
/// use copybook_core::{parse_copybook_with_feature_flags, ParseOptions, FeatureFlags, Feature};
///
/// let options = ParseOptions::default();
/// let mut feature_flags = FeatureFlags::default();
/// feature_flags.disable(Feature::SignSeparate);
/// let source = "01 AMOUNT PIC S9(5) SIGN IS LEADING SEPARATE.";
/// assert!(parse_copybook_with_feature_flags(source, &options, &feature_flags).is_err());
/// ```
///
/// # Errors
/// Returns an error if the copybook contains syntax errors or unsupported features.