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
pub use self::expr::*;
pub use self::stmt::*;

pub mod expr;
pub mod stmt;

use std::iter::FromIterator;

/// A Ruby program.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Program
{
    pub items: Vec<Item>,
}

/// An item.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Item
{
    Module(Module),
    Class(Class),
    Function(Function),
    Stmt(Stmt),
}

/// A module.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Module
{
    /// The name of the module.
    pub name: String,
    /// The items contained in the module.
    pub items: Vec<Item>,
}

/// A class.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Class
{
    /// The name of the class.
    pub name: String,
    /// The items contained in the class.
    pub items: Vec<Item>,
    /// The parent class.
    pub superclass: Option<Path>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Function
{
    /// The name of the function.
    pub name: String,
    /// The statements in the function.
    pub statements: Vec<Stmt>,
}

/// An identifier.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Identifier(pub String);

/// A constant.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Constant(pub String);

/// A list of identifiers separated by periods.
///
/// `my.object.do_thing`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Path
{
    /// The parts that make up the path.
    pub parts: Vec<PathSegment>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PathSegment
{
    pub kind: PathSegmentKind,
    pub separator: PathSeparator,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PathSegmentKind
{
    Constant(Constant),
    Identifier(Identifier),
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum PathSeparator
{
    /// The root part of a path.
    /// Should only be used on the very first segment.
    Root,
    Dot,
    DoubleColon,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Argument
{
    /// A standard positional argument.
    Positional(Expr),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Parameter
{
    pub name: String,
    pub default: Option<Box<Expr>>,
}

impl Program
{
    pub fn new() -> Self {
        Program { items: Vec::new() }
    }
}

impl Module
{
    pub fn new<S>(name: S) -> Self where S: Into<String> {
        Module { name: name.into(), items: Vec::new() }
    }
}

impl Class
{
    pub fn new<S>(name: S) -> Self where S: Into<String> {
        Class { name: name.into(), items: Vec::new(), superclass: None }
    }
}

impl PathSegmentKind
{
    pub fn new(text: String) -> Self {
        if text.chars().next().unwrap().is_uppercase() {
            PathSegmentKind::Constant(Constant(text))
        } else {
            PathSegmentKind::Identifier(Identifier(text))
        }
    }
}

impl Into<Item> for Class { fn into(self) -> Item { Item::Class(self) } }
impl Into<Item> for Module { fn into(self) -> Item { Item::Module(self) } }
impl Into<Item> for Function { fn into(self) -> Item { Item::Function(self) } }
impl Into<Item> for Stmt { fn into(self) -> Item { Item::Stmt(self) } }

impl FromIterator<PathSegment> for Path
{
    fn from_iter<T>(it: T) -> Self where T: IntoIterator<Item=PathSegment> {
        Path { parts: it.into_iter().collect() }
    }
}