zerodds-idl-parse 1.0.0-rc.7

Hand-written recursive-descent parser for OMG IDL 4.2, building a lossless CST with error recovery.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! The value of an `#if` or `#elif`, ISO/IEC 14882:2003 §16.1.
//!
//! What reaches here has had `defined` answered and every macro
//! replaced. §16.1/4: "After all replacements due to macro expansion and
//! the defined unary operator have been performed, all remaining
//! identifiers and keywords, except for true and false, are replaced
//! with the pp-number 0, and then each preprocessing token is converted
//! into a token. The resulting tokens comprise the controlling constant
//! expression", evaluated in `long`/`unsigned long` (here 64 bits) with
//! the usual arithmetic conversions.

use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

/// A value of the controlling expression: 64 bits, and whether the usual
/// arithmetic conversions made it unsigned.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Number {
    bits: u64,
    unsigned: bool,
}

impl Number {
    const fn signed(value: i64) -> Self {
        Self {
            bits: value.cast_unsigned(),
            unsigned: false,
        }
    }

    const fn truth(value: bool) -> Self {
        Self::signed(if value { 1 } else { 0 })
    }

    const fn is_true(self) -> bool {
        self.bits != 0
    }

    const fn as_signed(self) -> i64 {
        self.bits.cast_signed()
    }
}

/// The outcome of one condition: its truth, and the identifiers §16.1/4
/// replaced with 0, which the caller reports.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Outcome {
    pub value: bool,
    pub zeroed: Vec<String>,
}

/// Evaluate the tokens of an `#if` line.
///
/// # Errors
/// What makes the line no constant expression, in one sentence.
pub(crate) fn evaluate(tokens: &[String]) -> Result<Outcome, String> {
    if tokens.is_empty() {
        return Err("`#if` with no expression".to_string());
    }
    let mut parser = Parser {
        tokens,
        at: 0,
        zeroed: Vec::new(),
    };
    let value = parser.conditional(true)?;
    if let Some(extra) = tokens.get(parser.at) {
        return Err(format!("`{extra}` after the end of the expression"));
    }
    Ok(Outcome {
        value: value.is_true(),
        zeroed: parser.zeroed,
    })
}

struct Parser<'a> {
    tokens: &'a [String],
    at: usize,
    zeroed: Vec<String>,
}

/// zerodds-lint: recursion-depth 256 (parenthesis and unary nesting of
/// one `#if` line). Each descent consumes at least one token before it
/// recurses, and the line is bounded by the directive's length.
impl Parser<'_> {
    fn peek(&self) -> Option<&str> {
        self.tokens.get(self.at).map(String::as_str)
    }

    fn eat(&mut self, text: &str) -> bool {
        if self.peek() == Some(text) {
            self.at += 1;
            true
        } else {
            false
        }
    }

    /// `a ? b : c`. `live` is false in an operand the operators around
    /// it do not evaluate, where a division by zero is no error.
    ///
    /// zerodds-lint: recursion-depth 256 (expression nesting of one
    /// `#if` line; each level consumes a token).
    fn conditional(&mut self, live: bool) -> Result<Number, String> {
        let test = self.binary(0, live)?;
        if !self.eat("?") {
            return Ok(test);
        }
        let taken = test.is_true();
        let yes = self.conditional(live && taken)?;
        if !self.eat(":") {
            return Err("`?` without its `:`".to_string());
        }
        let no = self.conditional(live && !taken)?;
        let unsigned = yes.unsigned || no.unsigned;
        let chosen = if taken { yes } else { no };
        Ok(Number {
            bits: chosen.bits,
            unsigned,
        })
    }

    /// Precedence climbing over the binary operators, `||` loosest.
    ///
    /// zerodds-lint: recursion-depth 256 (expression nesting of one
    /// `#if` line; each level consumes a token).
    fn binary(&mut self, level: usize, live: bool) -> Result<Number, String> {
        const LEVELS: [&[&str]; 10] = [
            &["||"],
            &["&&"],
            &["|"],
            &["^"],
            &["&"],
            &["==", "!="],
            &["<", ">", "<=", ">="],
            &["<<", ">>"],
            &["+", "-"],
            &["*", "/", "%"],
        ];
        let Some(operators) = LEVELS.get(level) else {
            return self.unary(live);
        };
        let mut left = self.binary(level + 1, live)?;
        while let Some(operator) = self.peek().filter(|token| operators.contains(token)) {
            let operator = operator.to_string();
            self.at += 1;
            // `||` and `&&` do not evaluate their right operand when the
            // left one decides, §5.14/§5.15.
            let right_live = match operator.as_str() {
                "||" => live && !left.is_true(),
                "&&" => live && left.is_true(),
                _ => live,
            };
            let right = self.binary(level + 1, right_live)?;
            left = apply(&operator, left, right, right_live)?;
        }
        Ok(left)
    }

    /// zerodds-lint: recursion-depth 256 (unary operator nesting of one
    /// `#if` line; each level consumes a token).
    fn unary(&mut self, live: bool) -> Result<Number, String> {
        let Some(token) = self.peek() else {
            return Err("the expression ends where an operand was expected".to_string());
        };
        match token {
            "+" | "-" | "~" | "!" => {
                let operator = token.to_string();
                self.at += 1;
                let operand = self.unary(live)?;
                Ok(match operator.as_str() {
                    "+" => operand,
                    "-" => Number {
                        bits: operand.bits.wrapping_neg(),
                        unsigned: operand.unsigned,
                    },
                    "~" => Number {
                        bits: !operand.bits,
                        unsigned: operand.unsigned,
                    },
                    _ => Number::truth(!operand.is_true()),
                })
            }
            "(" => {
                self.at += 1;
                let inner = self.conditional(live)?;
                if !self.eat(")") {
                    return Err("`(` without its `)`".to_string());
                }
                Ok(inner)
            }
            _ => {
                let text = token.to_string();
                self.at += 1;
                primary(&text, &mut self.zeroed)
            }
        }
    }
}

