#![deny(clippy::all, clippy::if_not_else, clippy::enum_glob_use)]
extern crate proc_macro;
use std::iter::Peekable;
use proc_macro2::TokenTree::{Group, Literal, Punct};
use proc_macro2::{token_stream, TokenStream, TokenTree};
use quote::quote;
#[proc_macro]
pub fn generate_state_changes(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let item: TokenStream = item.into();
let mut iter = item.into_iter().peekable();
let fn_name = iter.next().unwrap();
expect_punct(&mut iter, ',');
let assignments_stream = states_stream(&mut iter);
quote!(
const fn #fn_name() -> [[u8; 256]; 16] {
let mut state_changes = [[0; 256]; 16];
#assignments_stream
state_changes
}
)
.into()
}
fn states_stream(iter: &mut impl Iterator<Item = TokenTree>) -> TokenStream {
let mut states_stream = next_group(iter).into_iter().peekable();
let mut tokens = quote!();
while states_stream.peek().is_some() {
tokens.extend(state_entry_stream(&mut states_stream));
optional_punct(&mut states_stream, ',');
}
tokens
}
fn state_entry_stream(iter: &mut Peekable<token_stream::IntoIter>) -> TokenStream {
let state = iter.next().unwrap();
let mut changes_stream = next_group(iter).into_iter().peekable();
let mut tokens = quote!();
while changes_stream.peek().is_some() {
tokens.extend(change_stream(&mut changes_stream, &state));
optional_punct(&mut changes_stream, ',');
}
tokens
}
fn change_stream(iter: &mut Peekable<token_stream::IntoIter>, state: &TokenTree) -> TokenStream {
let start = next_usize(iter);
let end = if optional_punct(iter, '.') {
expect_punct(iter, '.');
expect_punct(iter, '=');
next_usize(iter)
} else {
start
};
expect_punct(iter, '=');
expect_punct(iter, '>');
let mut target_change_stream = next_group(iter).into_iter().peekable();
let mut tokens = quote!();
while target_change_stream.peek().is_some() {
let (target_state, target_action) = target_change(&mut target_change_stream);
for byte in start..=end {
tokens.extend(quote!(
state_changes[State::#state as usize][#byte] =
pack(State::#target_state, Action::#target_action);
));
}
}
tokens
}
fn target_change(iter: &mut Peekable<token_stream::IntoIter>) -> (TokenTree, TokenTree) {
let target_state = iter.next().unwrap();
expect_punct(iter, ',');
let target_action = iter.next().unwrap();
(target_state, target_action)
}
fn optional_punct(iter: &mut Peekable<token_stream::IntoIter>, c: char) -> bool {
match iter.peek() {
Some(Punct(punct)) if punct.as_char() == c => iter.next().is_some(),
_ => false,
}
}
fn expect_punct(iter: &mut impl Iterator<Item = TokenTree>, c: char) {
match iter.next() {
Some(Punct(ref punct)) if punct.as_char() == c => (),
token => panic!("Expected punctuation '{}', but got {:?}", c, token),
}
}
fn next_usize(iter: &mut impl Iterator<Item = TokenTree>) -> usize {
match iter.next() {
Some(Literal(literal)) => {
let literal = literal.to_string();
if let Some(prefix) = literal.strip_prefix("0x") {
usize::from_str_radix(prefix, 16).unwrap()
} else {
literal.parse::<usize>().unwrap()
}
},
token => panic!("Expected literal, but got {:?}", token),
}
}
fn next_group(iter: &mut impl Iterator<Item = TokenTree>) -> TokenStream {
match iter.next() {
Some(Group(group)) => group.stream(),
token => panic!("Expected group, but got {:?}", token),
}
}