openehr-query 0.0.2

openEHR QUERY (AQL 1.1.0): hand-written lexer + parser + AST from the ANTLR4 grammar (no ANTLR runtime)
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
//! AQL lexer — a `logos` tokenizer transcribed from the authoritative
//! `AqlLexer.g4` (vendored at `vendor/grammar/`).
//!
//! No ANTLR runtime: the grammar is the spec, this is a hand-written DFA
//! lexer against it.
//!
//! Faithfulness notes:
//! - AQL keywords are **case-insensitive** (the grammar builds them from
//!   case-insensitive letter fragments), so each keyword uses
//!   `ignore(case)`.
//! - The grammar's grouped function-id tokens (`STRING_FUNCTION_ID`,
//!   `NUMERIC_FUNCTION_ID`, `DATE_TIME_FUNCTION_ID`) are **not** pre-grouped
//!   here: names like `length`/`abs`/`now` lex as [`Token::Identifier`] and the
//!   parser classifies a `name(args)` call. Structurally-distinct calls
//!   (aggregates, `terminology(...)`) keep dedicated keyword tokens because
//!   their argument grammar differs.
//! - `// NOTE:` quoted temporal literals (`DATE`/`TIME`/`DATETIME` in the
//!   grammar) are lexed as [`Token::String`]; typing them as temporals is a
//!   later semantic concern (the parser accepts a string where a primitive is
//!   expected). This keeps the lexer free of the fiddly ISO 8601-vs-string
//!   priority tangle. Per the QUERY spec §Dates and Times NOTE, the *typing*
//!   of a quoted value as a date/time is resolved from the identified-path
//!   context in the semantic pass, not from the literal — so an untyped
//!   `Token::String` is the faithful carrier here (all temporal literals are
//!   indistinguishable from strings at this layer, by design).
//! - `// NOTE:` the grammar's single-row function-id groups
//!   (`STRING_FUNCTION_ID`/`NUMERIC_FUNCTION_ID`/`DATE_TIME_FUNCTION_ID` —
//!   `length`, `abs`, `now`, …) are **not** reserved here: they lex as
//!   [`Token::Identifier`] and the parser classifies a `name(args)` call
//!   (`AqlParser.g4 functionCall` explicitly also admits a bare `IDENTIFIER`
//!   name). This makes the accepted set a *superset* of the grammar (it never
//!   rejects valid AQL; it additionally tolerates these words as identifiers).
//!   a superset accept-envelope is the sanctioned direction; the
//!   reserved-word restriction is a semantic concern, not a syntax one.

use logos::Logos;

/// A comparison operator (`COMPARISON_OPERATOR` in the grammar).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompOp {
    /// `=`
    Eq,
    /// `!=`
    Ne,
    /// `>`
    Gt,
    /// `>=`
    Ge,
    /// `<`
    Lt,
    /// `<=`
    Le,
}

