lex-core 0.8.5

Parser library for the lex format
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
456
457
458
459
460
461
462
//! Property-based tests for parameter parsing
//!
//! These tests ensure that parameter parsing is robust and handles
//! various valid inputs correctly according to the simplified grammar:
//! - Parameters must have key=value format (no boolean shorthand)
//! - Parameters are separated by commas only (not whitespace)
//! - Whitespace around parameters is ignored

use lex_core::lex::assembling::AttachRoot;
use lex_core::lex::escape::escape_quoted;
use lex_core::lex::parsing::engine::parse_from_flat_tokens;
use lex_core::lex::parsing::{parse_document, ContentItem, Document};
use lex_core::lex::testing::assert_ast;
use lex_core::lex::transforms::standard::LEXING;
use lex_core::lex::transforms::Runnable;
use proptest::prelude::*;

fn parse_annotation_without_attachment(source: &str) -> Result<Document, String> {
    let source = if !source.is_empty() && !source.ends_with('\n') {
        format!("{source}\n")
    } else {
        source.to_string()
    };
    let tokens = LEXING.run(source.clone()).map_err(|e| e.to_string())?;
    let root = parse_from_flat_tokens(tokens, &source)?;
    AttachRoot::new().run(root).map_err(|e| e.to_string())
}

/// Generate valid parameter keys
fn parameter_key_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        // Simple keys
        "[a-z][a-z0-9_-]{0,10}",
        // Keys with underscores
        "[a-z][a-z0-9_]{1,10}",
        // Keys with dashes
        "[a-z][a-z0-9-]{1,10}",
        // Mixed
        "[a-z][a-z0-9_-]{2,10}",
    ]
}

/// Generate valid unquoted parameter values
fn unquoted_value_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        // Simple alphanumeric values
        "[a-zA-Z0-9]+",
        // Values with dashes
        "[a-zA-Z0-9-]+",
        // Values with periods (for versions)
        "[0-9]+\\.[0-9]+",
        "[0-9]+\\.[0-9]+\\.[0-9]+",
    ]
}

/// Generate valid quoted parameter values
/// Note: We avoid commas and whitespace-only values for simplicity in testing
fn quoted_value_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        // Simple text with spaces (at least one non-space character)
        "[a-zA-Z0-9][a-zA-Z0-9 ]{0,19}",
        // Text with punctuation (no commas, at least one non-space)
        "[a-zA-Z0-9][a-zA-Z0-9 .-]{0,19}",
        // Simple alphanumeric text
        "[a-zA-Z0-9]{1,10}",
    ]
}

/// Generate semantic content that may contain quotes and backslashes.
/// The returned string is the plain text content (before escaping for embedding in source).
fn escaped_quoted_content_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        // Content with embedded quotes
        "[a-zA-Z]{1,5} \"[a-zA-Z]{1,5}\"",
        // Content with backslashes
        "[a-zA-Z]{1,5}\\\\[a-zA-Z]{1,5}",
        // Content with both
        "[a-zA-Z]{1,3}\\\\[a-zA-Z]{1,3} \"[a-zA-Z]{1,3}\"",
        // Simple content (no escapes needed)
        "[a-zA-Z0-9 ]{1,15}",
    ]
}

/// Generate a single valid parameter (key=value format only)
fn parameter_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        // Unquoted values
        (parameter_key_strategy(), unquoted_value_strategy()).prop_map(|(k, v)| format!("{k}={v}")),
        // Quoted values
        (parameter_key_strategy(), quoted_value_strategy())
            .prop_map(|(k, v)| format!("{k}=\"{v}\"")),
    ]
}

