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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright 2017 Jeremy Wall <jeremy@marzhillstudios.com>
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

//! Bottom up parser for precedence parsing of expressions separated by binary
//! operators.
use abortable_parser::combinators::eoi;
use abortable_parser::{Error, Peekable, Result, SliceIter};

use super::{non_op_expression, ParseResult};
use crate::ast::*;

macro_rules! abort_on_end {
    ($i:expr) => {{
        if eoi($i.clone()).is_complete() {
            return Result::Fail(Error::new(
                format!("Expected Expression found End Of Input"),
                Box::new($i.clone()),
            ));
        }
    }};
}

/// Defines the intermediate stages of our bottom up parser for precedence parsing.
#[derive(Debug, PartialEq, Clone)]
pub enum Element {
    Expr(Expression),
    Op(BinaryExprType),
}

make_fn!(
    dot_op_type<SliceIter<Token>, Element>,
    do_each!(
        _ => punct!("."),
        (Element::Op(BinaryExprType::DOT)))
);

make_fn!(
    math_op_type<SliceIter<Token>, Element>,
    either!(
        do_each!(
            _ => punct!("+"),
            (Element::Op(BinaryExprType::Add))),
        do_each!(
            _ => punct!("-"),
            (Element::Op(BinaryExprType::Sub))),
        do_each!(
            _ => punct!("*"),
            (Element::Op(BinaryExprType::Mul))),
        do_each!(
            _ => punct!("/"),
            (Element::Op(BinaryExprType::Div))),
        do_each!(
            _ => punct!("%%"),
            (Element::Op(BinaryExprType::Mod)))
    )
);

make_fn!(
    bool_op_type<SliceIter<Token>, Element>,
    either!(
        do_each!(
            _ => punct!("&&"),
            (Element::Op(BinaryExprType::AND))),
        do_each!(
            _ => punct!("||"),
            (Element::Op(BinaryExprType::OR)))
    )
);

fn parse_expression(i: SliceIter<Element>) -> Result<SliceIter<Element>, Expression> {
    let mut i_ = i.clone();
    abort_on_end!(i_);
    let el = i_.next();
    if let Some(&Element::Expr(ref expr)) = el {
        return Result::Complete(i_, expr.clone());
    }
    return Result::Fail(Error::new(
        format!(
            "Error while parsing Binary Expression Expected Expression got {:?}",
            el
        ),
        Box::new(i_),
    ));
}

fn parse_bool_operator(i: SliceIter<Element>) -> Result<SliceIter<Element>, BinaryExprType> {
    let mut i_ = i.clone();
    abort_on_end!(i_);
    let el = i_.next();
    if let Some(&Element::Op(ref op)) = el {
        match op {
            BinaryExprType::AND | BinaryExprType::OR => {
                return Result::Complete(i_, op.clone());
            }
            _other => {
                // noop
            }
        };
    }
    return Result::Fail(Error::new(
        format!(
            "Error while parsing Binary Expression Unexpected Operator {:?}",
            el
        ),
        Box::new(i_),
    ));
}

fn parse_dot_operator(i: SliceIter<Element>) -> Result<SliceIter<Element>, BinaryExprType> {
    let mut i_ = i.clone();
    abort_on_end!(i_);
    let el = i_.next();
    if let Some(&Element::Op(ref op)) = el {
        match op {
            &BinaryExprType::DOT => {
                return Result::Complete(i_, op.clone());
            }
            _other => {
                // noop
            }
        };
    }
    return Result::Fail(Error::new(
        format!(
            "Error while parsing Binary Expression Unexpected Operator {:?}",
            el
        ),
        Box::new(i_),
    ));
}

fn parse_sum_operator(i: SliceIter<Element>) -> Result<SliceIter<Element>, BinaryExprType> {
    let mut i_ = i.clone();
    abort_on_end!(i_);
    let el = i_.next();
    if let Some(&Element::Op(ref op)) = el {
        match op {
            &BinaryExprType::Add => {
                return Result::Complete(i_, op.clone());
            }
            &BinaryExprType::Sub => {
                return Result::Complete(i_, op.clone());
            }
            _other => {
                // noop
            }
        };
    }
    return Result::Fail(Error::new(
        format!(
            "Error while parsing Binary Expression Unexpected Operator {:?}",
            el
        ),
        Box::new(i_),
    ));
}

