Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

union child #4

Merged
merged 1 commit into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,35 @@ selector = _{ SOI ~ jsonPath ~ EOI }
jsonPath = ${ rootSelector ~ matcher* }
rootSelector = { "$" }

matcher = { dotChild }
matcher = { dotChild | union }

dotChild = _{ wildcardedDotChild | namedDotChild }
wildcardedDotChild = { ".*" }
namedDotChild = ${ "." ~ childName }
childName = @{ char+ }
char = {
!("\"" | "'" | "\\") ~ ANY
!("\"" | "'" | "\\") ~ ANY // char is still TBD in the draft
}

union = { "[" ~ unionElement ~ ("," ~ unionElement)* ~ "]" }
unionElement = _{ unionChild }
unionChild = { doubleQuotedString | singleQuotedString }

doubleQuotedString = _{ "\"" ~ doubleInner ~ "\"" }
doubleInner = @{ doubleChar* }
doubleChar = {
!("\"" | "\\" | '\u{00}'..'\u{1F}') ~ ANY
| "\\" ~ ("\"" | "\\" | "/" | "b" | "f" | "n" | "r" | "t")
| "\\" ~ ("u" ~ upperHexDigit{4})
}
upperHexDigit = _{ ASCII_DIGIT | "A" | "B" | "C" | "D" | "E" | "F" }

singleQuotedString = _{ "'" ~ singleInner ~ "'" }
singleInner = @{ singleChar* }
singleChar = {
!("'" | "\\" | '\u{00}'..'\u{1F}') ~ ANY
| "\\" ~ ("'" | "\\" | "/" | "b" | "f" | "n" | "r" | "t")
| "\\" ~ ("u" ~ upperHexDigit{4})
}

WHITESPACE = _{ " " }
30 changes: 26 additions & 4 deletions src/matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ impl Matcher for WildcardedChild {
}
}

pub struct DotChild {
pub struct Child {
name: String,
}

pub fn new_dot_child_matcher(name: String) -> DotChild {
DotChild { name }
pub fn new_child_matcher(name: String) -> Child {
Child { name }
}

impl Matcher for DotChild {
impl Matcher for Child {
fn select<'a>(&self, node: &'a Value) -> Box<dyn Iterator<Item = &'a Value> + 'a> {
if node.is_object() {
let mapping = node.as_object().unwrap();
Expand All @@ -55,3 +55,25 @@ impl Matcher for DotChild {
}
}
}

pub struct Union {
elements: Vec<Box<dyn Matcher>>,
}

pub fn new_union(elements: Vec<Box<dyn Matcher>>) -> Union {
Union { elements }
}

impl Matcher for Union {
fn select<'a, 'b>(&'a self, node: &'b Value) -> Box<dyn Iterator<Item = &'b Value> + 'b> {
// union of matches of the matchers in the union
let mut u = vec![];
for m in &self.elements {
let m_selection = m.select(node);
for s in m_selection {
u.push(s);
}
}
Box::new(u.into_iter())
}
}
80 changes: 77 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ pub fn parse<'a>(selector: &'a str) -> Result<Box<dyn path::Path + 'a>, String>
for r in selector_rule.into_inner() {
match r.as_rule() {
Rule::rootSelector => ms.push(Box::new(matchers::RootSelector {})),

Rule::matcher => {
for m in parse_matcher(r) {
ms.push(m)
}
}

_ => println!("r={:?}", r),
}
}
Expand All @@ -39,11 +41,19 @@ fn parse_matcher(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<Box<dyn match
for r in matcher_rule.into_inner() {
match r.as_rule() {
Rule::wildcardedDotChild => ms.push(Box::new(matchers::WildcardedChild {})),

Rule::namedDotChild => {
for m in parse_dot_child_matcher(r) {
ms.push(m)
}
}

Rule::union => {
for m in parse_union(r) {
ms.push(m)
}
}

_ => (),
}
}
Expand All @@ -56,10 +66,74 @@ fn parse_dot_child_matcher(
let mut ms: Vec<Box<dyn matchers::Matcher>> = Vec::new();
for r in matcher_rule.into_inner() {
if let Rule::childName = r.as_rule() {
ms.push(Box::new(matchers::new_dot_child_matcher(
r.as_str().to_owned(),
)));
ms.push(Box::new(matchers::new_child_matcher(r.as_str().to_owned())));
}
}
ms
}

fn parse_union(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<Box<dyn matchers::Matcher>> {
let mut ms: Vec<Box<dyn matchers::Matcher>> = Vec::new();
for r in matcher_rule.into_inner() {
if let Rule::unionChild = r.as_rule() {
for m in parse_union_child(r) {
ms.push(m)
}
}
}
vec![Box::new(matchers::new_union(ms))]
}

fn parse_union_child(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<Box<dyn matchers::Matcher>> {
let mut ms: Vec<Box<dyn matchers::Matcher>> = Vec::new();
for r in matcher_rule.into_inner() {
match r.as_rule() {
Rule::doubleInner => {
ms.push(Box::new(matchers::new_child_matcher(unescape(r.as_str()))));
}

Rule::singleInner => {
ms.push(Box::new(matchers::new_child_matcher(unescape(r.as_str()))));
}

_ => (),
}
}
ms
}

const ESCAPED: &str = "\"'\\/bfnrt";
const UNESCAPED: &str = "\"'\\/\u{0008}\u{000C}\u{000A}\u{000D}\u{0009}";

fn unescape(contents: &str) -> String {
let mut output = String::new();
let xs: Vec<char> = contents.chars().collect();
let mut i = 0;
while i < xs.len() {
if xs[i] == '\\' {
i += 1;
if xs[i] == 'u' {
i += 1;

// convert xs[i..i+4] to Unicode character and add it to the output
let x = xs[i..i + 4].iter().collect::<String>();
let n = u32::from_str_radix(&x, 16);
let u = std::char::from_u32(n.unwrap());
output.push(u.unwrap());

i += 4;
} else {
for (j, c) in ESCAPED.chars().enumerate() {
if xs[i] == c {
output.push(UNESCAPED.chars().nth(j).unwrap())
}
}
i += 1;
}
} else {
output.push(xs[i]);
i += 1;
}
}
output
}
Loading