Refactoring

This commit is contained in:
dalance 2019-07-17 19:06:50 +09:00
parent 7a27205333
commit a6c0a54f3c
3 changed files with 20 additions and 18 deletions

View File

@ -21,25 +21,27 @@ pub use source_text::*;
pub use specify_section::*;
pub use udp_declaration_and_instantiation::*;
pub type Span<'a> = nom_locate::LocatedSpanEx<&'a str, [u128; 10]>;
pub const RECURSIVE_FLAG_WORDS: usize = 1;
pub type Span<'a> = nom_locate::LocatedSpanEx<&'a str, [u128; RECURSIVE_FLAG_WORDS]>;
mod thread_context {
use crate::parser::RECURSIVE_FLAG_WORDS;
use std::cell::RefCell;
use std::collections::HashMap;
pub struct ParserIndex {
index: HashMap<&'static str, u32>,
allocated: [u128; 10],
index: HashMap<&'static str, usize>,
allocated: [u128; RECURSIVE_FLAG_WORDS],
}
impl ParserIndex {
pub fn get(&mut self, key: &'static str) -> Option<u32> {
pub fn get(&mut self, key: &'static str) -> Option<usize> {
if let Some(x) = self.index.get(key) {
Some(*x)
} else {
for i in 0..1280u32 {
let upper = (i / 128) as usize;
for i in 0..128 * RECURSIVE_FLAG_WORDS {
let upper = i / 128;
let lower = i % 128;
if ((self.allocated[upper] >> lower) & 1) == 0 {
let val = 1u128 << lower;
@ -56,7 +58,7 @@ mod thread_context {
thread_local!(
pub static PARSER_INDEX: RefCell<ParserIndex> = {
RefCell::new(ParserIndex{index: HashMap::new(), allocated: [0;10]})
RefCell::new(ParserIndex{index: HashMap::new(), allocated: [0;RECURSIVE_FLAG_WORDS]})
}
);
}

View File

@ -70,7 +70,7 @@ pub fn symbol<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult<Span<'a>, Symbol<'
);
}
let (s, x) = map(ws(tag(t.clone())), |x| Symbol { nodes: x })(s)?;
Ok((clear_bit(s), x))
Ok((clear_recursive_flags(s), x))
}
}
@ -214,15 +214,15 @@ pub fn concat<'a>(a: Span<'a>, b: Span<'a>) -> Option<Span<'a>> {
}
}
pub fn check_bit(s: Span, id: u32) -> bool {
let upper = (id / 128) as usize;
pub fn check_recursive_flag(s: Span, id: usize) -> bool {
let upper = id / 128;
let lower = id % 128;
((s.extra[upper] >> lower) & 1) == 1
}
pub fn set_bit(s: Span, id: u32, bit: bool) -> Span {
let upper = (id / 128) as usize;
pub fn set_recursive_flag(s: Span, id: usize, bit: bool) -> Span {
let upper = id / 128;
let lower = id % 128;
let val = if bit { 1u128 << lower } else { 0u128 };
@ -238,12 +238,12 @@ pub fn set_bit(s: Span, id: u32, bit: bool) -> Span {
}
}
pub fn clear_bit(s: Span) -> Span {
pub fn clear_recursive_flags(s: Span) -> Span {
Span {
offset: s.offset,
line: s.line,
fragment: s.fragment,
extra: [0; 10],
extra: [0; RECURSIVE_FLAG_WORDS],
}
}
@ -252,7 +252,7 @@ pub fn clear_bit(s: Span) -> Span {
#[cfg(test)]
macro_rules! parser_test {
( $x:expr, $y:expr, $z:pat ) => {
let ret = all_consuming($x)(Span::new_extra($y, [0; 10]));
let ret = all_consuming($x)(Span::new_extra($y, [0; RECURSIVE_FLAG_WORDS]));
if let $z = ret {
} else {
assert!(false, "{:?}", ret)

View File

@ -137,7 +137,7 @@ fn impl_parser(attr: &AttributeArgs, item: &ItemFn) -> TokenStream {
let checker = quote! {
if thread_context::PARSER_INDEX.with(|p| {
if let Some(i) = p.borrow_mut().get(stringify!(#ident)) {
return check_bit(s, i);
return check_recursive_flag(s, i);
} else {
return false
}
@ -154,7 +154,7 @@ fn impl_parser(attr: &AttributeArgs, item: &ItemFn) -> TokenStream {
let before = quote! {
let s = thread_context::PARSER_INDEX.with(|p| {
if let Some(i) = p.borrow_mut().get(stringify!(#ident)) {
set_bit(s, i, true)
set_recursive_flag(s, i, true)
} else {
if cfg!(feature = "trace") {
println!("{:<64} : allocate failed", stringify!(#ident));
@ -180,7 +180,7 @@ fn impl_parser(attr: &AttributeArgs, item: &ItemFn) -> TokenStream {
let body = parse_macro_input!(body as Stmt);
let after = quote! {
Ok((clear_bit(s), ret))
Ok((clear_recursive_flags(s), ret))
};
let after: TokenStream = after.into();
let after = parse_macro_input!(after as Expr);