code-product-lib 0.4.1

macro producing multiple expansions
Documentation
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
use proc_macro2::{Delimiter, Group, Punct, Spacing, TokenStream, TokenTree};

use std::cell::{Cell,RefCell};
use std::collections::HashMap;
use std::rc::Rc;

use crate::{
    token::{ExpandToken, Token, TokenIter},
    ScopeMode,
};

const PRODUCT_CHAR: char = '$';

/// Code is parsed into our internal 'Product' representation. All work is done on this.
/// Scopes are represented this way as well, once a scope becomes closed it is evaluated and
/// appended to the parent.
#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(Clone)]
pub(crate) struct Product(Rc<ProductInner>);

impl From<ProductInner> for Product {
    fn from(inner: ProductInner) -> Self {
        Self(Rc::new(inner))
    }
}

#[cfg_attr(feature = "debug", derive(Debug))]
pub(crate) struct ProductInner {
    parent: RefCell<Parent>,
    entries: RefCell<Vec<Token>>,
    input: RefCell<TokenIter>,
    products: RefCell<Vec<Vec<TokenStream>>>,
    name_lookup: RefCell<HashMap<String, usize>>,
    linear_scope: Cell<bool>
}

impl Product {
    /// Create a new Product. The input is the TokenStream to be parsed, the output is the
    /// TokenStream to which the parsed tokens are appended.
    pub fn new(input: TokenStream) -> Self {
        ProductInner {
            parent: RefCell::new(Parent::Root(TokenStream::new())),
            entries: RefCell::new(Vec::new()),
            input: RefCell::new(TokenIter::new(input)),
            products: RefCell::new(Vec::new()),
            name_lookup: RefCell::new(HashMap::new()),
            linear_scope: Cell::new(false),
        }
        .into()
    }

    /// A product scope is a scope that will be expanded to all permutations of its local
    /// definitions.
    pub fn new_product_scope(parent: &Product, input: TokenStream) -> Self {
        ProductInner {
            parent: RefCell::new(Parent::ParentScope(parent.clone())),
            entries: RefCell::new(Vec::new()),
            input: RefCell::new(TokenIter::new(input)),
            products: RefCell::new(Vec::new()),
            name_lookup: RefCell::new(HashMap::new()),
            linear_scope: Cell::new(false),
        }
        .into()
    }

    /// A linear scope is a scope that will be expanded all its local definitions in sequence.
    pub fn new_linear_scope(parent: &Product, input: TokenStream) -> Self {
        ProductInner {
            parent: RefCell::new(Parent::ParentScope(parent.clone())),
            entries: RefCell::new(Vec::new()),
            input: RefCell::new(TokenIter::new(input)),
            products: RefCell::new(Vec::new()),
            name_lookup: RefCell::new(HashMap::new()),
            linear_scope: Cell::new(true),
        }
        .into()
    }

    fn sub_group<F: FnOnce(&Self)>(&self, group: Group, recurse: F) {
        let delim = group.delimiter();
        let old_iter = std::mem::replace(
            &mut *self.0.input.borrow_mut(),
            TokenIter::new(group.stream()),
        );
        let old_entries = std::mem::take(&mut *self.0.entries.borrow_mut());
        recurse(self);

        let rec_entries = std::mem::replace(&mut *self.0.entries.borrow_mut(), old_entries);
        let new_group = Token::from_group(delim, rec_entries);
        self.push(new_group);

        _ = std::mem::replace(&mut *self.0.input.borrow_mut(), old_iter);
    }
}

impl Product {
    pub fn parse(self, mode: ScopeMode) -> Self {
        match mode {
            ScopeMode::Expand => self.parse_toplevel(),
            ScopeMode::SubExpand => self.parse_toplevel_noexpand(),
            ScopeMode::AutoBraceSemi => self.parse_toplevel_auto_bracesemi(),
        }
        self
    }
}

impl Product {
    fn push(&self, entry: Token) {
        self.0.entries.borrow_mut().push(entry);
    }

