Repository navigation
SQLite: parse the full PRAGMA value grammar #2469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3fdd2c8
946c350
320a5e2
1c3057d
ef53be5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| PRAGMA user_version = 0x10 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| PRAGMA case_sensitive_like = true; | ||
| PRAGMA case_sensitive_like = oN; | ||
| PRAGMA journal_mode = WAL; | ||
| PRAGMA cache_size = -2000; | ||
| PRAGMA cache_size = +2000; | ||
| PRAGMA optimize = 0x10002; | ||
| PRAGMA wal_checkpoint(TRUNCATE); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1737,6 +1737,7 @@ impl<'a> Parser<'a> { | |
| Token::SingleQuotedString(_) | ||
| | Token::DoubleQuotedString(_) | ||
| | Token::HexStringLiteral(_) | ||
| | Token::HexNumber(_) | ||
| if w.value.starts_with('_') => | ||
| { | ||
| Ok(Expr::Prefixed { | ||
|
|
@@ -2060,7 +2061,8 @@ impl<'a> Parser<'a> { | |
| | Token::NationalStringLiteral(_) | ||
| | Token::QuoteDelimitedStringLiteral(_) | ||
| | Token::NationalQuoteDelimitedStringLiteral(_) | ||
| | Token::HexStringLiteral(_) => { | ||
| | Token::HexStringLiteral(_) | ||
| | Token::HexNumber(_) => { | ||
| self.prev_token(); | ||
| Ok(Expr::Value(self.parse_value()?)) | ||
| } | ||
|
|
@@ -3281,7 +3283,8 @@ impl<'a> Parser<'a> { | |
| | Token::NationalStringLiteral(_) | ||
| | Token::QuoteDelimitedStringLiteral(_) | ||
| | Token::NationalQuoteDelimitedStringLiteral(_) | ||
| | Token::HexStringLiteral(_) => Some(Box::new(self.parse_expr()?)), | ||
| | Token::HexStringLiteral(_) | ||
| | Token::HexNumber(_) => Some(Box::new(self.parse_expr()?)), | ||
| _ => self.expected_ref( | ||
| "either filler, WITH, or WITHOUT in LISTAGG", | ||
| self.peek_token_ref(), | ||
|
|
@@ -13071,6 +13074,7 @@ impl<'a> Parser<'a> { | |
| ok_value(Value::UnicodeStringLiteral(s.to_string())) | ||
| } | ||
| Token::HexStringLiteral(ref s) => ok_value(Value::HexStringLiteral(s.to_string())), | ||
| Token::HexNumber(s) => ok_value(Value::HexNumber(s)), | ||
| Token::Placeholder(ref s) => ok_value(Value::Placeholder(s.to_string())), | ||
| tok @ Token::Colon | tok @ Token::AtSign => { | ||
| // 1. Not calling self.parse_identifier(false)? | ||
|
|
@@ -13184,6 +13188,7 @@ impl<'a> Parser<'a> { | |
| Token::HexStringLiteral(ref s) => Ok(Expr::Value( | ||
| Value::HexStringLiteral(s.to_string()).with_span(span), | ||
| )), | ||
| Token::HexNumber(s) => Ok(Expr::Value(Value::HexNumber(s).with_span(span))), | ||
| unexpected => self.expected( | ||
| "a string value", | ||
| TokenWithSpan { | ||
|
|
@@ -21043,44 +21048,53 @@ impl<'a> Parser<'a> { | |
| }) | ||
| } | ||
|
|
||
| fn parse_pragma_value(&mut self) -> Result<ValueWithSpan, ParserError> { | ||
| let v = self.parse_value()?; | ||
| match &v.value { | ||
| Value::SingleQuotedString(_) => Ok(v), | ||
| Value::DoubleQuotedString(_) => Ok(v), | ||
| Value::Number(_, _) => Ok(v), | ||
| Value::Placeholder(_) => Ok(v), | ||
| _ => { | ||
| self.prev_token(); | ||
| self.expected_ref("number or string or ? placeholder", self.peek_token_ref()) | ||
| /// Parse a SQLite `pragma-value`: `signed-number | name | signed-literal`. | ||
| fn parse_pragma_value(&mut self) -> Result<Expr, ParserError> { | ||
| if matches!(self.peek_token_ref().token, Token::Plus | Token::Minus) { | ||
| let op = match self.next_token().token { | ||
| Token::Plus => UnaryOperator::Plus, | ||
| _ => UnaryOperator::Minus, | ||
| }; | ||
| // SQLite only permits a sign before a number | ||
| if !matches!( | ||
| self.peek_token_ref().token, | ||
| Token::Number(..) | Token::HexNumber(_) | ||
| ) { | ||
| return self.expected_ref("number", self.peek_token_ref()); | ||
| } | ||
| return Ok(Expr::UnaryOp { | ||
| op, | ||
| expr: Box::new(Expr::Value(self.parse_value()?)), | ||
| }); | ||
| } | ||
| if matches!( | ||
| self.peek_token_ref().token, | ||
| Token::Word(Word { | ||
| keyword: Keyword::NULL, | ||
| .. | ||
| }) | Token::HexStringLiteral(_) | ||
| ) { | ||
| return self.expected_ref("number, name or string", self.peek_token_ref()); | ||
| } | ||
| match self.try_parse(|parser| parser.parse_value()) { | ||
| Ok(value) => Ok(Expr::Value(value)), | ||
| Err(_) => Ok(Expr::Identifier(self.parse_identifier()?)), | ||
| } | ||
|
Comment on lines
+21053
to
21082
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm can this be replaced by a call to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In terms of accepting things, yes it would work, but SQLite defines a pragma value as I understand I tend to lean much more strongly on the strict parser approach, but here I really believe allowing any expr would be excessively loose. |
||
| } | ||
|
|
||
| /// PRAGMA [schema-name '.'] pragma-name [('=' pragma-value) | '(' pragma-value ')'] | ||
| pub fn parse_pragma(&mut self) -> Result<Statement, ParserError> { | ||
| let name = self.parse_object_name(false)?; | ||
| if self.consume_token(&Token::LParen) { | ||
| let (value, is_eq) = if self.consume_token(&Token::LParen) { | ||
| let value = self.parse_pragma_value()?; | ||
| self.expect_token(&Token::RParen)?; | ||
| Ok(Statement::Pragma { | ||
| name, | ||
| value: Some(value), | ||
| is_eq: false, | ||
| }) | ||
| (Some(value), false) | ||
| } else if self.consume_token(&Token::Eq) { | ||
| Ok(Statement::Pragma { | ||
| name, | ||
| value: Some(self.parse_pragma_value()?), | ||
| is_eq: true, | ||
| }) | ||
| (Some(self.parse_pragma_value()?), true) | ||
| } else { | ||
| Ok(Statement::Pragma { | ||
| name, | ||
| value: None, | ||
| is_eq: false, | ||
| }) | ||
| } | ||
| (None, false) | ||
| }; | ||
| Ok(Statement::Pragma { name, value, is_eq }) | ||
| } | ||
|
|
||
| /// `INSTALL [extension_name]` | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be smt like this?
Current approach falls back to parse_identifier on any error, so real parse_value errors are hidden.