aboutsummaryrefslogtreecommitdiff
path: root/src/parser_impl
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-05 20:06:34 +0100
committerAleksey Kladov <[email protected]>2018-08-05 20:06:34 +0100
commit50a7daa042c5f652cd724de55a056f9785a22a85 (patch)
treea836549996bdad7e5f94b8ec041bfba18d2d32b6 /src/parser_impl
parent80366e90f5c1b809c8902e42dced42c0dc9d92ac (diff)
Smarter whitespace
Diffstat (limited to 'src/parser_impl')
-rw-r--r--src/parser_impl/event.rs47
1 files changed, 24 insertions, 23 deletions
diff --git a/src/parser_impl/event.rs b/src/parser_impl/event.rs
index 66a0b6fc0..e64087480 100644
--- a/src/parser_impl/event.rs
+++ b/src/parser_impl/event.rs
@@ -76,9 +76,19 @@ pub(crate) enum Event {
76 }, 76 },
77} 77}
78 78
79pub(super) fn process<'a>(builder: &mut impl Sink<'a>, tokens: &[Token], events: Vec<Event>) { 79pub(super) fn process<'a, S: Sink<'a>>(builder: &mut S, tokens: &[Token], events: Vec<Event>) {
80 let mut idx = 0; 80 let mut next_tok_idx = 0;
81 let eat_ws = |idx: &mut usize, builder: &mut S| {
82 while let Some(token) = tokens.get(*idx) {
83 if !token.kind.is_trivia() {
84 break;
85 }
86 builder.leaf(token.kind, token.len);
87 *idx += 1
88 }
89 };
81 90
91 let mut depth = 0;
82 let mut holes = Vec::new(); 92 let mut holes = Vec::new();
83 let mut forward_parents = Vec::new(); 93 let mut forward_parents = Vec::new();
84 94
@@ -112,41 +122,32 @@ pub(super) fn process<'a>(builder: &mut impl Sink<'a>, tokens: &[Token], events:
112 } 122 }
113 } 123 }
114 for &(idx, kind) in forward_parents.iter().into_iter().rev() { 124 for &(idx, kind) in forward_parents.iter().into_iter().rev() {
125 if depth > 0 {
126 eat_ws(&mut next_tok_idx, builder);
127 }
128 depth += 1;
115 builder.start_internal(kind); 129 builder.start_internal(kind);
116 holes.push(idx); 130 holes.push(idx);
117 } 131 }
118 holes.pop(); 132 holes.pop();
119 } 133 }
120 &Event::Finish => { 134 &Event::Finish => {
121 while idx < tokens.len() { 135 depth -= 1;
122 let token = tokens[idx]; 136 if depth == 0 {
123 if token.kind.is_trivia() { 137 eat_ws(&mut next_tok_idx, builder);
124 idx += 1;
125 builder.leaf(token.kind, token.len);
126 } else {
127 break;
128 }
129 } 138 }
130 builder.finish_internal() 139
140 builder.finish_internal();
131 } 141 }
132 &Event::Token { 142 &Event::Token {
133 kind, 143 kind,
134 mut n_raw_tokens, 144 mut n_raw_tokens,
135 } => { 145 } => {
136 // FIXME: currently, we attach whitespace to some random node 146 eat_ws(&mut next_tok_idx, builder);
137 // this should be done in a sensible manner instead
138 loop {
139 let token = tokens[idx];
140 if !token.kind.is_trivia() {
141 break;
142 }
143 builder.leaf(token.kind, token.len);
144 idx += 1
145 }
146 let mut len = 0.into(); 147 let mut len = 0.into();
147 for _ in 0..n_raw_tokens { 148 for _ in 0..n_raw_tokens {
148 len += tokens[idx].len; 149 len += tokens[next_tok_idx].len;
149 idx += 1; 150 next_tok_idx += 1;
150 } 151 }
151 builder.leaf(kind, len); 152 builder.leaf(kind, len);
152 } 153 }