    fn push_token(&self, token: TokenTree) {
        self.push(Token::from_tokentree(token));
    }
}

// The parser is a simple recursive descent parser with a lookahead of one token. This means
// that we have to split the parser into small functions to avoid backtracking.  The parse
// entry functions are `pub fn parse_toplevel_xxx(&self)` returning nothing. Syntax errors
// will not parse and either fall through though the final 'rust' terminal or panic earlier.
//
// parse functions have the signature
//    fn parse_xxx(&self) -> bool
//
// Returning 'true' indicates that that the parser matched and parsed the respective thing.
impl Product {
    // parser entry point, ScopeMode::Expand
    // parse = { product_entity | group | rust } ;
    pub fn parse_toplevel(&self) {
        while self.0.input.borrow().token_available() {
            let _ = self.parse_product_entity() || self.parse_group() || self.parse_rust();
        }
    }

    // parser entry point, ScopeMode::SubExpand, not use product expansion at the top level
    // scope
    // parse = { product_entity_noexpand | group_noexpand | rust } ;
    pub fn parse_toplevel_noexpand(&self) {
        while self.0.input.borrow().token_available() {
            let _ = self.parse_product_entity_noexpand()
                || self.parse_group_noexpand()
                || self.parse_rust();
        }
    }

    // parser entry point, ScopeMode::AutoBraceSemi
    // parse = { product_entity | bracesemi | group | rust } ;
    pub fn parse_toplevel_auto_bracesemi(&self) {
        while self.0.input.borrow().token_available() {
            let _ = self.parse_product_entity()
                || self.parse_bracesemi()
                || self.parse_group()
                || self.parse_rust();
        }
    }

    // rust tokens are consumed verbatim and always succeed
    // rust = ?any_valid_rust_token? ;
    fn parse_rust(&self) -> bool {
        self.push_token(self.0.input.borrow().next_token());
        true
    }

    // product_char = "$" ;
    fn parse_product_char(&self) -> bool {
        self.0.input.borrow().next_punct_with(PRODUCT_CHAR)
    }

    // product_entity = "$" ( scope | product_definition | product_reference | rust ) ;
    // the last 'rust' catches the escaped '$$' as '$'.
    fn parse_product_entity(&self) -> bool {
        self.parse_product_char()
            && (self.parse_scope()
                || self.parse_product_definition()
                || self.parse_product_reference()
                || self.parse_rust())
    }

    // bracesemi = brace | semicolon ; (* do auto_expand_cycle on success *)
    fn parse_bracesemi(&self) -> bool {
        if self.parse_brace() || self.parse_semicolon() {
            self.auto_expand_cycle();
            true
        } else {
            false
        }
    }

    // bracesemi = "{" { parse } "}" ;
    fn parse_brace(&self) -> bool {
        let group = self.0.input.borrow().next_group_with(Delimiter::Brace);
        if let Some(group) = group {
            self.sub_group(group, |s| {
                s.parse_toplevel();
            });
            true
        } else {
            false
        }
    }

    // semi = ";" ;
    fn parse_semicolon(&self) -> bool {
        if self.0.input.borrow().next_punct_with(';') {
            self.push_token(TokenTree::Punct(Punct::new(';', Spacing::Alone)));
            true
        } else {
            false
        }
    }

    // group = "(" { parse } ")" ; (* or any other bracket form *)
    fn parse_group(&self) -> bool {
        let group = self.0.input.borrow().next_group();
        if let Some(group) = group {
            self.sub_group(group, |s| {
                s.parse_toplevel();
            });
            true
        } else {
            false
        }
    }

    // product_entity_noexpand = "$" scope ;
    fn parse_product_entity_noexpand(&self) -> bool {
        self.parse_product_char()
            && (self.parse_scope() || {
                self.push_token(TokenTree::Punct(Punct::new(PRODUCT_CHAR, Spacing::Alone)));
                true
            })
    }