/// One operand: a number, a character, `true`, `false`, or an
/// identifier §16.1/4 replaces with 0.
fn primary(text: &str, zeroed: &mut Vec<String>) -> Result<Number, String> {
    match text {
        "true" => return Ok(Number::truth(true)),
        "false" => return Ok(Number::truth(false)),
        _ => {}
    }
    let first = text.as_bytes().first().copied().unwrap_or(0);
    if first.is_ascii_digit() {
        return integer(text);
    }
    if first == b'\'' || text.starts_with("L'") {
        return character(text);
    }
    if super::pptoken::is_identifier(text) {
        zeroed.push(text.to_string());
        return Ok(Number::signed(0));
    }
    Err(format!(
        "`{text}` is not an operand of a constant expression"
    ))
}

/// An integer literal, §2.13.1: decimal, octal or hexadecimal, with `u`
/// and `l` suffixes in any order.
fn integer(text: &str) -> Result<Number, String> {
    let digits = text.trim_end_matches(['u', 'U', 'l', 'L']);
    let suffix = &text[digits.len()..];
    let unsigned_suffix = suffix.contains(['u', 'U']);
    let (radix, body) = if let Some(hex) = digits
        .strip_prefix("0x")
        .or_else(|| digits.strip_prefix("0X"))
    {
        (16, hex)
    } else if digits.len() > 1 && digits.starts_with('0') {
        (8, &digits[1..])
    } else {
        (10, digits)
    };
    let value = u64::from_str_radix(body, radix)
        .map_err(|_| format!("`{text}` is not an integer a constant expression can hold"))?;
    Ok(Number {
        bits: value,
        // A literal past `long`'s range is `unsigned long`, §2.13.1/2.
        unsigned: unsigned_suffix || value > i64::MAX.cast_unsigned(),
    })
}

/// A character literal, §2.13.2: its value is the character's code.
fn character(text: &str) -> Result<Number, String> {
    let inner = text
        .trim_start_matches('L')
        .strip_prefix('\'')
        .and_then(|rest| rest.strip_suffix('\''))
        .ok_or_else(|| format!("`{text}` is not a character literal"))?;
    let mut chars = inner.chars();
    let value = match chars.next() {
        Some('\\') => {
            let escape: String = chars.collect();
            escape_value(&escape).ok_or_else(|| format!("`{text}` has no value"))?
        }
        Some(only) if chars.next().is_none() => u64::from(u32::from(only)),
        _ => return Err(format!("`{text}` is not one character")),
    };
    Ok(Number {
        bits: value,
        unsigned: false,
    })
}

fn escape_value(escape: &str) -> Option<u64> {
    let simple = match escape {
        "n" => Some(10),
        "t" => Some(9),
        "v" => Some(11),
        "b" => Some(8),
        "r" => Some(13),
        "f" => Some(12),
        "a" => Some(7),
        "\\" => Some(u64::from(b'\\')),
        "?" => Some(u64::from(b'?')),
        "'" => Some(u64::from(b'\'')),
        "\"" => Some(u64::from(b'"')),
        _ => None,
    };
    if simple.is_some() {
        return simple;
    }
    if let Some(hex) = escape.strip_prefix('x') {
        return u64::from_str_radix(hex, 16).ok();
    }
    if !escape.is_empty() && escape.len() <= 3 && escape.bytes().all(|b| (b'0'..=b'7').contains(&b))
    {
        return u64::from_str_radix(escape, 8).ok();
    }
    None
}

