diff options
-rw-r--r-- | src/tree/file_builder.rs | 29 |
1 files changed, 28 insertions, 1 deletions
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 { | |||
5 | fn leaf(&mut self, kind: SyntaxKind, len: TextUnit); | 5 | fn leaf(&mut self, kind: SyntaxKind, len: TextUnit); |
6 | fn start_internal(&mut self, kind: SyntaxKind); | 6 | fn start_internal(&mut self, kind: SyntaxKind); |
7 | fn finish_internal(&mut self); | 7 | fn finish_internal(&mut self); |
8 | fn error(&mut self) -> ErrorBuilder; | ||
8 | } | 9 | } |
9 | 10 | ||
10 | 11 | ||
@@ -52,6 +53,10 @@ impl Sink for FileBuilder { | |||
52 | self.add_len(id); | 53 | self.add_len(id); |
53 | } | 54 | } |
54 | } | 55 | } |
56 | |||
57 | fn error(&mut self) -> ErrorBuilder { | ||
58 | ErrorBuilder::new(self) | ||
59 | } | ||
55 | } | 60 | } |
56 | 61 | ||
57 | impl FileBuilder { | 62 | impl FileBuilder { |
@@ -132,4 +137,26 @@ fn fill<T>(slot: &mut Option<T>, value: T) { | |||
132 | fn grow(left: &mut TextRange, right: TextRange) { | 137 | fn grow(left: &mut TextRange, right: TextRange) { |
133 | assert_eq!(left.end(), right.start()); | 138 | assert_eq!(left.end(), right.start()); |
134 | *left = TextRange::from_to(left.start(), right.end()) | 139 | *left = TextRange::from_to(left.start(), right.end()) |
135 | } \ No newline at end of file | 140 | } |
141 | |||
142 | pub struct ErrorBuilder<'f> { | ||
143 | message: Option<String>, | ||
144 | builder: &'f mut FileBuilder | ||
145 | } | ||
146 | |||
147 | impl<'f> ErrorBuilder<'f> { | ||
148 | fn new(builder: &'f mut FileBuilder) -> Self { | ||
149 | ErrorBuilder { message: None, builder } | ||
150 | } | ||
151 | |||
152 | pub fn message<M: Into<String>>(mut self, m: M) -> Self { | ||
153 | self.message = Some(m.into()); | ||
154 | self | ||
155 | } | ||
156 | |||
157 | pub fn build(self) { | ||
158 | let message = self.message.expect("Error message not set"); | ||
159 | let node = self.builder.current_id(); | ||
160 | self.builder.errors.push(SyntaxErrorData { node, message }) | ||
161 | } | ||
162 | } | ||