-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathvalidation.rs
More file actions
513 lines (476 loc) Β· 17.9 KB
/
validation.rs
File metadata and controls
513 lines (476 loc) Β· 17.9 KB
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
// via: https://github.com/rust-lang/rust-analyzer/blob/d8887c0758bbd2d5f752d5bd405d4491e90e7ed6/crates/syntax/src/validation.rs
//! This module implements syntax validation that the parser doesn't handle.
//!
//! A failed validation emits a diagnostic.
use std::fmt;
use std::ops::RangeInclusive;
use crate::ast::AstNode;
use crate::{SyntaxNode, SyntaxToken, ast, match_ast, syntax_error::SyntaxError};
use rowan::{TextRange, TextSize};
use squawk_parser::SyntaxKind::*;
pub(crate) fn validate(root: &SyntaxNode, errors: &mut Vec<SyntaxError>) {
for node in root.descendants() {
match_ast! {
match node {
ast::AlterAggregate(it) => validate_aggregate_params(it.aggregate().and_then(|x| x.param_list()), errors),
ast::CreateAggregate(it) => validate_aggregate_params(it.param_list(), errors),
ast::CreateTable(it) => validate_create_table(it, errors),
ast::PrefixExpr(it) => validate_prefix_expr(it, errors),
ast::ArrayExpr(it) => validate_array_expr(it, errors),
ast::DropAggregate(it) => validate_drop_aggregate(it, errors),
ast::JoinExpr(it) => validate_join_expr(it, errors),
ast::Literal(it) => validate_literal(it, errors),
ast::NonStandardParam(it) => validate_non_standard_param(it, errors),
ast::Select(it) => validate_select(it, errors),
ast::SelectInto(it) => validate_select_into(it, errors),
_ => (),
}
}
}
for element in root.descendants_with_tokens() {
if let Some(token) = element.into_token()
&& token.kind() == IDENT
&& let Some(err) = validate_unicode_esc_ident(&token)
{
errors.push(err);
}
}
}
fn validate_select(it: ast::Select, acc: &mut Vec<SyntaxError>) {
let Some(from_clause) = it.from_clause() else {
return;
};
if let Some(select_clause) = it.select_clause() {
if from_clause.syntax().text_range().end() < select_clause.syntax().text_range().start() {
// Postgres dialect doesn't support leading from clauses, e.g., `from t select c`
acc.push(SyntaxError::new(
"Leading from clauses are not supported in Postgres",
from_clause.syntax().text_range(),
));
}
} else {
// Postgres dialect doesn't support missing select clauses, e.g., `from t`
acc.push(SyntaxError::new(
"Missing select clause",
TextRange::empty(from_clause.syntax().text_range().start()),
));
}
}
fn validate_select_into(it: ast::SelectInto, acc: &mut Vec<SyntaxError>) {
for (child, ancestor) in it.syntax().ancestors().zip(it.syntax().ancestors().skip(1)) {
let kind = ancestor.kind();
if ast::ParenSelect::can_cast(kind) {
continue;
} else if let Some(compound_select) = ast::CompoundSelect::cast(ancestor) {
if compound_select
.lhs()
.is_some_and(|lhs| lhs.syntax() == &child)
{
continue;
}
acc.push(SyntaxError::new(
"INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT",
it.syntax().text_range(),
));
return;
} else if ast::Explain::can_cast(kind)
|| ast::Prepare::can_cast(kind)
|| ast::SourceFile::can_cast(kind)
{
return;
}
acc.push(SyntaxError::new(
"SELECT ... INTO is not allowed here",
it.syntax().text_range(),
));
return;
}
}
fn validate_create_table(it: ast::CreateTable, acc: &mut Vec<SyntaxError>) {
let Some(arg_list) = it.table_arg_list() else {
return;
};
let type_required = it.partition_of().is_none() && it.of_type().is_none();
for arg in arg_list.args() {
match arg {
ast::TableArg::Column(column) => {
if let Some(col_name) = column.name()
&& type_required
&& column.ty().is_none()
{
let end = col_name.syntax().text_range().end();
acc.push(SyntaxError::new(
"Missing column type",
TextRange::new(end, end),
));
}
}
ast::TableArg::LikeClause(_) => (),
ast::TableArg::TableConstraint(_) => (),
}
}
}
enum LookingFor {
OpenString,
CloseString(TextSize, bool),
}
fn validate_literal(lit: ast::Literal, acc: &mut Vec<SyntaxError>) {
let mut state = LookingFor::OpenString;
let mut maybe_errors = vec![];
// Checking for string continuation issues, like comments between string
// literals or missing new lines.
for e in lit.syntax().children_with_tokens() {
match e {
rowan::NodeOrToken::Node(_) => {
// not sure when this would occur
state = LookingFor::OpenString;
}
rowan::NodeOrToken::Token(token) => {
match state {
LookingFor::OpenString => {
if token.kind() == STRING {
state = LookingFor::CloseString(token.text_range().end(), false);
}
}
LookingFor::CloseString(text_range_end, seen_new_line) => match token.kind() {
WHITESPACE => {
let seen_new_line = token.text().contains("\n");
state = LookingFor::CloseString(text_range_end, seen_new_line);
}
COMMENT => {
maybe_errors.push(SyntaxError::new(
"Comments between string literals are not allowed.",
token.text_range(),
));
}
STRING => {
// avoid warning twice for the same two string literals, so we check maybe_errors
if !seen_new_line && maybe_errors.is_empty() {
maybe_errors.push(SyntaxError::new(
"Expected new line or comma between string literals",
TextRange::new(text_range_end, token.text_range().start()),
));
}
acc.append(&mut maybe_errors);
state = LookingFor::CloseString(token.text_range().end(), false);
}
_ => {
maybe_errors.clear();
state = LookingFor::OpenString;
}
},
}
}
}
}
if let Some(err) = validate_unicode_esc_string(&lit) {
acc.push(err);
}
}
fn validate_unicode_esc_string(lit: &ast::Literal) -> Option<SyntaxError> {
let mut unicode_esc = None;
let mut seen_uescape = false;
let mut escape_char = '\\';
for e in lit.syntax().children_with_tokens() {
let Some(token) = e.into_token() else {
continue;
};
match token.kind() {
UNICODE_ESC_STRING => unicode_esc = Some(token),
UESCAPE_KW => seen_uescape = true,
STRING if seen_uescape => {
escape_char = match uescape_char(&token) {
Some(ch) => ch,
None => {
return Some(SyntaxError::new(
"Invalid unicode escape character",
token.text_range(),
));
}
};
break;
}
_ => (),
}
}
let token = unicode_esc?;
let text = token.text();
let inside = text
.strip_prefix("U&'")
.or_else(|| text.strip_prefix("u&'"))
.and_then(|s| s.strip_suffix('\''))?;
let err = check_unicode_esc_str(inside, escape_char)?;
Some(SyntaxError::new(err.to_string(), token.text_range()))
}
fn validate_unicode_esc_ident(token: &SyntaxToken) -> Option<SyntaxError> {
let text = token.text();
let inside = text
.strip_prefix("U&\"")
.or_else(|| text.strip_prefix("u&\""))
.and_then(|s| s.strip_suffix('"'))?;
let mut escape_char = '\\';
let mut seen_uescape = false;
let mut next = token.next_sibling_or_token();
while let Some(element) = next {
match element.kind() {
WHITESPACE | COMMENT => (),
UESCAPE_KW => seen_uescape = true,
STRING if seen_uescape => {
if let Some(string_token) = element.as_token() {
escape_char = match uescape_char(string_token) {
Some(ch) => ch,
None => {
return Some(SyntaxError::new(
"Invalid unicode escape character",
string_token.text_range(),
));
}
};
}
break;
}
_ => break,
}
next = element.next_sibling_or_token();
}
let err = check_unicode_esc_str(inside, escape_char)?;
Some(SyntaxError::new(err.to_string(), token.text_range()))
}
// https://github.com/postgres/postgres/blob/228a1f9542792c6533ef74c2e7aefad0da1d9a7a/src/backend/parser/parser.c#L350
const fn is_valid_uescape_char(byte: u8) -> bool {
!byte.is_ascii_hexdigit()
&& byte != b'+'
&& byte != b'\''
&& byte != b'"'
&& !matches!(
byte,
b' ' | b'\t' | b'\n' | b'\r' | /* b'\v' */ 0x0B | /* b'\f' */ 0x0C
)
}
fn uescape_char(string_token: &SyntaxToken) -> Option<char> {
let text = string_token.text();
let inner = text.strip_prefix('\'')?.strip_suffix('\'')?;
let &[byte] = inner.as_bytes() else {
return None;
};
is_valid_uescape_char(byte).then(|| char::from(byte))
}
enum UnicodeEscapeKind {
Short,
Extended,
}
impl UnicodeEscapeKind {
fn count(&self) -> u32 {
match self {
UnicodeEscapeKind::Short => 4,
UnicodeEscapeKind::Extended => 6,
}
}
}
enum UnicodeEscError {
InvalidEscape,
InvalidSurrogatePair,
OutOfRange,
RequiresHexDigits {
kind: UnicodeEscapeKind,
escape_char: char,
},
}
impl fmt::Display for UnicodeEscError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidEscape => f.write_str("Invalid Unicode escape sequence"),
Self::InvalidSurrogatePair => f.write_str("Invalid Unicode surrogate pair"),
Self::OutOfRange => f.write_str("Unicode escape value out of range"),
Self::RequiresHexDigits { kind, escape_char } => {
let required = kind.count();
let plus = match kind {
UnicodeEscapeKind::Short => "",
UnicodeEscapeKind::Extended => "+",
};
let xs = "X".repeat(required as usize);
write!(
f,
"Unicode escape requires {required} hex digits: {escape_char}{plus}{xs}"
)
}
}
}
}
fn check_unicode_esc_str(text: &str, escape_char: char) -> Option<UnicodeEscError> {
const HIGH_SURROGATE: RangeInclusive<u32> = 0xD800..=0xDBFF;
const LOW_SURROGATE: RangeInclusive<u32> = 0xDC00..=0xDFFF;
const MAX_CODEPOINT: u32 = 0x10FFFF;
let mut chars = text.chars().peekable();
let mut high_surrogate: Option<u32> = None;
while let Some(c) = chars.next() {
if c != escape_char {
continue;
}
let kind = match chars.peek() {
Some(&c) if c == escape_char => {
chars.next();
high_surrogate = None;
continue;
}
Some('+') => {
chars.next();
UnicodeEscapeKind::Extended
}
Some(c) if c.is_ascii_hexdigit() => UnicodeEscapeKind::Short,
_ => return Some(UnicodeEscError::InvalidEscape),
};
let mut codepoint: u32 = 0;
for _ in 0..kind.count() {
let radix = 16;
let Some(d) = chars.peek().and_then(|c| c.to_digit(radix)) else {
return Some(UnicodeEscError::RequiresHexDigits { kind, escape_char });
};
chars.next();
codepoint = codepoint * radix + d;
}
if high_surrogate.take().is_some() {
if !LOW_SURROGATE.contains(&codepoint) {
return Some(UnicodeEscError::InvalidSurrogatePair);
}
} else if codepoint > MAX_CODEPOINT {
return Some(UnicodeEscError::OutOfRange);
} else if HIGH_SURROGATE.contains(&codepoint) {
high_surrogate = Some(codepoint);
} else if LOW_SURROGATE.contains(&codepoint) {
return Some(UnicodeEscError::InvalidSurrogatePair);
}
}
high_surrogate.map(|_| UnicodeEscError::InvalidSurrogatePair)
}
fn validate_join_expr(join_expr: ast::JoinExpr, acc: &mut Vec<SyntaxError>) {
let Some(join) = join_expr.join() else {
return;
};
let Some(join_type) = join.join_type() else {
return;
};
enum JoinClause {
Required,
NotAllowed,
}
use JoinClause::*;
let join_clause = if join.natural_token().is_some() {
NotAllowed
} else {
match join_type {
ast::JoinType::JoinCross(_) => NotAllowed,
ast::JoinType::JoinFull(_)
| ast::JoinType::JoinInner(_)
| ast::JoinType::JoinLeft(_)
| ast::JoinType::JoinRight(_) => Required,
}
};
let join_name = if join.natural_token().is_some() {
"natural"
} else {
match join_type {
ast::JoinType::JoinCross(_) => "cross",
ast::JoinType::JoinFull(_) => "full",
ast::JoinType::JoinInner(_) => "inner",
ast::JoinType::JoinLeft(_) => "left",
ast::JoinType::JoinRight(_) => "right",
}
};
match join_clause {
Required => {
if join.on_clause().is_none() && join.using_clause().is_none() {
let end = join_expr.syntax().text_range().end();
acc.push(SyntaxError::new(
"Join missing condition.",
TextRange::new(end, end),
));
}
}
NotAllowed => {
if let Some(using_clause) = join.using_clause() {
acc.push(SyntaxError::new(
format!("Join `using` clause is not allowed for {join_name} joins."),
using_clause.syntax().text_range(),
));
}
}
}
}
fn validate_drop_aggregate(drop_agg: ast::DropAggregate, acc: &mut Vec<SyntaxError>) {
for agg in drop_agg.aggregates() {
validate_aggregate_params(agg.param_list(), acc);
}
}
fn validate_array_expr(array_expr: ast::ArrayExpr, acc: &mut Vec<SyntaxError>) {
if array_expr.array_token().is_none() {
let parent_kind = array_expr.syntax().parent().map(|x| x.kind());
if matches!(parent_kind, Some(ARRAY_EXPR)) {
return;
}
let expr_range = array_expr.syntax().text_range();
let range = TextRange::new(expr_range.start(), expr_range.start());
acc.push(SyntaxError::new("Array missing ARRAY keyword.", range));
}
}
fn validate_prefix_expr(prefix_expr: ast::PrefixExpr, acc: &mut Vec<SyntaxError>) {
let Some(op) = prefix_expr
.syntax()
.children()
.find_map(ast::CustomOp::cast)
else {
return;
};
validate_custom_op(op, acc);
}
// https://www.postgresql.org/docs/17/sql-createoperator.html
fn validate_custom_op(op: ast::CustomOp, acc: &mut Vec<SyntaxError>) {
// TODO: there's more we can validate
let mut found = 0;
for node_or_token in op.syntax().children_with_tokens() {
match node_or_token {
rowan::NodeOrToken::Node(_) => (),
rowan::NodeOrToken::Token(_) => {
found += 1;
}
}
if found >= 2 {
return;
}
}
let token = op.syntax().children_with_tokens().find_map(|x| match x {
rowan::NodeOrToken::Node(_) => None,
rowan::NodeOrToken::Token(tk) => Some(tk.kind()),
});
if let Some(STAR | SLASH | L_ANGLE | R_ANGLE | EQ | PERCENT | CARET) = token {
acc.push(SyntaxError::new(
"Invalid operator.",
op.syntax().text_range(),
));
}
}
fn validate_aggregate_params(aggregate_params: Option<ast::ParamList>, acc: &mut Vec<SyntaxError>) {
if let Some(params) = aggregate_params {
for p in params.params() {
if let Some(mode) = p.mode() {
match mode {
ast::ParamMode::ParamOut(param_out) => acc.push(SyntaxError::new(
"Out params are not allowed with aggregates.",
param_out.syntax().text_range(),
)),
ast::ParamMode::ParamInOut(param_in_out) => acc.push(SyntaxError::new(
"In Out params are not allowed with aggregates.",
param_in_out.syntax().text_range(),
)),
ast::ParamMode::ParamIn(_) | ast::ParamMode::ParamVariadic(_) => (),
}
}
}
}
}
fn validate_non_standard_param(param: ast::NonStandardParam, acc: &mut Vec<SyntaxError>) {
acc.push(SyntaxError::new(
"Invalid parameter type. Use positional params like $1 instead.",
param.syntax().text_range(),
))
}