mdqy 0.1.4

jq for markdown: query and transform Markdown with a hybrid selector and jq DSL
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
//! Hand-rolled lexer. Tokens carry byte offsets for source-caret errors.

use std::borrow::Cow;

use crate::error::CompileError;

/// Token plus its starting byte offset in the source.
#[derive(Debug, Clone)]
pub struct Spanned<'a> {
    pub tok: Tok<'a>,
    pub offset: usize,
}

#[derive(Debug, Clone)]
pub enum Tok<'a> {
    Dot,
    DotDot,
    LParen,
    RParen,
    LBracket,
    RBracket,
    LBrace,
    RBrace,
    Comma,
    Colon,
    Semicolon,
    Pipe,
    PipeEq,
    Eq,
    EqEq,
    NotEq,
    Slash,
    SlashSlash,
    Question,
    Plus,
    Minus,
    Star,
    Percent,
    Lt,
    Le,
    Gt,
    Ge,
    /// `#`..`######` for heading selector. Carries level 1..=6.
    Hash(u8),
    /// `:first`, `:last`, `:nth`, `:text`, `:lang`.
    ColonIdent(&'a str),
    Ident(&'a str),
    Str(Cow<'a, str>),
    Num(f64),
    DollarIdent(&'a str),
    KwIf,
    KwThen,
    KwElif,
    KwElse,
    KwEnd,
    KwAs,
    KwDef,
    KwTrue,
    KwFalse,
    KwNull,
    KwAnd,
    KwOr,
    KwNot,
    Eof,
}

/// Tokenise `source`. Output always ends with `Tok::Eof`.
pub fn tokenize(source: &str) -> Result<Vec<Spanned<'_>>, CompileError> {
    let bytes = source.as_bytes();
    let mut i = 0;
    let mut out = Vec::new();

    while i < bytes.len() {
        let start = i;
        let c = bytes[i];

        if c.is_ascii_whitespace() {
            i += 1;
            continue;
        }

        // Two-char punct first; otherwise a trailing `=` would get eaten.
        let two = [c, bytes.get(i + 1).copied().unwrap_or(0)];
        if let Some(tok) = match &two {
            b".." => Some(Tok::DotDot),
            b"|=" => Some(Tok::PipeEq),
            b"==" => Some(Tok::EqEq),
            b"!=" => Some(Tok::NotEq),
            b"<=" => Some(Tok::Le),
            b">=" => Some(Tok::Ge),
            b"//" => Some(Tok::SlashSlash),
            _ => None,
        } {
            out.push(Spanned { tok, offset: start });
            i += 2;
            continue;
        }

        if let Some(tok) = single_char_tok(c) {
            out.push(Spanned { tok, offset: start });
            i += 1;
            continue;
        }

        // Heading selector `#`..`######`.
        if c == b'#' {
            let mut count = 0u8;
            while i < bytes.len() && bytes[i] == b'#' && count < 6 {
                i += 1;
                count += 1;
            }
            if i < bytes.len() && bytes[i] == b'#' {
                return Err(CompileError::Lex {
                    offset: start,
                    message: format!("too many `#` (max 6), got at least {}", count + 1),
                });
            }
            out.push(Spanned {
                tok: Tok::Hash(count),
                offset: start,
            });
            continue;
        }

        // Colon-prefixed pseudo: `:first`, or bare `:`.
        if c == b':' {
            let next = bytes.get(i + 1).copied();
            let tok = if next.is_some_and(|b| b.is_ascii_alphabetic() || b == b'_') {
                i += 1;
                let id_start = i;
                while i < bytes.len() && is_ident_continue(bytes[i]) {
                    i += 1;
                }
                Tok::ColonIdent(&source[id_start..i])
            } else {
                i += 1;
                Tok::Colon
            };
            out.push(Spanned { tok, offset: start });
            continue;
        }

        // `@format` filter. Emit as a regular Ident with the `@`
        // preserved so the builtin registry dispatches on the full name.
        if c == b'@' {
            i = scan_ident(bytes, i + 1);
            if i == start + 1 {
                return Err(CompileError::Lex {
                    offset: start,
                    message: "bare `@` with no identifier".into(),
                });
            }
            out.push(Spanned {
                tok: Tok::Ident(&source[start..i]),
                offset: start,
            });
            continue;
        }

        // Dollar-prefixed variable reference.
        if c == b'$' {
            i += 1;
            let id_start = i;
            i = scan_ident(bytes, i);
            if id_start == i {
                return Err(CompileError::Lex {
                    offset: start,
                    message: "bare `$` with no identifier".into(),
                });
            }
            out.push(Spanned {
                tok: Tok::DollarIdent(&source[id_start..i]),
                offset: start,
            });
            continue;
        }

        if c == b'"' {
            let (value, end) = lex_string(source, i)?;
            out.push(Spanned {
                tok: Tok::Str(value),
                offset: start,
            });
            i = end;
            continue;
        }

        if c.is_ascii_digit() {
            let (num, end) = lex_number(source, i)?;
            out.push(Spanned {
                tok: Tok::Num(num),
                offset: start,
            });
            i = end;
            continue;
        }

        if is_ident_start(c) {
            let id_start = i;
            i = scan_ident(bytes, i + 1);
            let name = &source[id_start..i];
            let tok = keyword_for(name).unwrap_or(Tok::Ident(name));
            out.push(Spanned { tok, offset: start });
            continue;
        }

        return Err(CompileError::Lex {
            offset: start,
            message: format!("unexpected character: {}", c as char),
        });
    }

    out.push(Spanned {
        tok: Tok::Eof,
        offset: source.len(),
    });
    Ok(out)
}

fn scan_ident(bytes: &[u8], mut i: usize) -> usize {
    while i < bytes.len() && is_ident_continue(bytes[i]) {
        i += 1;
    }
    i
}

fn single_char_tok(c: u8) -> Option<Tok<'static>> {
    Some(match c {
        b'.' => Tok::Dot,
        b'(' => Tok::LParen,
        b')' => Tok::RParen,
        b'[' => Tok::LBracket,
        b']' => Tok::RBracket,
        b'{' => Tok::LBrace,
        b'}' => Tok::RBrace,
        b',' => Tok::Comma,
        b';' => Tok::Semicolon,
        b'|' => Tok::Pipe,
        b'=' => Tok::Eq,
        b'/' => Tok::Slash,
        b'?' => Tok::Question,
        b'+' => Tok::Plus,
        b'-' => Tok::Minus,
        b'*' => Tok::Star,
        b'%' => Tok::Percent,
        b'<' => Tok::Lt,
        b'>' => Tok::Gt,
        _ => return None,
    })
}

fn is_ident_start(c: u8) -> bool {
    c.is_ascii_alphabetic() || c == b'_'
}

fn is_ident_continue(c: u8) -> bool {
    c.is_ascii_alphanumeric() || c == b'_'
}

fn keyword_for(name: &str) -> Option<Tok<'_>> {
    Some(match name {
        "if" => Tok::KwIf,
        "then" => Tok::KwThen,
        "elif" => Tok::KwElif,
        "else" => Tok::KwElse,
        "end" => Tok::KwEnd,
        "as" => Tok::KwAs,
        "def" => Tok::KwDef,
        "true" => Tok::KwTrue,
        "false" => Tok::KwFalse,
        "null" => Tok::KwNull,
        "and" => Tok::KwAnd,
        "or" => Tok::KwOr,
        "not" => Tok::KwNot,
        _ => return None,
    })
}

