diff options
-rw-r--r-- | crates/ra_mbe/src/mbe_expander/transcriber.rs | 33 | ||||
-rw-r--r-- | crates/ra_parser/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_parser/src/parser.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/syntax_node.rs | 2 | ||||
-rw-r--r-- | crates/ra_tt/src/buffer.rs | 4 |
5 files changed, 27 insertions, 16 deletions
diff --git a/crates/ra_mbe/src/mbe_expander/transcriber.rs b/crates/ra_mbe/src/mbe_expander/transcriber.rs index 4b173edd3..7c9bb4d00 100644 --- a/crates/ra_mbe/src/mbe_expander/transcriber.rs +++ b/crates/ra_mbe/src/mbe_expander/transcriber.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | //! Transcraber takes a template, like `fn $ident() {}`, a set of bindings like | 1 | //! Transcriber takes a template, like `fn $ident() {}`, a set of bindings like |
2 | //! `$ident => foo`, interpolates variables in the template, to get `fn foo() {}` | 2 | //! `$ident => foo`, interpolates variables in the template, to get `fn foo() {}` |
3 | 3 | ||
4 | use ra_syntax::SmolStr; | 4 | use ra_syntax::SmolStr; |
@@ -53,7 +53,8 @@ impl Bindings { | |||
53 | pub(super) fn transcribe(template: &tt::Subtree, bindings: &Bindings) -> ExpandResult<tt::Subtree> { | 53 | pub(super) fn transcribe(template: &tt::Subtree, bindings: &Bindings) -> ExpandResult<tt::Subtree> { |
54 | assert!(template.delimiter == None); | 54 | assert!(template.delimiter == None); |
55 | let mut ctx = ExpandCtx { bindings: &bindings, nesting: Vec::new() }; | 55 | let mut ctx = ExpandCtx { bindings: &bindings, nesting: Vec::new() }; |
56 | expand_subtree(&mut ctx, template) | 56 | let mut arena: Vec<tt::TokenTree> = Vec::new(); |
57 | expand_subtree(&mut ctx, template, &mut arena) | ||
57 | } | 58 | } |
58 | 59 | ||
59 | #[derive(Debug)] | 60 | #[derive(Debug)] |
@@ -73,8 +74,13 @@ struct ExpandCtx<'a> { | |||
73 | nesting: Vec<NestingState>, | 74 | nesting: Vec<NestingState>, |
74 | } | 75 | } |
75 | 76 | ||
76 | fn expand_subtree(ctx: &mut ExpandCtx, template: &tt::Subtree) -> ExpandResult<tt::Subtree> { | 77 | fn expand_subtree( |
77 | let mut buf: Vec<tt::TokenTree> = Vec::new(); | 78 | ctx: &mut ExpandCtx, |
79 | template: &tt::Subtree, | ||
80 | arena: &mut Vec<tt::TokenTree>, | ||
81 | ) -> ExpandResult<tt::Subtree> { | ||
82 | // remember how many elements are in the arena now - when returning, we want to drain exactly how many elements we added. This way, the recursive uses of the arena get their own "view" of the arena, but will reuse the allocation | ||
83 | let start_elements = arena.len(); | ||
78 | let mut err = None; | 84 | let mut err = None; |
79 | for op in parse_template(template) { | 85 | for op in parse_template(template) { |
80 | let op = match op { | 86 | let op = match op { |
@@ -85,25 +91,27 @@ fn expand_subtree(ctx: &mut ExpandCtx, template: &tt::Subtree) -> ExpandResult<t | |||
85 | } | 91 | } |
86 | }; | 92 | }; |
87 | match op { | 93 | match op { |
88 | Op::TokenTree(tt @ tt::TokenTree::Leaf(..)) => buf.push(tt.clone()), | 94 | Op::TokenTree(tt @ tt::TokenTree::Leaf(..)) => arena.push(tt.clone()), |
89 | Op::TokenTree(tt::TokenTree::Subtree(tt)) => { | 95 | Op::TokenTree(tt::TokenTree::Subtree(tt)) => { |
90 | let ExpandResult(tt, e) = expand_subtree(ctx, tt); | 96 | let ExpandResult(tt, e) = expand_subtree(ctx, tt, arena); |
91 | err = err.or(e); | 97 | err = err.or(e); |
92 | buf.push(tt.into()); | 98 | arena.push(tt.into()); |
93 | } | 99 | } |
94 | Op::Var { name, kind: _ } => { | 100 | Op::Var { name, kind: _ } => { |
95 | let ExpandResult(fragment, e) = expand_var(ctx, name); | 101 | let ExpandResult(fragment, e) = expand_var(ctx, name); |
96 | err = err.or(e); | 102 | err = err.or(e); |
97 | push_fragment(&mut buf, fragment); | 103 | push_fragment(arena, fragment); |
98 | } | 104 | } |
99 | Op::Repeat { subtree, kind, separator } => { | 105 | Op::Repeat { subtree, kind, separator } => { |
100 | let ExpandResult(fragment, e) = expand_repeat(ctx, subtree, kind, separator); | 106 | let ExpandResult(fragment, e) = expand_repeat(ctx, subtree, kind, separator, arena); |
101 | err = err.or(e); | 107 | err = err.or(e); |
102 | push_fragment(&mut buf, fragment) | 108 | push_fragment(arena, fragment) |
103 | } | 109 | } |
104 | } | 110 | } |
105 | } | 111 | } |
106 | ExpandResult(tt::Subtree { delimiter: template.delimiter, token_trees: buf }, err) | 112 | // drain the elements added in this instance of expand_subtree |
113 | let tts = arena.drain(start_elements..arena.len()).collect(); | ||
114 | ExpandResult(tt::Subtree { delimiter: template.delimiter, token_trees: tts }, err) | ||
107 | } | 115 | } |
108 | 116 | ||
109 | fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult<Fragment> { | 117 | fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult<Fragment> { |
@@ -155,6 +163,7 @@ fn expand_repeat( | |||
155 | template: &tt::Subtree, | 163 | template: &tt::Subtree, |
156 | kind: RepeatKind, | 164 | kind: RepeatKind, |
157 | separator: Option<Separator>, | 165 | separator: Option<Separator>, |
166 | arena: &mut Vec<tt::TokenTree>, | ||
158 | ) -> ExpandResult<Fragment> { | 167 | ) -> ExpandResult<Fragment> { |
159 | let mut buf: Vec<tt::TokenTree> = Vec::new(); | 168 | let mut buf: Vec<tt::TokenTree> = Vec::new(); |
160 | ctx.nesting.push(NestingState { idx: 0, at_end: false, hit: false }); | 169 | ctx.nesting.push(NestingState { idx: 0, at_end: false, hit: false }); |
@@ -165,7 +174,7 @@ fn expand_repeat( | |||
165 | let mut counter = 0; | 174 | let mut counter = 0; |
166 | 175 | ||
167 | loop { | 176 | loop { |
168 | let ExpandResult(mut t, e) = expand_subtree(ctx, template); | 177 | let ExpandResult(mut t, e) = expand_subtree(ctx, template, arena); |
169 | let nesting_state = ctx.nesting.last_mut().unwrap(); | 178 | let nesting_state = ctx.nesting.last_mut().unwrap(); |
170 | if nesting_state.at_end || !nesting_state.hit { | 179 | if nesting_state.at_end || !nesting_state.hit { |
171 | break; | 180 | break; |
diff --git a/crates/ra_parser/src/lib.rs b/crates/ra_parser/src/lib.rs index e08ad4dae..eeb8ad66b 100644 --- a/crates/ra_parser/src/lib.rs +++ b/crates/ra_parser/src/lib.rs | |||
@@ -25,7 +25,7 @@ pub(crate) use token_set::TokenSet; | |||
25 | pub use syntax_kind::SyntaxKind; | 25 | pub use syntax_kind::SyntaxKind; |
26 | 26 | ||
27 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 27 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
28 | pub struct ParseError(pub String); | 28 | pub struct ParseError(pub Box<String>); |
29 | 29 | ||
30 | /// `TokenSource` abstracts the source of the tokens parser operates on. | 30 | /// `TokenSource` abstracts the source of the tokens parser operates on. |
31 | /// | 31 | /// |
diff --git a/crates/ra_parser/src/parser.rs b/crates/ra_parser/src/parser.rs index faa63d53f..4f59b0a23 100644 --- a/crates/ra_parser/src/parser.rs +++ b/crates/ra_parser/src/parser.rs | |||
@@ -192,7 +192,7 @@ impl<'t> Parser<'t> { | |||
192 | /// structured errors with spans and notes, like rustc | 192 | /// structured errors with spans and notes, like rustc |
193 | /// does. | 193 | /// does. |
194 | pub(crate) fn error<T: Into<String>>(&mut self, message: T) { | 194 | pub(crate) fn error<T: Into<String>>(&mut self, message: T) { |
195 | let msg = ParseError(message.into()); | 195 | let msg = ParseError(Box::new(message.into())); |
196 | self.push_event(Event::Error { msg }) | 196 | self.push_event(Event::Error { msg }) |
197 | } | 197 | } |
198 | 198 | ||
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index f9d379abf..e566af7e8 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs | |||
@@ -70,6 +70,6 @@ impl SyntaxTreeBuilder { | |||
70 | } | 70 | } |
71 | 71 | ||
72 | pub fn error(&mut self, error: ra_parser::ParseError, text_pos: TextSize) { | 72 | pub fn error(&mut self, error: ra_parser::ParseError, text_pos: TextSize) { |
73 | self.errors.push(SyntaxError::new_at_offset(error.0, text_pos)) | 73 | self.errors.push(SyntaxError::new_at_offset(*error.0, text_pos)) |
74 | } | 74 | } |
75 | } | 75 | } |
diff --git a/crates/ra_tt/src/buffer.rs b/crates/ra_tt/src/buffer.rs index 14b3f707d..5967f44cd 100644 --- a/crates/ra_tt/src/buffer.rs +++ b/crates/ra_tt/src/buffer.rs | |||
@@ -42,7 +42,9 @@ impl<'t> TokenBuffer<'t> { | |||
42 | buffers: &mut Vec<Box<[Entry<'t>]>>, | 42 | buffers: &mut Vec<Box<[Entry<'t>]>>, |
43 | next: Option<EntryPtr>, | 43 | next: Option<EntryPtr>, |
44 | ) -> usize { | 44 | ) -> usize { |
45 | let mut entries = vec![]; | 45 | // Must contain everything in tokens and then the Entry::End |
46 | let start_capacity = tokens.len() + 1; | ||
47 | let mut entries = Vec::with_capacity(start_capacity); | ||
46 | let mut children = vec![]; | 48 | let mut children = vec![]; |
47 | 49 | ||
48 | for (idx, tt) in tokens.iter().enumerate() { | 50 | for (idx, tt) in tokens.iter().enumerate() { |