/// Generate valid parameter lists (comma-separated)
fn parameter_list_strategy() -> impl Strategy<Value = String> {
    prop::collection::vec(parameter_strategy(), 1..5).prop_map(|params| params.join(","))
}

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

    // @audit: hardcoded_source
    proptest! {
        // Reduce cases to speed up slow tests
        #![proptest_config(ProptestConfig::with_cases(50))]

        // @audit: hardcoded_source
        #[test]
        fn test_single_parameter_parsing(param in parameter_strategy()) {
            let source = format!(":: note {param} ::\n\nText. {{{{paragraph}}}}\n");
            let result = parse_annotation_without_attachment(&source);

            // Should parse successfully
            prop_assert!(result.is_ok(), "Failed to parse: {}", source);

            if let Ok(doc) = result {
                let annotation = doc.root.children[0].as_annotation().unwrap();
                prop_assert_eq!(annotation.data.parameters.len(), 1);

                // Extract key and value from the parameter string
                let parts: Vec<&str> = param.splitn(2, '=').collect();
                prop_assert_eq!(&annotation.data.parameters[0].key, parts[0]);
            }
        }

        // @audit: hardcoded_source
        #[test]
        fn test_multiple_parameters_parsing(params in parameter_list_strategy()) {
            let source = format!(":: note {params} ::\n\nText. {{{{paragraph}}}}\n");
            let result = parse_annotation_without_attachment(&source);

            // Should parse successfully
            prop_assert!(result.is_ok(), "Failed to parse: {}", source);

            if let Ok(doc) = result {
                let annotation = doc.root.children[0].as_annotation().unwrap();
                let expected_count = params.split(',').count();
                prop_assert_eq!(annotation.data.parameters.len(), expected_count);
            }
        }

        // @audit: hardcoded_source
        #[test]
        fn test_parameter_key_preservation(key in parameter_key_strategy(), value in unquoted_value_strategy()) {
            let source = format!(":: note {key}={value} ::\n\nText. {{{{paragraph}}}}\n");
            let result = parse_annotation_without_attachment(&source);

            prop_assert!(result.is_ok(), "Failed to parse: {}", source);

            if let Ok(doc) = result {
                let annotation = doc.root.children[0].as_annotation().unwrap();
                prop_assert_eq!(&annotation.data.parameters[0].key, &key);
                prop_assert_eq!(&annotation.data.parameters[0].value, &value);
            }
        }

        // @audit: hardcoded_source
        #[test]
        fn test_quoted_value_preservation(key in parameter_key_strategy(), value in quoted_value_strategy()) {
            let source = format!(":: note {key}=\"{value}\" ::\n\nText. {{{{paragraph}}}}\n");
            let result = parse_annotation_without_attachment(&source);

            prop_assert!(result.is_ok(), "Failed to parse: {}", source);

            if let Ok(doc) = result {
                let annotation = doc.root.children[0].as_annotation().unwrap();
                prop_assert_eq!(&annotation.data.parameters[0].key, &key);
                // Quotes are preserved in the value
                let expected_value = format!("\"{value}\"");
                prop_assert_eq!(&annotation.data.parameters[0].value, &expected_value);
            }
        }

        #[test]
        fn test_parameter_order_preservation(params in parameter_list_strategy()) {
            let source = format!(":: note {params} ::\n\nText. {{{{paragraph}}}}\n");
            let result = parse_annotation_without_attachment(&source);

            prop_assert!(result.is_ok(), "Failed to parse: {}", source);

            if let Ok(doc) = result {
                let annotation = doc.root.children[0].as_annotation().unwrap();

                // Extract keys from the parameter string
                let expected_keys: Vec<&str> = params
                    .split(',')
                    .map(|p| p.split('=').next().unwrap())
                    .collect();

                let actual_keys: Vec<&str> = annotation.data.parameters
                    .iter()
                    .map(|p| p.key.as_str())
                    .collect();

                prop_assert_eq!(actual_keys, expected_keys);
            }
        }
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(50))]

        #[test]
        fn test_escaped_quoted_value_roundtrip(
            key in parameter_key_strategy(),
            content in escaped_quoted_content_strategy()
        ) {
            // Escape content for embedding in source (\" and \\)
            let escaped_content = escape_quoted(&content);
            let source = format!(":: note {key}=\"{escaped_content}\" ::\n\nText. {{{{paragraph}}}}\n");
            let result = parse_annotation_without_attachment(&source);

            prop_assert!(result.is_ok(), "Failed to parse: {}", source);

            if let Ok(doc) = result {
                let annotation = doc.root.children[0].as_annotation().unwrap();
                prop_assert_eq!(annotation.data.parameters.len(), 1);
                prop_assert_eq!(&annotation.data.parameters[0].key, &key);
                // Verify unquoted_value recovers the original content
                let recovered = annotation.data.parameters[0].unquoted_value();
                prop_assert_eq!(&recovered, &content,
                    "roundtrip failed: content={:?}, escaped={:?}, stored={:?}",
                    content, escaped_content, annotation.data.parameters[0].value);
            }
        }
    }
}

