Skip to content

Commit 03b7486

Browse files
committed
Add some more documentation
1 parent 779e807 commit 03b7486

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/ast/spans.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,25 @@ fn union_spans<I: Iterator<Item = Span>>(iter: I) -> Span {
3737
/// on which nodes are missing spans.
3838
///
3939
/// [this ticket]: https://github.com/apache/datafusion-sqlparser-rs/issues/1548
40+
///
41+
/// # Example
42+
/// ```
43+
/// # use sqlparser::parser::{Parser, ParserError};
44+
/// # use sqlparser::ast::Spanned;
45+
/// # use sqlparser::dialect::GenericDialect;
46+
/// # fn main() -> Result<(), ParserError> {
47+
/// let dialect = GenericDialect {};
48+
/// let statements = Parser::new(&dialect)
49+
/// .try_with_sql("SELECT * FROM table_1")?
50+
/// .parse_statements()?;
51+
/// let span = statements[0].span();
52+
/// assert!(span.
53+
/// # Ok(())
54+
/// # }
55+
/// ```
56+
///
4057
pub trait Spanned {
41-
/// Return the the source [`Span`] for this AST node, by recursively
58+
/// Return the source [`Span`] for this AST node, by recursively
4259
/// combining the spans of its children.
4360
fn span(&self) -> Span;
4461
}

src/tokenizer.rs

+39-3
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,35 @@ impl fmt::Display for Whitespace {
422422
}
423423

424424
/// Location in input string
425+
///
426+
/// # Create an "empty" (unknown) `Location`
427+
/// ```
428+
/// # use sqlparser::tokenizer::Location;
429+
/// let location = Location::empty();
430+
/// ```
431+
///
432+
/// # Create a `Location` from a line and column
433+
/// ```
434+
/// # use sqlparser::tokenizer::Location;
435+
/// let location = Location::new(1, 1);
436+
/// ```
437+
///
438+
/// # Create a `Location` from a pair
439+
/// ```
440+
/// # use sqlparser::tokenizer::Location;
441+
/// let location = Location::from((1, 1));
442+
/// ```
425443
#[derive(Eq, PartialEq, Hash, Clone, Copy, Ord, PartialOrd)]
426444
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
427445
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
428446
pub struct Location {
429-
/// Line number, starting from 1
447+
/// Line number, starting from 1.
448+
///
449+
/// Line 0 is used for empty spans
430450
pub line: u64,
431-
/// Line column, starting from 1
451+
/// Line column, starting from 1.
452+
///
453+
/// Column 0 is used for empty spans
432454
pub column: u64,
433455
}
434456

@@ -448,10 +470,24 @@ impl fmt::Debug for Location {
448470
}
449471

450472
impl Location {
451-
pub fn of(line: u64, column: u64) -> Self {
473+
/// Return an "empty" / unknown location
474+
pub fn empty() -> Self {
475+
Self { line: 0, column: 0 }
476+
}
477+
478+
/// Create a new `Location` for a given line and column
479+
pub fn new(line: u64, column: u64) -> Self {
452480
Self { line, column }
453481
}
454482

483+
/// Create a new location for a given line and column
484+
///
485+
// TODO: remove / deprecate in favor of` `new` for consistency?
486+
pub fn of(line: u64, column: u64) -> Self {
487+
Self::new(line, column)
488+
}
489+
490+
/// Combine self and `end` into a new `Span`
455491
pub fn span_to(self, end: Self) -> Span {
456492
Span { start: self, end }
457493
}

0 commit comments

Comments
 (0)