    // group = "(" { parse } ")" ; (* or any other bracket form *)
    fn parse_group_noexpand(&self) -> bool {
        let group = self.0.input.borrow().next_group();
        if let Some(group) = group {
            self.sub_group(group, |s| {
                s.parse_toplevel_noexpand();
            });
            true
        } else {
            false
        }
    }

    // scope = product_scope | linear_scope  ;
    fn parse_scope(&self) -> bool {
        self.parse_product_scope() | self.parse_linear_scope()
    }

    // product_scope = "{" parse "}" ;
    fn parse_product_scope(&self) -> bool {
        if let Some(group) = self.0.input.borrow().next_group_with(Delimiter::Brace) {
            let scope = Product::new_product_scope(self, group.stream());
            // Sub-scopes are always expanded
            scope.parse(ScopeMode::Expand).expand_scope();
            true
        } else {
            false
        }
    }

    // linear_scope = "[" parse "]" ;
    fn parse_linear_scope(&self) -> bool {
        if let Some(group) = self.0.input.borrow().next_group_with(Delimiter::Bracket) {
            let scope = Product::new_linear_scope(self, group.stream());
            // Sub-scopes are always expanded
            scope.parse(ScopeMode::Expand).expand_scope();
            true
        } else {
            false
        }
    }

    // product_definition = "(" [ [ "$" ] product_name ":" ] ( product | { "(" product ")" } ) ")" ;
    fn parse_product_definition(&self) -> bool {
        if let Some(group) = self
            .0
            .input
            .borrow()
            .next_group_with(Delimiter::Parenthesis)
        {
            let group = TokenIter::new(group.stream());

            // '$'
            let mut visible = group.next_punct_with(PRODUCT_CHAR);

            // 'name'
            let name = if let Some(TokenTree::Ident(i)) = group.peek() {
                group.next();
                // ':'
                assert!(
                    group.next_punct_with(':'),
                    "expected ':' after product name"
                );
                Some(i.to_string())
            } else {
                None
            };

            let mut products = Vec::new();

            if group.peek_group_with(Delimiter::Parenthesis) {
                // product = "(" { rust } ")" ;
                while let Some(group) = group.next_group_with(Delimiter::Parenthesis) {
                    products.push(group.stream());
                }
                assert!(
                    !group.token_available(),
                    "expected multiple product definitions to be parenthesized"
                );
            } else {
                // product = { rust } ;
                products.push(group.stream());
            }

            let mut defs = self.0.products.borrow_mut();
            defs.push(products);
            let index = defs.len() - 1;

            if let Some(name) = name {
                if self
                    .0
                    .name_lookup
                    .borrow_mut()
                    .insert(name, index)
                    .is_some()
                {
                    panic!("Key redefined");
                }
            } else {
                visible = true
            }

            if visible {
                self.push(Token::IndexedReference(index));
            }

            true
        } else {
            false
        }
    }

    // product_reference = ?number? | ?identifier? ;
    fn parse_product_reference(&self) -> bool {
        if let Some(ident) = self.0.input.borrow().next_ident() {
            let name = ident.to_string();

            if let Some(local) = self.0.name_lookup.borrow().get(name.as_str()) {
                // local names are already resolved to indices
                self.push(Token::IndexedReference(*local));
                return true;
            } else {
                fn name_in_parent(product: &Product, name: &str) -> bool {
                    let parent = product.0.parent.borrow();
                    match *parent {
                        Parent::Root(_) => false,
                        Parent::ParentScope(ref parent) => {
                            if parent.0.name_lookup.borrow().contains_key(name) {
                                true
                            } else {
                                name_in_parent(parent, name)
                            }
                        }
                    }
                }

                if !name_in_parent(self, name.as_str()) {
                    panic!("undefined reference {}", name);
                }
            }

            self.push(Token::NamedReference(name));

            true
        } else if let Some(literal) = self.0.input.borrow().next_literal() {
            let index = literal
                .to_string()
                .parse::<usize>()
                .expect("expected numeric reference");
            assert!(
                index < self.0.products.borrow().len(),
                "undefined reference {}",
                index
            );

            self.push(Token::IndexedReference(index));
            true
        } else {
            false
        }
    }
}

