xitep commented on code in PR #2107: URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2107#discussion_r2616274011
########## src/ast/comments.rs: ########## @@ -0,0 +1,280 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Provides a representation of source code comments in parsed SQL code. + +#[cfg(not(feature = "std"))] +use alloc::{string::String, vec::Vec}; + +use core::{ + ops::{Bound, Deref, RangeBounds}, + slice, +}; + +use crate::tokenizer::{Location, Span}; + +/// An opaque container for comments from a parse SQL source code. +#[derive(Default, Debug)] +pub struct Comments(Vec<CommentWithSpan>); + +impl Comments { + pub(crate) fn push(&mut self, comment: CommentWithSpan) { + debug_assert!(self + .0 + .last() + .map(|last| last.span < comment.span) + .unwrap_or(true)); + self.0.push(comment); + } + + /// Finds comments starting within the given location range. The order of + /// iterator reflects the order of the comments as encountered in the parsed + /// source code. + pub fn find<R: RangeBounds<Location>>(&self, range: R) -> Iter<'_> { + let (start, end) = ( + self.start_index(range.start_bound()), + self.end_index(range.end_bound()), + ); + // ~ in case the user specified a rever range + Iter(if start <= end { Review Comment: actually, it would be a programming error; the idea was that `start_index` and `end_index` return something in the range of `0..=self.0.len()`. i'll add appropriate debug assertions and document the helper methods. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
