lunarity_ast/
lib.rs

1extern crate toolshed;
2
3#[cfg(test)]
4#[macro_use]
5extern crate pretty_assertions;
6
7#[macro_use]
8mod impl_from;
9mod node;
10mod source;
11mod contract;
12mod function;
13mod type_name;
14mod expression;
15mod statement;
16mod assembly;
17
18use toolshed::list::{List, UnsafeList};
19use toolshed::Arena;
20use std::marker::PhantomData;
21
22pub use self::node::{Node, NodeInner, OptionalLocation};
23pub use self::source::*;
24pub use self::contract::*;
25pub use self::function::*;
26pub use self::type_name::*;
27pub use self::expression::*;
28pub use self::statement::*;
29pub use self::assembly::*;
30
31/// Useful for boolean flags that need location information via FlagNode,
32/// for example: `indexed` or `anonymous`.
33#[derive(Clone, Copy, Debug, PartialEq)]
34pub struct Flag;
35
36pub type Identifier<'ast> = &'ast str;
37pub type StringLiteral<'ast> = &'ast str;
38pub type VersionLiteral<'ast> = &'ast str;
39
40pub type FlagNode<'ast> = Node<'ast, Flag>;
41pub type NodeList<'ast, T> = List<'ast, Node<'ast, T>>;
42pub type SourceUnitNode<'ast> = Node<'ast, SourceUnit<'ast>>;
43pub type SourceUnitList<'ast> = NodeList<'ast, SourceUnit<'ast>>;
44pub type IdentifierNode<'ast> = Node<'ast, Identifier<'ast>>;
45pub type IdentifierList<'ast> = NodeList<'ast, Identifier<'ast>>;
46pub type StringLiteralNode<'ast> = Node<'ast, StringLiteral<'ast>>;
47
48
49/// A Solidity source code parsed to an AST
50pub struct Program<'ast> {
51    /// `SourceUnitList<'ast>` converted to an `UnsafeList` to deal with
52    /// the fact that the `Arena` on which it lives is also in this struct.
53    body: UnsafeList,
54
55    /// `Arena` on which the entire AST is allocated.
56    arena: Arena,
57
58    /// For lifetime safety :).
59    _phantom: PhantomData<SourceUnitList<'ast>>
60}
61
62impl<'ast> Program<'ast> {
63    #[inline]
64    pub fn new(body: UnsafeList, arena: Arena) -> Self {
65        Program {
66            body,
67            arena,
68            _phantom: PhantomData,
69        }
70    }
71
72    /// Get the list of `SourceUnit`s.
73    #[inline]
74    pub fn body(&self) -> SourceUnitList<'ast> {
75        unsafe { self.body.into_list() }
76    }
77
78    /// Get a reference to the `Arena` on which the AST is allocated.
79    #[inline]
80    pub fn arena(&'ast self) -> &'ast Arena {
81        &self.arena
82    }
83}