Skip to content

Commit

Permalink
feat(dependencies): update nom
Browse files Browse the repository at this point in the history
  • Loading branch information
uulm-janbaudisch committed Jan 31, 2025
1 parent c1c0b42 commit c36f883
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 47 deletions.
73 changes: 56 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ddnnife/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ csv = { workspace = true }
file_diff = "1.0.0"
itertools = "0.14"
log = { workspace = true }
nom = "7.1"
nom = "8.0"
num = "0.4"
once_cell = "1.20"
petgraph = "0.7"
Expand Down
11 changes: 6 additions & 5 deletions ddnnife/src/ddnnf/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::{char, digit1};
use nom::combinator::{map_res, opt, recognize};
use nom::sequence::{pair, tuple};
use nom::IResult;
use nom::sequence::pair;
use nom::{IResult, Parser};
use num::{BigInt, ToPrimitive};
use workctl::WorkQueue;

Expand Down Expand Up @@ -601,15 +601,15 @@ fn get_numbers(params: &[&str], boundary: u32) -> Result<(Vec<i32>, usize), Stri
}

fn signed_number(input: &str) -> IResult<&str, &str> {
recognize(pair(opt(char('-')), digit1))(input)
recognize(pair(opt(char('-')), digit1)).parse(input)
}

// try parsing by starting with the most specific combination and goind
// to the least specific one
let range: IResult<&str, Vec<i32>> = alt((
// limited range
map_res(
tuple((signed_number, tag(".."), signed_number)),
(signed_number, tag(".."), signed_number),
|s: (&str, &str, &str)| {
match (s.0.parse::<i32>(), s.2.parse::<i32>()) {
// start and stop are inclusive (removing the '=' would make stop exclusive)
Expand All @@ -630,7 +630,8 @@ fn get_numbers(params: &[&str], boundary: u32) -> Result<(Vec<i32>, usize), Stri
Ok(v) => Ok(vec![v]),
Err(e) => Err(e),
}),
))(param);
))
.parse(param);

match range {
// '0' isn't valid in this context and gets removed
Expand Down
26 changes: 16 additions & 10 deletions ddnnife/src/parser/c2d_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nom::{
combinator::{map, recognize, value},
multi::many1,
sequence::{pair, preceded},
IResult,
IResult, Parser,
};

#[derive(Debug, Copy, Clone, PartialEq)]
Expand Down Expand Up @@ -90,7 +90,8 @@ pub fn lex_line_c2d(line: &str) -> IResult<&str, C2DToken> {
lex_or,
lex_positive_literal,
lex_negative_literal,
))(line)
))
.parse(line)
}

// Lexes the header and only the header with the format "nnf v e n" where v is the number of nodes in the NNF,
Expand All @@ -106,7 +107,8 @@ fn lex_header(line: &str) -> IResult<&str, C2DToken> {
variables: nums[2],
}
},
)(line)
)
.parse(line)
}

// Lexes a And node which is a inner node with the format "A c i1 i2 ... ic" where c is the number
Expand All @@ -119,7 +121,8 @@ fn lex_and(line: &str) -> IResult<&str, C2DToken> {
nums.remove(0); // remove information about number of children
And { children: nums }
},
)(line)
)
.parse(line)
}

// Lexes a Or node which is a inner node with the format "O j c i1 i2" with i1 and i2 as childs.
Expand All @@ -135,14 +138,16 @@ fn lex_or(line: &str) -> IResult<&str, C2DToken> {
children: nums,
}
},
)(line)
)
.parse(line)
}

// Lexes a positive Literal node which is a leaf node with the format "L i1" with i1 as the variable number.
fn lex_positive_literal(line: &str) -> IResult<&str, C2DToken> {
map(preceded(tag("L "), recognize(digit1)), |s: &str| Literal {
feature: s.parse::<i32>().unwrap(),
})(line)
})
.parse(line)
}

// Lexes a negative Literal node which is a leaf node with the format "L i1" with i1 as the variable number.
Expand All @@ -152,17 +157,18 @@ fn lex_negative_literal(line: &str) -> IResult<&str, C2DToken> {
|s: &str| Literal {
feature: s.parse::<i32>().unwrap(),
},
)(line)
)
.parse(line)
}

// Lexes a True node which is a leaf node with the format "A 0".
fn lex_true(line: &str) -> IResult<&str, C2DToken> {
value(True, tag("A 0"))(line)
value(True, tag("A 0")).parse(line)
}

// Lexes a False node which is a leaf node with the format "O 0 0".
fn lex_false(line: &str) -> IResult<&str, C2DToken> {
value(False, tag("O 0 0"))(line)
value(False, tag("O 0 0")).parse(line)
}

// Returns a closure that lexes exactly one number which consists of multiple digits to form an T
Expand All @@ -182,7 +188,7 @@ pub(super) fn split_numbers<T: std::str::FromStr>(out: &str) -> Vec<T> {

// parses an alternating sequence of one space and multiple digits which form a number
pub(super) fn parse_alt_space1_number1(input: &str) -> IResult<&str, &str> {
recognize(many1(pair(char(' '), digit1)))(input)
recognize(many1(pair(char(' '), digit1))).parse(input)
}

#[allow(non_snake_case)]
Expand Down
19 changes: 10 additions & 9 deletions ddnnife/src/parser/d4_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nom::{
combinator::{map, recognize, value},
multi::many_m_n,
sequence::{pair, preceded, terminated},
IResult,
IResult, Parser,
};

use D4Token::*;
Expand Down Expand Up @@ -34,7 +34,7 @@ pub enum D4Token {
/// to least occuring, to lex faster
#[inline]
pub fn lex_line_d4(line: &str) -> IResult<&str, D4Token> {
alt((lex_edge, lex_or, lex_and, lex_true, lex_false))(line)
alt((lex_edge, lex_or, lex_and, lex_true, lex_false)).parse(line)
}

// Lexes an Edge Node with the format "F T F1 F2 F3 ... 0" with F being the from and T being the to node of the edge.
Expand All @@ -60,37 +60,38 @@ fn lex_edge(line: &str) -> IResult<&str, D4Token> {
features: ws_numbers[2..ws_numbers.len()].to_vec(),
}
},
)(line)
)
.parse(line)
}

// lexes multiple sequences of signed numbers
pub(super) fn parse_signed_alt_space1_number1(line: &str) -> IResult<&str, (&str, &str)> {
alt((pair(digit1, space1), pair(neg_digit1, space1)))(line)
alt((pair(digit1, space1), pair(neg_digit1, space1))).parse(line)
}

// lexes a sequence of numbers that start with a minues sign
fn neg_digit1(line: &str) -> IResult<&str, &str> {
recognize(pair(char('-'), digit1))(line)
recognize(pair(char('-'), digit1)).parse(line)
}

// Lexes an And node which is a inner node with the format "a N 0" with N as Node number.
fn lex_and(line: &str) -> IResult<&str, D4Token> {
value(And, preceded(tag("a "), digit1))(line)
value(And, preceded(tag("a "), digit1)).parse(line)
}

// Lexes an Or node which is a inner node with the format "o N 0" with N as Node number.
fn lex_or(line: &str) -> IResult<&str, D4Token> {
value(Or, preceded(tag("o "), digit1))(line)
value(Or, preceded(tag("o "), digit1)).parse(line)
}

// Lexes a True node which is a leaf node with the format "t N 0" with N as Node number.
fn lex_true(line: &str) -> IResult<&str, D4Token> {
value(True, preceded(tag("t "), digit1))(line)
value(True, preceded(tag("t "), digit1)).parse(line)
}

// Lexes a False node which is a leaf node with the format "f N 0" with N as Node number.
fn lex_false(line: &str) -> IResult<&str, D4Token> {
value(False, preceded(tag("f "), digit1))(line)
value(False, preceded(tag("f "), digit1)).parse(line)
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit c36f883

Please sign in to comment.