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
/*******************************************************************************
 * Copyright (c) 2017 Association Cénotélie (cenotelie.fr)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this program.
 * If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

//! Module for Abstract-Syntax Trees

use std::fmt::Display;
use std::fmt::Error;
use std::fmt::Formatter;

use super::symbols::SemanticElementTrait;
use super::symbols::Symbol;
use super::text::TextContext;
use super::text::TextPosition;
use super::text::TextSpan;
use super::tokens::Token;
use super::tokens::TokenRepository;
use super::utils::biglist::BigList;
use super::utils::iterable::Iterable;
use super::utils::EitherMut;

/// Represents a type of symbol table
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum TableType {
    /// Marks as other (used for SPPF nodes)
    None = 0,
    /// Table of tokens
    Token = 1,
    /// Table of variables
    Variable = 2,
    /// Tables of virtuals
    Virtual = 3
}

impl From<usize> for TableType {
    fn from(x: usize) -> Self {
        match x {
            1 => TableType::Token,
            2 => TableType::Variable,
            3 => TableType::Virtual,
            _ => TableType::None
        }
    }
}

/// Represents a compact reference to an element in a table
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct TableElemRef {
    /// The backend data
    data: usize
}

impl TableElemRef {
    /// Initializes this reference
    pub fn new(t: TableType, index: usize) -> TableElemRef {
        TableElemRef {
            data: ((t as usize) << 30) | index
        }
    }

    /// Gets the element's type
    pub fn get_type(&self) -> TableType {
        TableType::from(self.data >> 30)
    }

    /// Gets the element's index in its respective table
    pub fn get_index(&self) -> usize {
        (self.data & 0x3FFFFFFF)
    }
}

/// Represents a cell in an AST inner structure
#[derive(Copy, Clone)]
pub struct AstCell {
    /// The node's label
    pub label: TableElemRef,
    /// The number of children
    pub count: u32,
    /// The index of the first child
    pub first: u32
}

impl AstCell {
    /// Initializes this node
    pub fn new_empty(label: TableElemRef) -> AstCell {
        AstCell {
            label,
            count: 0,
            first: 0
        }
    }

    /// Initializes this node
    pub fn new(label: TableElemRef, count: u32, first: u32) -> AstCell {
        AstCell {
            label,
            count,
            first
        }
    }
}

/// Implementation of a simple AST with a tree structure
/// The nodes are stored in sequential arrays where the children of a node are an inner sequence.
/// The linkage is represented by each node storing its number of children and the index of its first child.
pub struct AstImpl {
    /// The nodes' labels
    nodes: BigList<AstCell>,
    /// The index of the tree's root node
    root: Option<usize>
}

impl AstImpl {
    /// Creates a new implementation
    pub fn new() -> AstImpl {
        AstImpl {
            nodes: BigList::<AstCell>::new(AstCell::new(
                TableElemRef::new(TableType::None, 0),
                0,
                0
            )),
            root: None
        }
    }

    /// Gets whether a root has been defined for this AST
    pub fn has_root(&self) -> bool {
        self.root.is_some()
    }
}

/// Represents a simple AST with a tree structure
/// The nodes are stored in sequential arrays where the children of a node are an inner sequence.
/// The linkage is represented by each node storing its number of children and the index of its first child.
pub struct Ast<'a> {
    /// The table of tokens
    tokens: Option<TokenRepository<'a>>,
    /// The table of variables
    variables: &'static [Symbol],
    /// The table of virtuals
    virtuals: &'static [Symbol],
    /// The data of the implementation
    data: EitherMut<'a, AstImpl>
}

impl<'a> Ast<'a> {
    /// Creates a new AST proxy structure
    pub fn new(
        tokens: TokenRepository<'a>,
        variables: &'static [Symbol],
        virtuals: &'static [Symbol],
        data: &'a AstImpl
    ) -> Ast<'a> {
        Ast {
            tokens: Some(tokens),
            variables,
            virtuals,
            data: EitherMut::Immutable(data)
        }
    }

    /// Creates a new AST proxy structure
    pub fn new_mut(
        variables: &'static [Symbol],
        virtuals: &'static [Symbol],
        data: &'a mut AstImpl
    ) -> Ast<'a> {
        Ast {
            tokens: None,
            variables,
            virtuals,
            data: EitherMut::Mutable(data)
        }
    }

    /// Gets the i-th token in the associated repository
    fn get_token(&self, index: usize) -> Token {
        match self.tokens {
            None => panic!("Missing token repository"),
            Some(ref x) => x.get_token(index)
        }
    }

    /// Gets the grammar variables for this AST
    pub fn get_variables(&self) -> &'static [Symbol] {
        &self.variables
    }

    /// Gets the grammar virtuals for this AST
    pub fn get_virtuals(&self) -> &'static [Symbol] {
        &self.virtuals
    }

    /// Gets whether a root has been defined for this AST
    pub fn has_root(&self) -> bool {
        self.data.get().has_root()
    }

    /// Gets the root node of this tree
    pub fn get_root(&self) -> AstNode {
        let data = self.data.get();
        match data.root {
            None => panic!("No root defined!"),
            Some(x) => AstNode {
                tree: self,
                index: x
            }
        }
    }

    /// Gets a specific node in this tree
    pub fn get_node(&self, id: usize) -> AstNode {
        AstNode {
            tree: self,
            index: id
        }
    }

    /// Gets the AST node (if any) that has the specified token as label
    pub fn find_node_for(&self, token: &Token<'a>) -> Option<AstNode> {
        let data = self.data.get();
        for i in 0..data.nodes.len() {
            let node = data.nodes[i];
            if node.label.get_type() == TableType::Token && node.label.get_index() == token.index {
                return Some(AstNode {
                    tree: self,
                    index: i
                });
            }
        }
        None
    }

    /// Gets the AST node (if any) that has
    /// a token label that contains the specified index in the input text
    pub fn find_node_at_index(&self, index: usize) -> Option<AstNode> {
        let token = self.tokens.as_ref().unwrap().find_token_at(index);
        match token {
            None => None,
            Some(token) => self.find_node_for(&token)
        }
    }

    /// Gets the AST node (if any) that has
    /// a token label that contains the specified index in the input text
    pub fn find_node_at_position(&self, position: TextPosition) -> Option<AstNode> {
        let tokens = self.tokens.as_ref().unwrap();
        let index = tokens.get_input().get_line_index(position.line) + position.column - 1;
        let token = tokens.find_token_at(index);
        match token {
            None => None,
            Some(token) => self.find_node_for(&token)
        }
    }

    /// Gets the parent of the specified node, if any
    pub fn find_parent_of(&self, node: usize) -> Option<AstNode> {
        let data = self.data.get();
        if data.root.is_none() {
            return None;
        }
        for i in 0..data.nodes.len() {
            let candidate = data.nodes[i];
            if candidate.count > 0
                && node >= candidate.first as usize
                && node < (candidate.first + candidate.count) as usize
            {
                return Some(AstNode {
                    tree: self,
                    index: i
                });
            }
        }
        None
    }

    /// Stores some children nodes in this AST
    pub fn store(&mut self, nodes: &Vec<AstCell>, index: usize, count: usize) -> usize {
        if count == 0 {
            0
        } else {
            match self.data.get_mut() {
                None => panic!("Got a mutable AST with an immutable implementation"),
                Some(data) => {
                    let result = data.nodes.push(nodes[index]);
                    for i in 1..count {
                        data.nodes.push(nodes[index + i]);
                    }
                    result
                }
            }
        }
    }

    /// Stores the root of this tree
    pub fn store_root(&mut self, node: AstCell) {
        match self.data.get_mut() {
            None => panic!("Got a mutable AST with an immutable implementation"),
            Some(data) => data.root = Some(data.nodes.push(node))
        }
    }
}

/// Represents a node in an Abstract Syntax Tree
#[derive(Clone)]
pub struct AstNode<'a> {
    /// The original parse tree
    tree: &'a Ast<'a>,
    /// The index of this node in the parse tree
    index: usize
}

impl<'a> AstNode<'a> {
    /// Gets the identifier of this node
    pub fn id(&self) -> usize {
        self.index
    }

    /// Gets the index of the token born by this node, if any
    pub fn get_token_index(&self) -> Option<usize> {
        let cell = self.tree.data.get().nodes[self.index];
        match cell.label.get_type() {
            TableType::Token => Some(cell.label.get_index()),
            _ => None
        }
    }

    /// Gets the parent of this node, if any
    pub fn parent(&self) -> Option<AstNode> {
        self.tree.find_parent_of(self.index)
    }

    /// Gets the children of this node
    pub fn children(&self) -> AstFamily {
        AstFamily {
            tree: self.tree,
            parent: self.index
        }
    }
}

impl<'a> SemanticElementTrait for AstNode<'a> {
    /// Gets the position in the input text of this element
    fn get_position(&self) -> Option<TextPosition> {
        let cell = self.tree.data.get().nodes[self.index];
        match cell.label.get_type() {
            TableType::Token => {
                let token = self.tree.get_token(cell.label.get_index());
                token.get_position()
            }
            _ => None
        }
    }

    /// Gets the span in the input text of this element
    fn get_span(&self) -> Option<TextSpan> {
        let cell = self.tree.data.get().nodes[self.index];
        match cell.label.get_type() {
            TableType::Token => {
                let token = self.tree.get_token(cell.label.get_index());
                token.get_span()
            }
            _ => None
        }
    }

    /// Gets the context of this element in the input
    fn get_context(&self) -> Option<TextContext> {
        let cell = self.tree.data.get().nodes[self.index];
        match cell.label.get_type() {
            TableType::Token => {
                let token = self.tree.get_token(cell.label.get_index());
                token.get_context()
            }
            _ => None
        }
    }

    /// Gets the grammar symbol associated to this element
    fn get_symbol(&self) -> Symbol {
        let cell = self.tree.data.get().nodes[self.index];
        match cell.label.get_type() {
            TableType::Token => {
                let token = self.tree.get_token(cell.label.get_index());
                token.get_symbol()
            }
            TableType::Variable => self.tree.variables[cell.label.get_index()],
            TableType::Virtual => self.tree.virtuals[cell.label.get_index()],
            TableType::None => {
                match self.tree.tokens {
                    None => panic!("Missing token repository"),
                    Some(ref repository) => repository.get_terminals()[0] // terminal epsilon
                }
            }
        }
    }

    /// Gets the value of this element, if any
    fn get_value(&self) -> Option<String> {
        let cell = self.tree.data.get().nodes[self.index];
        match cell.label.get_type() {
            TableType::Token => {
                let token = self.tree.get_token(cell.label.get_index());
                token.get_value()
            }
            _ => None
        }
    }
}

impl<'a> Display for AstNode<'a> {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        let cell = self.tree.data.get().nodes[self.index];
        match cell.label.get_type() {
            TableType::Token => {
                let token = self.tree.get_token(cell.label.get_index());
                let symbol = token.get_symbol();
                let value = token.get_value();
                write!(f, "{} = {}", symbol.name, value.unwrap())
            }
            TableType::Variable => {
                let symbol = self.tree.variables[cell.label.get_index()];
                write!(f, "{}", symbol.name)
            }
            TableType::Virtual => {
                let symbol = self.tree.virtuals[cell.label.get_index()];
                write!(f, "{}", symbol.name)
            }
            TableType::None => match self.tree.tokens {
                None => panic!("Missing token repository"),
                Some(ref repository) => {
                    let symbol = repository.get_terminals()[0];
                    write!(f, "{}", symbol.name)
                }
            }
        }
    }
}

/// Represents a family of children for an ASTNode
#[derive(Clone)]
pub struct AstFamily<'a> {
    /// The original parse tree
    tree: &'a Ast<'a>,
    /// The index of the parent node in the parse tree
    parent: usize
}

/// Represents and iterator for adjacents in this graph
pub struct AstFamilyIterator<'a> {
    /// The original parse tree
    tree: &'a Ast<'a>,
    /// The index of the current child in the parse tree
    current: usize,
    /// the index of the last child (excluded) in the parse tree
    end: usize
}

/// Implementation of the `Iterator` trait for `AstFamilyIterator`
impl<'a> Iterator for AstFamilyIterator<'a> {
    type Item = AstNode<'a>;
    fn next(&mut self) -> Option<Self::Item> {
        if self.current >= self.end {
            None
        } else {
            let result = AstNode {
                tree: self.tree,
                index: self.current
            };
            self.current += 1;
            Some(result)
        }
    }
}

/// Implementation of the `Iterable` trait for `AstFamily`
impl<'a> Iterable<'a> for AstFamily<'a> {
    type Item = AstNode<'a>;
    type IteratorType = AstFamilyIterator<'a>;
    fn iter(&'a self) -> Self::IteratorType {
        let cell = self.tree.data.get().nodes[self.parent];
        AstFamilyIterator {
            tree: self.tree,
            current: cell.first as usize,
            end: (cell.first + cell.count) as usize
        }
    }
}

impl<'a> AstFamily<'a> {
    /// Gets the number of children in this family
    pub fn len(&self) -> usize {
        self.tree.data.get().nodes[self.parent].count as usize
    }

    /// Gets the i-th child
    pub fn at(&self, index: usize) -> AstNode {
        let cell = self.tree.data.get().nodes[self.parent];
        AstNode {
            tree: self.tree,
            index: cell.first as usize + index
        }
    }
}