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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use crate::template::content::{START_PATTERN, END_PATTERN};
use std::fmt::{Debug, Display, Formatter};
use log::{debug, trace, log_enabled, Level};

/// Represents a node in an AST
///
/// Used to represent a template code for further evaluation.
///
/// Example:
/// `(plus 1 2)` can be represented as:
/// ```text
///     AnonymousNode {
///         starts_at: 0,
///         children: vec![
///              NamedNode {
///                  identifier: "plus".to_string(),
///                  starts_at: 1,
///                  children: vec![
///                      NamedNode {
///                          identifier: "1".to_string(),
///                          starts_at: 6,
///                          children: vec![],
///                      },
///                      NamedNode {
///                          identifier: "2".to_string(),
///                          starts_at: 8,
///                          children: vec![],
///                      },
///                  ],
///              },
///         ]
///     };
/// ```
///
#[derive(Clone, Debug, PartialEq)]
pub enum SyntaxNode {
    NamedNode {
        identifier: String,
        starts_at: Position,
        children: Vec<SyntaxNode>,
    },
    AnonymousNode {
        starts_at: Position,
        children: Vec<SyntaxNode>,
    },
}

/// Represents a position of node/symbol in template.
#[derive(Clone, Debug, PartialEq)]
pub enum Position {
    /// Used when there is no way to calculate the exact or approximate position.
    Unknown,
    /// Used to indicate a position relative to current function invocation.
    RelativeToInvocation(usize),
    /// Used to indicate a position relative to the start of currently evaluated block of code.
    RelativeToCodeStart(usize),
    /// Used to indicate an absolute position in current template.
    Absolute(usize),
}

impl Position {
    pub fn raw_value(&self) -> Option<usize> {
        match self {
            Position::Unknown => None,
            Position::RelativeToInvocation(pos) => Some(pos),
            Position::RelativeToCodeStart(pos) => Some(pos),
            Position::Absolute(pos) => Some(pos),
        }.cloned()
    }
}

impl Display for Position {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

/// Used for parsing AST for further evaluation.
///
/// This function tries to parse AST assuming it is Lisp-like syntax, which is practically
/// `function arg0 arg1 arg2`, where `function` is the function name, and `arg0...` are the arguments.
///
/// It also allows to use parenthesis to evaluate a nested function.
///
/// Reserved characters (cannot be used in names):
/// * ` ` - space
/// * `(` - left parenthesis
/// * `)` - right parenthesis
///
/// Examples:
/// * `(function 1 2 3)` - interpreted as `function` call with parameters `1`, `2` and `3`
/// * `plus 1 2 (times 3 4)` - interpreted as `1 + 2 + (3 * 4)`, given `plus` is an addition function and `times` is a multiplication function
///
pub fn parse_ast(source: &str) -> SyntaxNode {
    if log_enabled!(Level::Debug) {
        debug!("Starting to parse AST of: {}", source);
    }
    let source = source.strip_prefix(START_PATTERN).unwrap_or(source);
    let source = source.strip_suffix(END_PATTERN).unwrap_or(source);

    let result = next_node_of(source, 0, 0).0;
    if log_enabled!(Level::Debug) {
        debug!("Parsing complete, result: {:?}", result);
    }
    result
}

struct SyntaxScanResult(SyntaxNode, usize);

fn next_node_of(source: &str, offset: usize, level: usize) -> SyntaxScanResult {
    if log_enabled!(Level::Trace) {
        trace!("{:->width$}>{}", "", source, width = level);
    }
    let mut syntax_node = SyntaxNode::AnonymousNode {
        children: vec![],
        starts_at: Position::RelativeToCodeStart(offset),
    };
    let mut identifier = "".to_string();
    let mut string_started = false;
    let mut identifier_start: usize = offset;
    let mut skip_end: usize = 0;
    let mut source_length: usize = 0;

    for (index, char) in source.chars().enumerate() {
        let position = offset + index;
        source_length += 1;
        if position <= skip_end {
            continue;
        }
        let current_offset = index + 1;

        let (id, started) = extract_string(identifier, char, string_started);
        identifier = id;
        string_started = started;
        if string_started {
            continue;
        }

        if char == '(' {
            let (new_node, skip_pos) = start_node(syntax_node, &identifier, &source[current_offset..], identifier_start + 1, position, level);
            syntax_node = new_node;
            skip_end = skip_pos;
        } else {
            if char == ' ' || char == ')' {
                if let Some(node) = syntax_node.add_identifier_or_child(
                    &identifier,
                    identifier_start + 1,
                    level,
                ) {
                    syntax_node = node;
                }
                identifier.clear();
                identifier_start = position + 1;
            } else {
                identifier.push(char);
            }

            if char == ')' {
                if log_enabled!(Level::Trace) {
                    trace!("{:->width$}Parsing of the following fragment is complete (finished at {}): {}, result: {:?}", "", position, source, syntax_node, width = level);
                }
                return SyntaxScanResult(syntax_node, position);
            }
        }
    }

    let end_pos = offset + source_length;
    if log_enabled!(Level::Trace) {
        trace!("{:->width$}Parsing of the following fragment is complete (finished at {}): {}, result: {:?}", "", end_pos, source, syntax_node, width = level);
    }
    SyntaxScanResult(syntax_node, end_pos)
}

fn extract_string(identifier: String, char: char, string_started: bool) -> (String, bool) {
    let mut identifier = identifier;
    let mut string_started = string_started;

    if string_started && char != '"' {
        identifier.push(char);
    } else if string_started {
        string_started = false;
    } else if char == '"' {
        identifier.push(char);
        string_started = true;
    }

    (identifier, string_started)
}

fn start_node(syntax_node: SyntaxNode, identifier: &str, source_remainder: &str, identifier_start: usize, position: usize, level: usize) -> (SyntaxNode, usize) {
    let mut syntax_node = syntax_node;
    if let Some(node) = syntax_node.add_identifier_or_child(
        identifier,
        identifier_start,
        level,
    ) {
        syntax_node = node;
    }
    let SyntaxScanResult(child, skip_pos) = next_node_of(source_remainder, position, level + 1);
    syntax_node.add_child(child);

    (syntax_node, skip_pos)
}

impl SyntaxNode {
    fn add_child(&mut self, child: SyntaxNode) {
        match self {
            SyntaxNode::AnonymousNode { children, .. } => children.push(child),
            SyntaxNode::NamedNode { children, .. } => children.push(child),
        }
    }

