xacro-rs 0.2.2

A xml preprocessor for xacro files to generate URDF files
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! Utility functions for property evaluation and string processing
//!
//! This module provides helper functions for:
//! - UTF-8-safe string truncation
//! - Identifier extraction from expressions
//! - Python keyword and identifier validation

use regex::Regex;
use std::collections::HashSet;
use std::sync::OnceLock;

/// Cached regex for extracting variable names from expressions
/// Compiled once and reused across all property reference extractions
static VAR_REGEX: OnceLock<Regex> = OnceLock::new();

/// Truncate text to a safe length (100 chars) respecting UTF-8 boundaries
///
/// Used for error messages to prevent overwhelming output while ensuring
/// we don't break in the middle of a multi-byte UTF-8 character.
pub(super) fn truncate_snippet(text: &str) -> String {
    const CHAR_LIMIT: usize = 100;

    let char_count = text.chars().count();
    if char_count > CHAR_LIMIT {
        // Find the byte index of the 100th character
        let byte_idx = text
            .char_indices()
            .nth(CHAR_LIMIT)
            .map(|(idx, _)| idx)
            .unwrap_or(text.len());

        format!("{}...", &text[..byte_idx])
    } else {
        text.to_string()
    }
}

/// Extract identifiers from an expression, skipping those inside string literals
///
/// This function parses an expression and extracts variable names, but ignores
/// any identifiers that appear within single or double-quoted strings. This is
/// essential for dependency tracking where we only care about actual variable
/// references, not string content.
///
/// # Arguments
/// * `expr` - Expression to analyze (e.g., "x + 'hello' + y")
///
/// # Returns
/// Set of identifier names found outside string literals (e.g., {"x", "y"})
///
/// # Examples
/// ```text
/// extract("x + 'hello' + y") → {"x", "y"}
/// extract("'x' + y") → {"y"}
/// extract("name.attr['key']") → {"name"}
/// extract("lambda x: x + y") → {"lambda", "x", "y"}
/// ```
///
/// # Implementation Notes
/// - Uses lazy-compiled regex for efficiency
/// - Tracks string literal state while iterating
/// - Filters Python keywords and built-ins
/// - Does NOT filter lambda parameters (caller must use `is_lambda_parameter` if needed)
pub(super) fn extract_identifiers_outside_strings(expr: &str) -> HashSet<String> {
    let mut refs = HashSet::new();
    let var_regex = VAR_REGEX.get_or_init(|| {
        Regex::new(r"\b([a-zA-Z_][a-zA-Z0-9_]*)\b").expect("VAR_REGEX should be valid")
    });

    // Track string literal boundaries to skip identifiers inside strings
    let mut in_single_quote = false;
    let mut in_double_quote = false;
    let mut escaped = false;

    // Build a mask of which characters are inside strings
    let mut inside_string = vec![false; expr.len()];
    for (idx, ch) in expr.char_indices() {
        if escaped {
            escaped = false;
            if in_single_quote || in_double_quote {
                inside_string[idx] = true;
            }
            continue;
        }

        match ch {
            '\\' => {
                escaped = true;
                if in_single_quote || in_double_quote {
                    inside_string[idx] = true;
                }
            }
            '\'' if !in_double_quote => {
                inside_string[idx] = true; // Include the quote itself
                in_single_quote = !in_single_quote;
            }
            '"' if !in_single_quote => {
                inside_string[idx] = true; // Include the quote itself
                in_double_quote = !in_double_quote;
            }
            _ => {
                if in_single_quote || in_double_quote {
                    inside_string[idx] = true;
                }
            }
        }
    }

    // Extract identifiers, but only those outside of strings
    for cap in var_regex.captures_iter(expr) {
        if let Some(name_match) = cap.get(1) {
            let name = name_match.as_str();
            let start_pos = name_match.start();

            // Check if this identifier is inside a string
            if start_pos < inside_string.len()
                && !inside_string[start_pos]
                && !is_python_keyword(name)
            {
                refs.insert(name.to_string());
            }
        }
    }

    refs
}