/// An AQL token. Slices that carry text (identifiers, literals) hold an owned
/// `String` lexed from the source.
#[derive(Logos, Debug, Clone, PartialEq)]
#[logos(skip r"[ \t\r\n\f]+")] // WS -> skip (AqlLexer.g4 `WS`)
#[logos(skip "\u{feff}")] // UNICODE_BOM -> skip (AqlLexer.g4 `UNICODE_BOM`)
pub enum Token {
    // ── structural keywords (case-insensitive) ──────────────────────────────
    /// The `select` keyword token (`SELECT` in the grammar; case-insensitive).
    #[token("select", ignore(case))]
    Select,
    /// The `as` keyword token, binding a SELECT column alias.
    #[token("as", ignore(case))]
    As,
    /// The `from` keyword token, opening the FROM clause.
    #[token("from", ignore(case))]
    From,
    /// The `where` keyword token, opening the WHERE clause.
    #[token("where", ignore(case))]
    Where,
    /// The `order` keyword token of `ORDER BY`.
    #[token("order", ignore(case))]
    Order,
    /// The `by` keyword token of `ORDER BY`.
    #[token("by", ignore(case))]
    By,
    /// The `desc` keyword token — descending sort direction.
    #[token("desc", ignore(case))]
    Desc,
    /// The `descending` keyword token — the long spelling of `desc`.
    #[token("descending", ignore(case))]
    Descending,
    /// The `asc` keyword token — ascending sort direction.
    #[token("asc", ignore(case))]
    Asc,
    /// The `ascending` keyword token — the long spelling of `asc`.
    #[token("ascending", ignore(case))]
    Ascending,
    /// The `limit` keyword token, opening the LIMIT clause.
    #[token("limit", ignore(case))]
    Limit,
    /// The `offset` keyword token of the LIMIT clause.
    #[token("offset", ignore(case))]
    Offset,
    /// The `distinct` keyword token of a SELECT clause.
    #[token("distinct", ignore(case))]
    Distinct,
    /// The `version` keyword token — the VERSION class in a FROM clause.
    #[token("version", ignore(case))]
    Version,
    /// The `latest_version` keyword token — the latest-version predicate.
    #[token("latest_version", ignore(case))]
    LatestVersion,
    /// The `all_versions` keyword token — the all-versions predicate.
    #[token("all_versions", ignore(case))]
    AllVersions,
    /// The `top` keyword token of a SELECT clause.
    #[token("top", ignore(case))]
    Top,
    /// The `forward` keyword token — a `TOP` direction.
    #[token("forward", ignore(case))]
    Forward,
    /// The `backward` keyword token — a `TOP` direction.
    #[token("backward", ignore(case))]
    Backward,
    // NOTE: `TIMEWINDOW` is deliberately NOT a token — AQL 1.1 removed the clause
    // from the grammar (QUERY `master00-amendment_record.adoc`, SPECQUERY-20), so a
    // query using it is invalid AQL 1.1 and must fail to parse.
    /// The `contains` keyword token, joining a containment chain.
    #[token("contains", ignore(case))]
    Contains,
    /// The `and` keyword token — boolean conjunction.
    #[token("and", ignore(case))]
    And,
    /// The `or` keyword token — boolean disjunction.
    #[token("or", ignore(case))]
    Or,
    /// The `not` keyword token — boolean negation.
    #[token("not", ignore(case))]
    Not,
    /// The `exists` keyword token — the existence operator.
    #[token("exists", ignore(case))]
    Exists,
    /// The `like` keyword token — the pattern-match operator.
    #[token("like", ignore(case))]
    Like,
    /// The `matches` keyword token — the value-set match operator.
    #[token("matches", ignore(case))]
    Matches,

    // aggregate + terminology (distinct argument grammar → dedicated tokens)
    /// The `count` aggregate-function keyword token.
    #[token("count", ignore(case))]
    Count,
    /// The `min` aggregate-function keyword token.
    #[token("min", ignore(case))]
    Min,
    /// The `max` aggregate-function keyword token.
    #[token("max", ignore(case))]
    Max,
    /// The `sum` aggregate-function keyword token.
    #[token("sum", ignore(case))]
    Sum,
    /// The `avg` aggregate-function keyword token.
    #[token("avg", ignore(case))]
    Avg,
    /// The `terminology` function keyword token.
    #[token("terminology", ignore(case))]
    Terminology,

    // literal keywords
    /// The `true` boolean-literal keyword token.
    #[token("true", ignore(case))]
    True,
    /// The `false` boolean-literal keyword token.
    #[token("false", ignore(case))]
    False,
    /// The `null` literal keyword token.
    #[token("null", ignore(case))]
    Null,

    // ── operators & symbols ─────────────────────────────────────────────────
    /// A comparison operator: `=`, `!=`, `>=`, `<=`, `>` or `<`.
    #[token("=", |_| CompOp::Eq)]
    #[token("!=", |_| CompOp::Ne)]
    #[token(">=", |_| CompOp::Ge)]
    #[token("<=", |_| CompOp::Le)]
    #[token(">", |_| CompOp::Gt)]
    #[token("<", |_| CompOp::Lt)]
    Comparison(CompOp),

