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
extern crate toolshed;

#[cfg(test)]
#[macro_use]
extern crate pretty_assertions;

#[macro_use]
mod impl_from;
mod node;
mod source;
mod contract;
mod function;
mod type_name;
mod expression;
mod statement;
mod assembly;

use toolshed::list::{List, UnsafeList};
use toolshed::Arena;
use std::marker::PhantomData;

pub use self::node::{Node, NodeInner, OptionalLocation};
pub use self::source::*;
pub use self::contract::*;
pub use self::function::*;
pub use self::type_name::*;
pub use self::expression::*;
pub use self::statement::*;
pub use self::assembly::*;

/// Useful for boolean flags that need location information via FlagNode,
/// for example: `indexed` or `anonymous`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Flag;

pub type Identifier<'ast> = &'ast str;
pub type StringLiteral<'ast> = &'ast str;
pub type VersionLiteral<'ast> = &'ast str;

pub type FlagNode<'ast> = Node<'ast, Flag>;
pub type NodeList<'ast, T> = List<'ast, Node<'ast, T>>;
pub type SourceUnitNode<'ast> = Node<'ast, SourceUnit<'ast>>;
pub type SourceUnitList<'ast> = NodeList<'ast, SourceUnit<'ast>>;
pub type IdentifierNode<'ast> = Node<'ast, Identifier<'ast>>;
pub type IdentifierList<'ast> = NodeList<'ast, Identifier<'ast>>;
pub type StringLiteralNode<'ast> = Node<'ast, StringLiteral<'ast>>;


/// A Solidity source code parsed to an AST
pub struct Program<'ast> {
    /// `SourceUnitList<'ast>` converted to an `UnsafeList` to deal with
    /// the fact that the `Arena` on which it lives is also in this struct.
    body: UnsafeList,

    /// `Arena` on which the entire AST is allocated.
    arena: Arena,

    /// For lifetime safety :).
    _phantom: PhantomData<SourceUnitList<'ast>>
}

impl<'ast> Program<'ast> {
    #[inline]
    pub fn new(body: UnsafeList, arena: Arena) -> Self {
        Program {
            body,
            arena,
            _phantom: PhantomData,
        }
    }

    /// Get the list of `SourceUnit`s.
    #[inline]
    pub fn body(&self) -> SourceUnitList<'ast> {
        unsafe { self.body.into_list() }
    }

    /// Get a reference to the `Arena` on which the AST is allocated.
    #[inline]
    pub fn arena(&'ast self) -> &'ast Arena {
        &self.arena
    }
}