rust/crates/ra_syntax/src/yellow/builder.rs

53 lines
1.2 KiB
Rust
Raw Normal View History

2018-10-02 09:07:12 -05:00
use rowan::GreenNodeBuilder;
2018-07-29 05:51:55 -05:00
use {
2018-07-31 15:38:19 -05:00
parser_impl::Sink,
2018-10-02 09:07:12 -05:00
yellow::{GreenNode, SyntaxError, RaTypes},
2018-07-29 05:51:55 -05:00
SyntaxKind, TextRange, TextUnit,
};
2018-08-01 02:40:07 -05:00
pub(crate) struct GreenBuilder<'a> {
text: &'a str,
2018-07-29 05:51:55 -05:00
pos: TextUnit,
2018-07-29 07:16:07 -05:00
errors: Vec<SyntaxError>,
2018-10-02 09:07:12 -05:00
inner: GreenNodeBuilder<RaTypes>,
2018-07-29 05:51:55 -05:00
}
2018-08-01 02:40:07 -05:00
impl<'a> Sink<'a> for GreenBuilder<'a> {
2018-08-25 04:10:35 -05:00
type Tree = (GreenNode, Vec<SyntaxError>);
2018-07-29 07:16:07 -05:00
2018-08-01 02:40:07 -05:00
fn new(text: &'a str) -> Self {
2018-07-29 05:51:55 -05:00
GreenBuilder {
text,
pos: 0.into(),
errors: Vec::new(),
2018-10-02 09:07:12 -05:00
inner: GreenNodeBuilder::new(),
2018-07-29 05:51:55 -05:00
}
}
fn leaf(&mut self, kind: SyntaxKind, len: TextUnit) {
let range = TextRange::offset_len(self.pos, len);
self.pos += len;
2018-10-02 09:07:12 -05:00
let text = self.text[range].into();
self.inner.leaf(kind, text);
2018-07-29 05:51:55 -05:00
}
fn start_internal(&mut self, kind: SyntaxKind) {
2018-10-02 09:07:12 -05:00
self.inner.start_internal(kind)
2018-07-29 05:51:55 -05:00
}
fn finish_internal(&mut self) {
2018-10-02 09:07:12 -05:00
self.inner.finish_internal();
2018-07-29 05:51:55 -05:00
}
2018-07-29 06:37:48 -05:00
fn error(&mut self, message: String) {
2018-07-30 06:08:06 -05:00
self.errors.push(SyntaxError {
2018-08-09 13:27:44 -05:00
msg: message,
2018-07-30 06:08:06 -05:00
offset: self.pos,
})
2018-07-29 05:51:55 -05:00
}
2018-07-29 07:16:07 -05:00
2018-10-02 09:07:12 -05:00
fn finish(self) -> (GreenNode, Vec<SyntaxError>) {
(self.inner.finish(), self.errors)
2018-07-29 05:51:55 -05:00
}
}