Skip to content

Commit d89cf80

Browse files
authored
Improve Parser documentation (#1617)
1 parent df3c565 commit d89cf80

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

src/parser/mod.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -276,19 +276,58 @@ enum ParserState {
276276
ConnectBy,
277277
}
278278

279+
/// A SQL Parser
280+
///
281+
/// This struct is the main entry point for parsing SQL queries.
282+
///
283+
/// # Functionality:
284+
/// * Parsing SQL: see examples on [`Parser::new`] and [`Parser::parse_sql`]
285+
/// * Controlling recursion: See [`Parser::with_recursion_limit`]
286+
/// * Controlling parser options: See [`Parser::with_options`]
287+
/// * Providing your own tokens: See [`Parser::with_tokens`]
288+
///
289+
/// # Internals
290+
///
291+
/// The parser uses a [`Tokenizer`] to tokenize the input SQL string into a
292+
/// `Vec` of [`TokenWithSpan`]s and maintains an `index` to the current token
293+
/// being processed. The token vec may contain multiple SQL statements.
294+
///
295+
/// * The "current" token is the token at `index - 1`
296+
/// * The "next" token is the token at `index`
297+
/// * The "previous" token is the token at `index - 2`
298+
///
299+
/// If `index` is equal to the length of the token stream, the 'next' token is
300+
/// [`Token::EOF`].
301+
///
302+
/// For example, the SQL string "SELECT * FROM foo" will be tokenized into
303+
/// following tokens:
304+
/// ```text
305+
/// [
306+
/// "SELECT", // token index 0
307+
/// " ", // whitespace
308+
/// "*",
309+
/// " ",
310+
/// "FROM",
311+
/// " ",
312+
/// "foo"
313+
/// ]
314+
/// ```
315+
///
316+
///
279317
pub struct Parser<'a> {
318+
/// The tokens
280319
tokens: Vec<TokenWithSpan>,
281320
/// The index of the first unprocessed token in [`Parser::tokens`].
282321
index: usize,
283322
/// The current state of the parser.
284323
state: ParserState,
285-
/// The current dialect to use.
324+
/// The SQL dialect to use.
286325
dialect: &'a dyn Dialect,
287326
/// Additional options that allow you to mix & match behavior
288327
/// otherwise constrained to certain dialects (e.g. trailing
289328
/// commas) and/or format of parse (e.g. unescaping).
290329
options: ParserOptions,
291-
/// Ensure the stack does not overflow by limiting recursion depth.
330+
/// Ensures the stack does not overflow by limiting recursion depth.
292331
recursion_counter: RecursionCounter,
293332
}
294333

0 commit comments

Comments
 (0)