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
//! A module organizing the romulus abstract syntax tree

use regex::Regex;

mod parse;

pub use parse::parse;

/// A pattern match
///
/// ```text
/// ["some ${var}", _, /abc/, 'xyz']
/// ```
#[derive(Debug, PartialEq)]
pub struct PatternMatch {
    /// The sub patterns to be matched against
    pub patterns: Vec<Pattern>,
}

/// A sub pattern of a pattern match
#[derive(Debug)]
pub enum Pattern {
    /// Regex pattern
    Regex(Box<Regex>),

    /// String Literal pattern
    String(String, bool),

    /// Identifier to bind to
    Identifier(String),
}

/// A match node which guard a body statement
///
/// one of the basic concepts in romulus is to trigger
/// a body of code when a condition is meet.
///
/// Here is the basic form
///
/// ```text
/// <match condition> {
///   <actions>
/// }
/// ```
///
#[derive(Debug)]
pub enum Match {
    /// The case where the first line should be matched
    ///
    /// ```text
    /// ^ {
    ///   print("start!")
    /// }
    /// ```
    Begin,

    /// The case where the last line should be matched
    ///
    /// ```text
    /// $ {
    ///   print("end!")
    /// }
    /// ```
    End,

    /// The case to run a statements when a line number is reached
    ///
    /// ```text
    /// 1 {
    ///   print("Begin of input")
    /// }
    /// ```
    Index(i64),

    /// The case to run statements when a line matches a regex
    ///
    /// ```text
    /// /(?P<type>struct|enum) +(?P<name>[_a-z0-9]+)/ {
    ///     print("${name} is a ${type}")
    /// }
    /// ```
    ///
    /// This program not only will print when romulus sees "struct SomeType",
    /// but it will also extract variables form the line and allow the statements to use
    /// elements of the line freely.
    ///
    /// This also nests when statements in the body are matches as well.
    Regex(Box<Regex>),
}

/// A range has two matches seperated by a comma
/// When the first one is matched all of the lines until the end is matched will
/// execute the body statement.
///
/// Ranges are start inclusive but end exclusive
///
/// When a start match is a regex and has capture variables it's variables are stored and
/// supplied for each next until the range ends
///
/// ```text
/// /start: (?P<type>.*)/, /end/{
///   /elem: (?P<elem>.*)/ {
///     print("${type}: ${elem}")
///   }
/// }
/// ```
///
#[derive(Debug, PartialEq)]
pub struct Range(pub Match, pub Match);

/// A selector is a switch for a guard
#[derive(Debug, PartialEq)]
pub enum Selector {
    /// A match is given
    ///
    /// ```text
    /// 1 {
    ///   print("round one, fight!")
    /// }
    /// ```
    Match(Match),

    /// A range is given
    /// ```text
    /// /BEGIN/,/END/ {
    ///   print() # would print initial BEGIN line
    /// }
    /// ```
    Range(Range),

    /// A pattern is given
    /// ```text
    /// [/none/, _, id] {
    ///   print()
    /// }
    /// ```
    Pattern(PatternMatch),
}

/// A expression
#[derive(Debug, PartialEq)]
pub enum Expression {
    /// A string expression literal
    String(String, bool),
    /// A variable to be resolved
    Identifier(String),
}

/// A statement
#[derive(Debug)]
pub enum Statement {
    /// Print the given expression
    Print(Expression),
    Quit,
    Subst(Box<Regex>, Expression),
    Gsubst(Box<Regex>, Expression),
    Read(Expression),
    Write(Expression),
    Exec(Expression),

    /// Appends the value of the expression to the line
    Append(Expression),

    /// Sets the current line to an expression
    Set(Expression),
}

/// A guarded statement or a plain one
#[derive(Debug, PartialEq)]
pub enum Body {
    Bare(Statement),
    Single(Selector, Statement),
    Guard(Selector, Seq),
}

/// Contains multiple sub nodes
#[derive(Debug, PartialEq)]
pub struct Seq {
    pub subnodes: Vec<Body>,
    pub(crate) toplevel: bool,
}

impl PartialEq for Match {
    fn eq(&self, other: &Match) -> bool {
        match (self, other) {
            (Match::Index(a), Match::Index(b)) => a == b,
            (Match::Regex(a), Match::Regex(b)) => a.to_string() == b.to_string(),
            (Match::Begin, Match::Begin) => true,
            (Match::End, Match::End) => true,
            _ => false,
        }
    }
}

impl PartialEq for Pattern {
    fn eq(&self, other: &Pattern) -> bool {
        match (self, other) {
            (Pattern::Regex(a), Pattern::Regex(b)) => a.to_string() == b.to_string(),
            (Pattern::String(ss, si), Pattern::String(os, oi)) => ss == os && si == oi,
            (Pattern::Identifier(a), Pattern::Identifier(b)) => a == b,

            _ => false,
        }
    }
}

impl PartialEq for Statement {
    fn eq(&self, other: &Statement) -> bool {
        match (self, other) {
            (Statement::Quit, Statement::Quit) => true,
            (Statement::Print(se), Statement::Print(oe)) => se == oe,
            (Statement::Subst(sr, se), Statement::Subst(or, oe)) => {
                sr.to_string() == or.to_string() && se == oe
            }
            (Statement::Gsubst(sr, se), Statement::Gsubst(or, oe)) => {
                sr.to_string() == or.to_string() && se == oe
            }
            (Statement::Read(se), Statement::Read(oe)) => se == oe,
            (Statement::Write(se), Statement::Write(oe)) => se == oe,
            (Statement::Exec(se), Statement::Exec(oe)) => se == oe,
            (Statement::Append(se), Statement::Append(oe)) => se == oe,
            (Statement::Set(se), Statement::Set(oe)) => se == oe,
            _ => false,
        }
    }
}