fn parse_product_operator(i: SliceIter<Element>) -> Result<SliceIter<Element>, BinaryExprType> {
    let mut i_ = i.clone();
    abort_on_end!(i_);
    let el = i_.next();
    if let Some(&Element::Op(ref op)) = el {
        match op {
            &BinaryExprType::Mul => {
                return Result::Complete(i_, op.clone());
            }
            &BinaryExprType::Div => {
                return Result::Complete(i_, op.clone());
            }
            &BinaryExprType::Mod => {
                return Result::Complete(i_, op.clone());
            }
            _other => {
                // noop
            }
        };
    }
    return Result::Fail(Error::new(
        format!(
            "Error while parsing Binary Expression Unexpected Operator {:?}",
            el
        ),
        Box::new(i_),
    ));
}

make_fn!(
    compare_op_type<SliceIter<Token>, Element>,
    either!(
        do_each!(_ => punct!("=="), (Element::Op(BinaryExprType::Equal))),
        do_each!(_ => punct!("!="), (Element::Op(BinaryExprType::NotEqual))),
        do_each!(_ => punct!("~"), (Element::Op(BinaryExprType::REMatch))),
        do_each!(_ => punct!("!~"), (Element::Op(BinaryExprType::NotREMatch))),
        do_each!(_ => punct!("<="), (Element::Op(BinaryExprType::LTEqual))),
        do_each!(_ => punct!(">="), (Element::Op(BinaryExprType::GTEqual))),
        do_each!(_ => punct!("<"),  (Element::Op(BinaryExprType::LT))),
        do_each!(_ => punct!(">"),  (Element::Op(BinaryExprType::GT))),
        do_each!(_ => word!("in"),  (Element::Op(BinaryExprType::IN))),
        do_each!(_ => word!("is"),  (Element::Op(BinaryExprType::IS)))
    )
);

fn parse_compare_operator(i: SliceIter<Element>) -> Result<SliceIter<Element>, BinaryExprType> {
    let mut i_ = i.clone();
    abort_on_end!(i_);
    let el = i_.next();
    if let Some(&Element::Op(ref op)) = el {
        match op {
            &BinaryExprType::GT
            | &BinaryExprType::GTEqual
            | &BinaryExprType::LT
            | &BinaryExprType::LTEqual
            | &BinaryExprType::NotEqual
            | &BinaryExprType::REMatch
            | &BinaryExprType::NotREMatch
            | &BinaryExprType::Equal
            | &BinaryExprType::IS
            | &BinaryExprType::IN => {
                return Result::Complete(i_, op.clone());
            }
            _other => {
                // noop
            }
        };
    }
    return Result::Fail(Error::new(
        format!(
            "Error while parsing Binary Expression Unexpected Operator {:?}",
            el
        ),
        Box::new(i),
    ));
}

/// Parse a list of expressions separated by operators into a Vec<Element>.
fn parse_operand_list<'a>(i: SliceIter<'a, Token>) -> ParseResult<'a, Vec<Element>> {
    // 1. First try to parse a non_op_expression,
    let mut _i = i.clone();
    let mut list = Vec::new();
    // 1. loop
    let mut firstrun = true;
    loop {
        // 2. Parse a non_op_expression.
        match non_op_expression(_i.clone()) {
            Result::Fail(e) => {
                // A failure to parse an expression
                // is always an error.
                if !firstrun {
                    // if we have successfully parsed an element and an operator then
                    // failing to parse a second expression is an abort since we know
                    // for sure now that the next expression is supposed to be there.
                    let err = Error::new("Missing operand for binary expression", Box::new(_i));
                    return Result::Abort(err);
                }
                return Result::Fail(e);
            }
            Result::Abort(e) => {
                // A failure to parse an expression
                // is always an error.
                return Result::Abort(e);
            }
            Result::Incomplete(i) => {
                return Result::Incomplete(i);
            }
            Result::Complete(rest, expr) => {
                list.push(Element::Expr(expr));
                _i = rest.clone();
            }
        }
        // 3. Parse an operator.
        match either!(
            _i.clone(),
            dot_op_type,
            math_op_type,
            compare_op_type,
            bool_op_type
        ) {
            Result::Fail(e) => {
                if firstrun {
                    // If we don't find an operator in our first
                    // run then this is not an operand list.
                    return Result::Fail(e);
                }
                // if we don't find one on subsequent runs then
                // that's the end of the operand list.
                break;
            }
            Result::Abort(e) => {
                // A failure to parse an operator
                // is always an error.
                return Result::Abort(e);
            }
            Result::Incomplete(i) => {
                return Result::Incomplete(i);
            }
            Result::Complete(rest, el) => {
                list.push(el);
                _i = rest.clone();
            }
        }
        firstrun = false;
    }
    return Result::Complete(_i, list);
}

