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
//! The one CIP-223 duration-pattern tokenizer, shared by the formatter
//! (`format/duration.rs`) and the parser (`duration_pattern.rs`).
use super::field::{DurationField, PatternSymbol};
#[derive(Debug, Clone)]
pub(super) enum Token {
Literal(String),
/// One run of the *same letter*: its symbol and the run's width. The
/// parser keeps `yY` as two year tokens (a duplicate field); the
/// formatter merges adjacent same-symbol runs ([`merge_symbol_runs`]).
Symbol(PatternSymbol, usize),
}
/// A quote opened an escape the pattern never closed: Neo4j's
/// pattern-parsing failure. Each caller spells it in its own error family.
#[derive(Debug)]
pub(super) struct UnclosedEscape;
impl Token {
/// The duration field this token names, if any — the token stream is the
/// one record of which fields the pattern mentions.
pub(super) fn field(&self) -> Option<DurationField> {
match self {
Token::Literal(_) => None,
Token::Symbol(symbol, _) => Some(symbol.field()),
}
}
}
/// The pattern's token stream. Escaping follows Neo4j's
/// `DurationValue.format` state machine: a quote opens an escape, a quote
/// inside one closes it (emitting nothing), and a doubled quote inside one
/// emits a literal quote and stays escaped. A letter outside the duration
/// alphabet is literal text (Neo4j's `NOT_A_COMPONENT_TOKEN`).
pub(super) fn tokenize(pattern: &str) -> Result<Vec<Token>, UnclosedEscape> {
let chars: Vec<char> = pattern.chars().collect();
let mut tokens = Vec::new();
let mut literal = String::new();
let mut i = 0;
let mut escaped = false;
while i < chars.len() {
let c = chars[i];
if escaped {
if c == '\'' {
if chars.get(i + 1) == Some(&'\'') {
literal.push('\'');
i += 2;
} else {
escaped = false;
i += 1;
}
} else {
literal.push(c);
i += 1;
}
continue;
}
if c == '\'' {
escaped = true;
i += 1;
continue;
}
match DurationField::pattern_symbol(c) {
Some(symbol) => {
if !literal.is_empty() {
tokens.push(Token::Literal(std::mem::take(&mut literal)));
}
let mut count = 1;
while chars.get(i + count) == Some(&c) {
count += 1;
}
tokens.push(Token::Symbol(symbol, count));
i += count;
}
None => {
literal.push(c);
i += 1;
}
}
}
if escaped {
return Err(UnclosedEscape);
}
if !literal.is_empty() {
tokens.push(Token::Literal(literal));
}
Ok(tokens)
}
/// The formatter's grouping: a run is a contiguous stretch of the same
/// token type, not the same letter (`ML` is one two-wide month run, `Yuy`
/// one three-wide year run — Neo4j's `DurationToken` counters).
pub(super) fn merge_symbol_runs(tokens: Vec<Token>) -> Vec<Token> {
let mut merged: Vec<Token> = Vec::with_capacity(tokens.len());
for token in tokens {
match (merged.last_mut(), &token) {
(Some(Token::Symbol(prev, width)), Token::Symbol(next, more)) if prev == next => {
*width += more;
}
_ => merged.push(token),
}
}
merged
}