/// One binary operation under the usual arithmetic conversions.
fn apply(operator: &str, left: Number, right: Number, live: bool) -> Result<Number, String> {
    let unsigned = left.unsigned || right.unsigned;
    let arithmetic = |bits: u64| Number { bits, unsigned };
    let compare = |ordering: core::cmp::Ordering| {
        if unsigned {
            left.bits.cmp(&right.bits) == ordering
        } else {
            left.as_signed().cmp(&right.as_signed()) == ordering
        }
    };
    use core::cmp::Ordering::{Greater, Less};
    Ok(match operator {
        "||" => Number::truth(left.is_true() || right.is_true()),
        "&&" => Number::truth(left.is_true() && right.is_true()),
        "|" => arithmetic(left.bits | right.bits),
        "^" => arithmetic(left.bits ^ right.bits),
        "&" => arithmetic(left.bits & right.bits),
        "==" => Number::truth(left.bits == right.bits),
        "!=" => Number::truth(left.bits != right.bits),
        "<" => Number::truth(compare(Less)),
        ">" => Number::truth(compare(Greater)),
        "<=" => Number::truth(!compare(Greater)),
        ">=" => Number::truth(!compare(Less)),
        // The shift count is the right operand's value; past the width it
        // is undefined in C++, and here it is reported.
        "<<" | ">>" => {
            let count = right.bits;
            if count >= 64 {
                if !live {
                    return Ok(Number::signed(0));
                }
                return Err(format!(
                    "a shift by {count} past the 64 bits of the expression"
                ));
            }
            let count = u32::try_from(count).unwrap_or(0);
            if operator == "<<" {
                Number {
                    bits: left.bits.wrapping_shl(count),
                    unsigned: left.unsigned,
                }
            } else if left.unsigned {
                Number {
                    bits: left.bits.wrapping_shr(count),
                    unsigned: true,
                }
            } else {
                Number::signed(left.as_signed().wrapping_shr(count))
            }
        }
        "+" => arithmetic(left.bits.wrapping_add(right.bits)),
        "-" => arithmetic(left.bits.wrapping_sub(right.bits)),
        "*" => arithmetic(left.bits.wrapping_mul(right.bits)),
        "/" | "%" => {
            if right.bits == 0 {
                if !live {
                    return Ok(Number::signed(0));
                }
                return Err("a division by zero".to_string());
            }
            let bits = if unsigned {
                if operator == "/" {
                    left.bits / right.bits
                } else {
                    left.bits % right.bits
                }
            } else if operator == "/" {
                left.as_signed()
                    .wrapping_div(right.as_signed())
                    .cast_unsigned()
            } else {
                left.as_signed()
                    .wrapping_rem(right.as_signed())
                    .cast_unsigned()
            };
            arithmetic(bits)
        }
        other => {
            return Err(format!(
                "`{other}` is not an operator of a constant expression"
            ));
        }
    })
}

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

    fn value(line: &str) -> bool {
        let tokens: Vec<String> = super::super::pptoken::tokenize(line)
            .unwrap()
            .into_iter()
            .map(|piece| piece.text)
            .collect();
        evaluate(&tokens).unwrap().value
    }

    #[test]
    fn arithmetic_and_comparison() {
        assert!(value("2 * 3 > 5"));
        assert!(!value("2 * 3 > 6"));
        assert!(value("(1 + 2) * 3 == 9"));
        assert!(value("10 / 3 == 3 && 10 % 3 == 1"));
        assert!(value("1 << 4 == 16"));
        assert!(value("-1 < 0"));
        assert!(value("0x10 == 16 && 010 == 8"));
    }

    #[test]
    fn the_usual_arithmetic_conversions_make_a_comparison_unsigned() {
        // §5/9: -1 converts to the largest unsigned value.
        assert!(!value("-1 < 0u"));
        assert!(value("-1 > 0u"));
    }

    #[test]
    fn logic_short_circuits_and_spares_a_dead_division() {
        assert!(value("1 || 1 / 0"));
        assert!(!value("0 && 1 / 0"));
        assert!(value("0 ? 1 / 0 : 1"));
        assert!(evaluate(&["1".into(), "/".into(), "0".into()]).is_err());
    }

    #[test]
    fn identifiers_are_zero_and_true_is_one() {
        let outcome = evaluate(&["UNDEFINED".to_string()]).unwrap();
        assert!(!outcome.value);
        assert_eq!(outcome.zeroed, ["UNDEFINED"]);
        assert!(value("true && !false"));
    }

    #[test]
    fn characters_have_their_codes() {
        assert!(value("'A' == 65"));
        assert!(value("'\\n' == 10 && '\\x41' == 65 && '\\101' == 65"));
    }

    #[test]
    fn a_malformed_expression_is_an_error() {
        for line in ["", "1 +", "(1", "1 ? 2", "1 2", "1.5"] {
            let tokens: Vec<String> = super::super::pptoken::tokenize(line)
                .unwrap()
                .into_iter()
                .map(|piece| piece.text)
                .collect();
            assert!(evaluate(&tokens).is_err(), "{line:?}");
        }
    }
}