cel 0.14.5

A parser and interpreter for the Common Expression Language (CEL)
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
//! CEL string / bytes literal parsing.
//!
//! Implements the unescape rules from cel-spec (mirrors cel-go's
//! `parser/unescape.go`):
//!
//! * strips the optional `r`/`R` (raw) and — for bytes — `b`/`B` prefix,
//! * normalizes `\r\n` and lone `\r` inside the source to `\n`,
//! * unwraps single (`'"..."'`) or triple-quoted (`"""..."""`) literals,
//! * interprets `\a \b \f \n \r \t \v \\ \? \" \' \``, `\xHH` / `\XHH`,
//!   `\uHHHH`, `\UHHHHHHHH`, and `\ODD` (octal) escapes,
//! * `\uHHHH` / `\UHHHHHHHH` are rejected inside bytes literals,
//! * inside bytes, `\xHH` / `\XHH` and `\ODD` are raw byte values;
//!   inside strings they are Unicode codepoints that get UTF-8 encoded.
//!
//! `parse_string` / `parse_bytes` take the full lexer-produced literal
//! (`"..."`, `r'''...'''`, `br"..."`, …) and return the decoded value.

use std::num::ParseIntError;

/// Error type produced by [`parse_string`] / [`parse_bytes`].
#[derive(Debug, PartialEq)]
pub enum ParseSequenceError {
    InvalidEscape {
        escape: String,
        index: usize,
        string: String,
    },
    InvalidUnicode {
        source: ParseUnicodeError,
        index: usize,
        string: String,
    },
    MissingOpeningQuote,
    MissingClosingQuote,
    /// Catch-all for shape mismatches (missing/mismatched prefix, quote
    /// style, truncated escape, invalid UTF-8, …).
    Invalid(String),
}

#[derive(Debug, PartialEq, Clone)]
pub enum ParseUnicodeError {
    Hex {
        source: ParseIntError,
        string: String,
    },
    Unicode {
        value: u32,
    },
}

/// Parse a CEL string literal, including any surrounding quotes and
/// optional `r`/`R` raw prefix. Accepts single, double, and triple-quoted
/// forms.
pub fn parse_string(literal: &str) -> Result<String, ParseSequenceError> {
    let bytes = unescape(literal, false)?;
    String::from_utf8(bytes)
        .map_err(|_| ParseSequenceError::Invalid("invalid unicode code point".to_string()))
}

/// Parse a CEL bytes literal, including the `b`/`B` prefix, optional
/// `r`/`R` raw marker, and surrounding quotes. Accepts single, double, and
/// triple-quoted forms.
pub fn parse_bytes(literal: &str) -> Result<Vec<u8>, ParseSequenceError> {
    // Bytes literals are `[bB][rR]?"..."` per the grammar; peel the `b`/`B`
    // and hand off to the shared unescape.
    let rest = literal
        .strip_prefix('b')
        .or_else(|| literal.strip_prefix('B'))
        .ok_or_else(|| {
            ParseSequenceError::Invalid("bytes literal must start with b/B".to_string())
        })?;
    unescape(rest, true)
}