#[test]
fn test_parameter_only_header_is_not_annotation() {
    let source = ":: severity=high ::\n\nBody. {{paragraph}}\n";
    let doc = parse_document(source).expect("parser should not fail on invalid annotations");

    assert!(doc
        .root
        .children
        .iter()
        .all(|item| !matches!(item, ContentItem::Annotation(_))));
}

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

    #[test]
    fn test_comma_only_separator() {
        let source = ":: note key1=val1,key2=val2,key3=val3 ::\n\nText. {{paragraph}}\n";
        let result = parse_annotation_without_attachment(source);
        assert!(result.is_ok());

        let doc = result.unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(3)
                .has_parameter_with_value("key1", "val1")
                .has_parameter_with_value("key2", "val2")
                .has_parameter_with_value("key3", "val3");
        });
    }

    #[test]
    fn test_whitespace_around_commas_ignored() {
        let source = ":: note key1=val1 , key2=val2 , key3=val3 ::\n\nText. {{paragraph}}\n";
        let result = parse_annotation_without_attachment(source);
        assert!(result.is_ok());

        let doc = result.unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(3)
                .parameter(0, "key1", "val1")
                .parameter(1, "key2", "val2")
                .parameter(2, "key3", "val3");
        });
    }

    #[test]
    fn test_whitespace_around_equals_ignored() {
        let source = ":: note key1 = val1 , key2 = val2 ::\n\nText. {{paragraph}}\n";
        let result = parse_annotation_without_attachment(source);
        assert!(result.is_ok());

        let doc = result.unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(2)
                .has_parameter_with_value("key1", "val1")
                .has_parameter_with_value("key2", "val2");
        });
    }

    #[test]
    fn test_quoted_values_with_spaces() {
        let source = ":: note message=\"Hello World\" ::\n\nText. {{paragraph}}\n";
        let doc = parse_annotation_without_attachment(source).unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(1)
                .has_parameter_with_value("message", "\"Hello World\"");
        });
    }

    #[test]
    fn test_quoted_values_with_commas() {
        let source = ":: note message=\"value with, comma\" ::\n\nText. {{paragraph}}\n";
        let doc = parse_annotation_without_attachment(source).unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(1)
                .has_parameter_with_value("message", "\"value with, comma\"");
        });
    }

    #[test]
    fn test_empty_quoted_value() {
        let source = ":: note message=\"\" ::\n\nText. {{paragraph}}\n";
        let doc = parse_annotation_without_attachment(source).unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(1)
                .has_parameter_with_value("message", "\"\"");
        });
    }

    #[test]
    fn test_version_number_values() {
        let source = ":: note version=3.11.2 ::\n\nText. {{paragraph}}\n";
        let doc = parse_annotation_without_attachment(source).unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(1)
                .has_parameter_with_value("version", "3.11.2");
        });
    }

    #[test]
    fn test_keys_with_dashes_and_underscores() {
        let source = ":: note ref-id=123,api_version=2 ::\n\nText. {{paragraph}}\n";
        let doc = parse_annotation_without_attachment(source).unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(2)
                .parameter(0, "ref-id", "123")
                .parameter(1, "api_version", "2");
        });
    }

    #[test]
    fn test_escaped_quote_in_quoted_value() {
        let source = ":: note message=\"say \\\"hello\\\"\" ::\n\nText. {{paragraph}}\n";
        let doc = parse_annotation_without_attachment(source).unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(1)
                .has_parameter_with_value("message", "\"say \\\"hello\\\"\"");
        });
        // Verify unquoted_value resolves escapes
        let annotation = doc.root.children[0].as_annotation().unwrap();
        assert_eq!(
            annotation.data.parameters[0].unquoted_value(),
            "say \"hello\""
        );
    }

    #[test]
    fn test_escaped_backslash_in_quoted_value() {
        let source = ":: note path=\"C:\\\\Users\\\\name\" ::\n\nText. {{paragraph}}\n";
        let doc = parse_annotation_without_attachment(source).unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(1)
                .has_parameter_with_value("path", "\"C:\\\\Users\\\\name\"");
        });
        let annotation = doc.root.children[0].as_annotation().unwrap();
        assert_eq!(
            annotation.data.parameters[0].unquoted_value(),
            "C:\\Users\\name"
        );
    }

    #[test]
    fn test_escaped_backslash_before_closing_quote() {
        // \\" = escaped backslash then real closing quote
        let source = ":: note trail=\"end\\\\\" ::\n\nText. {{paragraph}}\n";
        let doc = parse_annotation_without_attachment(source).unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(1)
                .has_parameter_with_value("trail", "\"end\\\\\"");
        });
        let annotation = doc.root.children[0].as_annotation().unwrap();
        assert_eq!(annotation.data.parameters[0].unquoted_value(), "end\\");
    }

    #[test]
    fn test_unquoted_value_has_no_escaping() {
        let source = ":: note key=simple ::\n\nText. {{paragraph}}\n";
        let doc = parse_annotation_without_attachment(source).unwrap();
        let annotation = doc.root.children[0].as_annotation().unwrap();
        assert_eq!(annotation.data.parameters[0].unquoted_value(), "simple");
    }

    #[test]
    fn test_quoted_value_unquoted_value_strips_quotes() {
        let source = ":: note message=\"Hello World\" ::\n\nText. {{paragraph}}\n";
        let doc = parse_annotation_without_attachment(source).unwrap();
        let annotation = doc.root.children[0].as_annotation().unwrap();
        assert_eq!(
            annotation.data.parameters[0].unquoted_value(),
            "Hello World"
        );
    }

    #[test]
    fn test_lex_marker_inside_quoted_value() {
        let source = ":: note foo=\":: jane\" ::\n\nText. {{paragraph}}\n";
        let doc = parse_annotation_without_attachment(source).unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(1)
                .has_parameter_with_value("foo", "\":: jane\"");
        });
    }

    #[test]
    fn test_multiple_lex_markers_inside_quoted_value() {
        let source = ":: note msg=\"a :: b :: c\" ::\n\nText. {{paragraph}}\n";
        let doc = parse_annotation_without_attachment(source).unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(1)
                .has_parameter_with_value("msg", "\"a :: b :: c\"");
        });
    }

    #[test]
    fn test_single_colon_inside_quoted_value() {
        let source = ":: note title=\"Chapter: Introduction\" ::\n\nText. {{paragraph}}\n";
        let doc = parse_annotation_without_attachment(source).unwrap();
        assert_ast(&doc).item(0, |item| {
            item.assert_annotation()
                .label("note")
                .parameter_count(1)
                .has_parameter_with_value("title", "\"Chapter: Introduction\"");
        });
    }
}