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
//! This crate provides the `jmespath!` macro used to statically
//! compile JMESPath expressions.
//!
//! By statically compiling JMESPath expressions, you pay the cost of
//! parsing and compiling JMESPath expressions at compile time rather
//! than at runtime, and you can be sure that the expression is valid
//! if your program compiles.
//!
//! Note: This only works with a nightly compiler.
//!
//! ```
//! #![feature(plugin)]
//!
//! #![plugin(jmespath-macros)]
//! extern crate jmespath;
//!
//! fn main() {
//!     // Create our statically compiled expression. The build will fail
//!     // if the expression is invalid.
//!     let expr = jmespath!("foo.bar");
//!
//!     // Parse some JSON data into a JMESPath variable
//!     let json_str = "{\"foo\":{\"bar\":true}}";
//!     let data = jmespath::Variable::from_json(json_str).unwrap();
//!     let result = expr.search(data).unwrap();
//!     assert_eq!(true, result.as_boolean().unwrap());
//! }
//! ```

#![crate_type="dylib"]
#![feature(plugin_registrar, quote, rustc_private)]

extern crate syntax;
extern crate rustc;
extern crate rustc_plugin;

extern crate jmespath;

use syntax::ast;
use syntax::codemap;
use syntax::ext::base::{ExtCtxt, MacResult, MacEager, DummyResult};
use syntax::parse::token;
use syntax::print::pprust;
use syntax::tokenstream;
use syntax::fold::Folder;
use rustc_plugin::Registry;
use syntax::ptr::P;

use jmespath::{Variable, Rcvar};
use jmespath::ast::{Ast, Comparator};

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
    reg.register_macro("jmespath", expand_jp);
}

fn expand_jp(cx: &mut ExtCtxt,
             sp: codemap::Span,
             tts: &[tokenstream::TokenTree])
             -> Box<MacResult + 'static> {
    // Parse the arguments of the macro.
    let expression_str = match parse(cx, tts) {
        Some(e) => e,
        None => return DummyResult::any(sp),
    };

    // Parse the expression and show an error if needed.
    let ast = match jmespath::parse(&expression_str) {
        Ok(e) => e,
        Err(err) => {
            cx.span_err(sp, &format!("jmespath! error: {}", err));
            return DummyResult::any(sp);
        }
    };

    let jmespath_ast = generate_ast(cx, &ast);

    MacEager::expr(quote_expr!(cx, {
        use ::jmespath::ast::Ast;
        use ::jmespath::{Expression, DEFAULT_RUNTIME};
        Expression::new($expression_str, $jmespath_ast, &DEFAULT_RUNTIME)
    }))
}

/// Looks for a single string literal and returns it.
///
/// Based on rust-regex macro: https://github.com/rust-lang-nursery/regex
fn parse(cx: &mut ExtCtxt, tts: &[tokenstream::TokenTree]) -> Option<String> {
    let mut parser = cx.new_parser_from_tts(tts);
    if let Ok(expr) = parser.parse_expr() {
        let entry = cx.expander().fold_expr(expr);
        let expr = match entry.node {
            ast::ExprKind::Lit(ref lit) => {
                match lit.node {
                    ast::LitKind::Str(ref s, _) => s.to_string(),
                    _ => {
                        let message = &format!("expected string literal but got `{}`",
                                               pprust::lit_to_string(&**lit));
                        cx.span_err(entry.span, message);
                        return None;
                    }
                }
            }
            _ => {
                let message = &format!("expected string literal but got `{}`",
                                       pprust::expr_to_string(&*entry));
                cx.span_err(entry.span, message);
                return None;
            }
        };
        if !parser.eat(&token::Eof) {
            cx.span_err(parser.span, "only one string literal allowed");
            return None;
        }
        Some(expr)
    } else {
        cx.parse_sess().span_diagnostic.err("failure parsing token tree");
        None
    }
}

/// Creates a Vec that contains a number of generated Ast elements.
fn generate_vec_ast(cx: &mut ExtCtxt, elements: &Vec<Ast>) -> P<ast::Expr> {
    // Creates the AST expressions necessary for pushing each node onto the Vec.
    let elements_ast = elements.iter()
        .fold(quote_expr!(cx, {}), |acc, element| {
            let element_expr = generate_ast(cx, &element);
            quote_expr!(cx, {
                $acc;
                nodes.push($element_expr);
            })
        });
    quote_expr!(cx, {
        let mut nodes = Vec::new();
        $elements_ast;
        nodes
    })
}

