Skip to content

Commit

Permalink
Add + operator for infix space
Browse files Browse the repository at this point in the history
  • Loading branch information
wrsturgeon committed Sep 27, 2023
1 parent f77bbb8 commit 7a51a04
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion example/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//!
//! /// Surround this language in parentheses.
//! pub fn parenthesized(p: Parser<char>) -> Parser<char> {
//! c('(') >> p >> c(')')
//! c('(') + p + c(')')
//! }
//!
//! /// Accept any of these tokens here.
Expand Down
1 change: 1 addition & 0 deletions example/src/inator_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![deny(dead_code)]
#![allow(non_snake_case)]

pub mod abc_tuple {
Expand Down
11 changes: 6 additions & 5 deletions src/dfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,14 +771,15 @@ impl<I: Clone + Ord + Expression> core::fmt::Display for Graph<I> {

impl<I: Clone + Ord + Expression> core::fmt::Display for State<I> {
#[inline]
#[allow(clippy::use_debug)]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
writeln!(
f,
"({}accepting):",
if self.accepting { "" } else { "NOT " }
)?;
for (input, transitions) in &self.transitions {
writeln!(f, " {input} --> {transitions}")?;
writeln!(f, " {input:?} --> {transitions}")?;
}
Ok(())
}
Expand Down Expand Up @@ -901,8 +902,8 @@ impl<I: Clone + Ord> State<I> {
match minimal_input.split_first() {
None => "[this is the initial state]".to_owned(),
Some((head, tail)) =>
tail.iter().fold(head.to_string(), |acc, token| {
acc + &format!(" -> {token}")
tail.iter().fold(format!("{head:?}"), |acc, token| {
acc + &format!(" -> {token:?}")
}),
},
),
Expand Down Expand Up @@ -1525,8 +1526,8 @@ impl<I: Clone + Ord> State<I> {
match minimal_input.split_first() {
None => "[this is the initial state]".to_owned(),
Some((head, tail)) =>
tail.iter().fold(head.to_string(), |acc, token| {
acc + &format!(" -> {token}")
tail.iter().fold(format!("{head:?}"), |acc, token| {
acc + &format!(" -> {token:?}")
}),
},
),
Expand Down
5 changes: 3 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use proc_macro2::Span;

/// Convert to a variety of source-code-related formats.
pub trait Expression: core::fmt::Display {
pub trait Expression: core::fmt::Debug {
/// Convert `&Self -> syn::Expr`.
#[must_use]
fn to_expr(&self) -> syn::Expr;
Expand Down Expand Up @@ -60,12 +60,13 @@ impl Expression for char {
// If it ever does for you, let me know.
match *self {
'A'..='Z' | 'a'..='z' | '0'..='9' => return core::iter::once(self).collect(),
'\u{1}'..='\u{8}' | '\u{b}'..='\u{c}' | '\u{e}'..='\u{1f}' | '\u{80}'.. => {
'\u{1}'..='\u{8}' | '\u{b}' | '\u{e}'..='\u{1f}' | '\u{80}'.. => {
return format!("_x{:X}_", u32::from(*self))
}
'\0' => "_null_",
'\t' => "_tab_",
'\n' => "_newline_",
'\u{c}' => "_newpage_",
'\r' => "_return_",
' ' => "_space_",
'!' => "_bang_",
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub fn opt<I: Clone + Ord>(token: I) -> Parser<I> {
#[inline]
#[must_use]
pub fn space() -> Parser<char> {
any((0..u8::MAX).filter(u8::is_ascii_whitespace).map(char::from))
any((0..u8::MAX).filter(u8::is_ascii_whitespace).map(char::from)).star()
}

/// Surround this language in parentheses.
Expand All @@ -215,5 +215,5 @@ pub fn space() -> Parser<char> {
#[must_use]
#[allow(clippy::arithmetic_side_effects)]
pub fn parenthesized(p: Parser<char>) -> Parser<char> {
c('(') >> space() >> p >> space() >> c(')')
c('(') + p + c(')')
}
2 changes: 1 addition & 1 deletion src/nfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl<I: Clone + Ord + Expression> core::fmt::Display for State<I> {
writeln!(f, " epsilon --> {:?}", self.epsilon)?;
}
for (input, transitions) in &self.non_epsilon {
writeln!(f, " {input} --> {transitions:?}")?;
writeln!(f, " {input:?} --> {transitions:?}")?;
}
Ok(())
}
Expand Down
13 changes: 11 additions & 2 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl<I: Clone + Ord> core::ops::AddAssign<usize> for nfa::State<I> {
}

impl<I: Clone + Ord> core::ops::BitOr for Nfa<I> {
type Output = Nfa<I>;
type Output = Self;
#[inline]
#[allow(clippy::arithmetic_side_effects, clippy::suspicious_arithmetic_impl)]
fn bitor(mut self, mut rhs: Self) -> Self::Output {
Expand All @@ -46,7 +46,7 @@ impl<I: Clone + Ord> core::ops::BitOr for Nfa<I> {
}

impl<I: Clone + Ord> core::ops::Shr for Nfa<I> {
type Output = Nfa<I>;
type Output = Self;
#[inline]
#[allow(clippy::todo)] // FIXME
#[allow(clippy::arithmetic_side_effects, clippy::suspicious_arithmetic_impl)]
Expand All @@ -69,3 +69,12 @@ impl<I: Clone + Ord> core::ops::Shr for Nfa<I> {
self
}
}

impl core::ops::Add for Nfa<char> {
type Output = Self;
#[inline]
#[allow(clippy::arithmetic_side_effects, clippy::suspicious_arithmetic_impl)]
fn add(self, rhs: Self) -> Self::Output {
self >> crate::space() >> rhs
}
}

0 comments on commit 7a51a04

Please sign in to comment.