make_fn!(
    parse_operator_element<SliceIter<Element>, BinaryExprType>,
    either!(
        parse_dot_operator,
        parse_sum_operator,
        parse_product_operator,
        parse_compare_operator,
        parse_bool_operator
    )
);

macro_rules! try_parse {
    ($r:expr) => {
        match $r {
            Result::Abort(e) => return Result::Abort(e),
            Result::Fail(e) => return Result::Fail(e),
            Result::Incomplete(i) => return Result::Incomplete(i),
            Result::Complete(rest, op_type) => (rest, op_type),
        }
    };
}

fn parse_op(
    mut lhs: Expression,
    mut i: SliceIter<Element>,
    min_precedence: u32,
) -> Result<SliceIter<Element>, Expression> {
    // Termination condition
    if eoi(i.clone()).is_complete() {
        return Result::Complete(i, lhs);
    }
    let (_, mut lookahead_op) = try_parse!(parse_operator_element(i.clone()));
    while !eoi(i.clone()).is_complete() && (lookahead_op.precedence_level() >= min_precedence) {
        // Stash a copy of our lookahead operator for future use.
        let op = lookahead_op.clone();
        // Advance to next element.
        i.next();
        let (rest, mut rhs) = try_parse!(parse_expression(i.clone()));
        i = rest;
        if !eoi(i.clone()).is_complete() {
            let (_, peek_op) = try_parse!(parse_operator_element(i.clone()));
            lookahead_op = peek_op;
        }
        while !eoi(i.clone()).is_complete()
            && (lookahead_op.precedence_level() > op.precedence_level())
        {
            let (rest, inner_rhs) =
                try_parse!(parse_op(rhs, i.clone(), lookahead_op.precedence_level()));
            i = rest;
            rhs = inner_rhs;
            // Before we check for another operator we should see
            // if we are at the end of the input.
            if eoi(i.clone()).is_complete() {
                break;
            }
            let (_, peek_op) = try_parse!(parse_operator_element(i.clone()));
            lookahead_op = peek_op;
        }
        let pos = lhs.pos().clone();
        lhs = Expression::Binary(BinaryOpDef {
            kind: op.clone(),
            left: Box::new(lhs.clone()),
            right: Box::new(rhs),
            pos: pos,
        });
    }
    return Result::Complete(i, lhs);
}

pub fn parse_precedence(i: SliceIter<Element>) -> Result<SliceIter<Element>, Expression> {
    match parse_expression(i) {
        Result::Abort(e) => Result::Abort(e),
        Result::Fail(e) => Result::Fail(e),
        Result::Incomplete(i) => Result::Incomplete(i),
        Result::Complete(rest, expr) => parse_op(expr, rest, 0),
    }
}

/// Parse a binary operator expression.
pub fn op_expression<'a>(i: SliceIter<'a, Token>) -> Result<SliceIter<Token>, Expression> {
    let preparse = parse_operand_list(i.clone());
    match preparse {
        Result::Fail(e) => Result::Fail(e),
        Result::Abort(e) => Result::Abort(e),
        Result::Incomplete(i) => Result::Incomplete(i),
        Result::Complete(rest, oplist) => {
            let i_ = SliceIter::new(&oplist);
            let parse_result = parse_precedence(i_);
            match parse_result {
                Result::Fail(e) => {
                    let err = Error::new(e.get_msg(), Box::new(rest.clone()));
                    Result::Fail(err)
                }
                Result::Abort(e) => {
                    let err = Error::new(e.get_msg(), Box::new(rest.clone()));
                    Result::Abort(err)
                }
                Result::Incomplete(_) => Result::Incomplete(i.clone()),
                Result::Complete(rest_ops, expr) => {
                    if rest_ops.peek_next().is_some() {
                        panic!("premature abort parsing Operator expression!");
                    }
                    Result::Complete(rest.clone(), expr)
                }
            }
        }
    }
}