/// Creates the AST nodes for a Comparator.
fn generate_comparator_ast(cx: &mut ExtCtxt, comparator: &Comparator) -> P<ast::Expr> {
    match *comparator {
        Comparator::Equal => quote_expr!(cx, {::jmespath::ast::Comparator::Equal}),
        Comparator::NotEqual => quote_expr!(cx, {::jmespath::ast::Comparator::NotEqual}),
        Comparator::GreaterThan => quote_expr!(cx, {::jmespath::ast::Comparator::GreaterThan}),
        Comparator::GreaterThanEqual => {
            quote_expr!(cx, {::jmespath::ast::Comparator::GreaterThanEqual})
        }
        Comparator::LessThan => quote_expr!(cx, {::jmespath::ast::Comparator::LessThan}),
        Comparator::LessThanEqual => quote_expr!(cx, {::jmespath::ast::Comparator::LessThanEqual}),
    }
}

/// Generates the Rust AST expression nodes for each JMESPath AST node.
fn generate_ast(cx: &mut ExtCtxt, ast: &Ast) -> P<ast::Expr> {
    use jmespath::ast::Ast::*;
    match *ast {
        Field { offset, ref name } => {
            quote_expr!(cx, Ast::Field {
                offset: $offset,
                name: $name.to_owned()
            })
        }
        Subexpr { offset, ref lhs, ref rhs } => {
            let left = generate_ast(cx, lhs);
            let right = generate_ast(cx, rhs);
            quote_expr!(cx, Ast::Subexpr {
                offset: $offset,
                lhs: Box::new($left),
                rhs: Box::new($right)
            })
        }
        Index { offset, idx } => {
            quote_expr!(cx, Ast::Index {
                offset: $offset,
                idx: $idx
            })
        }
        Condition { offset, ref predicate, ref then } => {
            let predicate = generate_ast(cx, &*predicate);
            let then = generate_ast(cx, then);
            quote_expr!(cx, Ast::Condition {
                offset: $offset,
                predicate: $predicate,
                then: $then
            })
        }
        Identity { offset } => {
            quote_expr!(cx, Ast::Identity {
                offset: $offset
            })
        }
        Expref { offset, ref ast } => {
            let inner = generate_ast(cx, ast);
            quote_expr!(cx, Ast::Expref {
                offset: $offset,
                ast: Box::new($inner)
            })
        }
        Flatten { offset, ref node } => {
            let inner = generate_ast(cx, node);
            quote_expr!(cx, Ast::Flatten {
                offset: $offset,
                node: Box::new($inner)
            })
        }
        Not { offset, ref node } => {
            let inner = generate_ast(cx, node);
            quote_expr!(cx, Ast::Not {
                offset: $offset,
                node: Box::new($inner)
            })
        }
        Projection { offset, ref lhs, ref rhs } => {
            let left = generate_ast(cx, lhs);
            let right = generate_ast(cx, rhs);
            quote_expr!(cx, Ast::Projection {
                offset: $offset,
                lhs: Box::new($left),
                rhs: Box::new($right)
            })
        }
        ObjectValues { offset, ref node } => {
            let inner = generate_ast(cx, node);
            quote_expr!(cx, Ast::ObjectValues {
                offset: $offset,
                node: Box::new($inner)
            })
        }
        And { offset, ref lhs, ref rhs } => {
            let left = generate_ast(cx, lhs);
            let right = generate_ast(cx, rhs);
            quote_expr!(cx, Ast::And {
                offset: $offset,
                lhs: Box::new($left),
                rhs: Box::new($right)
            })
        }
        Or { offset, ref lhs, ref rhs } => {
            let left = generate_ast(cx, lhs);
            let right = generate_ast(cx, rhs);
            quote_expr!(cx, Ast::Or {
                offset: $offset,
                lhs: Box::new($left),
                rhs: Box::new($right)
            })
        }
        Slice { offset, start, stop, step } => {
            let start = generate_slice_option_ast(cx, start);
            let stop = generate_slice_option_ast(cx, stop);
            quote_expr!(cx, Ast::Slice {
                offset: $offset,
                start: $start,
                stop: $stop,
                step: $step
            })
        }
        Comparison { offset, ref comparator, ref lhs, ref rhs } => {
            let left = generate_ast(cx, lhs);
            let right = generate_ast(cx, rhs);
            let comparator_ast = generate_comparator_ast(cx, comparator);
            quote_expr!(cx, Ast::Comparison {
                offset: $offset,
                comparator: $comparator_ast,
                lhs: Box::new($left),
                rhs: Box::new($right)
            })
        }
        Function { offset, ref name, ref args } => {
            let elements_ast = generate_vec_ast(cx, args);
            quote_expr!(cx, {
                Ast::Function {
                    offset: $offset,
                    name: $name.to_owned(),
                    args: $elements_ast,
                }
            })
        }
        MultiList { offset, ref elements } => {
            let elements_ast = generate_vec_ast(cx, elements);
            quote_expr!(cx, {
                Ast::MultiList {
                    offset: $offset,
                    elements: $elements_ast,
                }
            })
        }
        MultiHash { offset, ref elements } => {
            // Create the AST nodes for inserting each key value pair into the MultiHash map.
            let elements_ast = elements.iter()
                .fold(quote_expr!(cx, {}), |acc, element| {
                    let element_key = element.key.to_owned();
                    let element_expr = generate_ast(cx, &element.value);
                    quote_expr!(cx, {
                        $acc;
                        nodes.push(::jmespath::ast::KeyValuePair {
                            key: $element_key.to_owned(),
                            value: $element_expr,
                        });
                    })
                });
            quote_expr!(cx, {
                Ast::MultiHash {
                    offset: $offset,
                    elements: {
                        let mut nodes = Vec::new();
                        $elements_ast;
                        nodes
                    },
                }
            })
        }
        Literal { offset, ref value } => {
            let value_ast = generate_var_ast(cx, value);
            quote_expr!(cx, {
                Ast::Literal {
                    offset: $offset,
                    value: ::std::rc::Rc::new($value_ast),
                }
            })
        }
    }
}

