mirror of
https://github.com/Noratrieb/mono-fmt.git
synced 2026-01-14 15:25:08 +01:00
state!
This commit is contained in:
parent
5b1531c38e
commit
6273a35732
2 changed files with 81 additions and 0 deletions
|
|
@ -11,6 +11,8 @@ use syn::{
|
||||||
Expr, LitStr, Token,
|
Expr, LitStr, Token,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mod parser;
|
||||||
|
|
||||||
// TODO: Rewrite using state machine please
|
// TODO: Rewrite using state machine please
|
||||||
|
|
||||||
struct Input {
|
struct Input {
|
||||||
|
|
|
||||||
79
mono-fmt-macro/src/parser.rs
Normal file
79
mono-fmt-macro/src/parser.rs
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
use std::{iter::Peekable, str::Chars};
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Default)]
|
||||||
|
pub struct Align {
|
||||||
|
amount: usize,
|
||||||
|
char: Option<char>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Default)]
|
||||||
|
pub enum Argument {
|
||||||
|
#[default]
|
||||||
|
Positional,
|
||||||
|
PositionalExplicit(usize),
|
||||||
|
Keyword(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Default)]
|
||||||
|
pub enum FmtType {
|
||||||
|
#[default]
|
||||||
|
Default,
|
||||||
|
Debug,
|
||||||
|
LowerHex,
|
||||||
|
UpperHex,
|
||||||
|
Other(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Default)]
|
||||||
|
pub struct FmtSpec {
|
||||||
|
arg: Argument,
|
||||||
|
align: Option<Align>,
|
||||||
|
sign: Option<char>,
|
||||||
|
alternate: bool,
|
||||||
|
zero: bool,
|
||||||
|
width: Option<usize>,
|
||||||
|
precision: Option<usize>,
|
||||||
|
kind: FmtType,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FmtSpecParser<'a> {
|
||||||
|
chars: &'a mut Peekable<Chars<'a>>,
|
||||||
|
state: State,
|
||||||
|
argument: FmtSpec,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
enum State {
|
||||||
|
Initial,
|
||||||
|
Argument,
|
||||||
|
// : here
|
||||||
|
Fill,
|
||||||
|
Align,
|
||||||
|
Sign,
|
||||||
|
Zero,
|
||||||
|
Width,
|
||||||
|
Precision,
|
||||||
|
Type,
|
||||||
|
Done,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> FmtSpecParser<'a> {
|
||||||
|
pub fn new(chars: &'a mut Peekable<Chars<'a>>) -> Self {
|
||||||
|
Self {
|
||||||
|
chars,
|
||||||
|
state: State::Initial,
|
||||||
|
argument: FmtSpec::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(mut self) -> Result<FmtSpec, ()> {
|
||||||
|
while self.state != State::Done {
|
||||||
|
self.step()?;
|
||||||
|
}
|
||||||
|
Ok(self.argument)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn step(&mut self) -> Result<(), ()> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue