mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
Error builder
This commit is contained in:
parent
50132db4c6
commit
8671a892c5
1 changed files with 28 additions and 1 deletions
|
@ -5,6 +5,7 @@ pub trait Sink {
|
|||
fn leaf(&mut self, kind: SyntaxKind, len: TextUnit);
|
||||
fn start_internal(&mut self, kind: SyntaxKind);
|
||||
fn finish_internal(&mut self);
|
||||
fn error(&mut self) -> ErrorBuilder;
|
||||
}
|
||||
|
||||
|
||||
|
@ -52,6 +53,10 @@ impl Sink for FileBuilder {
|
|||
self.add_len(id);
|
||||
}
|
||||
}
|
||||
|
||||
fn error(&mut self) -> ErrorBuilder {
|
||||
ErrorBuilder::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl FileBuilder {
|
||||
|
@ -132,4 +137,26 @@ fn fill<T>(slot: &mut Option<T>, value: T) {
|
|||
fn grow(left: &mut TextRange, right: TextRange) {
|
||||
assert_eq!(left.end(), right.start());
|
||||
*left = TextRange::from_to(left.start(), right.end())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ErrorBuilder<'f> {
|
||||
message: Option<String>,
|
||||
builder: &'f mut FileBuilder
|
||||
}
|
||||
|
||||
impl<'f> ErrorBuilder<'f> {
|
||||
fn new(builder: &'f mut FileBuilder) -> Self {
|
||||
ErrorBuilder { message: None, builder }
|
||||
}
|
||||
|
||||
pub fn message<M: Into<String>>(mut self, m: M) -> Self {
|
||||
self.message = Some(m.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) {
|
||||
let message = self.message.expect("Error message not set");
|
||||
let node = self.builder.current_id();
|
||||
self.builder.errors.push(SyntaxErrorData { node, message })
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue