Skip to content

Commit

Permalink
Improve code style and fix nemo-wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
rlwww authored and mmarx committed Aug 28, 2023
1 parent dbe8b0a commit 8aa3ace
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
1 change: 1 addition & 0 deletions nemo-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ impl NemoResults {
Term::NumericLiteral(_) => JsValue::from(rdf.to_string()),
Term::StringLiteral(s) => JsValue::from(s),
Term::RdfLiteral(lit) => JsValue::from(lit.to_string()),
Term::Aggregate(_) => panic!("Aggregates should not occur as results!"),
},
PrimitiveLogicalValueT::String(s) => JsValue::from(String::from(s)),
PrimitiveLogicalValueT::Integer(i) => JsValue::from(i64::from(i)),
Expand Down
2 changes: 1 addition & 1 deletion nemo/src/execution/planning/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn termtree_to_operationtree(
.map(|t| termtree_to_operationtree(t, order, logical_type))
.collect(),
),
TermOperation::Function(_) => panic!("function terms are not implemented yet."),
TermOperation::Function(_) => todo!("function terms are not implemented yet."),
}
}

Expand Down
51 changes: 23 additions & 28 deletions nemo/src/io/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use nom::{
use macros::traced;

mod types;
use nom_locate::LocatedSpan;
use types::{IntermediateResult, Span};
pub(crate) mod iri;
pub(crate) mod rfc5234;
Expand Down Expand Up @@ -330,21 +329,21 @@ impl<'a> RuleParser<'a> {
/// Matches an opening parenthesis,
/// then gets an object from the parser,
/// and finally matches an closing parenthesis.
pub fn in_parenthesis<'b, O, F>(
pub fn parenthesised<'b, O, F>(
&'a self,
parser: F,
) -> impl FnMut(Span<'a>) -> IntermediateResult<O>
where
O: Debug + 'a,
F: FnMut(LocatedSpan<&'a str>) -> IntermediateResult<O> + 'a,
F: FnMut(Span<'a>) -> IntermediateResult<O> + 'a,
{
traced(
"in_parenthesis",
"parenthesised",
map_error(
delimited(
delimited(multispace_or_comment0, token("("), multispace_or_comment0),
self.parse_open_parenthesis(),
parser,
delimited(multispace_or_comment0, token(")"), multispace_or_comment0),
self.parse_close_parenthesis(),
),
|| ParseError::ExpectedParenthesisedExpression,
),
Expand Down Expand Up @@ -605,7 +604,7 @@ impl<'a> RuleParser<'a> {
let (remainder, (predicate, terms)) = terminated(
pair(
self.parse_iri_like_identifier(),
self.in_parenthesis(separated_list1(
self.parenthesised(separated_list1(
self.parse_comma(),
parse_ground_term(&self.prefixes),
)),
Expand Down Expand Up @@ -804,17 +803,13 @@ impl<'a> RuleParser<'a> {
let (remainder, _) = nom::character::complete::char('#')(input)?;
let (remainder, aggregate_identifier) =
self.parse_bare_iri_like_identifier()(remainder)?;
let (remainder, variable_identifiers) = delimited(
self.parse_open_parenthesis(),
cut(separated_list1(
self.parse_comma(),
map(self.parse_universal_variable(), |variable| match variable {
Variable::Universal(identifier) => identifier,
Variable::Existential(_) => panic!(),
}),
)),
cut(self.parse_close_parenthesis()),
)(remainder)?;
let (remainder, variable_identifiers) = self.parenthesised(separated_list1(
self.parse_comma(),
map(self.parse_universal_variable(), |variable| match variable {
Variable::Universal(identifier) => identifier,
Variable::Existential(_) => panic!(),
}),
))(remainder)?;

let aggregate = Aggregate {
aggregate_identifier,
Expand Down Expand Up @@ -951,7 +946,7 @@ impl<'a> RuleParser<'a> {
)
}

/// Parse an term tree.
/// Parse a term tree.
///
/// This may consist of:
/// * A function term
Expand All @@ -966,7 +961,7 @@ impl<'a> RuleParser<'a> {
alt((
self.parse_function_term(),
self.parse_arithmetic_expression(),
self.parse_term_tree_in_parenthesis(),
self.parse_parenthesised_term_tree(),
)),
multispace_or_comment0,
)(input)
Expand All @@ -977,13 +972,13 @@ impl<'a> RuleParser<'a> {
}

/// Parse a parenthesised term tree.
pub fn parse_term_tree_in_parenthesis(
pub fn parse_parenthesised_term_tree(
&'a self,
) -> impl FnMut(Span<'a>) -> IntermediateResult<TermTree> {
traced(
"parse_term_tree_in_parenthesis",
map_error(self.in_parenthesis(self.parse_term_tree()), || {
ParseError::ExpectedTermTreeInParenthesis
"parse_parenthesised_term_tree",
map_error(self.parenthesised(self.parse_term_tree()), || {
ParseError::ExpectedParenthesisedTermTree
}),
)
}
Expand All @@ -996,7 +991,7 @@ impl<'a> RuleParser<'a> {
move |input| {
let (remainder, identifier) = self.parse_iri_like_identifier()(input)?;

let (remainder, subtrees) = (self.in_parenthesis(separated_list0(
let (remainder, subtrees) = (self.parenthesised(separated_list0(
self.parse_comma(),
self.parse_term_tree(),
)))(remainder)?;
Expand Down Expand Up @@ -1086,7 +1081,7 @@ impl<'a> RuleParser<'a> {
map_error(
alt((
map(self.parse_term(), TermTree::leaf),
self.parse_term_tree_in_parenthesis(),
self.parse_parenthesised_term_tree(),
)),
|| ParseError::ExpectedArithmeticFactor,
),
Expand Down Expand Up @@ -1735,9 +1730,9 @@ mod test {
ParseError::ExpectedArithmeticFactor
);
assert_parse_error!(
parser.parse_term_tree_in_parenthesis(),
parser.parse_parenthesised_term_tree(),
"",
ParseError::ExpectedArithmeticParenthesis
ParseError::ExpectedParenthesisedTermTree
);
assert_parse_error!(
parser.parse_arithmetic_product(),
Expand Down
5 changes: 1 addition & 4 deletions nemo/src/io/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ pub enum ParseError {
/// Expected an arithmetic factor expression.
#[error("Expected an arithmetic factor expression")]
ExpectedArithmeticFactor,
/// Expected an arithmetic parenthesised expression.
#[error("Expected an arithmetic parenthesised expression")]
ExpectedArithmeticParenthesis,
/// Encountered a base declaration after any other directive.
#[error("A @base declaration can only be the first statement in the program")]
LateBaseDeclaration,
Expand All @@ -241,7 +238,7 @@ pub enum ParseError {
ExpectedParenthesisedExpression,
/// Expected an parenthesised term tree.
#[error("Expected an parenthesised term tree")]
ExpectedTermTreeInParenthesis,
ExpectedParenthesisedTermTree,
}

impl ParseError {
Expand Down

0 comments on commit 8aa3ace

Please sign in to comment.