formatparse-pyo3 0.8.1

PyO3 bindings for formatparse (native _formatparse extension; use PyPI for Python installs)
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
use crate::parser::matching::FieldCaptureSlices;
use crate::result::ParseResult;
use fancy_regex::Regex;
use formatparse_core::count_capturing_groups;
use formatparse_core::parser::MAX_FIELDS;
use formatparse_core::{FieldSpec, FieldType};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use std::collections::HashMap;
use std::sync::Arc;

/// Field layout produced at pattern-compile time (narrow interface for matchers).
pub(crate) struct CompiledFields {
    pub field_specs: Vec<FieldSpec>,
    pub field_names: Vec<Option<String>>,
    pub normalized_names: Vec<Option<String>>,
    pub custom_type_groups: Vec<usize>,
    pub has_nested_dict_fields: Vec<bool>,
    pub nested_parsers: Vec<Option<Arc<FormatParser>>>,
    pub field_count: usize,
}

impl CompiledFields {
    pub fn capture_slices(&self) -> FieldCaptureSlices<'_> {
        FieldCaptureSlices {
            field_specs: &self.field_specs,
            field_names: &self.field_names,
            normalized_names: &self.normalized_names,
            custom_type_groups: &self.custom_type_groups,
            has_nested_dict_fields: &self.has_nested_dict_fields,
            nested_parsers: &self.nested_parsers,
        }
    }
}

#[pyclass(module = "_formatparse")]
/// Compiled format pattern for parsing strings.
///
/// Construct with :func:`formatparse.compile` (or ``FormatParser(pattern, extra_types=...)`` in Python).
///
/// **Custom types:** converters passed as ``extra_types`` at compile time are stored
/// and merged with any ``extra_types`` passed per call to ``parse`` or ``search``
/// (per-call keys override stored keys).
///
/// **Pickling:** Only the pattern string is serialized. If the parser was built
/// with ``extra_types``, those converters are **not** restored after unpickling;
/// call ``compile(pattern, extra_types=...)`` again with the same mapping.
pub struct FormatParser {
    #[pyo3(get)]
    // Note: This field is actually used in __getstate__, format getter, and accessed from Python.
    // The dead_code warning is a false positive - the compiler doesn't recognize PyO3 getter usage.
    pub pattern: String,
    pub(crate) regex: Regex,
    pub(crate) regex_str: String, // Store the regex string for _expression property
    pub(crate) regex_case_insensitive: Option<Regex>,
    pub(crate) search_regex: Regex, // Pre-compiled search regex (case-sensitive, no anchors)
    pub(crate) search_regex_case_insensitive: Option<Regex>, // Pre-compiled search regex (case-insensitive, no anchors)
    pub(crate) fields: CompiledFields,
    #[allow(dead_code)]
    pub(crate) name_mapping: std::collections::HashMap<String, String>, // Map normalized -> original
    pub(crate) stored_extra_types: Option<HashMap<String, PyObject>>, // Store extra_types for use during conversion
    pub(crate) allows_empty_default_string_match: bool, // True iff parse("") can use empty-field fast path (issue #16)
}

impl FormatParser {
    /// Returns true when this parser matches a cache lookup: same normalized pattern and
    /// the same `extra_types` fingerprint as [`crate::extract_extra_types_identity`].
    /// Used after an LRU hit on the hash key to rule out collisions.
    pub(crate) fn matches_pattern_cache_request(
        &self,
        py: Python<'_>,
        normalized_pattern: &str,
        extra_types: &Option<HashMap<String, PyObject>>,
    ) -> bool {
        if self.pattern != normalized_pattern {
            return false;
        }
        let requested = crate::extract_extra_types_identity(py, extra_types);
        let stored = crate::extract_extra_types_identity(py, &self.stored_extra_types);
        requested == stored
    }

    pub fn new(pattern: &str) -> PyResult<Self> {
        Self::new_with_extra_types(pattern, None)
    }

    pub fn new_with_extra_types(
        pattern: &str,
        extra_types: Option<HashMap<String, PyObject>>,
    ) -> PyResult<Self> {
        let pattern_owned = crate::pattern_normalize::prepare_compiled_pattern(pattern)?;
        let custom_patterns = Python::with_gil(|py| -> PyResult<HashMap<String, String>> {
            let mut patterns = HashMap::new();
            if let Some(ref extra_types_map) = extra_types {
                for (name, converter_obj) in extra_types_map {
                    // Try to get the pattern attribute from the converter function
                    let converter_ref = converter_obj.bind(py);
                    if let Ok(pattern_attr) = converter_ref.getattr("pattern") {
                        if let Ok(pattern_str) = pattern_attr.extract::<String>() {
                            patterns.insert(name.clone(), pattern_str);
                        }
                    }
                }
            }
            Ok(patterns)
        })?;

        let (
            regex_str_with_anchors,
            regex_str,
            field_specs,
            field_names,
            normalized_names,
            name_mapping,
            allows_empty_default_string_match,
        ) = formatparse_core::parser::pattern::parse_pattern(
            &pattern_owned,
            &custom_patterns,
            true,
            0,
        )
        .map_err(crate::parser::pattern::pattern_compile_error_to_py)?;

        // Search/findall use a separate compile path without "empty delimited" `.*?` groups so
        // unanchored matching does not stop early (e.g. `{}, {}` on "Hello, World").
        let (regex_str_search_anchored, _, _, _, _, _, _) =
            formatparse_core::parser::pattern::parse_pattern(
                &pattern_owned,
                &custom_patterns,
                false,
                0,
            )
            .map_err(crate::parser::pattern::pattern_compile_error_to_py)?;

        // Validate field count
        if field_specs.len() > MAX_FIELDS {
            return Err(PyValueError::new_err(format!(
                "Pattern contains {} fields, which exceeds the maximum allowed count of {}",
                field_specs.len(),
                MAX_FIELDS
            )));
        }

        let nested_parsers: Vec<Option<Arc<FormatParser>>> =
            Python::with_gil(|py| -> PyResult<_> {
                let mut out = Vec::with_capacity(field_specs.len());
                for spec in &field_specs {
                    if matches!(spec.field_type, FieldType::Nested) {
                        let sub = spec.nested_subpattern.as_ref().ok_or_else(|| {
                            PyValueError::new_err("internal error: nested field missing subpattern")
                        })?;
                        let cloned_et = extra_types.as_ref().map(|m| {
                            m.iter()
                                .map(|(k, v)| (k.clone(), v.clone_ref(py)))
                                .collect::<HashMap<_, _>>()
                        });
                        out.push(Some(Arc::new(FormatParser::new_with_extra_types(
                            sub, cloned_et,
                        )?)));
                    } else {
                        out.push(None);
                    }
                }
                Ok(out)
            })?;
        // Pre-compute custom type validation results (pattern_groups per field)
        // This avoids calling validate_custom_type_pattern for every match
        let custom_type_groups = Python::with_gil(|py| -> PyResult<Vec<usize>> {
            let mut groups = Vec::with_capacity(field_specs.len());
            let empty_map = std::collections::HashMap::new();
            let custom_converters = extra_types
                .as_ref()
                .map(|et| et as &HashMap<String, PyObject>)
                .unwrap_or(&empty_map);

            for spec in &field_specs {
                let pattern_groups = if matches!(spec.field_type, FieldType::Nested) {
                    spec.nested_regex_body
                        .as_ref()
                        .map(|b| count_capturing_groups(b))
                        .unwrap_or(0)
                } else if !custom_converters.is_empty() {
                    crate::parser::matching::validate_custom_type_pattern(
                        spec,
                        custom_converters,
                        py,
                    )?
                } else {
                    0
                };
                groups.push(pattern_groups);
            }
            Ok(groups)
        })?;

        // Pre-compute which fields have nested dict names (contain '[')
        // This avoids checking original_name.contains('[') in the hot path
        let has_nested_dict_fields: Vec<bool> = field_names
            .iter()
            .map(|name_opt| name_opt.as_ref().map(|n| n.contains('[')).unwrap_or(false))
            .collect();

        // Build regex with DOTALL flag
        let regex = formatparse_core::build_regex(&regex_str_with_anchors)
            .map_err(crate::error::core_error_to_py_err)?;

        let regex_search_anchored = formatparse_core::build_regex(&regex_str_search_anchored)
            .map_err(crate::error::core_error_to_py_err)?;

        // Build case-insensitive regex
        let regex_case_insensitive =
            formatparse_core::build_case_insensitive_regex(&regex_str_with_anchors);

        // Pre-compile search regex variants (without anchors)
        let search_regex =
            formatparse_core::build_search_regex(regex_search_anchored.as_str(), true)
                .map_err(crate::error::core_error_to_py_err)?;
        let search_regex_case_insensitive =
            formatparse_core::build_search_regex(regex_search_anchored.as_str(), false).ok();

        let field_count = field_specs.len();
        Ok(Self {
            pattern: pattern_owned,
            regex,
            regex_str,
            regex_case_insensitive,
            search_regex,
            search_regex_case_insensitive,
            fields: CompiledFields {
                field_specs,
                field_names,
                normalized_names,
                custom_type_groups,
                has_nested_dict_fields,
                nested_parsers,
                field_count,
            },
            name_mapping,
            stored_extra_types: extra_types,
            allows_empty_default_string_match,
        })
    }

