@@ -422,13 +422,35 @@ impl fmt::Display for Whitespace {
422
422
}
423
423
424
424
/// 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
+ /// ```
425
443
#[ derive( Eq , PartialEq , Hash , Clone , Copy , Ord , PartialOrd ) ]
426
444
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
427
445
#[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
428
446
pub struct Location {
429
- /// Line number, starting from 1
447
+ /// Line number, starting from 1.
448
+ ///
449
+ /// Line 0 is used for empty spans
430
450
pub line : u64 ,
431
- /// Line column, starting from 1
451
+ /// Line column, starting from 1.
452
+ ///
453
+ /// Column 0 is used for empty spans
432
454
pub column : u64 ,
433
455
}
434
456
@@ -448,10 +470,24 @@ impl fmt::Debug for Location {
448
470
}
449
471
450
472
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 {
452
480
Self { line, column }
453
481
}
454
482
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`
455
491
pub fn span_to ( self , end : Self ) -> Span {
456
492
Span { start : self , end }
457
493
}
0 commit comments