fn unescape(value: &str, is_bytes: bool) -> Result<Vec<u8>, ParseSequenceError> {
    // Normalize source-level newlines. Escape sequences `\r`/`\n` are
    // untouched (they aren't literal CR/LF at this point).
    let normalized = value.replace("\r\n", "\n").replace('\r', "\n");
    let mut val = normalized.as_str();

    if val.len() < 2 {
        return Err(ParseSequenceError::Invalid(
            "literal too short to be a valid string".to_string(),
        ));
    }

    let is_raw = if val.starts_with('r') || val.starts_with('R') {
        val = &val[1..];
        true
    } else {
        false
    };
    if val.len() < 2 {
        return Err(ParseSequenceError::Invalid(
            "literal too short to be a valid string".to_string(),
        ));
    }

    let first = val.chars().next().unwrap();
    let last = val.chars().last().unwrap();
    if first != '"' && first != '\'' {
        return Err(ParseSequenceError::MissingOpeningQuote);
    }
    if last != first {
        return Err(ParseSequenceError::MissingClosingQuote);
    }

    // Peel triple-quotes, falling back to a single pair.
    if val.len() >= 6 && (val.starts_with("'''") || val.starts_with("\"\"\"")) {
        let triple = if first == '\'' { "'''" } else { "\"\"\"" };
        if !val.ends_with(triple) || val.len() < 6 {
            return Err(ParseSequenceError::MissingClosingQuote);
        }
        val = &val[3..val.len() - 3];
    } else {
        val = &val[1..val.len() - 1];
    }

    // Raw or no escape: return content verbatim.
    if is_raw || !val.contains('\\') {
        return Ok(val.as_bytes().to_vec());
    }

    // The non-raw slow path: interpret escape sequences.
    let mut out = Vec::with_capacity(val.len());
    let mut chars = val.char_indices();
    while let Some((idx, c)) = chars.next() {
        if c != '\\' {
            let mut buf = [0u8; 4];
            out.extend_from_slice(c.encode_utf8(&mut buf).as_bytes());
            continue;
        }
        let (_, esc) = chars
            .next()
            .ok_or_else(|| ParseSequenceError::InvalidEscape {
                escape: "\\".to_string(),
                index: idx,
                string: val.to_string(),
            })?;
        match esc {
            'a' => out.push(0x07),
            'b' => out.push(0x08),
            'f' => out.push(0x0C),
            'n' => out.push(b'\n'),
            'r' => out.push(b'\r'),
            't' => out.push(b'\t'),
            'v' => out.push(0x0B),
            '\\' => out.push(b'\\'),
            '\'' => out.push(b'\''),
            '"' => out.push(b'"'),
            '`' => out.push(b'`'),
            '?' => out.push(b'?'),
            'x' | 'X' | 'u' | 'U' => {
                let width = match esc {
                    'x' | 'X' => 2,
                    'u' => {
                        if is_bytes {
                            return Err(ParseSequenceError::InvalidEscape {
                                escape: format!("\\{esc}"),
                                index: idx,
                                string: val.to_string(),
                            });
                        }
                        4
                    }
                    'U' => {
                        if is_bytes {
                            return Err(ParseSequenceError::InvalidEscape {
                                escape: format!("\\{esc}"),
                                index: idx,
                                string: val.to_string(),
                            });
                        }
                        8
                    }
                    _ => unreachable!(),
                };
                let mut hex = String::with_capacity(width);
                for _ in 0..width {
                    let (_, h) =
                        chars
                            .next()
                            .ok_or_else(|| ParseSequenceError::InvalidUnicode {
                                source: ParseUnicodeError::Unicode { value: 0 },
                                index: idx,
                                string: val.to_string(),
                            })?;
                    hex.push(h);
                }
                let v = u32::from_str_radix(&hex, 16).map_err(|e| {
                    ParseSequenceError::InvalidUnicode {
                        source: ParseUnicodeError::Hex {
                            source: e,
                            string: hex.clone(),
                        },
                        index: idx,
                        string: val.to_string(),
                    }
                })?;
                if is_bytes && (esc == 'x' || esc == 'X') {
                    out.push(v as u8);
                } else {
                    let ch = char::from_u32(v).ok_or(ParseSequenceError::InvalidUnicode {
                        source: ParseUnicodeError::Unicode { value: v },
                        index: idx,
                        string: val.to_string(),
                    })?;
                    let mut buf = [0u8; 4];
                    out.extend_from_slice(ch.encode_utf8(&mut buf).as_bytes());
                }
            }
            n @ '0'..='3' => {
                let mut v = (n as u32) - ('0' as u32);
                let mut oct = String::from(n);
                for _ in 0..2 {
                    let (_, d) =
                        chars
                            .next()
                            .ok_or_else(|| ParseSequenceError::InvalidUnicode {
                                source: ParseUnicodeError::Unicode { value: 0 },
                                index: idx,
                                string: val.to_string(),
                            })?;
                    if !('0'..='7').contains(&d) {
                        return Err(ParseSequenceError::InvalidEscape {
                            escape: format!("\\{oct}"),
                            index: idx,
                            string: val.to_string(),
                        });
                    }
                    oct.push(d);
                    v = v * 8 + (d as u32 - '0' as u32);
                }
                if is_bytes {
                    out.push(v as u8);
                } else {
                    let ch = char::from_u32(v).ok_or(ParseSequenceError::InvalidUnicode {
                        source: ParseUnicodeError::Unicode { value: v },
                        index: idx,
                        string: val.to_string(),
                    })?;
                    let mut buf = [0u8; 4];
                    out.extend_from_slice(ch.encode_utf8(&mut buf).as_bytes());
                }
            }
            _ => {
                return Err(ParseSequenceError::InvalidEscape {
                    escape: format!("\\{esc}"),
                    index: idx,
                    string: val.to_string(),
                });
            }
        }
    }
    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::{parse_bytes, parse_string, ParseSequenceError};

    fn ok(s: &str, expected: &str) {
        assert_eq!(parse_string(s).as_deref(), Ok(expected), "input: {s}");
    }

    fn ok_bytes(s: &str, expected: &[u8]) {
        assert_eq!(parse_bytes(s).as_deref(), Ok(expected), "input: {s}");
    }

    fn err(s: &str) {
        assert!(parse_string(s).is_err(), "expected error for: {s}");
    }

    #[test]
    fn single_quoted_escapes() {
        ok(r"'Hello \a'", "Hello \u{07}");
        ok(r"'Hello \b'", "Hello \u{08}");
        ok(r"'Hello \v'", "Hello \u{0b}");
        ok(r"'Hello \f'", "Hello \u{0c}");
        ok(r"'Hello \n'", "Hello \n");
        ok(r"'Hello \r'", "Hello \r");
        ok(r"'Hello \t'", "Hello \t");
        ok(r"'Hello \\'", "Hello \\");
        ok(r"'Hello \?'", "Hello ?");
        ok(r"'Hello \''", "Hello '");
        ok(r#"'Hello \`'"#, "Hello `");
        ok(r"'Hello \x20'", "Hello  ");
        ok(r"'Hello ✌'", "Hello ✌");
        ok(r"'Hello \U0001f431'", "Hello 🐱");
        ok(r"'Hello \040'", "Hello  ");
    }

    #[test]
    fn double_quoted_escapes() {
        ok(r#""Hello \n""#, "Hello \n");
        ok(r#""Hello \x60""#, "Hello `");
        ok(r#""Hello ✌""#, "Hello ✌");
        ok(r#""Hello \040""#, "Hello  ");
    }

    #[test]
    fn uppercase_x_hex_escape() {
        ok(r"' \X00 \X0A \X7F '", " \x00 \n \x7f ");
    }

    #[test]
    fn mixed_case_hex_escape() {
        ok(
            r#"" \x4a \x4B \X4c \X4D ƫ \U000001aB ""#,
            " \u{4a} \u{4b} \u{4c} \u{4d} \u{1ab} \u{1ab} ",
        );
    }

    #[test]
    fn triple_double_quoted_plain() {
        ok(r#""""hello""""#, "hello");
    }

    #[test]
    fn triple_double_quoted_with_embedded_quote() {
        // Per cel-spec, single unescaped quotes are legal inside triple-quoted
        // strings — the whole `""" ? " ' ` """` should decode literally.
        ok(r#"""" ? " ' ` """"#, " ? \" ' ` ");
    }

    #[test]
    fn triple_double_quoted_with_escapes() {
        ok(r#"""" \n """"#, " \n ");
    }

    #[test]
    fn raw_string_verbatim() {
        // Raw strings pass every byte between the outer quotes through
        // untouched; backslashes are never interpreted.
        ok(r#"r"\a\n\x00""#, r"\a\n\x00");
        ok(r#"R"\a\n\x00""#, r"\a\n\x00");
    }

    #[test]
    fn raw_triple_quoted_verbatim() {
        ok(
            r#"r""" \\ \? \` \a \b \f \t \v \n \r \000 \x00 \X00  \U00000000 """"#,
            r" \\ \? \` \a \b \f \t \v \n \r \000 \x00 \X00  \U00000000 ",
        );
    }

    #[test]
    fn bytes_standard_escapes() {
        ok_bytes("b' \\\\ \\? \\\" \\' \\` '", b" \\ ? \" ' ` ");
        ok_bytes(r#"b'\n'"#, b"\n");
        ok_bytes(r#"b'\x20'"#, b" ");
        ok_bytes(r#"b'\376'"#, &[0xFE]);
        ok_bytes(r#"b'\xFF'"#, &[0xFF]);
    }

    #[test]
    fn bytes_uppercase_prefix() {
        ok_bytes(r#"B"hi""#, b"hi");
    }

    #[test]
    fn bytes_triple_double_quoted() {
        ok_bytes(r#"b"""hi""""#, b"hi");
    }

    #[test]
    fn bytes_raw() {
        ok_bytes(r#"br"\n""#, br"\n");
        ok_bytes(r#"BR"\a\b""#, br"\a\b");
    }

    #[test]
    fn bytes_rejects_unicode_escape() {
        // \u and \U escapes are unicode-only; using them inside a bytes
        // literal must error per cel-spec.
        assert!(parse_bytes("b'\\u0041'").is_err());
        assert!(parse_bytes("b'\\U00000041'").is_err());
    }

    #[test]
    fn newlines_normalized() {
        // A literal CR-LF or lone CR inside the source is normalized to LF.
        ok("\"a\r\nb\"", "a\nb");
        ok("\"a\rb\"", "a\nb");
    }

    #[test]
    fn errors() {
        err("'unterminated");
        err("unquoted'");
        // Octal must be in range.
        assert!(matches!(
            parse_string(r"'\440'"),
            Err(ParseSequenceError::InvalidEscape { .. })
        ));
    }

    #[test]
    fn parses_bytes_smoke() {
        let bytes = parse_bytes(r#"b"abc💖\xFF\376""#).expect("must parse");
        assert_eq!(
            &*bytes,
            &[b'a', b'b', b'c', 0xF0, 0x9F, 0x92, 0x96, 0xFF, 0xFE]
        );
    }
}