    pub fn search_pattern(
        &self,
        string: &str,
        case_sensitive: bool,
        extra_types: Option<HashMap<String, PyObject>>,
        evaluate_result: bool,
    ) -> PyResult<Option<PyObject>> {
        // Use pre-compiled search regex
        let search_regex = if case_sensitive {
            &self.search_regex
        } else {
            self.search_regex_case_insensitive
                .as_ref()
                .unwrap_or(&self.search_regex)
        };

        Python::with_gil(|py| {
            if search_regex
                .captures(string)
                .map_err(crate::error::fancy_regex_match_error)?
                .is_some()
            {
                let extra_types_ref = if let Some(ref et) = extra_types {
                    et
                } else {
                    &HashMap::new()
                };
                let f = &self.fields;
                return crate::parser::matching::match_with_regex(
                    search_regex,
                    &crate::parser::matching::RegexMatchContext {
                        string,
                        pattern: &self.pattern,
                        field_specs: &f.field_specs,
                        field_names: &f.field_names,
                        normalized_names: &f.normalized_names,
                        nested_parsers: &f.nested_parsers,
                        py,
                        custom_converters: extra_types_ref,
                        evaluate_result,
                    },
                );
            }
            Ok(None)
        })
    }

    pub(crate) fn parse_internal(
        &self,
        string: &str,
        case_sensitive: bool,
        extra_types: Option<&HashMap<String, PyObject>>,
        evaluate_result: bool,
    ) -> PyResult<Option<PyObject>> {
        Python::with_gil(|py| {
            let empty = HashMap::<String, PyObject>::new();
            let extra_types_ref = extra_types.unwrap_or(&empty);

            // Use existing regex (custom type handling is done in convert_value)
            let regex = if case_sensitive {
                &self.regex
            } else {
                self.regex_case_insensitive.as_ref().unwrap_or(&self.regex)
            };

            let f = &self.fields;
            if string.is_empty()
                && self.allows_empty_default_string_match
                && !f.field_specs.is_empty()
            {
                if let Some(obj) = crate::parser::matching::match_empty_default_string_parse(
                    &self.pattern,
                    &f.field_specs,
                    &f.field_names,
                    &f.normalized_names,
                    py,
                    extra_types_ref,
                    evaluate_result,
                )? {
                    return Ok(Some(obj));
                }
            }

            crate::parser::matching::match_with_regex(
                regex,
                &crate::parser::matching::RegexMatchContext {
                    string,
                    pattern: &self.pattern,
                    field_specs: &f.field_specs,
                    field_names: &f.field_names,
                    normalized_names: &f.normalized_names,
                    nested_parsers: &f.nested_parsers,
                    py,
                    custom_converters: extra_types_ref,
                    evaluate_result,
                },
            )
        })
    }

    /// Get the search regex for a given case sensitivity
    pub(crate) fn get_search_regex(&self, case_sensitive: bool) -> &Regex {
        if case_sensitive {
            &self.search_regex
        } else {
            self.search_regex_case_insensitive
                .as_ref()
                .unwrap_or(&self.search_regex)
        }
    }

    /// Parse one capture slice with this parser's pattern (nested fields, issue #12).
    pub(crate) fn parse_nested_capture(
        &self,
        py: Python<'_>,
        slice: &str,
        custom_converters: &HashMap<String, PyObject>,
    ) -> PyResult<Option<Py<ParseResult>>> {
        use pyo3::types::PyAnyMethods;
        let mut merged = HashMap::new();
        if let Some(ref stored) = self.stored_extra_types {
            for (k, v) in stored {
                merged.insert(k.clone(), v.clone_ref(py));
            }
        }
        for (k, v) in custom_converters {
            merged.insert(k.clone(), v.clone_ref(py));
        }
        let opt = self.parse_internal(slice, true, Some(&merged), true)?;
        let Some(obj) = opt else {
            return Ok(None);
        };
        let bound = obj.bind(py);
        let pr = bound.downcast::<ParseResult>().map_err(|_| {
            PyValueError::new_err("internal error: nested parse did not return ParseResult")
        })?;
        Ok(Some(pr.clone().unbind()))
    }
}

impl Clone for FormatParser {
    fn clone(&self) -> Self {
        Python::with_gil(|py| Self {
            pattern: self.pattern.clone(),
            regex: self.regex.clone(),
            regex_str: self.regex_str.clone(),
            regex_case_insensitive: self.regex_case_insensitive.clone(),
            search_regex: self.search_regex.clone(),
            search_regex_case_insensitive: self.search_regex_case_insensitive.clone(),
            fields: CompiledFields {
                field_specs: self.fields.field_specs.clone(),
                field_names: self.fields.field_names.clone(),
                normalized_names: self.fields.normalized_names.clone(),
                custom_type_groups: self.fields.custom_type_groups.clone(),
                has_nested_dict_fields: self.fields.has_nested_dict_fields.clone(),
                nested_parsers: self.fields.nested_parsers.clone(),
                field_count: self.fields.field_count,
            },
            name_mapping: self.name_mapping.clone(),
            stored_extra_types: self.stored_extra_types.as_ref().map(|m| {
                m.iter()
                    .map(|(k, v)| (k.clone(), v.clone_ref(py)))
                    .collect()
            }),
            allows_empty_default_string_match: self.allows_empty_default_string_match,
        })
    }
}