    fn add_identifier_or_child(&self, new_identifier: &str, identifier_starts_at: usize, level: usize) -> Option<SyntaxNode> {
        if new_identifier.is_empty() {
            return None;
        }

        trace!("{:->width$}+\"{}\" at {}", "", new_identifier, identifier_starts_at, width = level);
        match self {
            SyntaxNode::AnonymousNode { children, .. } =>
                Some(SyntaxNode::NamedNode {
                    identifier: new_identifier.to_string(),
                    children: children.clone(),
                    starts_at: Position::RelativeToCodeStart(identifier_starts_at),
                }),

            SyntaxNode::NamedNode { identifier, children, starts_at } => {
                if log_enabled!(Level::Trace) {
                    trace!("{:->width$}-\"{}\" at {} (child of \"{}\" at {})", "", new_identifier, identifier_starts_at, identifier, starts_at, width = level);
                }
                let mut children = children.clone();
                children.push(SyntaxNode::NamedNode {
                    identifier: new_identifier.to_string(),
                    children: vec![],
                    starts_at: Position::RelativeToCodeStart(identifier_starts_at),
                });
                Some(SyntaxNode::NamedNode {
                    identifier: identifier.clone(),
                    children,
                    starts_at: starts_at.clone(),
                })
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::evaluator::ast::{parse_ast, Position};
    use crate::evaluator::ast::SyntaxNode::{AnonymousNode, NamedNode};
    use log::LevelFilter;

    fn init() {
        let _ = env_logger::builder()
            .filter_level(LevelFilter::Trace)
            .is_test(true)
            .try_init();
    }

    #[test]
    fn should_parse_ast() {
        init();

        let input = "{{ (list 1 2 (if a b c)) }}";
        let actual = parse_ast(input);

        let expected = AnonymousNode {
            starts_at: Position::RelativeToCodeStart(0),
            children: vec![
                NamedNode {
                    identifier: "list".to_string(),
                    starts_at: Position::RelativeToCodeStart(2),
                    children: vec![
                        NamedNode {
                            identifier: "1".to_string(),
                            children: vec![],
                            starts_at: Position::RelativeToCodeStart(7),
                        },
                        NamedNode {
                            identifier: "2".to_string(),
                            starts_at: Position::RelativeToCodeStart(9),
                            children: vec![],
                        },
                        NamedNode {
                            identifier: "if".to_string(),
                            starts_at: Position::RelativeToCodeStart(11),
                            children: vec![
                                NamedNode {
                                    identifier: "a".to_string(),
                                    starts_at: Position::RelativeToCodeStart(14),
                                    children: vec![],
                                },
                                NamedNode {
                                    identifier: "b".to_string(),
                                    starts_at: Position::RelativeToCodeStart(16),
                                    children: vec![],
                                },
                                NamedNode {
                                    identifier: "c".to_string(),
                                    starts_at: Position::RelativeToCodeStart(18),
                                    children: vec![],
                                },
                            ],
                        },
                    ],
                },
            ],
        };
        assert_eq!(expected, actual);
    }
}