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
#[macro_use]
mod variable;
mod operator;
// mod types;
mod function;
mod literal;
pub mod node;
pub mod expression;
pub mod statement;

use toolshed::list::List;
use std::ops::Deref;

pub use crate::ast::variable::*;
pub use crate::ast::operator::*;
pub use crate::ast::node::Node;
// pub use ast::types::{Type, Primitive};
pub use crate::ast::expression::{Expression, Property, PropertyKey};
pub use crate::ast::statement::{Statement, Declarator, BlockStatement};
pub use crate::ast::function::{Function, Class, ClassMember, Method, MethodKind};
pub use crate::ast::function::{Name, EmptyName, OptionalName, MandatoryName};
pub use crate::ast::literal::Literal;


#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Block<'ast, T> {
    pub body: NodeList<'ast, T>
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Pattern<'ast> {
    /// Only used inside ArrayPattern
    Void,
    Identifier(Identifier<'ast>),
    ObjectPattern {
        properties: NodeList<'ast, Property<'ast>>
    },
    ArrayPattern {
        elements: NodeList<'ast, Pattern<'ast>>
    },
    RestElement {
        argument: IdentifierNode<'ast>
    },
    AssignmentPattern {
        left: Node<'ast, Pattern<'ast>>,
        right: ExpressionNode<'ast>,
    }
}

// Handful of useful aliases
pub type Identifier<'ast> = &'ast str;
pub type NodeList<'ast, T> = List<'ast, Node<'ast, T>>;
pub type BlockNode<'ast, T> = Node<'ast, Block<'ast, T>>;
pub type PatternList<'ast> = NodeList<'ast, Pattern<'ast>>;
pub type PropertyNode<'ast> = Node<'ast, Property<'ast>>;
pub type ExpressionNode<'ast> = Node<'ast, Expression<'ast>>;
pub type ExpressionList<'ast> = NodeList<'ast, Expression<'ast>>;
pub type StatementNode<'ast> = Node<'ast, Statement<'ast>>;
pub type StatementList<'ast> = NodeList<'ast, Statement<'ast>>;
pub type IdentifierNode<'ast> = Node<'ast, &'ast str>;
pub type IdentifierList<'ast> = NodeList<'ast, &'ast str>;
// pub type TypeNode<'ast> = NodeList<'ast, Type<'ast>>;
// pub type TypeList<'ast> = NodeList<'ast, Type<'ast>>;

#[derive(Debug, Clone)]
pub struct Loc<T> {
    pub start: u32,
    pub end: u32,
    pub item: T,
}

impl<T: Copy> Copy for Loc<T> {}

impl<T> Deref for Loc<T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &T {
        &self.item
    }
}

pub struct Program<'ast> {
    pub source: &'ast str,
    pub body: NodeList<'ast, Statement<'ast>>,
}

impl<T> Loc<T> {
    #[inline]
    pub fn new(start: u32, end: u32, item: T) -> Self {
        Loc {
            start,
            end,
            item,
        }
    }
}

impl<T: PartialEq> PartialEq for Loc<T> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.item.eq(&other.item)
    }
}

impl<'ast> Program<'ast> {
    #[inline]
    pub fn statements(&'ast self) -> &'ast NodeList<'ast, Statement<'ast>> {
        &self.body
    }
}