-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c2a386
commit 647323b
Showing
14 changed files
with
207 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
workspace = { members = ["examples/digit"] } | ||
[package] | ||
name = "inator" | ||
authors = ["Will Sturgeon <[email protected]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "digit" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
|
||
[build-dependencies] | ||
inator = { path = "../.." } | ||
|
||
[dev-dependencies] | ||
quickcheck = "1.0.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use std::io; | ||
|
||
type I = u8; | ||
|
||
fn main() -> Result<io::Result<()>, inator::IllFormed<I, (), usize>> { | ||
inator::digit().to_file("src/parser.rs") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
mod parser; | ||
|
||
#[cfg(test)] | ||
mod test; | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//! Automatically generated with [inator](https://crates.io/crates/inator). | ||
|
||
#![allow(dead_code, unused_variables)] | ||
|
||
/// Descriptive parsing error. | ||
#[allow(dead_code)] | ||
#[derive(Clone, Debug, PartialEq)] | ||
pub enum Error { | ||
/// Token without any relevant rule. | ||
Absurd { | ||
/// Index of the token that caused this error. | ||
index: usize, | ||
/// Particular token that didn't correspond to a rule. | ||
token: u8, | ||
}, | ||
/// Token that would have closed a delimiter, but the delimiter wasn't open. | ||
Unopened { | ||
/// Index of the token that caused this error. | ||
index: usize, | ||
/// Type of thing that wasn't opened (e.g. parentheses). | ||
delimiter: (), | ||
/// What actually was open (e.g. you tried to close parentheses, but a bracket was open). | ||
instead: Option<()>, | ||
}, | ||
/// After parsing all input, a delimiter remains open (e.g. "(a, b, c"). | ||
Unclosed { | ||
/// Index at which the delimiter was opened (e.g., for parentheses, the index of the relevant '('). | ||
opened: usize, | ||
/// Type of thing that wasn't closed (e.g. parentheses). | ||
delimiter: (), | ||
}, | ||
/// Ended on a user-defined non-accepting state. | ||
UserDefined { | ||
/// User-defined error message. | ||
messages: &'static [&'static str], | ||
}, | ||
} | ||
|
||
type R<I> = Result<(Option<(usize, (), Option<F<I>>)>, ()), Error>; | ||
|
||
#[repr(transparent)] | ||
struct F<I>(fn(&mut I, Option<()>, ()) -> R<I>); | ||
|
||
#[inline] | ||
pub fn parse<I: IntoIterator<Item = u8>>(input: I) -> Result<(), Error> { | ||
match state_1(&mut input.into_iter().enumerate(), None, Default::default())? { | ||
(None, out) => Ok(out), | ||
(Some((index, context, None)), out) => panic!("Some(({index:?}, {context:?}, None))"), | ||
(Some((index, delimiter, Some(F(_)))), _) => Err(Error::Unopened { | ||
index, | ||
delimiter, | ||
instead: None, | ||
}), | ||
} | ||
} | ||
|
||
#[inline] | ||
fn state_0<I: Iterator<Item = (usize, u8)>>(input: &mut I, context: Option<()>, acc: ()) -> R<I> { | ||
match input.next() { | ||
None => Ok((None, acc)), | ||
Some((index, token)) => match (&context, &token) { | ||
_ => Err(Error::Absurd { index, token }), | ||
}, | ||
} | ||
} | ||
|
||
#[inline] | ||
fn state_1<I: Iterator<Item = (usize, u8)>>(input: &mut I, context: Option<()>, acc: ()) -> R<I> { | ||
match input.next() { | ||
None => Err(Error::UserDefined { | ||
messages: &[ | ||
"Expected only a single token on [b\'0\'..=b\'9\'] but got another token after it", | ||
], | ||
}), | ||
Some((index, token)) => match (&context, &token) { | ||
(&_, &(b'0'..=b'9')) => match state_0(input, context, (|(), i| i)(acc, token))? { | ||
(None, _) => todo!(), | ||
(done @ Some((_, _, None)), acc) => Ok((done, acc)), | ||
(Some((idx, ctx, Some(F(f)))), out) => f(input, Some(ctx), out), | ||
}, | ||
_ => Err(Error::Absurd { index, token }), | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use core::iter; | ||
|
||
quickcheck::quickcheck! { | ||
fn correct(i: u8) -> bool { | ||
let d = b'0' + (i % 10); | ||
crate::parser::parse(iter::once(d)) == Ok(d) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.