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

Allow single-line comments #629

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 16 additions & 3 deletions pica-matcher/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::fmt::{self, Display};
use nom::branch::alt;
use nom::bytes::complete::{is_not, tag};
use nom::character::complete::{char, multispace0, multispace1};
use nom::combinator::{map, map_res, value, verify};
use nom::combinator::{map, map_res, opt, value, verify};
use nom::multi::fold_many0;
use nom::sequence::{delimited, preceded};
use nom::IResult;
use nom::sequence::{delimited, pair, preceded};
use nom::{IResult, Parser};
use pica_record::parser::ParseResult;

/// Boolean Operators.
Expand All @@ -26,6 +26,19 @@ where
delimited(multispace0, inner, multispace0)
}

pub(crate) fn comment<'a, O, E: nom::error::ParseError<&'a [u8]>, F>(
mut inner: F,
) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
where
F: Parser<&'a [u8], O, E>,
{
move |i: &'a [u8]| {
let (i, o) = inner.parse(i)?;
let (i, _) = opt(pair(tag("//"), is_not("\n\r")))(i)?;
Ok((i, o))
}
}

/// Relational Operator
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum RelationalOp {
Expand Down
50 changes: 25 additions & 25 deletions pica-matcher/src/field_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use pica_record::parser::ParseResult;
use pica_record::Field;

use crate::common::{
parse_relational_op_usize, ws, BooleanOp, RelationalOp,
comment, parse_relational_op_usize, ws, BooleanOp, RelationalOp,
};
use crate::occurrence_matcher::{
parse_occurrence_matcher, OccurrenceMatcher,
Expand Down Expand Up @@ -579,21 +579,21 @@ fn parse_field_matcher_not(i: &[u8]) -> ParseResult<FieldMatcher> {
fn parse_field_matcher_and(i: &[u8]) -> ParseResult<FieldMatcher> {
let (i, (first, remainder)) = tuple((
alt((
ws(parse_field_matcher_group),
ws(parse_field_matcher_cardinality),
ws(parse_field_matcher_singleton),
ws(parse_field_matcher_not),
ws(parse_field_matcher_exists),
comment(ws(parse_field_matcher_group)),
comment(ws(parse_field_matcher_cardinality)),
comment(ws(parse_field_matcher_singleton)),
comment(ws(parse_field_matcher_not)),
comment(ws(parse_field_matcher_exists)),
)),
many1(preceded(
ws(tag("&&")),
alt((
ws(parse_field_matcher_group),
ws(parse_field_matcher_cardinality),
ws(parse_field_matcher_singleton),
ws(parse_field_matcher_not),
ws(parse_field_matcher_exists),
)),
comment(ws(tag("&&"))),
cut(alt((
comment(ws(parse_field_matcher_group)),
comment(ws(parse_field_matcher_cardinality)),
comment(ws(parse_field_matcher_singleton)),
comment(ws(parse_field_matcher_not)),
comment(ws(parse_field_matcher_exists)),
))),
)),
))(i)?;

Expand All @@ -606,20 +606,20 @@ fn parse_field_matcher_and(i: &[u8]) -> ParseResult<FieldMatcher> {
fn parse_field_matcher_or(i: &[u8]) -> ParseResult<FieldMatcher> {
let (i, (first, remainder)) = tuple((
alt((
ws(parse_field_matcher_group),
ws(parse_field_matcher_and),
ws(parse_field_matcher_cardinality),
ws(parse_field_matcher_singleton),
ws(parse_field_matcher_exists),
comment(ws(parse_field_matcher_group)),
comment(ws(parse_field_matcher_and)),
comment(ws(parse_field_matcher_cardinality)),
comment(ws(parse_field_matcher_singleton)),
comment(ws(parse_field_matcher_exists)),
)),
many1(preceded(
ws(tag("||")),
comment(ws(tag("||"))),
cut(alt((
ws(parse_field_matcher_group),
ws(parse_field_matcher_and),
ws(parse_field_matcher_cardinality),
ws(parse_field_matcher_singleton),
ws(parse_field_matcher_exists),
comment(ws(parse_field_matcher_group)),
comment(ws(parse_field_matcher_and)),
comment(ws(parse_field_matcher_cardinality)),
comment(ws(parse_field_matcher_singleton)),
comment(ws(parse_field_matcher_exists)),
))),
)),
))(i)?;
Expand Down
40 changes: 20 additions & 20 deletions pica-matcher/src/subfield_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use regex::Regex;
use strsim::normalized_levenshtein;

use crate::common::{
parse_relational_op_str, parse_relational_op_usize, parse_string,
ws, BooleanOp, RelationalOp,
comment, parse_relational_op_str, parse_relational_op_usize,
parse_string, ws, BooleanOp, RelationalOp,
};
use crate::{MatcherOptions, ParseMatcherError};

Expand Down Expand Up @@ -735,18 +735,18 @@ fn parse_group_matcher(i: &[u8]) -> ParseResult<SubfieldMatcher> {
fn parse_or_matcher(i: &[u8]) -> ParseResult<SubfieldMatcher> {
let (i, (first, remainder)) = tuple((
alt((
ws(parse_group_matcher),
ws(parse_and_matcher),
ws(parse_subfield_singleton_matcher),
ws(parse_not_matcher),
comment(ws(parse_group_matcher)),
comment(ws(parse_and_matcher)),
comment(ws(parse_subfield_singleton_matcher)),
comment(ws(parse_not_matcher)),
)),
many1(preceded(
ws(tag("||")),
comment(ws(tag("||"))),
cut(alt((
ws(parse_group_matcher),
ws(parse_and_matcher),
ws(parse_subfield_singleton_matcher),
ws(parse_not_matcher),
comment(ws(parse_group_matcher)),
comment(ws(parse_and_matcher)),
comment(ws(parse_subfield_singleton_matcher)),
comment(ws(parse_not_matcher)),
))),
)),
))(i)?;
Expand All @@ -760,22 +760,22 @@ fn parse_or_matcher(i: &[u8]) -> ParseResult<SubfieldMatcher> {
fn parse_and_matcher(i: &[u8]) -> ParseResult<SubfieldMatcher> {
let (i, (first, remainder)) = tuple((
alt((
ws(parse_group_matcher),
map(
comment(ws(parse_group_matcher)),
comment(map(
ws(parse_singleton_matcher),
SubfieldMatcher::Singleton,
),
ws(parse_not_matcher),
)),
comment(ws(parse_not_matcher)),
)),
many1(preceded(
ws(tag("&&")),
comment(ws(tag("&&"))),
alt((
ws(parse_group_matcher),
map(
comment(ws(parse_group_matcher)),
comment(map(
ws(parse_singleton_matcher),
SubfieldMatcher::Singleton,
),
ws(parse_not_matcher),
)),
comment(ws(parse_not_matcher)),
)),
)),
))(i)?;
Expand Down