utopia_query/ast/grammar.rs
1//! The lexical rules for names the serializer writes verbatim.
2
3/// Whitespace a cast type may hold between its words: `" \t\n\v\f\r"`.
4const WHITESPACE: &[u8] = b" \t\n\x0B\x0C\r";
5
6/// The lexical rules for function names and cast types, which the
7/// [`Serializer`](super::Serializer) writes verbatim, so a quoted identifier
8/// cannot carry SQL into them.
9///
10/// PHP: `Utopia\Query\AST\Grammar`.
11#[derive(Debug, Clone, Copy, Default)]
12pub struct Grammar;
13
14impl Grammar {
15 /// Whether `value` is a bare identifier: ASCII letters, digits and `_`,
16 /// not starting with a digit.
17 ///
18 /// PHP: `Grammar::isIdentifier()`.
19 ///
20 /// ```
21 /// use utopia_query::ast::Grammar;
22 ///
23 /// assert!(Grammar::is_identifier("LOWER"));
24 /// assert!(!Grammar::is_identifier("NOW() OR 1=1"));
25 /// ```
26 ///
27 /// ```php
28 /// use Utopia\Query\AST\Grammar;
29 ///
30 /// var_dump(Grammar::isIdentifier('LOWER')); // bool(true)
31 /// var_dump(Grammar::isIdentifier('NOW() OR 1=1')); // bool(false)
32 /// ```
33 pub fn is_identifier(value: &str) -> bool {
34 let bytes = value.as_bytes();
35 !bytes.is_empty() && !bytes[0].is_ascii_digit() && bytes.iter().all(|&b| is_identifier_byte(b))
36 }
37
38 /// Whether `value` is a type name: whitespace-separated identifiers,
39 /// optionally followed by parenthesised arguments of identifiers, commas
40 /// and whitespace (`DECIMAL(10, 2)`, `TIMESTAMP WITH TIME ZONE`).
41 ///
42 /// PHP: `Grammar::isType()`.
43 ///
44 /// ```
45 /// use utopia_query::ast::Grammar;
46 ///
47 /// assert!(Grammar::is_type("DECIMAL(10, 2)"));
48 /// assert!(!Grammar::is_type("TEXT) OR (1=1"));
49 /// ```
50 ///
51 /// ```php
52 /// use Utopia\Query\AST\Grammar;
53 ///
54 /// var_dump(Grammar::isType('DECIMAL(10, 2)')); // bool(true)
55 /// var_dump(Grammar::isType('TEXT) OR (1=1')); // bool(false)
56 /// ```
57 pub fn is_type(value: &str) -> bool {
58 let bytes = value.as_bytes();
59 let Some(open) = bytes.iter().position(|&b| b == b'(') else {
60 return is_words(bytes);
61 };
62 if bytes.last() != Some(&b')') {
63 return false;
64 }
65 let arguments = &bytes[open + 1..bytes.len() - 1];
66 let mut head = &bytes[..open];
67 while let Some((last, rest)) = head.split_last() {
68 if !WHITESPACE.contains(last) {
69 break;
70 }
71 head = rest;
72 }
73 !arguments.is_empty()
74 && arguments.iter().all(|&b| is_identifier_byte(b) || b == b',' || WHITESPACE.contains(&b))
75 && is_words(head)
76 }
77}
78
79fn is_identifier_byte(b: u8) -> bool {
80 b.is_ascii_alphanumeric() || b == b'_'
81}
82
83/// PHP's `Grammar::isWords()`: identifiers separated by whitespace, with none
84/// leading or trailing.
85fn is_words(value: &[u8]) -> bool {
86 let mut words = value.split(|b| WHITESPACE.contains(b));
87 let first = words.next().unwrap_or_default();
88 if first.is_empty() || value.last().is_none_or(|b| WHITESPACE.contains(b)) {
89 return false;
90 }
91 std::iter::once(first)
92 .chain(words)
93 .all(|word| word.is_empty() || (!word[0].is_ascii_digit() && word.iter().all(|&b| is_identifier_byte(b))))
94}