From 8671a892c5169fe608d0467270a2af7797df8c36 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 7 Jan 2018 10:55:43 +0300 Subject: Error builder --- src/tree/file_builder.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src/tree') diff --git a/src/tree/file_builder.rs b/src/tree/file_builder.rs index 83aa4186f..a1b004892 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 }) + } +} -- cgit v1.2.3