MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / tokenize

Function tokenize

nodedb/src/control/security/predicate_parser.rs:53–133  ·  view source on GitHub ↗
(input: &str)

Source from the content-addressed store, hash-verified

51// ── Tokenizer ───────────────────────────────────────────────────────────
52
53fn tokenize(input: &str) -> Result<Vec<String>, PredicateParseError> {
54 let mut tokens = Vec::new();
55 let mut chars = input.chars().peekable();
56
57 while let Some(&ch) = chars.peek() {
58 if ch.is_whitespace() {
59 chars.next();
60 continue;
61 }
62
63 if ch == '(' || ch == ')' {
64 tokens.push(ch.to_string());
65 chars.next();
66 continue;
67 }
68
69 // Quoted string literal.
70 if ch == '\'' {
71 chars.next(); // consume opening quote
72 let mut s = String::new();
73 loop {
74 match chars.next() {
75 Some('\'') => {
76 // Check for escaped quote ('')
77 if chars.peek() == Some(&'\'') {
78 s.push('\'');
79 chars.next();
80 } else {
81 break;
82 }
83 }
84 Some(c) => s.push(c),
85 None => {
86 return Err(PredicateParseError::InvalidLiteral(
87 "unterminated string".into(),
88 ));
89 }
90 }
91 }
92 tokens.push(format!("'{s}'"));
93 continue;
94 }
95
96 // Operators: =, !=, <>, <, <=, >, >=
97 if matches!(ch, '=' | '!' | '<' | '>') {
98 let mut op = String::new();
99 op.push(ch);
100 chars.next();
101 if let Some(&next) = chars.peek()
102 && ((ch == '!' && next == '=')
103 || (ch == '<' && matches!(next, '=' | '>'))
104 || (ch == '>' && next == '='))
105 {
106 op.push(next);
107 chars.next();
108 }
109 tokens.push(op);
110 continue;

Callers 5

parse_predicateFunction · 0.70
parse_series_matcherFunction · 0.50
range_queryFunction · 0.50
instant_queryFunction · 0.50
annotationsFunction · 0.50

Calls 4

to_stringMethod · 0.80
peekMethod · 0.45
nextMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected