Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Parse query #6

Merged
merged 4 commits into from
Jan 18, 2024
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
12 changes: 12 additions & 0 deletions query.pest
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
tag = {"#" ~ (LETTER|NUMBER|CONNECTOR_PUNCTUATION|DASH_PUNCTUATION)+}

operation = _{and | or}
and = {"&"}
or = {"|"}

expr = {term ~ (operation ~ term)*}
term = _{tag | "(" ~ expr ~ ")"}

tagsearch = _{SOI ~ expr ~ EOI}

WHITESPACE = _{" " | "\t" | NEWLINE}
100 changes: 90 additions & 10 deletions src/parsers.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
use pest_derive::Parser;
pub mod tagline {
use pest_derive::Parser;

#[derive(Parser)]
#[grammar = "tagline.pest"]
/// TaglineParser is responsible for parsing the taglines at the start of each searched file.
pub struct TaglineParser;
#[derive(Parser)]
#[grammar = "tagline.pest"]
/// TaglineParser is responsible for parsing the taglines at the start of each searched file.
/// The relevant rule is `tagline`.
pub struct TaglineParser;
}

pub mod query {
use pest_derive::Parser;

#[derive(Parser)]
#[grammar = "query.pest"]
/// QueryParser is responsible for parsing the search query.
/// The relevant rule is `tagsearch`.
pub struct QueryParser;
}

#[cfg(test)]
mod tests {
use pest::Parser;
use super::query;
use super::tagline;

use super::*;
use pest::Parser;

#[test]
fn test_tagline_parser() {
Expand Down Expand Up @@ -60,9 +74,9 @@ mod tests {
];

test_cases.iter().for_each(|test_case| {
println!("Testing {}", test_case.name);
println!("test_tagline_parser: \n\t{}", test_case.name);

let res = TaglineParser::parse(Rule::tagline, test_case.input);
let res = tagline::TaglineParser::parse(tagline::Rule::tagline, test_case.input);
if res.is_err() {
assert!(test_case.expected_error);
return;
Expand All @@ -72,12 +86,78 @@ mod tests {

for (i, tag) in res.unwrap().next().unwrap().into_inner().enumerate() {
match tag.as_rule() {
Rule::tag => {
tagline::Rule::tag => {
assert!(tag.as_str() == test_case.expected_tags[i])
}
_ => unreachable!(),
}
}
})
}

#[test]
fn test_query_parser() {
struct TestCase<'a> {
name: &'a str,
input: &'a str,
expected_error: bool,
}

let test_cases = [
TestCase {
name: "success_with_space",
input: "#a & #b",
expected_error: false,
},
TestCase {
name: "success_without_space",
input: "#a&#b",
expected_error: false,
},
TestCase {
name: "success_with_newline",
input: "#a\n|\n#b",
expected_error: false,
},
TestCase {
name: "success_nested",
input: "#a & (#b | #c)",
expected_error: false,
},
TestCase {
name: "fail_wrong_tag",
input: "##",
expected_error: true,
},
TestCase {
name: "fail_no_following_tag",
input: "#a &",
expected_error: true,
},
TestCase {
name: "fail_no_open_parentheses",
input: "#a & #b)",
expected_error: true,
},
TestCase {
name: "fail_no_closing_parentheses",
input: "(#a & #b",
expected_error: true,
},
];

test_cases.iter().for_each(|test_case| {
println!("test_tagline_parser: \n\t{}", test_case.name);

let res = query::QueryParser::parse(query::Rule::tagsearch, test_case.input);
if res.is_err() {
assert!(test_case.expected_error);
return;
}

assert!(!test_case.expected_error);

assert_eq!(test_case.input, res.unwrap().next().unwrap().as_str())
})
}
}
2 changes: 1 addition & 1 deletion tagline.pest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tag = {"#" ~ (LETTER|NUMBER|CONNECTOR_PUNCTUATION|DASH_PUNCTUATION)+}
taglist = _{"[" ~ tag* ~ "]"}
tagline = _{"tags:" ~ taglist}
tagline = _{SOI ~ "tags:" ~ taglist ~ EOI}

WHITESPACE = _{" " | "\t" | NEWLINE}