    /// The `(` symbol token.
    #[token("(")]
    LeftParen,
    /// The `)` symbol token.
    #[token(")")]
    RightParen,
    /// The `[` symbol token, opening a node predicate.
    #[token("[")]
    LeftBracket,
    /// The `]` symbol token, closing a node predicate.
    #[token("]")]
    RightBracket,
    /// The `{` symbol token.
    #[token("{")]
    LeftCurly,
    /// The `}` symbol token.
    #[token("}")]
    RightCurly,
    /// The `,` symbol token.
    #[token(",")]
    Comma,
    /// The `/` symbol token — a path separator.
    #[token("/")]
    Slash,
    /// The `*` symbol token — the wildcard path/column.
    #[token("*")]
    Asterisk,
    /// The `;` symbol token.
    #[token(";")]
    Semicolon,
    /// The `-` symbol token.
    #[token("-")]
    Minus,
    /// `--` — the grammar's `SYM_DOUBLE_DASH` optional statement terminator.
    ///
    /// Per `AqlLexer.g4` `COMMENT`, `--` introduces a line comment on a hidden
    /// channel when it is followed by a space (`-- text`) or immediately by an
    /// end-of-line/EOF (bare `--\n` / `--<EOF>`); the callback skips
    /// those, consuming to end of line. Only the rare `--` immediately followed
    /// by a non-space, non-newline char (e.g. `--foo`, two minus signs) is
    /// emitted as this token — matching ANTLR's `SYM_DOUBLE_DASH` fallback.
    #[token("--", line_comment)]
    DoubleDash,

    // ── literals & names ────────────────────────────────────────────────────
    /// A `$name` query parameter, slice included (the grammar's `PARAMETER`).
    #[regex(r"\$[a-zA-Z][a-zA-Z0-9_]*", |lex| lex.slice().to_owned())]
    Parameter(String),

    // `idNN[.NN]*` / `atNN[.NN]*` node codes (higher priority than Identifier).
    // The grammar's `CODE_STR` permits leading-zero runs (`at0001`), so a plain
    // `[0-9]+(.[0-9]+)*` after the `id`/`at` prefix is the faithful shape.
    /// An `idNN[.NN]*` archetype node code, slice included.
    #[regex(r"id[0-9]+(\.[0-9]+)*", |lex| lex.slice().to_owned())]
    IdCode(String),
    /// An `atNN[.NN]*` archetype node code, slice included.
    #[regex(r"at[0-9]+(\.[0-9]+)*", |lex| lex.slice().to_owned())]
    AtCode(String),

    // Archetype HRID, e.g. `openEHR-EHR-OBSERVATION.blood_pressure.v1`
    // (optionally namespaced). Detected by the `-x-x.…vN` shape. The version
    // tail admits the grammar's `VERSION_ID` `-rc`/`-alpha` pre-release suffix
    // (`…v1.0.0-rc.2`), and the namespace prefix admits `-` per `NAMESPACE`/
    // `LABEL` (`NAME_CHAR` includes `-`).
    #[regex(
        r"([a-zA-Z][a-zA-Z0-9_.\-]*::)?[a-zA-Z][a-zA-Z0-9_]*-[a-zA-Z][a-zA-Z0-9_]*-[a-zA-Z][a-zA-Z0-9_]*\.[a-zA-Z][a-zA-Z0-9_-]*\.v[0-9]+(\.[0-9]+)*((-rc|-alpha)(\.[0-9]+)?)?",
        |lex| lex.slice().to_owned()
    )]
    /// An archetype HRID (optionally namespaced), slice included — e.g.
    /// `openEHR-EHR-OBSERVATION.blood_pressure.v1`.
    ArchetypeHrid(String),

    // A term code, e.g. `local::at0001`, `SNOMED-CT::1234|text|` or
    // `ISO_639-1::en`. Per `AqlLexer.g4` `TERM_CODE`, every code segment is
    // `TERM_CODE_CHAR+` where `TERM_CODE_CHAR = NAME_CHAR | '.'` and
    // `NAME_CHAR = WORD_CHAR | '-'` — so hyphens are legal in both the
    // terminology id and the code. A `::` is still required, so a
    // bare subtraction like `a-b` (no `::`) is unaffected.
    #[regex(
        r"[a-zA-Z0-9._\-]+(\([a-zA-Z0-9._\-]+\))?::[a-zA-Z0-9._\-]+(\|[^|\[\]]+\|)?",
        |lex| lex.slice().to_owned()
    )]
    /// A term code, slice included — e.g. `local::at0001` or
    /// `SNOMED-CT::1234|cyanosis|` (the grammar's `TERM_CODE`).
    TermCode(String),

    // A URI, e.g. `http://example.org/x`. Recognised by a `scheme://` lead.
    #[regex(r"[a-zA-Z][a-zA-Z0-9+.\-]*://[^ \t\r\n{}]*", |lex| lex.slice().to_owned())]
    /// A URI, slice included — recognised by its `scheme://` lead.
    Uri(String),

    // A contained regex, e.g. `{/pattern/}` or `{/pattern/; 'name'}` (used in a
    // node predicate's `objectPath MATCHES CONTAINED_REGEX`). Whole thing is one
    // token so its inner `/` and `{}` are not mistaken for other symbols.
    #[regex(
        r"\{[ \t\r\n]*/(\\.|[^/\r\n])*/[ \t\r\n]*(;[ \t\r\n]*'([^'\\]|\\.)*')?[ \t\r\n]*\}",
        |lex| lex.slice().to_owned()
    )]
    /// A contained regex, slice included — `{/pattern/}` or
    /// `{/pattern/; 'name'}` (the grammar's `CONTAINED_REGEX`).
    ContainedRegex(String),

    // Scientific and plain numerics (order: sci before plain via length).
    /// An integer in scientific notation, lexeme included — e.g. `1e10`.
    #[regex(r"[0-9]+[eE][+\-]?[0-9]+", |lex| lex.slice().to_owned())]
    SciInteger(String),
    /// A real in scientific notation, lexeme included — e.g. `1.5e-3`.
    #[regex(r"[0-9]*\.[0-9]+[eE][+\-]?[0-9]+", |lex| lex.slice().to_owned())]
    SciReal(String),
    /// A plain real literal, lexeme included — e.g. `3.14`.
    #[regex(r"[0-9]*\.[0-9]+", |lex| lex.slice().to_owned())]
    Real(String),
    /// A plain integer literal, lexeme included — e.g. `42`.
    #[regex(r"[0-9]+", |lex| lex.slice().to_owned())]
    Integer(String),

    // Single- or double-quoted string (also carries quoted temporals; see the
    // module NOTE). Escapes are preserved in the slice, unescaped later.
    /// A single- or double-quoted string literal, quotes and escapes
    /// preserved in the slice (also carries quoted temporals — see the module
    /// docs).
    #[regex(r"'([^'\\]|\\.)*'", |lex| lex.slice().to_owned())]
    #[regex(r#""([^"\\]|\\.)*""#, |lex| lex.slice().to_owned())]
    String(String),

    // Plain identifier (lowest-priority word token).
    #[regex(r"[a-zA-Z][a-zA-Z0-9_]*", |lex| lex.slice().to_owned())]
    /// A plain identifier, slice included — the lowest-priority word token.
    Identifier(String),
}

/// Callback for the `--` token implementing `AqlLexer.g4` `COMMENT`.
///
/// The grammar treats `--` as a line comment (hidden channel, i.e. skipped)
/// when it is followed by a space and text, or immediately by end-of-line/EOF.
/// Anything else (`--` glued to a non-space token) is emitted as the
/// `SYM_DOUBLE_DASH` terminator. When skipping, the rest of the line is
/// consumed (the trailing newline is handled by the `WS` skip).
fn line_comment(lex: &mut logos::Lexer<Token>) -> logos::Filter<()> {
    let rem = lex.remainder();
    let is_comment = rem.is_empty() || rem.starts_with([' ', '\t', '\r', '\n']);
    if is_comment {
        let end = rem.find(['\r', '\n']).unwrap_or(rem.len());
        lex.bump(end);
        logos::Filter::Skip
    } else {
        logos::Filter::Emit(())
    }
}