/// Generate the AST expression for creating a JMESPath variable.
///
/// JMESPath variables are used in Literal nodes.
fn generate_var_ast(cx: &mut ExtCtxt, var: &Rcvar) -> P<ast::Expr> {
    match **var {
        Variable::Null => quote_expr!(cx, ::jmespath::Variable::Null),
        Variable::Bool(b) => quote_expr!(cx, ::jmespath::Variable::Bool($b)),
        Variable::String(ref s) => quote_expr!(cx, ::jmespath::Variable::String($s.to_owned())),
        Variable::Number(n) => {
            // f64 does not implement to_tokens, so we must parse a float from a string.
            let float_str = n.to_string();
            quote_expr!(cx, ::jmespath::Variable::Number($float_str.parse().unwrap()))
        }
        Variable::Expref(ref node) => {
            let node_ast = generate_ast(cx, node);
            quote_expr!(cx, ::jmespath::Variable::Expref($node_ast))
        }
        Variable::Array(ref array) => {
            // Create the AST nodes for inserting each value into the array.
            let elements_ast = array.iter()
                .fold(quote_expr!(cx, {}), |acc, element| {
                    let element_expr = generate_var_ast(cx, &element);
                    quote_expr!(cx, {
                        $acc;
                        nodes.push(::std::rc::Rc::new($element_expr));
                    })
                });
            quote_expr!(cx, ::jmespath::Variable::Array({
                let mut nodes = Vec::new();
                $elements_ast;
                nodes
            }))
        }
        Variable::Object(ref map) => {
            // Create the AST nodes for inserting each key value pair into the BTreeMap.
            let elements_ast = map.keys()
                .fold(quote_expr!(cx, {}), |acc, key| {
                    let element_expr = generate_var_ast(cx, map.get(key).unwrap());
                    quote_expr!(cx, {
                        $acc;
                        map.insert($key.to_owned(), ::std::rc::Rc::new($element_expr));
                    })
                });
            quote_expr!(cx, ::jmespath::Variable::Object({
                let mut map = ::std::collections::BTreeMap::new();
                $elements_ast;
                map
            }))
        }
    }
}

fn generate_slice_option_ast(cx: &mut ExtCtxt, value: Option<i32>) -> P<ast::Expr> {
    match value {
        Some(v) => quote_expr!(cx, Some($v)),
        None => quote_expr!(cx, None),
    }
}