diff --git a/src/tree/file_builder.rs b/src/tree/file_builder.rs index 83aa4186f2..a1b0048924 100644 --- a/src/tree/file_builder.rs +++ b/src/tree/file_builder.rs @@ -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(slot: &mut Option, value: T) { fn grow(left: &mut TextRange, right: TextRange) { assert_eq!(left.end(), right.start()); *left = TextRange::from_to(left.start(), right.end()) -} \ No newline at end of file +} + +pub struct ErrorBuilder<'f> { + message: Option, + builder: &'f mut FileBuilder +} + +impl<'f> ErrorBuilder<'f> { + fn new(builder: &'f mut FileBuilder) -> Self { + ErrorBuilder { message: None, builder } + } + + pub fn message>(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 }) + } +}