Skip to content

Commit b6f083b

Browse files
committed
Gate things on feature=parser all over the library
Caveats: - `dialect` is completely disabled which is not the final goal. - `parser/mod.rs` and `tokenizer/mod.rs` should be split so that the number of annotations is greatly reduced and applied at the file level instead of struct/function level.
1 parent bc1e5f2 commit b6f083b

File tree

6 files changed

+68
-8
lines changed

6 files changed

+68
-8
lines changed

Diff for: Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,11 @@ pretty_assertions = "1"
6565
[package.metadata.docs.rs]
6666
# Document these features on docs.rs
6767
features = ["parser", "serde", "visitor"]
68+
69+
[[example]]
70+
name = "cli"
71+
required-features = ["parser"]
72+
73+
[[example]]
74+
name = "parse_select"
75+
required-features = ["parser"]

Diff for: src/ast/helpers/stmt_create_table.rs

+1
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
588588

589589
/// Helper return type when parsing configuration for a `CREATE TABLE` statement.
590590
#[derive(Default)]
591+
#[cfg(feature = "parser")]
591592
pub(crate) struct CreateTableConfiguration {
592593
pub partition_by: Option<Box<Expr>>,
593594
pub cluster_by: Option<WrappedCollection<Vec<Ident>>>,

Diff for: src/ast/spans.rs

+7
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ fn union_spans<I: Iterator<Item = Span>>(iter: I) -> Span {
5959
///
6060
/// # Example
6161
/// ```
62+
/// # #[cfg(feature = "parser")]
6263
/// # use sqlparser::parser::{Parser, ParserError};
6364
/// # use sqlparser::ast::Spanned;
65+
/// # #[cfg(feature = "parser")]
6466
/// # use sqlparser::dialect::GenericDialect;
6567
/// # use sqlparser::tokenizer::Location;
68+
/// # #[cfg(feature = "parser")]
6669
/// # fn main() -> Result<(), ParserError> {
6770
/// let dialect = GenericDialect {};
6871
/// let sql = r#"SELECT *
@@ -78,6 +81,9 @@ fn union_spans<I: Iterator<Item = Span>>(iter: I) -> Span {
7881
/// assert_eq!(span.end, Location::new(2, 15));
7982
/// # Ok(())
8083
/// # }
84+
/// #
85+
/// # #[cfg(not(feature = "parser"))]
86+
/// # fn main() {}
8187
/// ```
8288
///
8389
pub trait Spanned {
@@ -2167,6 +2173,7 @@ impl Spanned for TableObject {
21672173
}
21682174

21692175
#[cfg(test)]
2176+
#[cfg(feature = "parser")]
21702177
pub mod tests {
21712178
use crate::dialect::{Dialect, GenericDialect, SnowflakeDialect};
21722179
use crate::parser::Parser;

Diff for: src/lib.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,23 @@
3232
//! # Example parsing SQL text
3333
//!
3434
//! ```
35+
//! # #[cfg(feature = "parser")]
3536
//! use sqlparser::dialect::GenericDialect;
37+
//! # #[cfg(feature = "parser")]
3638
//! use sqlparser::parser::Parser;
3739
//!
40+
//! # #[cfg(feature = "parser")]
3841
//! let dialect = GenericDialect {}; // or AnsiDialect
3942
//!
4043
//! let sql = "SELECT a, b, 123, myfunc(b) \
4144
//! FROM table_1 \
4245
//! WHERE a > b AND b < 100 \
4346
//! ORDER BY a DESC, b";
4447
//!
48+
//! # #[cfg(feature = "parser")]
4549
//! let ast = Parser::parse_sql(&dialect, sql).unwrap();
4650
//!
51+
//! # #[cfg(feature = "parser")]
4752
//! println!("AST: {:?}", ast);
4853
//! ```
4954
//!
@@ -54,13 +59,17 @@
5459
//! useful for tools that analyze and manipulate SQL.
5560
//!
5661
//! ```
62+
//! # #[cfg(feature = "parser")]
5763
//! # use sqlparser::dialect::GenericDialect;
64+
//! # #[cfg(feature = "parser")]
5865
//! # use sqlparser::parser::Parser;
5966
//! let sql = "SELECT a FROM table_1";
6067
//!
68+
//! # #[cfg(feature = "parser")]
6169
//! // parse to a Vec<Statement>
6270
//! let ast = Parser::parse_sql(&GenericDialect, sql).unwrap();
6371
//!
72+
//! # #[cfg(feature = "parser")]
6473
//! // The original SQL text can be generated from the AST
6574
//! assert_eq!(ast[0].to_string(), sql);
6675
//! ```
@@ -141,13 +150,17 @@ extern crate pretty_assertions;
141150

142151
pub mod ast;
143152
#[macro_use]
153+
#[cfg(feature = "parser")]
144154
pub mod dialect;
145155
pub mod keywords;
146156
pub mod parser;
147157
pub mod tokenizer;
148158

149-
#[doc(hidden)]
150159
// This is required to make utilities accessible by both the crate-internal
151160
// unit-tests and by the integration tests <https://stackoverflow.com/a/44541071/1026>
152-
// External users are not supposed to rely on this module.
161+
// External users are not supposed to rely on these modules.
162+
#[doc(hidden)]
163+
#[cfg(feature = "parser")]
164+
pub mod test_dialect_utils;
165+
#[doc(hidden)]
153166
pub mod test_utils;

Diff for: src/parser/mod.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,36 @@ use alloc::{
2020
vec,
2121
vec::Vec,
2222
};
23+
#[allow(unused_imports)]
2324
use core::{
2425
fmt::{self, Display},
2526
str::FromStr,
2627
};
28+
#[allow(unused_imports)]
2729
use helpers::attached_token::AttachedToken;
2830

31+
#[allow(unused_imports)]
2932
use log::debug;
3033

34+
#[cfg(feature = "parser")]
3135
use recursion::RecursionCounter;
36+
#[allow(unused_imports)]
3237
use IsLateral::*;
38+
#[allow(unused_imports)]
3339
use IsOptional::*;
3440

41+
#[cfg(feature = "parser")]
3542
use crate::ast::helpers::stmt_create_table::{CreateTableBuilder, CreateTableConfiguration};
43+
#[allow(unused_imports)]
3644
use crate::ast::Statement::CreatePolicy;
3745
use crate::ast::*;
46+
#[cfg(feature = "parser")]
3847
use crate::dialect::*;
48+
#[allow(unused_imports)]
3949
use crate::keywords::{Keyword, ALL_KEYWORDS};
4050
use crate::tokenizer::*;
4151

52+
#[cfg(feature = "parser")]
4253
mod alter;
4354

4455
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -49,13 +60,15 @@ pub enum ParserError {
4960
}
5061

5162
// Use `Parser::expected` instead, if possible
63+
#[allow(unused_macros)]
5264
macro_rules! parser_err {
5365
($MSG:expr, $loc:expr) => {
5466
Err(ParserError::ParserError(format!("{}{}", $MSG, $loc)))
5567
};
5668
}
5769

5870
#[cfg(feature = "std")]
71+
#[cfg(feature = "parser")]
5972
/// Implementation [`RecursionCounter`] if std is available
6073
mod recursion {
6174
use std::cell::Cell;
@@ -184,9 +197,11 @@ impl fmt::Display for ParserError {
184197
impl std::error::Error for ParserError {}
185198

186199
// By default, allow expressions up to this deep before erroring
200+
#[allow(unused)]
187201
const DEFAULT_REMAINING_DEPTH: usize = 50;
188202

189203
// A constant EOF token that can be referenced.
204+
#[allow(unused)]
190205
const EOF_TOKEN: TokenWithSpan = TokenWithSpan {
191206
token: Token::EOF,
192207
span: Span {
@@ -207,8 +222,10 @@ const EOF_TOKEN: TokenWithSpan = TokenWithSpan {
207222
/// child type.
208223
///
209224
/// See [Parser::parse_data_type] for details
225+
#[cfg(feature = "parser")]
210226
struct MatchedTrailingBracket(bool);
211227

228+
#[cfg(feature = "parser")]
212229
impl From<bool> for MatchedTrailingBracket {
213230
fn from(value: bool) -> Self {
214231
Self(value)
@@ -264,6 +281,7 @@ impl ParserOptions {
264281
}
265282

266283
#[derive(Copy, Clone)]
284+
#[allow(unused)]
267285
enum ParserState {
268286
/// The default state of the parser.
269287
Normal,
@@ -309,8 +327,7 @@ enum ParserState {
309327
/// "foo"
310328
/// ]
311329
/// ```
312-
///
313-
///
330+
#[cfg(feature = "parser")]
314331
pub struct Parser<'a> {
315332
/// The tokens
316333
tokens: Vec<TokenWithSpan>,
@@ -328,6 +345,7 @@ pub struct Parser<'a> {
328345
recursion_counter: RecursionCounter,
329346
}
330347

348+
#[cfg(feature = "parser")]
331349
impl<'a> Parser<'a> {
332350
/// Create a parser for a [`Dialect`]
333351
///

Diff for: src/tokenizer/mod.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ use serde::{Deserialize, Serialize};
4040
#[cfg(feature = "visitor")]
4141
use sqlparser_derive::{Visit, VisitMut};
4242

43-
use crate::dialect::Dialect;
43+
#[cfg(feature = "parser")]
4444
use crate::dialect::{
45-
BigQueryDialect, DuckDbDialect, GenericDialect, MySqlDialect, PostgreSqlDialect,
46-
SnowflakeDialect,
45+
BigQueryDialect, Dialect, DuckDbDialect, GenericDialect, HiveDialect, MySqlDialect,
46+
PostgreSqlDialect, SnowflakeDialect,
4747
};
4848
use crate::keywords::{Keyword, ALL_KEYWORDS, ALL_KEYWORDS_INDEX};
49-
use crate::{ast::DollarQuotedString, dialect::HiveDialect};
49+
use crate::ast::DollarQuotedString;
5050

5151
/// SQL Token enumeration
5252
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
@@ -690,12 +690,14 @@ impl fmt::Display for TokenizerError {
690690
#[cfg(feature = "std")]
691691
impl std::error::Error for TokenizerError {}
692692

693+
#[allow(unused)]
693694
struct State<'a> {
694695
peekable: Peekable<Chars<'a>>,
695696
pub line: u64,
696697
pub col: u64,
697698
}
698699

700+
#[allow(unused)]
699701
impl State<'_> {
700702
/// return the next character and advance the stream
701703
pub fn next(&mut self) -> Option<char> {
@@ -727,6 +729,7 @@ impl State<'_> {
727729
}
728730

729731
/// Represents how many quote characters enclose a string literal.
732+
#[allow(unused)]
730733
#[derive(Copy, Clone)]
731734
enum NumStringQuoteChars {
732735
/// e.g. `"abc"`, `'abc'`, `r'abc'`
@@ -736,6 +739,7 @@ enum NumStringQuoteChars {
736739
}
737740

738741
/// Settings for tokenizing a quoted string literal.
742+
#[allow(unused)]
739743
struct TokenizeQuotedStringSettings {
740744
/// The character used to quote the string.
741745
quote_style: char,
@@ -753,6 +757,7 @@ struct TokenizeQuotedStringSettings {
753757
}
754758

755759
/// SQL Tokenizer
760+
#[cfg(feature = "parser")]
756761
pub struct Tokenizer<'a> {
757762
dialect: &'a dyn Dialect,
758763
query: &'a str,
@@ -761,6 +766,7 @@ pub struct Tokenizer<'a> {
761766
unescape: bool,
762767
}
763768

769+
#[cfg(feature = "parser")]
764770
impl<'a> Tokenizer<'a> {
765771
/// Create a new SQL tokenizer for the specified SQL statement
766772
///
@@ -1948,6 +1954,7 @@ impl<'a> Tokenizer<'a> {
19481954
/// Read from `chars` until `predicate` returns `false` or EOF is hit.
19491955
/// Return the characters read as String, and keep the first non-matching
19501956
/// char available as `chars.next()`.
1957+
#[cfg(feature = "parser")]
19511958
fn peeking_take_while(chars: &mut State, mut predicate: impl FnMut(char) -> bool) -> String {
19521959
let mut s = String::new();
19531960
while let Some(&ch) = chars.peek() {
@@ -1962,6 +1969,7 @@ fn peeking_take_while(chars: &mut State, mut predicate: impl FnMut(char) -> bool
19621969
}
19631970

19641971
/// Same as peeking_take_while, but also passes the next character to the predicate.
1972+
#[cfg(feature = "parser")]
19651973
fn peeking_next_take_while(
19661974
chars: &mut State,
19671975
mut predicate: impl FnMut(char, Option<char>) -> bool,
@@ -1979,14 +1987,17 @@ fn peeking_next_take_while(
19791987
s
19801988
}
19811989

1990+
#[cfg(feature = "parser")]
19821991
fn unescape_single_quoted_string(chars: &mut State<'_>) -> Option<String> {
19831992
Unescape::new(chars).unescape()
19841993
}
19851994

1995+
#[cfg(feature = "parser")]
19861996
struct Unescape<'a: 'b, 'b> {
19871997
chars: &'b mut State<'a>,
19881998
}
19891999

2000+
#[cfg(feature = "parser")]
19902001
impl<'a: 'b, 'b> Unescape<'a, 'b> {
19912002
fn new(chars: &'b mut State<'a>) -> Self {
19922003
Self { chars }
@@ -2127,6 +2138,7 @@ impl<'a: 'b, 'b> Unescape<'a, 'b> {
21272138
}
21282139
}
21292140

2141+
#[cfg(feature = "parser")]
21302142
fn unescape_unicode_single_quoted_string(chars: &mut State<'_>) -> Result<String, TokenizerError> {
21312143
let mut unescaped = String::new();
21322144
chars.next(); // consume the opening quote
@@ -2162,6 +2174,7 @@ fn unescape_unicode_single_quoted_string(chars: &mut State<'_>) -> Result<String
21622174
})
21632175
}
21642176

2177+
#[cfg(feature = "parser")]
21652178
fn take_char_from_hex_digits(
21662179
chars: &mut State<'_>,
21672180
max_digits: usize,

0 commit comments

Comments
 (0)