/// Check if a name is a lambda parameter in the given lambda expression
///
/// Detects lambda parameters to avoid treating them as property references
/// during dependency tracking. For example, in "lambda x: x + y", x is a
/// parameter (not a property) but y is a property reference.
///
/// # Arguments
/// * `lambda_expr` - Full expression that may contain lambda
/// * `name` - Identifier to check
///
/// # Returns
/// `true` if name is a lambda parameter in the expression
///
/// # Examples
/// ```text
/// is_lambda_parameter("lambda x: x + y", "x") → true
/// is_lambda_parameter("lambda x: x + y", "y") → false
/// is_lambda_parameter("lambda a, b: a + b", "a") → true
/// is_lambda_parameter("x + y", "x") → false
/// ```
///
/// # Limitations
///
/// This implementation uses simple string parsing and does not handle:
/// - Default parameter values: `lambda x=1: x + y`
/// - Type annotations (Python 3): `lambda x: int: x`
/// - Nested parentheses in parameter defaults
///
/// These edge cases are acceptable for xacro's typical lambda usage patterns.
pub(super) fn is_lambda_parameter(
    lambda_expr: &str,
    name: &str,
) -> bool {
    // Check if the expression starts with "lambda " (with proper word boundary)
    if let Some(after_lambda) = lambda_expr.strip_prefix("lambda") {
        // Find the colon that separates parameters from body
        if let Some(colon_pos) = after_lambda.find(':') {
            let params_section = &after_lambda[..colon_pos].trim();
            // Check if name matches any parameter (comma-separated)
            params_section.split(',').any(|param| param.trim() == name)
        } else {
            false
        }
    } else {
        false
    }
}

/// Check if a name is a Python keyword or built-in that shouldn't be treated as a property
///
/// Used during dependency tracking to filter out identifiers that are Python
/// language constructs or preprocessed functions, not property references.
///
/// Only includes:
/// - Python keywords (cannot be shadowed)
/// - Math functions that are preprocessed and cannot be shadowed
///
/// Does NOT include general built-ins (len, str, int, file, etc.) because they
/// CAN be shadowed by macro parameters or properties.
///
/// # Arguments
/// * `name` - Identifier to check
///
/// # Returns
/// `true` if name is a Python keyword or preprocessed function
pub(super) fn is_python_keyword(name: &str) -> bool {
    matches!(
        name,
        // Python keywords (but NOT built-in functions that can be shadowed)
        "True"
            | "False"
            | "None"
            | "and"
            | "or"
            | "not"
            | "is"
            | "in"
            | "if"
            | "else"
            | "elif"
            | "for"
            | "while"
            | "lambda"
            | "def"
            | "class"
            | "return"
            | "yield"
            | "try"
            | "except"
            | "finally"
            | "raise"
            | "with"
            | "as"
            | "import"
            | "from"
            | "pass"
            | "break"
            | "continue"
            | "global"
            | "nonlocal"
            | "assert"
            | "del"
            // Math functions that are preprocessed (cannot be shadowed due to preprocessing)
            // See preprocess_math_functions() in src/eval/interpreter/math.rs
            | "abs"
            | "sin"
            | "cos"
            | "tan"
            | "asin"
            | "acos"
            | "atan"
            | "atan2"
            | "sqrt"
            | "floor"
            | "ceil" // NOTE: radians() and degrees() are NOT filtered here because they are
                     // implemented as lambda functions in pyisheval, so they CAN be shadowed.
                     // NOTE: len, min, max, sum, range, int, float, str, bool, list, tuple, dict
                     // are also NOT filtered here because they can be shadowed by macro parameters
                     // or properties. Python allows: def foo(len): return len * 2
    )
}