/// Lex `src` into a token vector, or report the byte span of the first token
/// that fails to lex.
///
/// # Errors
/// Returns [`LexError`] on the first unrecognized token.
pub fn lex(src: &str) -> Result<Vec<Token>, LexError> {
    let mut out = Vec::new();
    let mut lexer = Token::lexer(src);
    while let Some(res) = lexer.next() {
        match res {
            Ok(tok) => out.push(tok),
            Err(()) => {
                return Err(LexError {
                    span: lexer.span(),
                    slice: lexer.slice().to_owned(),
                });
            }
        }
    }
    Ok(out)
}

/// A lexing failure at a byte span.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("unexpected token {slice:?} at bytes {span:?}")]
pub struct LexError {
    /// Byte range of the offending input.
    pub span: std::ops::Range<usize>,
    /// The offending slice.
    pub slice: String,
}

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

    fn toks(src: &str) -> Vec<Token> {
        lex(src).unwrap_or_else(|e| panic!("lex failed: {e}"))
    }

    #[test]
    fn keywords_are_case_insensitive() {
        assert_eq!(toks("SELECT"), vec![Token::Select]);
        assert_eq!(toks("select"), vec![Token::Select]);
        assert_eq!(toks("Contains"), vec![Token::Contains]);
    }

    #[test]
    fn simple_select_from() {
        // SELECT c FROM COMPOSITION c
        let t = toks("SELECT c FROM COMPOSITION c");
        assert_eq!(
            t,
            vec![
                Token::Select,
                Token::Identifier("c".into()),
                Token::From,
                Token::Identifier("COMPOSITION".into()),
                Token::Identifier("c".into()),
            ]
        );
    }

    #[test]
    fn path_with_comparison_and_string() {
        // e/ehr_id/value = 'x'
        let t = toks("e/ehr_id/value = 'x'");
        assert_eq!(
            t,
            vec![
                Token::Identifier("e".into()),
                Token::Slash,
                Token::Identifier("ehr_id".into()),
                Token::Slash,
                Token::Identifier("value".into()),
                Token::Comparison(CompOp::Eq),
                Token::String("'x'".into()),
            ]
        );
    }

    #[test]
    fn node_codes_and_archetype_hrid() {
        assert_eq!(toks("at0001"), vec![Token::AtCode("at0001".into())]);
        assert_eq!(toks("id1.2.3"), vec![Token::IdCode("id1.2.3".into())]);
        assert_eq!(
            toks("openEHR-EHR-OBSERVATION.blood_pressure.v1"),
            vec![Token::ArchetypeHrid(
                "openEHR-EHR-OBSERVATION.blood_pressure.v1".into()
            )]
        );
    }

    #[test]
    fn id_prefix_alone_is_identifier() {
        // `id` / `at` without a code are plain identifiers.
        assert_eq!(toks("id"), vec![Token::Identifier("id".into())]);
        assert_eq!(
            toks("attribute"),
            vec![Token::Identifier("attribute".into())]
        );
    }

    #[test]
    fn numbers_and_params() {
        assert_eq!(toks("42"), vec![Token::Integer("42".into())]);
        assert_eq!(toks("3.14"), vec![Token::Real("3.14".into())]);
        assert_eq!(toks("1e10"), vec![Token::SciInteger("1e10".into())]);
        assert_eq!(toks("$ehrId"), vec![Token::Parameter("$ehrId".into())]);
    }

    #[test]
    fn comparison_operators() {
        assert_eq!(toks(">="), vec![Token::Comparison(CompOp::Ge)]);
        assert_eq!(toks("!="), vec![Token::Comparison(CompOp::Ne)]);
        assert_eq!(toks("<"), vec![Token::Comparison(CompOp::Lt)]);
    }

    #[test]
    fn contains_query_shape() {
        // FROM EHR e CONTAINS COMPOSITION c
        let t = toks("FROM EHR e CONTAINS COMPOSITION c");
        assert_eq!(
            t,
            vec![
                Token::From,
                Token::Identifier("EHR".into()),
                Token::Identifier("e".into()),
                Token::Contains,
                Token::Identifier("COMPOSITION".into()),
                Token::Identifier("c".into()),
            ]
        );
    }

    #[test]
    fn hyphenated_term_codes_lex_as_one_token() {
        // TERM_CODE_CHAR includes '-' (via NAME_CHAR), so hyphenated
        // terminology ids lex as a single TERM_CODE, not `id Minus code`.
        assert_eq!(
            toks("SNOMED-CT::1234"),
            vec![Token::TermCode("SNOMED-CT::1234".into())]
        );
        assert_eq!(
            toks("ISO_639-1::en"),
            vec![Token::TermCode("ISO_639-1::en".into())]
        );
        assert_eq!(
            toks("SNOMED-CT::1234|cyanosis|"),
            vec![Token::TermCode("SNOMED-CT::1234|cyanosis|".into())]
        );
        // The parenthesised terminology-version form also admits hyphens.
        assert_eq!(
            toks("snomed-ct(3.1)::3415004"),
            vec![Token::TermCode("snomed-ct(3.1)::3415004".into())]
        );
    }

    #[test]
    fn subtraction_is_not_a_term_code_regression() {
        // Plain subtraction must not regress: without `::` there is no
        // TERM_CODE, so `a-b` and `a - 1` stay separate tokens.
        assert_eq!(
            toks("a-b"),
            vec![
                Token::Identifier("a".into()),
                Token::Minus,
                Token::Identifier("b".into()),
            ]
        );
        assert_eq!(
            toks("a - 1"),
            vec![
                Token::Identifier("a".into()),
                Token::Minus,
                Token::Integer("1".into()),
            ]
        );
    }

    #[test]
    fn archetype_hrid_version_suffixes_and_namespace_hyphen() {
        // `VERSION_ID` `-rc`/`-alpha` pre-release suffixes and a
        // hyphenated namespace both lex as a single ARCHETYPE_HRID.
        assert_eq!(
            toks("openEHR-EHR-OBSERVATION.blood_pressure.v1.0.0-rc.2"),
            vec![Token::ArchetypeHrid(
                "openEHR-EHR-OBSERVATION.blood_pressure.v1.0.0-rc.2".into()
            )]
        );
        assert_eq!(
            toks("openEHR-EHR-OBSERVATION.blood_pressure.v2-alpha"),
            vec![Token::ArchetypeHrid(
                "openEHR-EHR-OBSERVATION.blood_pressure.v2-alpha".into()
            )]
        );
        assert_eq!(
            toks("org-x::openEHR-EHR-OBSERVATION.blood_pressure.v1"),
            vec![Token::ArchetypeHrid(
                "org-x::openEHR-EHR-OBSERVATION.blood_pressure.v1".into()
            )]
        );
    }

    #[test]
    fn line_comments_are_skipped() {
        // `-- text` to end of line is a comment (skipped).
        assert_eq!(
            toks("SELECT c -- trailing comment\nFROM COMPOSITION c"),
            vec![
                Token::Select,
                Token::Identifier("c".into()),
                Token::From,
                Token::Identifier("COMPOSITION".into()),
                Token::Identifier("c".into()),
            ]
        );
        // A bare `--` at end-of-input is a comment too (skipped).
        assert_eq!(toks("c --"), vec![Token::Identifier("c".into())]);
        assert_eq!(toks("c --\n"), vec![Token::Identifier("c".into())]);
        // `--` glued to a non-space token is the `SYM_DOUBLE_DASH` terminator.
        assert_eq!(
            toks("--foo"),
            vec![Token::DoubleDash, Token::Identifier("foo".into())]
        );
    }

    #[test]
    fn utf_bom_is_skipped() {
        // a leading BOM is skipped, not a lex error.
        assert_eq!(toks("\u{feff}SELECT"), vec![Token::Select]);
    }

    #[test]
    fn term_code_and_predicate_brackets() {
        // [openEHR-EHR-OBSERVATION.x.v1] and local::at0001
        assert_eq!(
            toks("local::at0001"),
            vec![Token::TermCode("local::at0001".into())]
        );
        let t = toks("[at0001]");
        assert_eq!(
            t,
            vec![
                Token::LeftBracket,
                Token::AtCode("at0001".into()),
                Token::RightBracket
            ]
        );
    }
}