Skip to content

Commit 376889a

Browse files
authored
chore(docs): refine docs (#1326)
1 parent f9ab8dc commit 376889a

File tree

1 file changed

+53
-52
lines changed

1 file changed

+53
-52
lines changed

src/parser/mod.rs

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ mod recursion {
7171
use super::ParserError;
7272

7373
/// Tracks remaining recursion depth. This value is decremented on
74-
/// each call to `try_decrease()`, when it reaches 0 an error will
74+
/// each call to [`RecursionCounter::try_decrease()`], when it reaches 0 an error will
7575
/// be returned.
7676
///
77-
/// Note: Uses an Rc and Cell in order to satisfy the Rust
78-
/// borrow checker so the automatic DepthGuard decrement a
77+
/// Note: Uses an [`std::rc::Rc`] and [`std::cell::Cell`] in order to satisfy the Rust
78+
/// borrow checker so the automatic [`DepthGuard`] decrement a
7979
/// reference to the counter.
8080
pub(crate) struct RecursionCounter {
8181
remaining_depth: Rc<Cell<usize>>,
@@ -92,7 +92,7 @@ mod recursion {
9292

9393
/// Decreases the remaining depth by 1.
9494
///
95-
/// Returns `Err` if the remaining depth falls to 0.
95+
/// Returns [`Err`] if the remaining depth falls to 0.
9696
///
9797
/// Returns a [`DepthGuard`] which will adds 1 to the
9898
/// remaining depth upon drop;
@@ -131,7 +131,7 @@ mod recursion {
131131
/// Implementation [`RecursionCounter`] if std is NOT available (and does not
132132
/// guard against stack overflow).
133133
///
134-
/// Has the same API as the std RecursionCounter implementation
134+
/// Has the same API as the std [`RecursionCounter`] implementation
135135
/// but does not actually limit stack depth.
136136
pub(crate) struct RecursionCounter {}
137137

@@ -270,17 +270,17 @@ enum ParserState {
270270

271271
pub struct Parser<'a> {
272272
tokens: Vec<TokenWithLocation>,
273-
/// The index of the first unprocessed token in `self.tokens`
273+
/// The index of the first unprocessed token in [`Parser::tokens`].
274274
index: usize,
275275
/// The current state of the parser.
276276
state: ParserState,
277-
/// The current dialect to use
277+
/// The current dialect to use.
278278
dialect: &'a dyn Dialect,
279279
/// Additional options that allow you to mix & match behavior
280280
/// otherwise constrained to certain dialects (e.g. trailing
281-
/// commas) and/or format of parse (e.g. unescaping)
281+
/// commas) and/or format of parse (e.g. unescaping).
282282
options: ParserOptions,
283-
/// ensure the stack does not overflow by limiting recursion depth
283+
/// Ensure the stack does not overflow by limiting recursion depth.
284284
recursion_counter: RecursionCounter,
285285
}
286286

@@ -313,7 +313,6 @@ impl<'a> Parser<'a> {
313313

314314
/// Specify the maximum recursion limit while parsing.
315315
///
316-
///
317316
/// [`Parser`] prevents stack overflows by returning
318317
/// [`ParserError::RecursionLimitExceeded`] if the parser exceeds
319318
/// this depth while processing the query.
@@ -338,7 +337,6 @@ impl<'a> Parser<'a> {
338337

339338
/// Specify additional parser options
340339
///
341-
///
342340
/// [`Parser`] supports additional options ([`ParserOptions`])
343341
/// that allow you to mix & match behavior otherwise constrained
344342
/// to certain dialects (e.g. trailing commas).
@@ -824,7 +822,7 @@ impl<'a> Parser<'a> {
824822
})
825823
}
826824

827-
/// Parse a new expression including wildcard & qualified wildcard
825+
/// Parse a new expression including wildcard & qualified wildcard.
828826
pub fn parse_wildcard_expr(&mut self) -> Result<Expr, ParserError> {
829827
let index = self.index;
830828

@@ -867,13 +865,13 @@ impl<'a> Parser<'a> {
867865
self.parse_expr()
868866
}
869867

870-
/// Parse a new expression
868+
/// Parse a new expression.
871869
pub fn parse_expr(&mut self) -> Result<Expr, ParserError> {
872870
let _guard = self.recursion_counter.try_decrease()?;
873871
self.parse_subexpr(0)
874872
}
875873

876-
/// Parse tokens until the precedence changes
874+
/// Parse tokens until the precedence changes.
877875
pub fn parse_subexpr(&mut self, precedence: u8) -> Result<Expr, ParserError> {
878876
debug!("parsing expr");
879877
let mut expr = self.parse_prefix()?;
@@ -908,8 +906,7 @@ impl<'a> Parser<'a> {
908906
Ok(expr)
909907
}
910908

911-
/// Get the precedence of the next token
912-
/// With AND, OR, and XOR
909+
/// Get the precedence of the next token, with AND, OR, and XOR.
913910
pub fn get_next_interval_precedence(&self) -> Result<u8, ParserError> {
914911
let token = self.peek_token();
915912

@@ -944,7 +941,7 @@ impl<'a> Parser<'a> {
944941
Ok(Statement::ReleaseSavepoint { name })
945942
}
946943

947-
/// Parse an expression prefix
944+
/// Parse an expression prefix.
948945
pub fn parse_prefix(&mut self) -> Result<Expr, ParserError> {
949946
// allow the dialect to override prefix parsing
950947
if let Some(prefix) = self.dialect.parse_prefix(self) {
@@ -1456,8 +1453,7 @@ impl<'a> Parser<'a> {
14561453
}
14571454
}
14581455

1459-
/// parse a group by expr. a group by expr can be one of group sets, roll up, cube, or simple
1460-
/// expr.
1456+
/// Parse a group by expr. Group by expr can be one of group sets, roll up, cube, or simple expr.
14611457
fn parse_group_by_expr(&mut self) -> Result<Expr, ParserError> {
14621458
if self.dialect.supports_group_by_expr() {
14631459
if self.parse_keywords(&[Keyword::GROUPING, Keyword::SETS]) {
@@ -1484,7 +1480,7 @@ impl<'a> Parser<'a> {
14841480
}
14851481
}
14861482

1487-
/// parse a tuple with `(` and `)`.
1483+
/// Parse a tuple with `(` and `)`.
14881484
/// If `lift_singleton` is true, then a singleton tuple is lifted to a tuple of length 1, otherwise it will fail.
14891485
/// If `allow_empty` is true, then an empty tuple is allowed.
14901486
fn parse_tuple(
@@ -1953,13 +1949,11 @@ impl<'a> Parser<'a> {
19531949
}
19541950
}
19551951

1956-
/// Parses fulltext expressions [(1)]
1952+
/// Parses fulltext expressions [`sqlparser::ast::Expr::MatchAgainst`]
19571953
///
19581954
/// # Errors
19591955
/// This method will raise an error if the column list is empty or with invalid identifiers,
19601956
/// the match expression is not a literal string, or if the search modifier is not valid.
1961-
///
1962-
/// [(1)]: Expr::MatchAgainst
19631957
pub fn parse_match_against(&mut self) -> Result<Expr, ParserError> {
19641958
let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
19651959

@@ -2004,17 +1998,19 @@ impl<'a> Parser<'a> {
20041998
})
20051999
}
20062000

2007-
/// Parse an INTERVAL expression.
2001+
/// Parse an `INTERVAL` expression.
20082002
///
20092003
/// Some syntactically valid intervals:
20102004
///
2011-
/// 1. `INTERVAL '1' DAY`
2012-
/// 2. `INTERVAL '1-1' YEAR TO MONTH`
2013-
/// 3. `INTERVAL '1' SECOND`
2014-
/// 4. `INTERVAL '1:1:1.1' HOUR (5) TO SECOND (5)`
2015-
/// 5. `INTERVAL '1.1' SECOND (2, 2)`
2016-
/// 6. `INTERVAL '1:1' HOUR (5) TO MINUTE (5)`
2017-
/// 7. (MySql and BigQuey only):`INTERVAL 1 DAY`
2005+
/// ```sql
2006+
/// 1. INTERVAL '1' DAY
2007+
/// 2. INTERVAL '1-1' YEAR TO MONTH
2008+
/// 3. INTERVAL '1' SECOND
2009+
/// 4. INTERVAL '1:1:1.1' HOUR (5) TO SECOND (5)
2010+
/// 5. INTERVAL '1.1' SECOND (2, 2)
2011+
/// 6. INTERVAL '1:1' HOUR (5) TO MINUTE (5)
2012+
/// 7. (MySql & BigQuey only): INTERVAL 1 DAY
2013+
/// ```
20182014
///
20192015
/// Note that we do not currently attempt to parse the quoted value.
20202016
pub fn parse_interval(&mut self) -> Result<Expr, ParserError> {
@@ -2210,15 +2206,15 @@ impl<'a> Parser<'a> {
22102206
))
22112207
}
22122208

2213-
/// Parse a field definition in a struct [1] or tuple [2].
2209+
/// Parse a field definition in a [struct] or [tuple].
22142210
/// Syntax:
22152211
///
22162212
/// ```sql
22172213
/// [field_name] field_type
22182214
/// ```
22192215
///
2220-
/// [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#declaring_a_struct_type
2221-
/// [2]: https://clickhouse.com/docs/en/sql-reference/data-types/tuple
2216+
/// [struct]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#declaring_a_struct_type
2217+
/// [tuple]: https://clickhouse.com/docs/en/sql-reference/data-types/tuple
22222218
fn parse_struct_field_def(
22232219
&mut self,
22242220
) -> Result<(StructField, MatchedTrailingBracket), ParserError> {
@@ -2272,15 +2268,15 @@ impl<'a> Parser<'a> {
22722268
Ok(fields)
22732269
}
22742270

2275-
/// DuckDB specific: Parse a duckdb dictionary [1]
2271+
/// DuckDB specific: Parse a duckdb [dictionary]
22762272
///
22772273
/// Syntax:
22782274
///
22792275
/// ```sql
22802276
/// {'field_name': expr1[, ... ]}
22812277
/// ```
22822278
///
2283-
/// [1]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
2279+
/// [dictionary]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
22842280
fn parse_duckdb_struct_literal(&mut self) -> Result<Expr, ParserError> {
22852281
self.expect_token(&Token::LBrace)?;
22862282

@@ -2291,13 +2287,15 @@ impl<'a> Parser<'a> {
22912287
Ok(Expr::Dictionary(fields))
22922288
}
22932289

2294-
/// Parse a field for a duckdb dictionary [1]
2290+
/// Parse a field for a duckdb [dictionary]
2291+
///
22952292
/// Syntax
2293+
///
22962294
/// ```sql
22972295
/// 'name': expr
22982296
/// ```
22992297
///
2300-
/// [1]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
2298+
/// [dictionary]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
23012299
fn parse_duckdb_dictionary_field(&mut self) -> Result<DictionaryField, ParserError> {
23022300
let key = self.parse_identifier(false)?;
23032301

@@ -2311,13 +2309,15 @@ impl<'a> Parser<'a> {
23112309
})
23122310
}
23132311

2314-
/// Parse clickhouse map [1]
2312+
/// Parse clickhouse [map]
2313+
///
23152314
/// Syntax
2315+
///
23162316
/// ```sql
23172317
/// Map(key_data_type, value_data_type)
23182318
/// ```
23192319
///
2320-
/// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/map
2320+
/// [map]: https://clickhouse.com/docs/en/sql-reference/data-types/map
23212321
fn parse_click_house_map_def(&mut self) -> Result<(DataType, DataType), ParserError> {
23222322
self.expect_keyword(Keyword::MAP)?;
23232323
self.expect_token(&Token::LParen)?;
@@ -2329,13 +2329,15 @@ impl<'a> Parser<'a> {
23292329
Ok((key_data_type, value_data_type))
23302330
}
23312331

2332-
/// Parse clickhouse tuple [1]
2332+
/// Parse clickhouse [tuple]
2333+
///
23332334
/// Syntax
2335+
///
23342336
/// ```sql
23352337
/// Tuple([field_name] field_type, ...)
23362338
/// ```
23372339
///
2338-
/// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/tuple
2340+
/// [tuple]: https://clickhouse.com/docs/en/sql-reference/data-types/tuple
23392341
fn parse_click_house_tuple_def(&mut self) -> Result<Vec<StructField>, ParserError> {
23402342
self.expect_keyword(Keyword::TUPLE)?;
23412343
self.expect_token(&Token::LParen)?;
@@ -2649,7 +2651,7 @@ impl<'a> Parser<'a> {
26492651
}
26502652
}
26512653

2652-
/// parse the ESCAPE CHAR portion of LIKE, ILIKE, and SIMILAR TO
2654+
/// Parse the `ESCAPE CHAR` portion of `LIKE`, `ILIKE`, and `SIMILAR TO`
26532655
pub fn parse_escape_char(&mut self) -> Result<Option<String>, ParserError> {
26542656
if self.parse_keyword(Keyword::ESCAPE) {
26552657
Ok(Some(self.parse_literal_string()?))
@@ -2836,7 +2838,7 @@ impl<'a> Parser<'a> {
28362838
})
28372839
}
28382840

2839-
/// Parses the parens following the `[ NOT ] IN` operator
2841+
/// Parses the parens following the `[ NOT ] IN` operator.
28402842
pub fn parse_in(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
28412843
// BigQuery allows `IN UNNEST(array_expression)`
28422844
// https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#in_operators
@@ -2873,7 +2875,7 @@ impl<'a> Parser<'a> {
28732875
Ok(in_op)
28742876
}
28752877

2876-
/// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed
2878+
/// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed.
28772879
pub fn parse_between(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
28782880
// Stop parsing subexpressions for <low> and <high> on tokens with
28792881
// precedence lower than that of `BETWEEN`, such as `AND`, `IS`, etc.
@@ -2888,7 +2890,7 @@ impl<'a> Parser<'a> {
28882890
})
28892891
}
28902892

2891-
/// Parse a postgresql casting style which is in the form of `expr::datatype`
2893+
/// Parse a postgresql casting style which is in the form of `expr::datatype`.
28922894
pub fn parse_pg_cast(&mut self, expr: Expr) -> Result<Expr, ParserError> {
28932895
Ok(Expr::Cast {
28942896
kind: CastKind::DoubleColon,
@@ -2898,7 +2900,7 @@ impl<'a> Parser<'a> {
28982900
})
28992901
}
29002902

2901-
// use https://www.postgresql.org/docs/7.0/operators.htm#AEN2026 as a reference
2903+
// Use https://www.postgresql.org/docs/7.0/operators.htm#AEN2026 as a reference
29022904
// higher number = higher precedence
29032905
//
29042906
// NOTE: The pg documentation is incomplete, e.g. the AT TIME ZONE operator
@@ -3217,7 +3219,7 @@ impl<'a> Parser<'a> {
32173219

32183220
/// If the current token is one of the given `keywords`, consume the token
32193221
/// and return the keyword that matches. Otherwise, no tokens are consumed
3220-
/// and returns `None`.
3222+
/// and returns [`None`].
32213223
#[must_use]
32223224
pub fn parse_one_of_keywords(&mut self, keywords: &[Keyword]) -> Option<Keyword> {
32233225
match self.peek_token().token {
@@ -3393,8 +3395,7 @@ impl<'a> Parser<'a> {
33933395
self.parse_comma_separated(f)
33943396
}
33953397

3396-
/// Run a parser method `f`, reverting back to the current position
3397-
/// if unsuccessful.
3398+
/// Run a parser method `f`, reverting back to the current position if unsuccessful.
33983399
#[must_use]
33993400
fn maybe_parse<T, F>(&mut self, mut f: F) -> Option<T>
34003401
where
@@ -3409,8 +3410,8 @@ impl<'a> Parser<'a> {
34093410
}
34103411
}
34113412

3412-
/// Parse either `ALL`, `DISTINCT` or `DISTINCT ON (...)`. Returns `None` if `ALL` is parsed
3413-
/// and results in a `ParserError` if both `ALL` and `DISTINCT` are found.
3413+
/// Parse either `ALL`, `DISTINCT` or `DISTINCT ON (...)`. Returns [`None`] if `ALL` is parsed
3414+
/// and results in a [`ParserError`] if both `ALL` and `DISTINCT` are found.
34143415
pub fn parse_all_or_distinct(&mut self) -> Result<Option<Distinct>, ParserError> {
34153416
let loc = self.peek_token().location;
34163417
let all = self.parse_keyword(Keyword::ALL);

0 commit comments

Comments
 (0)