/// The expansion engine expands all local definitions and sends it to the parent scope. Named
/// references are passed verbatim and will be expanded in the parent.
impl Product {
    /// expands a toplevel scope into a `TokenStream`
    pub fn expand(self) -> TokenStream {
        self.expand_scope();

        let Parent::Root(ts) = Rc::into_inner(self.0).unwrap().parent.into_inner() else {
            panic!("can only expand() a top level scope")
        };

        ts
    }

    // does the auto scope handling, expands self to the parent, then reinitalizes self
    fn auto_expand_cycle(&self) {
        self.expand_scope();
        self.0.entries.borrow_mut().clear();
        self.0.products.borrow_mut().clear();
        self.0.name_lookup.borrow_mut().clear();
    }

    // The expansion engine dispatch
    fn expand_scope(&self) {
        if self.0.linear_scope.get() {
            self.expand_linear_scope();
        } else {
            self.expand_product_scope();
        }
    }

    // Product expansion permutes to all local definitions, expands the code and
    // sends it to the parent scope. Named references are passed verbatim and will be expanded
    // in the parent.
    fn expand_product_scope(&self) {
        // Generate all permutations of the local definitions
        let permutations = {
            let mut permutations = vec![vec![]];

            for defs in self.0.products.borrow().iter() {
                let mut tmp = Vec::new();
                for item in defs {
                    for perm in &permutations {
                        let mut new_perm = perm.clone();
                        new_perm.push(item.clone());
                        tmp.push(new_perm);
                    }
                }
                permutations = tmp;
            }
            permutations
        };

        // Expand all permutations
        let mut parent = self.0.parent.borrow_mut();
        match &mut *parent {
            Parent::Root(ref mut stream) => {
                for perms in permutations {
                    for token in self.0.entries.borrow().iter() {
                        stream.expand_token(token, &perms);
                    }
                }
            }
            Parent::ParentScope(parent) => {
                for perms in permutations {
                    for token in self.0.entries.borrow().iter() {
                        parent.0.entries.borrow_mut().expand_token(token, &perms);
                    }
                }
            }
        }
    }

    // Linear expansion expands all local definitions sequentially together and sends it to
    // the parent scope. Named references are passed verbatim and will be expanded in the
    // parent.
    fn expand_linear_scope(&self) {
        let definitions = self.0.products.borrow();
        // check that all definitions have the same number of elements
        let len = if !definitions.is_empty() {
            let len = definitions[0].len();
            for def in definitions.iter().skip(1) {
                assert_eq!(
                    len,
                    def.len(),
                    "linear scope definitions must have the same number of elements"
                );
            }
            len
        } else {
            0
        };

        for i in 0..(len.max(1)) {
            let defs = definitions.iter().map(|d| d[i].clone()).collect::<Vec<_>>();

            let mut parent = self.0.parent.borrow_mut();
            match &mut *parent {
                Parent::Root(ref mut stream) => {
                    for token in self.0.entries.borrow().iter() {
                        stream.expand_token(token, &defs);
                    }
                }
                Parent::ParentScope(parent) => {
                    for token in self.0.entries.borrow().iter() {
                        parent.0.entries.borrow_mut().expand_token(token, &defs);
                    }
                }
            }
        }
    }
}

/// Linking scopes together, the Root scope will send data to a TokenStream, while a Product
/// scope will send data to a parent scope Product. We also note here that the Root scope may
/// not expand production, later this can be used for steaming operation.
#[cfg_attr(feature = "debug", derive(Debug))]
enum Parent {
    /// The root scope, this is the top level scope and will send data to a TokenStream
    Root(TokenStream),
    /// A product scope, this will send data to a parent scope
    ParentScope(Product),
}