|
| 1 | +/* |
| 2 | + * Copyright 2020 VMware, Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +use crate::ast::*; |
| 8 | +use crate::pest::Parser; |
| 9 | + |
| 10 | +#[derive(Parser)] |
| 11 | +#[grammar = "grammar.pest"] |
| 12 | +struct PathParser; |
| 13 | + |
| 14 | +pub fn parse(selector: &str) -> Result<Path, String> { |
| 15 | + let selector_rule = PathParser::parse(Rule::selector, selector) |
| 16 | + .map_err(|e| format!("{}", e))? |
| 17 | + .next() |
| 18 | + .unwrap(); |
| 19 | + |
| 20 | + let mut res = Path::Root(); |
| 21 | + for r in selector_rule.into_inner() { |
| 22 | + res = match r.as_rule() { |
| 23 | + Rule::rootSelector => res, |
| 24 | + Rule::matcher => Path::Sel(Box::new(res), parse_selector(r)), |
| 25 | + _ => panic!("invalid parse tree {:?}", r), |
| 26 | + } |
| 27 | + } |
| 28 | + Ok(res) |
| 29 | +} |
| 30 | + |
| 31 | +fn parse_selector(matcher_rule: pest::iterators::Pair<Rule>) -> Selector { |
| 32 | + for r in matcher_rule.into_inner() { |
| 33 | + match r.as_rule() { |
| 34 | + Rule::wildcardedDotChild => {} // TODO: fix grammar so that this is a silent rule since we don't need it |
| 35 | + Rule::namedDotChild => return Selector::Dot(parse_child_name(r)), |
| 36 | + Rule::union => return Selector::Union(parse_union_indices(r)), |
| 37 | + _ => panic!("invalid parse tree {:?}", r), |
| 38 | + } |
| 39 | + } |
| 40 | + panic!("invalid parse tree") |
| 41 | +} |
| 42 | + |
| 43 | +fn parse_child_name(matcher_rule: pest::iterators::Pair<Rule>) -> Index { |
| 44 | + let r = matcher_rule.into_inner().nth(0).unwrap(); |
| 45 | + match r.as_rule() { |
| 46 | + Rule::childName => Index::Field(r.as_str().to_owned()), |
| 47 | + _ => panic!("invalid parse tree {:?}", r), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +fn parse_union_indices(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<Index> { |
| 52 | + let mut res = Vec::new(); |
| 53 | + |
| 54 | + for r in matcher_rule.into_inner() { |
| 55 | + match r.as_rule() { |
| 56 | + Rule::unionChild => res.append(&mut parse_union_child(r)), |
| 57 | + Rule::unionArrayIndex => res.push(parse_union_array_index(r)), |
| 58 | + _ => panic!("invalid parse tree {:?}", r), |
| 59 | + } |
| 60 | + } |
| 61 | + res |
| 62 | +} |
| 63 | + |
| 64 | +fn parse_union_child(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<Index> { |
| 65 | + let mut res = Vec::new(); |
| 66 | + for r in matcher_rule.into_inner() { |
| 67 | + match r.as_rule() { |
| 68 | + Rule::doubleInner => res.push(Index::Field(unescape(r.as_str()))), |
| 69 | + Rule::singleInner => res.push(Index::Field(unescape_single(r.as_str()))), |
| 70 | + _ => panic!("invalid parse tree {:?}", r), |
| 71 | + } |
| 72 | + } |
| 73 | + res |
| 74 | +} |
| 75 | + |
| 76 | +fn parse_union_array_index(matcher_rule: pest::iterators::Pair<Rule>) -> Index { |
| 77 | + let i = matcher_rule.as_str().parse().unwrap(); |
| 78 | + Index::Number(i) |
| 79 | +} |
| 80 | + |
| 81 | +fn unescape(contents: &str) -> String { |
| 82 | + let s = format!(r#""{}""#, contents); |
| 83 | + serde_json::from_str(&s).unwrap() |
| 84 | +} |
| 85 | + |
| 86 | +fn unescape_single(contents: &str) -> String { |
| 87 | + let d = to_double_quoted(contents); |
| 88 | + unescape(&d) |
| 89 | +} |
| 90 | + |
| 91 | +// converts a single quoted string body into a string that can be unescaped |
| 92 | +// by a function that knows how to unescape double quoted string, |
| 93 | +// It works by unescaping single quotes and escaping double quotes while leaving |
| 94 | +// everything else untouched. |
| 95 | +fn to_double_quoted(contents: &str) -> String { |
| 96 | + let mut output = String::new(); |
| 97 | + let mut escaping = false; |
| 98 | + for ch in contents.chars() { |
| 99 | + if !escaping { |
| 100 | + if ch == '\\' { |
| 101 | + escaping = true; |
| 102 | + } else { |
| 103 | + if ch == '"' { |
| 104 | + output.push('\\'); |
| 105 | + } |
| 106 | + output.push(ch); |
| 107 | + } |
| 108 | + } else { |
| 109 | + escaping = false; |
| 110 | + if ch != '\'' { |
| 111 | + output.push('\\'); |
| 112 | + }; |
| 113 | + output.push(ch); |
| 114 | + } |
| 115 | + } |
| 116 | + output |
| 117 | +} |
| 118 | + |
| 119 | +#[cfg(test)] |
| 120 | +mod test { |
| 121 | + use super::*; |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn test_to_double() { |
| 125 | + assert_eq!(to_double_quoted(r#"ab"#), r#"ab"#); |
| 126 | + assert_eq!(to_double_quoted(r#"a"b"#), r#"a\"b"#); |
| 127 | + assert_eq!(to_double_quoted(r#"a\'b"#), r#"a'b"#); |
| 128 | + assert_eq!(to_double_quoted(r#"a\nb"#), r#"a\nb"#); |
| 129 | + assert_eq!(to_double_quoted(r#"a\bb"#), r#"a\bb"#); |
| 130 | + assert_eq!(to_double_quoted(r#"a\\b"#), r#"a\\b"#); |
| 131 | + } |
| 132 | +} |
0 commit comments