Skip to content

Commit

Permalink
Merge pull request apache#7 from zhyass/main
Browse files Browse the repository at this point in the history
Process escape character in quoted string
  • Loading branch information
sundy-li authored Nov 7, 2021
2 parents 699fddd + 42bf210 commit eca5b86
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,14 @@ impl fmt::Display for WindowSpec {
write!(f, "ORDER BY {}", display_comma_separated(&self.order_by))?;
}
if let Some(window_frame) = &self.window_frame {
f.write_str(delim)?;
if let Some(end_bound) = &window_frame.end_bound {
f.write_str(delim)?;
write!(
f,
"{} BETWEEN {} AND {}",
window_frame.units, window_frame.start_bound, end_bound
)?;
} else {
f.write_str(delim)?;
write!(f, "{} {}", window_frame.units, window_frame.start_bound)?;
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1620,9 +1620,10 @@ impl<'a> Parser<'a> {
loop {
if let Some(constraint) = self.parse_optional_table_constraint()? {
constraints.push(constraint);
} else if let Token::Word(_) = self.peek_token() {
columns.push(self.parse_column_def()?);
} else if let Token::BackQuotedString(_) = self.peek_token() {
} else if matches!(
self.peek_token(),
Token::Word(_) | Token::BackQuotedString(_)
) {
columns.push(self.parse_column_def()?);
} else {
return self.expected("column name or constraint definition", self.peek_token());
Expand Down Expand Up @@ -2793,10 +2794,10 @@ impl<'a> Parser<'a> {
// followed by some joins or (B) another level of nesting.
let mut table_and_joins = self.parse_table_and_joins()?;

if !table_and_joins.joins.is_empty() {
self.expect_token(&Token::RParen)?;
Ok(TableFactor::NestedJoin(Box::new(table_and_joins))) // (A)
} else if let TableFactor::NestedJoin(_) = &table_and_joins.relation {
if !table_and_joins.joins.is_empty()
|| matches!(&table_and_joins.relation, TableFactor::NestedJoin(_))
{
// (A)
// (B): `table_and_joins` (what we found inside the parentheses)
// is a nested join `(foo JOIN bar)`, not followed by other joins.
self.expect_token(&Token::RParen)?;
Expand Down
22 changes: 19 additions & 3 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,9 @@ impl<'a> Tokenizer<'a> {
) -> Result<String, TokenizerError> {
let mut s = String::new();
chars.next(); // consume the opening quote
while let Some(&ch) = chars.peek() {
while let Some(ch) = chars.next() {
match ch {
'\'' => {
chars.next(); // consume
let escaped_quote = chars.peek().map(|c| *c == '\'').unwrap_or(false);
if escaped_quote {
s.push('\'');
Expand All @@ -655,8 +654,25 @@ impl<'a> Tokenizer<'a> {
return Ok(s);
}
}
'\\' => {
if let Some(c) = chars.next() {
match c {
'n' => s.push('\n'),
't' => s.push('\t'),
'r' => s.push('\r'),
'b' => s.push('\u{08}'),
'0' => s.push('\0'),
'\'' => s.push('\''),
'\\' => s.push('\\'),
'\"' => s.push('\"'),
_ => {
s.push('\\');
s.push(c);
}
}
}
}
_ => {
chars.next(); // consume
s.push(ch);
}
}
Expand Down

0 comments on commit eca5b86

Please sign in to comment.