genco_macros/
encoder.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use crate::ast::{Ast, Control, ControlKind, Delimiter, MatchArm};
use crate::cursor::Cursor;
use crate::fake::LineColumn;
use crate::requirements::Requirements;
use crate::static_buffer::StaticBuffer;
use crate::Ctxt;

use proc_macro2::{Span, TokenStream};
use syn::Result;

/// Struct to deal with emitting the necessary spacing.
pub(crate) struct Encoder<'a> {
    /// Context for encoding.
    cx: &'a Ctxt,
    /// Use to modify the initial line/column in case something was processed
    /// before the input was handed off to the quote parser.
    ///
    /// See [QuoteInParser].
    span_start: Option<LineColumn>,
    /// Override the end span of the quote parser.
    ///
    /// This causes whitespace to be emitted at the tail of the expression,
    /// unless it specifically reached the end of the span.
    span_end: Option<LineColumn>,
    /// TODO: make private.
    item_buffer: StaticBuffer<'a>,
    /// The token stream we are constructing.
    output: TokenStream,
    /// Currently stored cursor.
    last: Option<Cursor>,
    /// Which column the last line start on.
    last_start_column: Option<usize>,
    /// Indentation columns.
    indents: Vec<(usize, Option<Span>)>,
    /// Indicates if the encoder has encountered a string which requires eval
    /// support in the target language.
    pub(crate) requirements: Requirements,
}

impl<'a> Encoder<'a> {
    pub(crate) fn new(
        cx: &'a Ctxt,
        span_start: Option<LineColumn>,
        span_end: Option<LineColumn>,
    ) -> Self {
        Self {
            cx,
            span_start,
            span_end,
            item_buffer: StaticBuffer::new(cx),
            output: TokenStream::new(),
            last: None,
            last_start_column: None,
            indents: Vec::new(),
            requirements: Requirements::default(),
        }
    }

    /// Encode a single item into the encoder.
    pub(crate) fn encode(&mut self, cursor: Cursor, ast: Ast) -> Result<()> {
        self.step(cursor)?;

        match ast {
            Ast::Tree { tt, .. } => {
                self.encode_literal(&tt.to_string());
            }
            Ast::String { has_eval, stream } => {
                self.requirements.lang_supports_eval |= has_eval;
                self.encode_string(has_eval, stream);
            }
            Ast::Quoted { s } => {
                self.encode_quoted(s);
            }
            Ast::Literal { string } => {
                self.encode_literal(&string);
            }
            Ast::Control { control, .. } => {
                self.encode_control(control);
            }
            Ast::Scope {
                binding, content, ..
            } => {
                self.encode_scope(binding, content);
            }
            Ast::EvalIdent { ident } => {
                self.encode_eval_ident(ident);
            }
            Ast::Eval { expr, .. } => {
                self.encode_eval(expr);
            }
            Ast::Loop {
                pattern,
                expr,
                join,
                stream,
                ..
            } => {
                self.encode_repeat(*pattern, *expr, join, stream);
            }
            Ast::DelimiterOpen { delimiter, .. } => {
                self.encode_open_delimiter(delimiter);
            }
            Ast::DelimiterClose { delimiter, .. } => {
                self.encode_close_delimiter(delimiter);
            }
            Ast::Condition {
                condition,
                then_branch,
                else_branch,
                ..
            } => {
                self.encode_condition(condition, then_branch, else_branch);
            }
            Ast::Match {
                condition, arms, ..
            } => {
                self.encode_match(condition, arms);
            }
            Ast::Let { name, expr } => {
                self.encode_let(name, expr);
            }
        }

        Ok(())
    }

    /// Finalize and translate into a token stream.
    pub(crate) fn into_output(mut self) -> Result<(Requirements, TokenStream)> {
        self.finalize()?;
        Ok((self.requirements, self.output))
    }

    pub(crate) fn step(&mut self, next: Cursor) -> Result<()> {
        if let Some(from) = self.from() {
            // Insert spacing if appropriate.
            self.tokenize_whitespace(from, next.start, Some(next.span))?;
        }

        // Assign the current cursor to the next item.
        // This will then be used to make future indentation decisions.
        self.last = Some(next);
        Ok(())
    }

    pub(crate) fn encode_open_delimiter(&mut self, d: Delimiter) {
        d.encode_open(&mut self.item_buffer);
    }

    pub(crate) fn encode_close_delimiter(&mut self, d: Delimiter) {
        d.encode_close(&mut self.item_buffer);
    }

