Skip to content

Commit 07e0d7b

Browse files
committed
removed os-flags and now split on lines. Fixes #14
1 parent 635eb6f commit 07e0d7b

4 files changed

Lines changed: 100 additions & 126 deletions

File tree

src/comments.rs

Lines changed: 84 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -244,87 +244,86 @@ impl Comments {
244244
let mut start_line = 1u64;
245245
let mut start_col = 1u64;
246246

247-
let mut line = 1u64;
247+
let mut line_num = 1u64;
248248
let mut col = 1u64;
249249

250250
let mut in_single = false;
251251
let mut in_multi = false;
252252

253253
let mut buf = String::new();
254254

255-
let mut chars = src.chars().peekable();
256-
257-
while let Some(c) = chars.next() {
258-
match (in_single, in_multi, c) {
259-
(false, false, '-') => {
260-
if chars.peek().copied() == Some('-') {
261-
chars.next();
262-
in_single = true;
263-
start_line = line;
264-
start_col = col;
265-
buf.clear();
266-
col += 1;
255+
for line in src.lines() {
256+
col = 1;
257+
let mut chars = line.chars().peekable();
258+
while let Some(c) = chars.next() {
259+
match (in_single, in_multi, c) {
260+
(false, false, '-') => {
261+
if chars.peek().copied() == Some('-') {
262+
chars.next();
263+
in_single = true;
264+
start_line = line_num;
265+
start_col = col;
266+
buf.clear();
267+
col += 1;
268+
}
267269
}
268-
}
269-
(false, false, '/') => {
270-
if chars.peek().copied() == Some('*') {
271-
chars.next();
272-
in_multi = true;
273-
start_line = line;
274-
start_col = col;
275-
buf.clear();
276-
col += 1;
270+
(false, false, '/') => {
271+
if chars.peek().copied() == Some('*') {
272+
chars.next();
273+
in_multi = true;
274+
start_line = line_num;
275+
start_col = col;
276+
buf.clear();
277+
col += 1;
278+
}
277279
}
278-
}
279-
(false, false, '*') => {
280-
if chars.peek().copied() == Some('*') {
281-
let loc = Location::new(line, col);
282-
return Err(CommentError::UnmatchedMultilineCommentStart { location: loc });
280+
(false, false, '*') => {
281+
if chars.peek().copied() == Some('/') {
282+
let loc = Location::new(line_num, col);
283+
return Err(CommentError::UnmatchedMultilineCommentStart {
284+
location: loc,
285+
});
286+
}
283287
}
284-
}
285-
(true, false, '\n') => {
286-
let end_loc = Location::new(line, col);
287-
comments.push(Comment::new(
288-
CommentKind::SingleLine(buf.trim().to_string()),
289-
Span::new(Location { line: start_line, column: start_col }, end_loc),
290-
));
291-
in_single = false;
292-
buf.clear();
293-
}
294-
(false, true, '*') => {
295-
if chars.peek().copied() == Some('/') {
296-
chars.next();
297-
let end_loc = Location::new(line, col + 1);
298-
comments.push(Comment::new(
299-
CommentKind::MultiLine(buf.trim().to_string()),
300-
Span::new(Location { line: start_line, column: start_col }, end_loc),
301-
));
302-
in_multi = false;
303-
buf.clear();
304-
col += 1;
305-
} else {
306-
buf.push('*');
288+
(false, true, '*') => {
289+
if chars.peek().copied() == Some('/') {
290+
chars.next();
291+
let end_loc = Location::new(line_num, col + 1);
292+
comments.push(Comment::new(
293+
CommentKind::MultiLine(buf.trim().to_string()),
294+
Span::new(
295+
Location { line: start_line, column: start_col },
296+
end_loc,
297+
),
298+
));
299+
in_multi = false;
300+
buf.clear();
301+
col += 1;
302+
} else {
303+
buf.push('*');
304+
}
305+
}
306+
(false, true, ch) | (true, false, ch) => {
307+
buf.push(ch);
308+
}
309+
(false, false, _) => {}
310+
(true, true, _) => {
311+
unreachable!("should not be possible to be in multiline and single line")
307312
}
308313
}
309-
(false, true, '\n') => {
310-
buf.push('\n');
311-
}
312-
(false, true, ch) | (true, false, ch) => {
313-
buf.push(ch);
314-
}
315-
(false, false, _) => {}
316-
(true, true, _) => {
317-
unreachable!("should not be possible to be in multiline and single line")
318-
}
319-
}
320-
if c == '\n' {
321-
line += 1;
322-
col = 0;
323-
} else {
324314
col += 1;
325315
}
316+
if in_single {
317+
in_single = false;
318+
let end_loc = Location::new(line_num, col);
319+
comments.push(Comment::new(
320+
CommentKind::SingleLine(buf.trim().to_string()),
321+
Span::new(Location { line: start_line, column: start_col }, end_loc),
322+
));
323+
buf.clear();
324+
}
325+
line_num += 1;
326326
}
327-
328327
// EOF: close any open comments
329328
if in_multi {
330329
return Err(CommentError::UnterminatedMultiLineComment {
@@ -333,7 +332,7 @@ impl Comments {
333332
}
334333

335334
if in_single && !buf.is_empty() {
336-
let end_loc = Location::new(line, col);
335+
let end_loc = Location::new(line_num, col);
337336
comments.push(Comment::new(
338337
CommentKind::SingleLine(buf.trim_end().to_string()),
339338
Span::new(Location { line: start_line, column: start_col }, end_loc),
@@ -410,7 +409,7 @@ mod tests {
410409

411410
#[test]
412411
fn multiline_comment_span() {
413-
let kind = CommentKind::MultiLine("/* hello\nworld */".to_string());
412+
let kind = CommentKind::MultiLine("/* hello world */".to_string());
414413
let span = Span::new(Location { line: 1, column: 1 }, Location { line: 2, column: 9 });
415414

416415
let comment = Comment::new(kind.clone(), span);
@@ -495,25 +494,25 @@ mod tests {
495494

496495
fn expected_multiline_comments() -> &'static [&'static str] {
497496
&[
498-
"Users table stores user account information \nmultiline",
499-
"Primary key \n multiline",
500-
"Username for login \n multiline",
501-
"Email address \n multiline",
502-
"When the user registered \n multiline",
503-
"Posts table stores blog posts \nmultiline",
504-
"Primary key \n multiline",
505-
"Post title \n multiline",
506-
"Foreign key linking to users \n multiline",
507-
"Main body text \n multiline",
508-
"When the post was created \n multiline",
497+
"Users table stores user account information multiline",
498+
"Primary key multiline",
499+
"Username for login multiline",
500+
"Email address multiline",
501+
"When the user registered multiline",
502+
"Posts table stores blog posts multiline",
503+
"Primary key multiline",
504+
"Post title multiline",
505+
"Foreign key linking to users multiline",
506+
"Main body text multiline",
507+
"When the post was created multiline",
509508
]
510509
}
511510

512511
fn expected_mixed_comments() -> &'static [&'static str] {
513512
&[
514513
"interstitial Comment above statements (should be ignored)",
515514
"Users table stores user account information",
516-
"users interstitial comment \n(should be ignored)",
515+
"users interstitial comment (should be ignored)",
517516
"Primary key",
518517
"Id comment that is interstitial (should be ignored)",
519518
"Username for login",
@@ -556,8 +555,8 @@ mod tests {
556555
assert_eq!(first.span().end(), &Location::new(1, 47));
557556
let primary_key = &comments[1];
558557
assert_eq!(primary_key.kind().comment(), "Primary key");
559-
assert_eq!(primary_key.span().start(), &Location::new(3, 4));
560-
assert_eq!(primary_key.span().end(), &Location::new(3, 18));
558+
assert_eq!(primary_key.span().start(), &Location::new(3, 5));
559+
assert_eq!(primary_key.span().end(), &Location::new(3, 19));
561560
assert!(
562561
primary_key.span().end().column() > primary_key.span().start().column(),
563562
"end column should be after start column",
@@ -588,19 +587,16 @@ mod tests {
588587
let comments = comments.comments();
589588
assert_eq!(comments.len(), 11);
590589
let first = &comments[0];
591-
assert_eq!(
592-
first.kind().comment(),
593-
"Users table stores user account information \nmultiline"
594-
);
590+
assert_eq!(first.kind().comment(), "Users table stores user account information multiline");
595591
assert_eq!(first.span().start(), &Location::new(1, 1));
596592
assert_eq!(first.span().end().line(), 2);
597593
assert!(
598594
first.span().end().column() > first.span().start().column(),
599595
"end column should be after start column for first multiline comment",
600596
);
601597
let primary_key = &comments[1];
602-
assert_eq!(primary_key.kind().comment(), "Primary key \n multiline");
603-
assert_eq!(primary_key.span().start(), &Location::new(4, 4));
598+
assert_eq!(primary_key.kind().comment(), "Primary key multiline");
599+
assert_eq!(primary_key.span().start(), &Location::new(4, 5));
604600
assert_eq!(primary_key.span().end().line(), 5);
605601
assert!(
606602
primary_key.span().end().column() > primary_key.span().start().column(),

src/docs.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -283,49 +283,43 @@ mod tests {
283283
let second_docs = SqlDocs::new(vec![
284284
TableDoc::new(
285285
"users".to_string(),
286-
Some("Users table stores user account information \nmultiline".to_string()),
286+
Some("Users table stores user account information multiline".to_string()),
287287
vec![
288-
ColumnDoc::new(
289-
"id".to_string(),
290-
Some("Primary key \n multiline".to_string()),
291-
),
288+
ColumnDoc::new("id".to_string(), Some("Primary key multiline".to_string())),
292289
ColumnDoc::new(
293290
"username".to_string(),
294-
Some("Username for login \n multiline".to_string()),
291+
Some("Username for login multiline".to_string()),
295292
),
296293
ColumnDoc::new(
297294
"email".to_string(),
298-
Some("Email address \n multiline".to_string()),
295+
Some("Email address multiline".to_string()),
299296
),
300297
ColumnDoc::new(
301298
"created_at".to_string(),
302-
Some("When the user registered \n multiline".to_string()),
299+
Some("When the user registered multiline".to_string()),
303300
),
304301
],
305302
),
306303
TableDoc::new(
307304
"posts".to_string(),
308-
Some("Posts table stores blog posts \nmultiline".to_string()),
305+
Some("Posts table stores blog posts multiline".to_string()),
309306
vec![
310-
ColumnDoc::new(
311-
"id".to_string(),
312-
Some("Primary key \n multiline".to_string()),
313-
),
307+
ColumnDoc::new("id".to_string(), Some("Primary key multiline".to_string())),
314308
ColumnDoc::new(
315309
"title".to_string(),
316-
Some("Post title \n multiline".to_string()),
310+
Some("Post title multiline".to_string()),
317311
),
318312
ColumnDoc::new(
319313
"user_id".to_string(),
320-
Some("Foreign key linking to users \n multiline".to_string()),
314+
Some("Foreign key linking to users multiline".to_string()),
321315
),
322316
ColumnDoc::new(
323317
"body".to_string(),
324-
Some("Main body text \n multiline".to_string()),
318+
Some("Main body text multiline".to_string()),
325319
),
326320
ColumnDoc::new(
327321
"published_at".to_string(),
328-
Some("When the post was created \n multiline".to_string()),
322+
Some("When the post was created multiline".to_string()),
329323
),
330324
],
331325
),

src/files.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,7 @@ impl SqlFile {
116116
///
117117
/// Returns an [`io::Error`] if the file cannot be read.
118118
pub fn new(path: &Path) -> io::Result<Self> {
119-
let mut content = fs::read_to_string(path)?;
120-
// handle edge case where sql is being parsed on windows and adds \r
121-
if cfg!(windows) {
122-
content = content.replace("\r\n", "\n");
123-
}
119+
let content = fs::read_to_string(path)?;
124120
Ok(Self { path: path.to_owned(), content })
125121
}
126122

src/lib.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub fn generate_docs_from_dir_no_deny<P: AsRef<Path>>(
140140
/// Method for the fuzzer to use
141141
#[cfg(feature = "fuzzing")]
142142
pub fn fuzz_entry(sql: &str) {
143-
use crate::{ast::ParsedSqlFileSet, comments::Comments, files::SqlFileSet, SqlDocs};
143+
use crate::{SqlDocs, ast::ParsedSqlFileSet, comments::Comments, files::SqlFileSet};
144144
use std::env;
145145
use std::fs;
146146
use std::io::Write;
@@ -149,31 +149,19 @@ pub fn fuzz_entry(sql: &str) {
149149
return;
150150
}
151151
let file_path = base.join("fuzz_input.sql");
152-
let mut file = match fs::File::create(&file_path) {
153-
Ok(f) => f,
154-
Err(_) => return,
155-
};
152+
let Ok(mut file) = fs::File::create(&file_path) else { return };
156153
if file.write_all(sql.as_bytes()).is_err() {
157154
return;
158155
}
159-
let set = match SqlFileSet::new(base.as_path(), None) {
160-
Ok(s) => s,
161-
Err(_) => return,
162-
};
163-
let parsed_set = match ParsedSqlFileSet::parse_all(set) {
164-
Ok(p) => p,
165-
Err(_) => return,
166-
};
156+
let Ok(set) = SqlFileSet::new(base.as_path(), None) else { return };
157+
let Ok(parsed_set) = ParsedSqlFileSet::parse_all(set) else { return };
167158
for file in parsed_set.files() {
168159
if let Ok(comments) = Comments::parse_all_comments_from_file(file) {
169160
let _docs = SqlDocs::from_parsed_file(file, &comments);
170161
}
171162
}
172163
}
173164

174-
175-
176-
177165
#[cfg(test)]
178166
mod tests {
179167
use sqlparser::parser::ParserError;

0 commit comments

Comments
 (0)