/// Check if a string is a simple identifier (not a complex expression)
///
/// Used to determine if a property name is a simple variable reference
/// (eligible for direct lookup) vs. a complex expression that needs evaluation.
///
/// # Arguments
/// * `name` - String to check
///
/// # Returns
/// `true` if name is a simple identifier (alphanumeric + underscore, starts with letter/underscore)
///
/// # Examples
/// ```text
/// is_simple_identifier("x") → true
/// is_simple_identifier("my_var") → true
/// is_simple_identifier("var123") → true
/// is_simple_identifier("x + y") → false
/// is_simple_identifier("123abc") → false
/// is_simple_identifier("True") → false (Python keyword)
/// ```
pub(super) fn is_simple_identifier(name: &str) -> bool {
    !is_python_keyword(name)
        && name.chars().all(|c| c.is_alphanumeric() || c == '_')
        && name
            .chars()
            .next()
            .is_some_and(|c| c.is_alphabetic() || c == '_')
}

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

    #[test]
    fn test_extract_identifiers_basic() {
        let expr = "x + y * z";
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 3);
        assert!(ids.contains("x"));
        assert!(ids.contains("y"));
        assert!(ids.contains("z"));
    }

    #[test]
    fn test_extract_identifiers_single_quote_string() {
        let expr = "data['initial_positions']";
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 1);
        assert!(ids.contains("data"));
        assert!(
            !ids.contains("initial_positions"),
            "String literal should be skipped"
        );
    }

    #[test]
    fn test_extract_identifiers_double_quote_string() {
        let expr = r#"data["key_name"]"#;
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 1);
        assert!(ids.contains("data"));
        assert!(
            !ids.contains("key_name"),
            "String literal should be skipped"
        );
    }

    #[test]
    fn test_extract_identifiers_escaped_quote_single() {
        let expr = r"message['can\'t']";
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 1);
        assert!(ids.contains("message"));
        assert!(!ids.contains("can"));
        assert!(!ids.contains("t"));
    }

    #[test]
    fn test_extract_identifiers_escaped_quote_double() {
        let expr = r#"data["quote\"inside"]"#;
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 1);
        assert!(ids.contains("data"));
        assert!(!ids.contains("quote"));
        assert!(!ids.contains("inside"));
    }

    #[test]
    fn test_extract_identifiers_mixed_quotes() {
        let expr = r#"func('single') + other("double")"#;
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 2);
        assert!(ids.contains("func"));
        assert!(ids.contains("other"));
        assert!(!ids.contains("single"));
        assert!(!ids.contains("double"));
    }

    #[test]
    fn test_extract_identifiers_quote_inside_different_quote() {
        // Single quote inside double quotes
        let expr = r#"data["it's"]"#;
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 1);
        assert!(ids.contains("data"));
        assert!(!ids.contains("it"));
        assert!(!ids.contains("s"));

        // Double quote inside single quotes
        let expr2 = r#"data['"quoted"']"#;
        let ids2 = extract_identifiers_outside_strings(expr2);
        assert_eq!(ids2.len(), 1);
        assert!(ids2.contains("data"));
        assert!(!ids2.contains("quoted"));
    }

    #[test]
    fn test_extract_identifiers_function_call() {
        let expr = "load_yaml(filename)";
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 2);
        assert!(ids.contains("load_yaml"));
        assert!(ids.contains("filename"));
    }

    #[test]
    fn test_extract_identifiers_complex_expression() {
        let expr = "load_yaml(file)['initial_positions']['joint1']";
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 2);
        assert!(ids.contains("load_yaml"));
        assert!(ids.contains("file"));
        assert!(
            !ids.contains("initial_positions"),
            "String literal in brackets"
        );
        assert!(!ids.contains("joint1"), "String literal in brackets");
    }

    #[test]
    fn test_extract_identifiers_python_keywords_filtered() {
        let expr = "if x and y or z";
        let ids = extract_identifiers_outside_strings(expr);
        // Keywords (if, and, or) should be filtered out
        assert_eq!(ids.len(), 3);
        assert!(ids.contains("x"));
        assert!(ids.contains("y"));
        assert!(ids.contains("z"));
        assert!(!ids.contains("if"));
        assert!(!ids.contains("and"));
        assert!(!ids.contains("or"));
    }

    #[test]
    fn test_extract_identifiers_empty_string() {
        let expr = "";
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 0);
    }

    #[test]
    fn test_extract_identifiers_only_strings() {
        let expr = "'hello' + \"world\"";
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 0, "Only string literals, no identifiers");
    }

    #[test]
    fn test_extract_identifiers_multiple_occurrences() {
        let expr = "x + x * x";
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 1, "Set should deduplicate");
        assert!(ids.contains("x"));
    }

    #[test]
    fn test_extract_identifiers_underscore_names() {
        let expr = "_private + __dunder__ + normal_name";
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 3);
        assert!(ids.contains("_private"));
        assert!(ids.contains("__dunder__"));
        assert!(ids.contains("normal_name"));
    }

    #[test]
    fn test_extract_identifiers_numbers_excluded() {
        let expr = "x + 123 + y";
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 2);
        assert!(ids.contains("x"));
        assert!(ids.contains("y"));
        // Numbers should not be captured by the identifier regex
    }

    #[test]
    fn test_extract_identifiers_empty_quotes() {
        let expr = "data[''] + other[\"\"]";
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 2);
        assert!(ids.contains("data"));
        assert!(ids.contains("other"));
    }

    #[test]
    fn test_extract_identifiers_adjacent_strings() {
        let expr = "'first''second'";
        let ids = extract_identifiers_outside_strings(expr);
        assert_eq!(ids.len(), 0, "Adjacent string literals");
    }

    #[test]
    fn test_extract_identifiers_backslash_at_end_of_string() {
        let expr = r"data['path\\'] + x";
        let ids = extract_identifiers_outside_strings(expr);
        assert!(ids.contains("data"));
        assert!(ids.contains("x"));
        assert!(!ids.contains("path"));
    }

    // Tests for truncate_snippet

    #[test]
    fn test_truncate_snippet_short_string() {
        let text = "Short string";
        assert_eq!(truncate_snippet(text), "Short string");
    }

    #[test]
    fn test_truncate_snippet_exactly_100_chars() {
        let text = "a".repeat(100);
        assert_eq!(truncate_snippet(&text), text);
    }

    #[test]
    fn test_truncate_snippet_longer_than_100_chars() {
        let text = "a".repeat(150);
        let result = truncate_snippet(&text);
        assert!(result.ends_with("..."));
        assert!(result.len() <= 103); // 100 chars + "..."
    }

    #[test]
    fn test_truncate_snippet_utf8_boundary() {
        // Create a string with multibyte UTF-8 characters near the boundary
        let mut text = "a".repeat(98);
        text.push_str("🦀"); // 4-byte emoji
        text.push_str(&"x".repeat(10));
        let result = truncate_snippet(&text);
        assert!(result.ends_with("..."));
        // Should not panic on UTF-8 boundary
        assert!(result.is_char_boundary(result.len() - 3));
    }

    #[test]
    fn test_truncate_snippet_counts_chars_not_bytes() {
        // 50 emoji = 200 bytes but only 50 characters
        let text = "🦀".repeat(120); // 120 chars, 480 bytes
        let result = truncate_snippet(&text);
        assert!(result.ends_with("..."));
        // Should truncate at 100 chars (not 100 bytes)
        let result_without_ellipsis = &result[..result.len() - 3];
        assert_eq!(result_without_ellipsis.chars().count(), 100);
    }

    // Tests for is_lambda_parameter

    #[test]
    fn test_is_lambda_parameter_single_param() {
        assert!(is_lambda_parameter("lambda x: x + 1", "x"));
        assert!(!is_lambda_parameter("lambda x: x + 1", "y"));
    }

    #[test]
    fn test_is_lambda_parameter_multiple_params() {
        assert!(is_lambda_parameter("lambda a, b, c: a + b", "a"));
        assert!(is_lambda_parameter("lambda a, b, c: a + b", "b"));
        assert!(is_lambda_parameter("lambda a, b, c: a + b", "c"));
        assert!(!is_lambda_parameter("lambda a, b, c: a + b", "d"));
    }

    #[test]
    fn test_is_lambda_parameter_with_spaces() {
        assert!(is_lambda_parameter("lambda  x , y  : x + y", "x"));
        assert!(is_lambda_parameter("lambda  x , y  : x + y", "y"));
    }

    #[test]
    fn test_is_lambda_parameter_not_lambda() {
        assert!(!is_lambda_parameter("x + y", "x"));
        assert!(!is_lambda_parameter("def foo(x): return x", "x"));
    }

    #[test]
    fn test_is_lambda_parameter_word_boundary() {
        // "notlambda" should not be detected as lambda
        assert!(!is_lambda_parameter("notlambda x: x", "x"));
        // "lambda_var" should not be detected as lambda
        assert!(!is_lambda_parameter("lambda_var: something", "var"));
    }

    #[test]
    fn test_is_lambda_parameter_no_colon() {
        assert!(!is_lambda_parameter("lambda x", "x"));
    }

    #[test]
    fn test_is_lambda_parameter_underscore_names() {
        assert!(is_lambda_parameter("lambda _x, __y: _x + __y", "_x"));
        assert!(is_lambda_parameter("lambda _x, __y: _x + __y", "__y"));
    }

    // Tests for is_simple_identifier

    #[test]
    fn test_is_simple_identifier_valid() {
        assert!(is_simple_identifier("x"));
        assert!(is_simple_identifier("my_var"));
        assert!(is_simple_identifier("var123"));
        assert!(is_simple_identifier("_private"));
        assert!(is_simple_identifier("__dunder__"));
    }

    #[test]
    fn test_is_simple_identifier_invalid() {
        assert!(!is_simple_identifier("x + y"));
        assert!(!is_simple_identifier("data['key']"));
        assert!(!is_simple_identifier("obj.attr"));
        assert!(!is_simple_identifier("123abc")); // starts with number
        assert!(!is_simple_identifier("")); // empty
        assert!(!is_simple_identifier("x-y")); // hyphen not allowed
    }

    #[test]
    fn test_is_simple_identifier_keywords() {
        assert!(!is_simple_identifier("True"));
        assert!(!is_simple_identifier("False"));
        assert!(!is_simple_identifier("None"));
        assert!(!is_simple_identifier("lambda"));
        assert!(!is_simple_identifier("if"));
    }

    #[test]
    fn test_is_simple_identifier_numbers() {
        assert!(!is_simple_identifier("123"));
        assert!(!is_simple_identifier("3.14"));
    }
}