    pub(crate) fn encode_literal(&mut self, string: &str) {
        self.item_buffer.push_str(string);
    }

    pub(crate) fn encode_string(&mut self, has_eval: bool, stream: TokenStream) {
        let Ctxt { receiver, module } = self.cx;

        self.item_buffer.flush(&mut self.output);

        self.output.extend(q::quote! {
            #receiver.append(#module::tokens::Item::OpenQuote(#has_eval));
            #stream
            #receiver.append(#module::tokens::Item::CloseQuote);
        });
    }

    pub(crate) fn encode_quoted(&mut self, s: syn::LitStr) {
        let Ctxt { receiver, module } = self.cx;

        self.item_buffer.flush(&mut self.output);

        self.output.extend(q::quote! {
            #receiver.append(#module::tokens::Item::OpenQuote(false));
            #receiver.append(#module::tokens::ItemStr::Static(#s));
            #receiver.append(#module::tokens::Item::CloseQuote);
        });
    }

    pub(crate) fn encode_control(&mut self, control: Control) {
        let Ctxt { receiver, .. } = self.cx;

        self.item_buffer.flush(&mut self.output);

        match control.kind {
            ControlKind::Space => {
                self.output
                    .extend(q::quote_spanned!(control.span => #receiver.space();));
            }
            ControlKind::Push => {
                self.output
                    .extend(q::quote_spanned!(control.span => #receiver.push();));
            }
            ControlKind::Line => {
                self.output
                    .extend(q::quote_spanned!(control.span => #receiver.line();));
            }
        }
    }

    pub(crate) fn encode_scope(&mut self, binding: Option<syn::Ident>, content: TokenStream) {
        let Ctxt { receiver, .. } = self.cx;

        if binding.is_some() {
            self.item_buffer.flush(&mut self.output);
        }

        let binding = binding.map(|b| q::quote_spanned!(b.span() => let #b = &mut *#receiver;));

        self.output.extend(q::quote! {{
            #binding
            #content
        }});
    }

    /// Encode an evaluation of the given expression.
    pub(crate) fn encode_eval_ident(&mut self, ident: syn::Ident) {
        let Ctxt { receiver, .. } = self.cx;

        self.item_buffer.flush(&mut self.output);
        self.output.extend(q::quote! {
            #receiver.append(#ident);
        });
    }

    /// Encode an evaluation of the given expression.
    pub(crate) fn encode_eval(&mut self, expr: syn::Expr) {
        let Ctxt { receiver, .. } = self.cx;

        self.item_buffer.flush(&mut self.output);
        self.output.extend(q::quote! {
            #receiver.append(#expr);
        });
    }

    pub(crate) fn encode_repeat(
        &mut self,
        pattern: syn::Pat,
        expr: syn::Expr,
        join: Option<TokenStream>,
        stream: TokenStream,
    ) {
        self.item_buffer.flush(&mut self.output);

        if let Some(join) = join {
            self.output.extend(q::quote! {
                {
                    let mut __it = IntoIterator::into_iter(#expr).peekable();

                    while let Some(#pattern) = __it.next() {
                        #stream

                        if __it.peek().is_some() {
                            #join
                        }
                    }
                }
            });
        } else {
            self.output.extend(q::quote! {
                for #pattern in #expr {
                    #stream
                }
            });
        }
    }

    /// Encode an if statement with an inner stream.
    pub(crate) fn encode_condition(
        &mut self,
        condition: syn::Expr,
        then_branch: TokenStream,
        else_branch: Option<TokenStream>,
    ) {
        self.item_buffer.flush(&mut self.output);

        let else_branch = else_branch.map(|stream| q::quote!(else { #stream }));

        self.output.extend(q::quote! {
            if #condition { #then_branch } #else_branch
        });
    }

    /// Encode an if statement with an inner stream.
    pub(crate) fn encode_match(&mut self, condition: syn::Expr, arms: Vec<MatchArm>) {
        self.item_buffer.flush(&mut self.output);

        let mut stream = TokenStream::new();

        for MatchArm {
            attr,
            pattern,
            condition,
            block,
        } in arms
        {
            let condition = condition.map(|c| q::quote!(if #c));
            stream.extend(q::quote!(#(#attr)* #pattern #condition => { #block },));
        }

        let m = q::quote! {
            match #condition { #stream }
        };

        self.output.extend(m);
    }

    /// Encode a let statement
    pub(crate) fn encode_let(&mut self, name: syn::Pat, expr: syn::Expr) {
        self.item_buffer.flush(&mut self.output);

        self.output.extend(q::quote! {
            let #name = #expr;
        })
    }

    fn from(&mut self) -> Option<LineColumn> {
        // So we've (potentially) encountered the first ever token, while we
        // have a spanned start like `quote_in! { out => foo }`, `foo` is now
        // `next`.
        //
        // What we want to do is treat the beginning out `out` as the
        // indentation position, so we adjust the token.
        //
        // But we also want to avoid situations like this:
        //
        // ```
        // quote_in! { out =>
        //     foo
        //     bar
        // }
        // ```
        //
        // If we would treat `out` as the start, `foo` would be seen as
        // unindented. So check if the first encountered token is on the
        // same line as the binding `out` or not before adjusting them!
        if let Some(span_start) = self.span_start.take() {
            self.last_start_column = Some(span_start.column);
            return Some(span_start);
        }

        if let Some(last) = self.last {
            if self.last_start_column.is_none() {
                self.last_start_column = Some(last.start.column);
            }

            return Some(last.end);
        }

        None
    }

    /// Finalize the encoder.
    fn finalize(&mut self) -> Result<()> {
        let Ctxt { receiver, .. } = self.cx;

        // evaluate whitespace in case we have an explicit end span.
        while let Some(to) = self.span_end.take() {
            if let Some(from) = self.from() {
                // Insert spacing if appropriate, up until the "fake" end.
                self.tokenize_whitespace(from, to, None)?;
            }
        }

        self.item_buffer.flush(&mut self.output);

        while self.indents.pop().is_some() {
            self.output.extend(q::quote!(#receiver.unindent();));
        }

        Ok(())
    }

    /// If we are in a nightly genco, insert indentation and spacing if
    /// appropriate in the output token stream.
    fn tokenize_whitespace(
        &mut self,
        from: LineColumn,
        to: LineColumn,
        to_span: Option<Span>,
    ) -> Result<()> {
        let Ctxt { receiver: r, .. } = self.cx;

        // Do nothing if empty span.
        if from == to {
            return Ok(());
        }

        // Insert spacing if we are on the same line, but column has changed.
        if from.line == to.line {
            // Same line, but next item doesn't match.
            if from.column < to.column {
                self.item_buffer.flush(&mut self.output);
                self.output.extend(q::quote!(#r.space();));
            }

            return Ok(());
        }

        // Line changed. Determine whether to indent, unindent, or hard break the
        // line.
        self.item_buffer.flush(&mut self.output);

        debug_assert!(from.line < to.line);

        let line = to.line - from.line > 1;

        if let Some(last_start_column) = self.last_start_column.take() {
            if last_start_column < to.column {
                self.indents.push((last_start_column, to_span));
                self.output.extend(q::quote!(#r.indent();));

                if line {
                    self.output.extend(q::quote!(#r.line();));
                }
            } else if last_start_column > to.column {
                while let Some((column, _)) = self.indents.pop() {
                    if column > to.column && !self.indents.is_empty() {
                        self.output.extend(q::quote!(#r.unindent();));

                        if line {
                            self.output.extend(q::quote!(#r.line();));
                        }

                        continue;
                    } else if column == to.column {
                        self.output.extend(q::quote!(#r.unindent();));

                        if line {
                            self.output.extend(q::quote!(#r.line();));
                        }

                        break;
                    }

                    return Err(indentation_error(to.column, column, to_span));
                }
            } else if line {
                self.output.extend(q::quote!(#r.line();));
            } else {
                self.output.extend(q::quote!(#r.push();));
            }
        }

        return Ok(());

        fn indentation_error(
            to_column: usize,
            from_column: usize,
            to_span: Option<Span>,
        ) -> syn::Error {
            let error = if to_column > from_column {
                let len = to_column.saturating_sub(from_column);

                format!(
                    "expected {} less {} of indentation",
                    len,
                    if len == 1 { "space" } else { "spaces" }
                )
            } else {
                let len = from_column.saturating_sub(to_column);

                format!(
                    "expected {} more {} of indentation",
                    len,
                    if len == 1 { "space" } else { "spaces" }
                )
            };

            if let Some(span) = to_span {
                syn::Error::new(span, error)
            } else {
                syn::Error::new(Span::call_site(), error)
            }
        }
    }
}