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 specify_section::*;
pub use udp_declaration_and_instantiation::*; 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 { mod thread_context {
use crate::parser::RECURSIVE_FLAG_WORDS;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
pub struct ParserIndex { pub struct ParserIndex {
index: HashMap<&'static str, u32>, index: HashMap<&'static str, usize>,
allocated: [u128; 10], allocated: [u128; RECURSIVE_FLAG_WORDS],
} }
impl ParserIndex { 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) { if let Some(x) = self.index.get(key) {
Some(*x) Some(*x)
} else { } else {
for i in 0..1280u32 { for i in 0..128 * RECURSIVE_FLAG_WORDS {
let upper = (i / 128) as usize; let upper = i / 128;
let lower = i % 128; let lower = i % 128;
if ((self.allocated[upper] >> lower) & 1) == 0 { if ((self.allocated[upper] >> lower) & 1) == 0 {
let val = 1u128 << lower; let val = 1u128 << lower;
@ -56,7 +58,7 @@ mod thread_context {
thread_local!( thread_local!(
pub static PARSER_INDEX: RefCell<ParserIndex> = { 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)?; 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 { pub fn check_recursive_flag(s: Span, id: usize) -> bool {
let upper = (id / 128) as usize; let upper = id / 128;
let lower = id % 128; let lower = id % 128;
((s.extra[upper] >> lower) & 1) == 1 ((s.extra[upper] >> lower) & 1) == 1
} }
pub fn set_bit(s: Span, id: u32, bit: bool) -> Span { pub fn set_recursive_flag(s: Span, id: usize, bit: bool) -> Span {
let upper = (id / 128) as usize; let upper = id / 128;
let lower = id % 128; let lower = id % 128;
let val = if bit { 1u128 << lower } else { 0u128 }; 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 { Span {
offset: s.offset, offset: s.offset,
line: s.line, line: s.line,
fragment: s.fragment, fragment: s.fragment,
extra: [0; 10], extra: [0; RECURSIVE_FLAG_WORDS],
} }
} }
@ -252,7 +252,7 @@ pub fn clear_bit(s: Span) -> Span {
#[cfg(test)] #[cfg(test)]
macro_rules! parser_test { macro_rules! parser_test {
( $x:expr, $y:expr, $z:pat ) => { ( $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 { if let $z = ret {
} else { } else {
assert!(false, "{:?}", ret) assert!(false, "{:?}", ret)

View File

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