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

Add open-close/push-pop semantics #50

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions automata/examples/matched_parentheses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ fn shitpost<R: RngCore>(rng: &mut R) -> String {
let mut s = String::new();
loop {
let i = rng.next_u32();
if i & 2 == 0 {
if (i & 2) == 0 {
return s;
}
s.push(if i & 1 == 0 { '(' } else { ')' });
s.push(if (i & 1) == 0 { '(' } else { ')' });
}
}

Expand Down
2 changes: 1 addition & 1 deletion automata/examples/matched_parentheses_codegen/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ use std::io;

pub fn main() -> Result<io::Result<()>, IllFormed<char, usize>> {
// Very manually constructed parser recognizing only valid parentheses.
dyck_d().to_file("src/parser.rs")
dyck_d().to_file("src/autogen.rs")
}
8 changes: 4 additions & 4 deletions automata/examples/matched_parentheses_codegen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unreachable_code, unused_variables)]

mod parser;
mod autogen;

use rand::{thread_rng, RngCore};

Expand Down Expand Up @@ -55,17 +55,17 @@ fn main() {
for _ in 0..50 {
let s = generate(&mut rng, 32);
println!("\"{s}\"");
assert_eq!(parser::parse(s.chars()), Ok(()));
assert_eq!(autogen::parse(s.chars()), Ok(()));
}

// Reject all invalid strings
for _ in 0..50 {
let s = shitpost(&mut rng);
println!("\"{s}\"");
if accept(s.chars()) {
assert_eq!(parser::parse(s.chars()), Ok(()));
assert_eq!(autogen::parse(s.chars()), Ok(()));
} else {
assert!(parser::parse(s.chars()).is_err());
assert!(autogen::parse(s.chars()).is_err());
}
}
}
73 changes: 0 additions & 73 deletions automata/examples/matched_parentheses_codegen/src/parser.rs

This file was deleted.

4 changes: 2 additions & 2 deletions automata/examples/matched_parentheses_nondeterministic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ fn shitpost<R: RngCore>(rng: &mut R) -> String {
let mut s = String::new();
loop {
let i = rng.next_u32();
if i & 2 == 0 {
if (i & 2) == 0 {
return s;
}
s.push(if i & 1 == 0 { '(' } else { ')' });
s.push(if (i & 1) == 0 { '(' } else { ')' });
}
}

Expand Down
131 changes: 131 additions & 0 deletions automata/examples/paren_a_star.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
use core::iter;
use inator_automata::*;
use rand::{thread_rng, RngCore};
use std::collections::{BTreeMap, BTreeSet};

/// Check if this string consists of _n_ `a`s in parentheses.
fn accept<I: Iterator<Item = char>>(mut iter: I) -> bool {
if !matches!(iter.next(), Some('(')) {
return false;
}
loop {
match iter.next() {
Some('a') => continue,
Some(')') => return true,
_ => return false,
}
}
}

/// Output a jumble of characters with a very low chance of being valid.
fn shitpost<R: RngCore>(rng: &mut R) -> String {
let mut s = String::new();
loop {
let i = rng.next_u32();
if (i & 9) == 0 {
return s;
}
s.push(char::from(i as u8));
}
}

pub fn main() {
let parser = Graph {
states: vec![
State {
transitions: Curry::Scrutinize {
filter: RangeMap(
iter::once((
Range::unit('('),
Transition::Call {
region: "parentheses",
detour: 1,
dst: Box::new(Transition::Lateral {
dst: 2,
update: None,
}),
combine: ff!(|(), ()| ()),
},
))
.collect(),
),
fallback: None,
},
non_accepting: iter::once("No input".to_owned()).collect(),
},
State {
transitions: Curry::Scrutinize {
filter: RangeMap(
[
(
Range::unit('a'),
Transition::Lateral {
dst: 1,
update: None,
},
),
(
Range::unit(')'),
Transition::Return {
region: "parentheses",
},
),
]
.into_iter()
.collect(),
),
fallback: None,
},
non_accepting: iter::once("Unclosed parentheses".to_owned()).collect(),
},
State {
transitions: Curry::Scrutinize {
filter: RangeMap(BTreeMap::new()),
fallback: None,
},
non_accepting: BTreeSet::new(),
},
],
initial: 0,
};
parser.check().unwrap();

// Print the Rust source representation of this parser
println!("{}", parser.to_src().unwrap());

let mut rng = thread_rng();

// Accept all valid strings
for _ in 0..10 {
let n = (rng.next_u32() & 15) as usize;
let s: String = iter::once('(')
.chain(iter::repeat('a').take(n))
.chain(iter::once(')'))
.collect();
println!();
println!("{s:?}");
let mut run = s.chars().run(&parser);
println!(" {run:?}");
while let Some(r) = run.next() {
let Ok(c) = r else { panic!("{r:?}") };
println!("{c:?} {run:?}");
}
}

// Reject all invalid strings
'examples: for _ in 0..10 {
let s = shitpost(&mut rng);
println!();
println!("{s:?}");
let mut run = s.chars().run(&parser);
println!(" {run:?}");
while let Some(r) = run.next() {
let Ok(c) = r else {
assert!(!accept(s.chars()));
continue 'examples;
};
println!("{c:?} {run:?}");
}
assert!(accept(s.chars()));
}
}
2 changes: 1 addition & 1 deletion automata/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl<I: Input, C: Ctrl<I>> Graph<I, C> {
ref mut fallback,
} => {
filter.star(&s.initial, &accepting);
if let &mut Some(ref mut f) = fallback {
if let Some(ref mut f) = *fallback {
f.star(&s.initial, &accepting);
}
}
Expand Down
Loading
Loading