Skip to content

Commit

Permalink
Merge pull request #6 from AikidoSec/carriage-return
Browse files Browse the repository at this point in the history
Start new line if `\r` and dialect is postgres
  • Loading branch information
willem-delbare authored Jan 8, 2025
2 parents 91b78cc + f5f916b commit 182dc31
Showing 1 changed file with 57 additions and 6 deletions.
63 changes: 57 additions & 6 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1390,11 +1390,17 @@ impl<'a> Tokenizer<'a> {

// Consume characters until newline
fn tokenize_single_line_comment(&self, chars: &mut State) -> String {
let mut comment = peeking_take_while(chars, |ch| ch != '\n');
let mut comment = peeking_take_while(chars, |ch| match ch {
'\n' => false, // Always stop at \n
'\r' if dialect_of!(self is PostgreSqlDialect) => false, // Stop at \r for Postgres
_ => true, // Keep consuming for other characters
});

if let Some(ch) = chars.next() {
assert_eq!(ch, '\n');
assert!(ch == '\n' || ch == '\r');
comment.push(ch);
}

comment
}

Expand Down Expand Up @@ -2425,17 +2431,62 @@ mod tests {

#[test]
fn tokenize_comment() {
let sql = String::from("0--this is a comment\n1");
let test_cases = vec![
(
String::from("0--this is a comment\n1"),
vec![
Token::Number("0".to_string(), false),
Token::Whitespace(Whitespace::SingleLineComment {
prefix: "--".to_string(),
comment: "this is a comment\n".to_string(),
}),
Token::Number("1".to_string(), false),
],
),
(
String::from("0--this is a comment\r1"),
vec![
Token::Number("0".to_string(), false),
Token::Whitespace(Whitespace::SingleLineComment {
prefix: "--".to_string(),
comment: "this is a comment\r1".to_string(),
}),
],
),
(
String::from("0--this is a comment\r\n1"),
vec![
Token::Number("0".to_string(), false),
Token::Whitespace(Whitespace::SingleLineComment {
prefix: "--".to_string(),
comment: "this is a comment\r\n".to_string(),
}),
Token::Number("1".to_string(), false),
],
),
];

let dialect = GenericDialect {};

for (sql, expected) in test_cases {
let tokens = Tokenizer::new(&dialect, &sql).tokenize().unwrap();
compare(expected, tokens);
}
}

#[test]
fn tokenize_comment_postgres() {
let sql = String::from("1--\r0");

let dialect = PostgreSqlDialect {};
let tokens = Tokenizer::new(&dialect, &sql).tokenize().unwrap();
let expected = vec![
Token::Number("0".to_string(), false),
Token::Number("1".to_string(), false),
Token::Whitespace(Whitespace::SingleLineComment {
prefix: "--".to_string(),
comment: "this is a comment\n".to_string(),
comment: "\r".to_string(),
}),
Token::Number("1".to_string(), false),
Token::Number("0".to_string(), false),
];
compare(expected, tokens);
}
Expand Down

0 comments on commit 182dc31

Please sign in to comment.