fn lex_string(source: &str, start: usize) -> Result<(Cow<'_, str>, usize), CompileError> {
    let bytes = source.as_bytes();
    debug_assert_eq!(bytes[start], b'"');
    let mut i = start + 1;
    let mut owned: Option<String> = None;
    let mut raw_start = i;
    // Paren depth inside `\(...)`. Nonzero means `"` opens a nested
    // string instead of terminating this one.
    let mut depth = 0usize;

    while i < bytes.len() {
        if depth > 0 {
            match bytes[i] {
                b'(' => {
                    depth += 1;
                    i += 1;
                }
                b')' => {
                    depth -= 1;
                    i += 1;
                }
                b'"' => {
                    let (_inner, end) = lex_string(source, i)?;
                    i = end;
                }
                _ => i += 1,
            }
            continue;
        }
        match bytes[i] {
            b'"' => {
                let tail = &source[raw_start..i];
                let value = match owned {
                    Some(mut s) => {
                        s.push_str(tail);
                        Cow::Owned(s)
                    }
                    None => Cow::Borrowed(&source[start + 1..i]),
                };
                return Ok((value, i + 1));
            }
            b'\\' => {
                let mut buf = owned.take().unwrap_or_default();
                buf.push_str(&source[raw_start..i]);
                i += 1;
                if i >= bytes.len() {
                    break;
                }
                match bytes[i] {
                    b'"' | b'\\' | b'/' => buf.push(bytes[i] as char),
                    b'n' => buf.push('\n'),
                    b't' => buf.push('\t'),
                    b'r' => buf.push('\r'),
                    b'0' => buf.push('\0'),
                    // `\(expr)` stays raw for the parser to re-tokenise.
                    b'(' => {
                        buf.push_str("\\(");
                        depth = 1;
                    }
                    other => {
                        return Err(CompileError::Lex {
                            offset: i - 1,
                            message: format!("unknown escape `\\{}`", other as char),
                        });
                    }
                }
                i += 1;
                raw_start = i;
                owned = Some(buf);
            }
            b'\n' => {
                return Err(CompileError::Lex {
                    offset: start,
                    message: "unterminated string literal (newline before close)".into(),
                });
            }
            _ => i += 1,
        }
    }
    Err(CompileError::Lex {
        offset: start,
        message: "unterminated string literal".into(),
    })
}

fn lex_number(source: &str, start: usize) -> Result<(f64, usize), CompileError> {
    let bytes = source.as_bytes();
    let mut i = start;
    let consume_digits = |bytes: &[u8], i: &mut usize| {
        while *i < bytes.len() && bytes[*i].is_ascii_digit() {
            *i += 1;
        }
    };
    consume_digits(bytes, &mut i);
    if i < bytes.len() && bytes[i] == b'.' && bytes.get(i + 1).is_some_and(u8::is_ascii_digit) {
        i += 1;
        consume_digits(bytes, &mut i);
    }
    if i < bytes.len() && matches!(bytes[i], b'e' | b'E') {
        i += 1;
        if i < bytes.len() && matches!(bytes[i], b'+' | b'-') {
            i += 1;
        }
        consume_digits(bytes, &mut i);
    }
    let text = &source[start..i];
    let value: f64 = text.parse().map_err(|_| CompileError::Lex {
        offset: start,
        message: format!("invalid number literal `{text}`"),
    })?;
    Ok((value, i))
}

#[cfg(test)]
mod tests {
    //! Lexer behavior is exercised transitively by `tests/queries.rs`
    //! (every query compiled there passes through this module). The
    //! only error we want to pin down here is unterminated strings,
    //! because compile-error surface is part of the public contract.

    use super::*;

    #[test]
    fn unterminated_string_is_an_error() {
        assert!(tokenize(r#""oops"#).is_err());
        assert!(tokenize("\"line\nwrap\"").is_err());
    }
}