Skip to content

Commit a096c57

Browse files
committed
chore: cleanup parser
1 parent d09f95c commit a096c57

File tree

1 file changed

+0
-79
lines changed

1 file changed

+0
-79
lines changed

crates/pg_statement_splitter/src/parser.rs

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
use std::cmp::min;
2-
31
use pg_lexer::{SyntaxKind, Token, TokenType, WHITESPACE_TOKENS};
4-
use text_size::{TextRange, TextSize};
5-
6-
use crate::syntax_error::SyntaxError;
72

8-
/// Main parser that exposes the `cstree` api, and collects errors and statements
93
pub struct Parser {
10-
/// The ranges of the statements
11-
ranges: Vec<(usize, usize)>,
12-
/// The syntax errors accumulated during parsing
13-
errors: Vec<SyntaxError>,
14-
/// The start of the current statement, if any
15-
current_stmt_start: Option<usize>,
164
/// The tokens to parse
175
pub tokens: Vec<Token>,
186
/// The current position in the token stream
@@ -23,62 +11,16 @@ pub struct Parser {
2311
eof_token: Token,
2412
}
2513

26-
/// Result of Building
27-
#[derive(Debug)]
28-
pub struct Parse {
29-
/// The ranges of the errors
30-
pub ranges: Vec<TextRange>,
31-
/// The syntax errors accumulated during parsing
32-
pub errors: Vec<SyntaxError>,
33-
}
34-
3514
impl Parser {
3615
pub fn new(tokens: Vec<Token>) -> Self {
3716
Self {
3817
eof_token: Token::eof(usize::from(tokens.last().unwrap().span.end())),
39-
ranges: Vec::new(),
40-
errors: Vec::new(),
41-
current_stmt_start: None,
4218
tokens,
4319
pos: 0,
4420
whitespace_token_buffer: None,
4521
}
4622
}
4723

48-
pub fn finish(self) -> Parse {
49-
Parse {
50-
ranges: self
51-
.ranges
52-
.iter()
53-
.map(|(start, end)| {
54-
let from = self.tokens.get(*start);
55-
let to = self.tokens.get(end - 1);
56-
// get text range from token range
57-
let text_start = from.unwrap().span.start();
58-
let text_end = to.unwrap().span.end();
59-
60-
TextRange::new(
61-
TextSize::try_from(text_start).unwrap(),
62-
TextSize::try_from(text_end).unwrap(),
63-
)
64-
})
65-
.collect(),
66-
errors: self.errors,
67-
}
68-
}
69-
70-
/// collects an SyntaxError with an `error` message at `pos`
71-
pub fn error_at_pos(&mut self, error: String, pos: usize) {
72-
self.errors.push(SyntaxError::new_at_offset(
73-
error,
74-
self.tokens
75-
.get(min(self.tokens.len() - 1, pos))
76-
.unwrap()
77-
.span
78-
.start(),
79-
));
80-
}
81-
8224
/// applies token and advances
8325
pub fn advance(&mut self) {
8426
assert!(!self.eof());
@@ -203,25 +145,4 @@ impl Parser {
203145
pub fn at(&self, kind: SyntaxKind) -> bool {
204146
self.nth(0, false).kind == kind
205147
}
206-
207-
pub fn expect(&mut self, kind: SyntaxKind) {
208-
if self.eat(kind) {
209-
return;
210-
}
211-
if self.whitespace_token_buffer.is_some() {
212-
self.error_at_pos(
213-
format!(
214-
"Expected {:#?}, found {:#?}",
215-
kind,
216-
self.tokens[self.whitespace_token_buffer.unwrap()].kind
217-
),
218-
self.whitespace_token_buffer.unwrap(),
219-
);
220-
} else {
221-
self.error_at_pos(
222-
format!("Expected {:#?}, found {:#?}", kind, self.nth(0, false)),
223-
self.pos + 1,
224-
);
225-
}
226-
}
227148
}

0 commit comments

Comments
 (0)