Refactoring

This commit is contained in:
dalance 2019-07-10 20:29:13 +09:00
parent 9983826bdf
commit bb76dcf854
4 changed files with 209 additions and 102 deletions

View File

@ -21,13 +21,13 @@ A parser library for System Verilog.
| declaration | type_declarations | | | | | declaration | type_declarations | | | |
| declaration | net_and_variable_types | | | | | declaration | net_and_variable_types | | | |
| declaration | strengths | | | | | declaration | strengths | | | |
| declaration | delays | | | | | declaration | delays | x | | |
| declaration | declaration_lists | | | | | declaration | declaration_lists | | | |
| declaration | declaration_assignments | | | | | declaration | declaration_assignments | | | |
| declaration | declaration_ranges | | | | | declaration | declaration_ranges | | | |
| declaration | function_declarations | | | | | declaration | function_declarations | | | |
| declaration | task_declarations | | | | | declaration | task_declarations | | | |
| declaration | block_item_declarations | | | | | declaration | block_item_declarations | x | x | |
| declaration | interface_declarations | | | | | declaration | interface_declarations | | | |
| declaration | assertion_declarations | | | | | declaration | assertion_declarations | | | |
| declaration | covergroup_declarations | | | | | declaration | covergroup_declarations | | | |
@ -40,7 +40,7 @@ A parser library for System Verilog.
| instantiations | interface_instantiation | x | x | | | instantiations | interface_instantiation | x | x | |
| instantiations | program_instantiation | x | x | | | instantiations | program_instantiation | x | x | |
| instantiations | checker_instantiation | x | x | | | instantiations | checker_instantiation | x | x | |
| instantiations | generated_instantiation | | | | | instantiations | generated_instantiation | x | x | |
| udp_declaration_and_instantiation | udp_declaration | | | | | udp_declaration_and_instantiation | udp_declaration | | | |
| udp_declaration_and_instantiation | udp_ports | | | | | udp_declaration_and_instantiation | udp_ports | | | |
| udp_declaration_and_instantiation | udp_body | | | | | udp_declaration_and_instantiation | udp_body | | | |

View File

@ -1,8 +1,7 @@
use crate::parser::*; use crate::parser::*;
//use nom::branch::*; use nom::branch::*;
//use nom::combinator::*; use nom::multi::*;
use nom::error::*; use nom::IResult;
use nom::{Err, IResult};
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -21,12 +20,20 @@ pub struct BlockItemDeclarationData<'a> {
#[derive(Debug)] #[derive(Debug)]
pub struct BlockItemDeclarationLocalParameter<'a> { pub struct BlockItemDeclarationLocalParameter<'a> {
pub nodes: (Vec<AttributeInstance<'a>>, LocalParameterDeclaration<'a>), pub nodes: (
Vec<AttributeInstance<'a>>,
LocalParameterDeclaration<'a>,
Symbol<'a>,
),
} }
#[derive(Debug)] #[derive(Debug)]
pub struct BlockItemDeclarationParameter<'a> { pub struct BlockItemDeclarationParameter<'a> {
pub nodes: (Vec<AttributeInstance<'a>>, ParameterDeclaration<'a>), pub nodes: (
Vec<AttributeInstance<'a>>,
ParameterDeclaration<'a>,
Symbol<'a>,
),
} }
#[derive(Debug)] #[derive(Debug)]
@ -37,5 +44,50 @@ pub struct BlockItemDeclarationLet<'a> {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
pub fn block_item_declaration(s: Span) -> IResult<Span, BlockItemDeclaration> { pub fn block_item_declaration(s: Span) -> IResult<Span, BlockItemDeclaration> {
Err(Err::Error(make_error(s, ErrorKind::Fix))) alt((
block_item_declaration_data,
block_item_declaration_local_parameter,
block_item_declaration_parameter,
block_item_declaration_let,
))(s)
}
pub fn block_item_declaration_data(s: Span) -> IResult<Span, BlockItemDeclaration> {
let (s, a) = many0(attribute_instance)(s)?;
let (s, b) = data_declaration(s)?;
Ok((
s,
BlockItemDeclaration::Data(BlockItemDeclarationData { nodes: (a, b) }),
))
}
pub fn block_item_declaration_local_parameter(s: Span) -> IResult<Span, BlockItemDeclaration> {
let (s, a) = many0(attribute_instance)(s)?;
let (s, b) = local_parameter_declaration(s)?;
let (s, c) = symbol(";")(s)?;
Ok((
s,
BlockItemDeclaration::LocalParameter(BlockItemDeclarationLocalParameter {
nodes: (a, b, c),
}),
))
}
pub fn block_item_declaration_parameter(s: Span) -> IResult<Span, BlockItemDeclaration> {
let (s, a) = many0(attribute_instance)(s)?;
let (s, b) = parameter_declaration(s)?;
let (s, c) = symbol(";")(s)?;
Ok((
s,
BlockItemDeclaration::Parameter(BlockItemDeclarationParameter { nodes: (a, b, c) }),
))
}
pub fn block_item_declaration_let(s: Span) -> IResult<Span, BlockItemDeclaration> {
let (s, a) = many0(attribute_instance)(s)?;
let (s, b) = let_declaration(s)?;
Ok((
s,
BlockItemDeclaration::Let(BlockItemDeclarationLet { nodes: (a, b) }),
))
} }

View File

@ -8,27 +8,46 @@ use nom::{Err, IResult};
#[derive(Debug)] #[derive(Debug)]
pub enum Delay3<'a> { pub enum Delay3<'a> {
DelayValue(DelayValue<'a>), Single(Delay3Single<'a>),
Mintypmax(Delay3Mintypmax<'a>), Mintypmax(Delay3Mintypmax<'a>),
} }
#[derive(Debug)]
pub struct Delay3Single<'a> {
pub nodes: (Symbol<'a>, DelayValue<'a>),
}
#[derive(Debug)] #[derive(Debug)]
pub struct Delay3Mintypmax<'a> { pub struct Delay3Mintypmax<'a> {
pub nodes: ( pub nodes: (
Symbol<'a>,
Paren<
'a,
(
MintypmaxExpression<'a>, MintypmaxExpression<'a>,
Option<(MintypmaxExpression<'a>, Option<MintypmaxExpression<'a>>)>, Option<(MintypmaxExpression<'a>, Option<MintypmaxExpression<'a>>)>,
), ),
>,
),
} }
#[derive(Debug)] #[derive(Debug)]
pub enum Delay2<'a> { pub enum Delay2<'a> {
DelayValue(DelayValue<'a>), Single(Delay2Single<'a>),
Mintypmax(Delay2Mintypmax<'a>), Mintypmax(Delay2Mintypmax<'a>),
} }
#[derive(Debug)]
pub struct Delay2Single<'a> {
pub nodes: (Symbol<'a>, DelayValue<'a>),
}
#[derive(Debug)] #[derive(Debug)]
pub struct Delay2Mintypmax<'a> { pub struct Delay2Mintypmax<'a> {
pub nodes: (MintypmaxExpression<'a>, Option<MintypmaxExpression<'a>>), pub nodes: (
Symbol<'a>,
Paren<'a, (MintypmaxExpression<'a>, Option<MintypmaxExpression<'a>>)>,
),
} }
#[derive(Debug)] #[derive(Debug)]
@ -37,7 +56,7 @@ pub enum DelayValue<'a> {
RealNumber(RealNumber<'a>), RealNumber(RealNumber<'a>),
Identifier(Identifier<'a>), Identifier(Identifier<'a>),
TimeLiteral(TimeLiteral<'a>), TimeLiteral(TimeLiteral<'a>),
Step1, Step1(Symbol<'a>),
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@ -9,26 +9,41 @@ use nom::IResult;
#[derive(Debug)] #[derive(Debug)]
pub struct GenerateRegion<'a> { pub struct GenerateRegion<'a> {
pub nodes: (Vec<GenerateItem<'a>>,), pub nodes: (Symbol<'a>, Vec<GenerateItem<'a>>, Symbol<'a>),
} }
#[derive(Debug)] #[derive(Debug)]
pub struct LoopGenerateConstruct<'a> { pub struct LoopGenerateConstruct<'a> {
pub nodes: ( pub nodes: (
Symbol<'a>,
Paren<
'a,
(
GenvarInitialization<'a>, GenvarInitialization<'a>,
Symbol<'a>,
GenvarExpression<'a>, GenvarExpression<'a>,
Symbol<'a>,
GenvarIteration<'a>, GenvarIteration<'a>,
),
>,
GenerateBlock<'a>, GenerateBlock<'a>,
), ),
} }
#[derive(Debug)] #[derive(Debug)]
pub struct GenvarInitialization<'a> { pub struct GenvarInitialization<'a> {
pub nodes: (Option<Genvar>, GenvarIdentifier<'a>, ConstantExpression<'a>), pub nodes: (
Option<Genvar<'a>>,
GenvarIdentifier<'a>,
Symbol<'a>,
ConstantExpression<'a>,
),
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Genvar {} pub struct Genvar<'a> {
pub nodes: (Symbol<'a>,),
}
#[derive(Debug)] #[derive(Debug)]
pub enum GenvarIteration<'a> { pub enum GenvarIteration<'a> {
@ -65,15 +80,21 @@ pub enum ConditionalGenerateConstruct<'a> {
#[derive(Debug)] #[derive(Debug)]
pub struct IfGenerateConstruct<'a> { pub struct IfGenerateConstruct<'a> {
pub nodes: ( pub nodes: (
ConstantExpression<'a>, Symbol<'a>,
Paren<'a, ConstantExpression<'a>>,
GenerateBlock<'a>, GenerateBlock<'a>,
Option<GenerateBlock<'a>>, Option<(Symbol<'a>, GenerateBlock<'a>)>,
), ),
} }
#[derive(Debug)] #[derive(Debug)]
pub struct CaseGenerateConstruct<'a> { pub struct CaseGenerateConstruct<'a> {
pub nodes: (ConstantExpression<'a>, Vec<CaseGenerateItem<'a>>), pub nodes: (
Symbol<'a>,
Paren<'a, ConstantExpression<'a>>,
Vec<CaseGenerateItem<'a>>,
Symbol<'a>,
),
} }
#[derive(Debug)] #[derive(Debug)]
@ -84,73 +105,74 @@ pub enum CaseGenerateItem<'a> {
#[derive(Debug)] #[derive(Debug)]
pub struct CaseGenerateItemNondefault<'a> { pub struct CaseGenerateItemNondefault<'a> {
pub nodes: (Vec<ConstantExpression<'a>>, GenerateBlock<'a>), pub nodes: (
List<Symbol<'a>, ConstantExpression<'a>>,
Symbol<'a>,
GenerateBlock<'a>,
),
} }
#[derive(Debug)] #[derive(Debug)]
pub struct CaseGenerateItemDefault<'a> { pub struct CaseGenerateItemDefault<'a> {
pub nodes: (GenerateBlock<'a>,), pub nodes: (Symbol<'a>, Option<Symbol<'a>>, GenerateBlock<'a>),
} }
#[derive(Debug)] #[derive(Debug)]
pub enum GenerateBlock<'a> { pub enum GenerateBlock<'a> {
Single(GenerateItem<'a>), GenerateItem(GenerateItem<'a>),
Multiple(GenerateBlockMultiple<'a>), Multiple(GenerateBlockMultiple<'a>),
} }
#[derive(Debug)] #[derive(Debug)]
pub struct GenerateBlockMultiple<'a> { pub struct GenerateBlockMultiple<'a> {
pub nodes: ( pub nodes: (
Option<GenerateBlockIdentifier<'a>>, Option<(GenerateBlockIdentifier<'a>, Symbol<'a>)>,
Option<GenerateBlockIdentifier<'a>>, Symbol<'a>,
Option<(Symbol<'a>, GenerateBlockIdentifier<'a>)>,
Vec<GenerateItem<'a>>, Vec<GenerateItem<'a>>,
Option<GenerateBlockIdentifier<'a>>, Symbol<'a>,
Option<(Symbol<'a>, GenerateBlockIdentifier<'a>)>,
), ),
} }
#[derive(Debug)] #[derive(Debug)]
pub enum GenerateItem<'a> { pub enum GenerateItem<'a> {
Module(ModuleOrGenerateItem<'a>), ModuleOrGenerateItem(ModuleOrGenerateItem<'a>),
Interface(InterfaceOrGenerateItem<'a>), InterfaceOrGenerateItem(InterfaceOrGenerateItem<'a>),
Checker(CheckerOrGenerateItem<'a>), CheckerOrGenerateItem(CheckerOrGenerateItem<'a>),
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
pub fn generate_region(s: Span) -> IResult<Span, GenerateRegion> { pub fn generate_region(s: Span) -> IResult<Span, GenerateRegion> {
let (s, _) = symbol("generate")(s)?; let (s, a) = symbol("generate")(s)?;
let (s, x) = many0(generate_item)(s)?; let (s, b) = many0(generate_item)(s)?;
let (s, _) = symbol("endgenerate")(s)?; let (s, c) = symbol("endgenerate")(s)?;
Ok((s, GenerateRegion { nodes: (x,) })) Ok((s, GenerateRegion { nodes: (a, b, c) }))
} }
pub fn loop_generate_construct(s: Span) -> IResult<Span, LoopGenerateConstruct> { pub fn loop_generate_construct(s: Span) -> IResult<Span, LoopGenerateConstruct> {
let (s, _) = symbol("for")(s)?; let (s, a) = symbol("for")(s)?;
let (s, _) = symbol("(")(s)?; let (s, b) = paren2(tuple((
let (s, x) = generate_initialization(s)?; generate_initialization,
let (s, _) = symbol(";")(s)?; symbol(";"),
let (s, y) = genvar_expression(s)?; genvar_expression,
let (s, _) = symbol(";")(s)?; symbol(";"),
let (s, z) = genvar_iteration(s)?; genvar_iteration,
let (s, _) = symbol(")")(s)?; )))(s)?;
let (s, v) = generate_block(s)?; let (s, c) = generate_block(s)?;
Ok(( Ok((s, LoopGenerateConstruct { nodes: (a, b, c) }))
s,
LoopGenerateConstruct {
nodes: (x, y, z, v),
},
))
} }
pub fn generate_initialization(s: Span) -> IResult<Span, GenvarInitialization> { pub fn generate_initialization(s: Span) -> IResult<Span, GenvarInitialization> {
let (s, x) = opt(symbol("genvar"))(s)?; let (s, a) = opt(map(symbol("genvar"), |x| Genvar { nodes: (x,) }))(s)?;
let (s, y) = genvar_identifier(s)?; let (s, b) = genvar_identifier(s)?;
let (s, _) = symbol("=")(s)?; let (s, c) = symbol("=")(s)?;
let (s, z) = constant_expression(s)?; let (s, d) = constant_expression(s)?;
Ok(( Ok((
s, s,
GenvarInitialization { GenvarInitialization {
nodes: (x.map(|_| Genvar {}), y, z), nodes: (a, b, c, d),
}, },
)) ))
} }
@ -164,30 +186,30 @@ pub fn genvar_iteration(s: Span) -> IResult<Span, GenvarIteration> {
} }
pub fn genvar_iteration_assignment(s: Span) -> IResult<Span, GenvarIteration> { pub fn genvar_iteration_assignment(s: Span) -> IResult<Span, GenvarIteration> {
let (s, x) = genvar_identifier(s)?; let (s, a) = genvar_identifier(s)?;
let (s, y) = assignment_operator(s)?; let (s, b) = assignment_operator(s)?;
let (s, z) = genvar_expression(s)?; let (s, c) = genvar_expression(s)?;
Ok(( Ok((
s, s,
GenvarIteration::Assignment(GenvarIterationAssignment { nodes: (x, y, z) }), GenvarIteration::Assignment(GenvarIterationAssignment { nodes: (a, b, c) }),
)) ))
} }
pub fn genvar_iteration_prefix(s: Span) -> IResult<Span, GenvarIteration> { pub fn genvar_iteration_prefix(s: Span) -> IResult<Span, GenvarIteration> {
let (s, x) = inc_or_dec_operator(s)?; let (s, a) = inc_or_dec_operator(s)?;
let (s, y) = genvar_identifier(s)?; let (s, b) = genvar_identifier(s)?;
Ok(( Ok((
s, s,
GenvarIteration::Prefix(GenvarIterationPrefix { nodes: (x, y) }), GenvarIteration::Prefix(GenvarIterationPrefix { nodes: (a, b) }),
)) ))
} }
pub fn genvar_iteration_suffix(s: Span) -> IResult<Span, GenvarIteration> { pub fn genvar_iteration_suffix(s: Span) -> IResult<Span, GenvarIteration> {
let (s, x) = genvar_identifier(s)?; let (s, a) = genvar_identifier(s)?;
let (s, y) = inc_or_dec_operator(s)?; let (s, b) = inc_or_dec_operator(s)?;
Ok(( Ok((
s, s,
GenvarIteration::Suffix(GenvarIterationSuffix { nodes: (x, y) }), GenvarIteration::Suffix(GenvarIterationSuffix { nodes: (a, b) }),
)) ))
} }
@ -203,19 +225,29 @@ pub fn conditional_generate_construct(s: Span) -> IResult<Span, ConditionalGener
} }
pub fn if_generate_construct(s: Span) -> IResult<Span, IfGenerateConstruct> { pub fn if_generate_construct(s: Span) -> IResult<Span, IfGenerateConstruct> {
let (s, _) = symbol("if")(s)?; let (s, a) = symbol("if")(s)?;
let (s, x) = paren(constant_expression)(s)?; let (s, b) = paren2(constant_expression)(s)?;
let (s, y) = generate_block(s)?; let (s, c) = generate_block(s)?;
let (s, z) = opt(preceded(symbol("else"), generate_block))(s)?; let (s, d) = opt(pair(symbol("else"), generate_block))(s)?;
Ok((s, IfGenerateConstruct { nodes: (x, y, z) })) Ok((
s,
IfGenerateConstruct {
nodes: (a, b, c, d),
},
))
} }
pub fn case_generate_construct(s: Span) -> IResult<Span, CaseGenerateConstruct> { pub fn case_generate_construct(s: Span) -> IResult<Span, CaseGenerateConstruct> {
let (s, _) = symbol("case")(s)?; let (s, a) = symbol("case")(s)?;
let (s, x) = paren(constant_expression)(s)?; let (s, b) = paren2(constant_expression)(s)?;
let (s, y) = many1(case_generate_item)(s)?; let (s, c) = many1(case_generate_item)(s)?;
let (s, _) = symbol("endcase")(s)?; let (s, d) = symbol("endcase")(s)?;
Ok((s, CaseGenerateConstruct { nodes: (x, y) })) Ok((
s,
CaseGenerateConstruct {
nodes: (a, b, c, d),
},
))
} }
pub fn case_generate_item(s: Span) -> IResult<Span, CaseGenerateItem> { pub fn case_generate_item(s: Span) -> IResult<Span, CaseGenerateItem> {
@ -223,54 +255,58 @@ pub fn case_generate_item(s: Span) -> IResult<Span, CaseGenerateItem> {
} }
pub fn case_generate_item_nondefault(s: Span) -> IResult<Span, CaseGenerateItem> { pub fn case_generate_item_nondefault(s: Span) -> IResult<Span, CaseGenerateItem> {
let (s, x) = separated_nonempty_list(symbol(","), constant_expression)(s)?; let (s, a) = list(symbol(","), constant_expression)(s)?;
let (s, _) = symbol(":")(s)?; let (s, b) = symbol(":")(s)?;
let (s, y) = generate_block(s)?; let (s, c) = generate_block(s)?;
Ok(( Ok((
s, s,
CaseGenerateItem::Nondefault(CaseGenerateItemNondefault { nodes: (x, y) }), CaseGenerateItem::Nondefault(CaseGenerateItemNondefault { nodes: (a, b, c) }),
)) ))
} }
pub fn case_generate_item_default(s: Span) -> IResult<Span, CaseGenerateItem> { pub fn case_generate_item_default(s: Span) -> IResult<Span, CaseGenerateItem> {
let (s, _) = symbol("default")(s)?; let (s, a) = symbol("default")(s)?;
let (s, _) = opt(symbol(":"))(s)?; let (s, b) = opt(symbol(":"))(s)?;
let (s, x) = generate_block(s)?; let (s, c) = generate_block(s)?;
Ok(( Ok((
s, s,
CaseGenerateItem::Default(CaseGenerateItemDefault { nodes: (x,) }), CaseGenerateItem::Default(CaseGenerateItemDefault { nodes: (a, b, c) }),
)) ))
} }
pub fn generate_block(s: Span) -> IResult<Span, GenerateBlock> { pub fn generate_block(s: Span) -> IResult<Span, GenerateBlock> {
alt((generate_block_single, generate_block_multiple))(s) alt((
} map(generate_item, |x| GenerateBlock::GenerateItem(x)),
generate_block_multiple,
pub fn generate_block_single(s: Span) -> IResult<Span, GenerateBlock> { ))(s)
let (s, x) = generate_item(s)?;
Ok((s, GenerateBlock::Single(x)))
} }
pub fn generate_block_multiple(s: Span) -> IResult<Span, GenerateBlock> { pub fn generate_block_multiple(s: Span) -> IResult<Span, GenerateBlock> {
let (s, x) = opt(terminated(generate_block_identifier, symbol(":")))(s)?; let (s, a) = opt(pair(generate_block_identifier, symbol(":")))(s)?;
let (s, _) = symbol("begin")(s)?; let (s, b) = symbol("begin")(s)?;
let (s, y) = opt(preceded(symbol(":"), generate_block_identifier))(s)?; let (s, c) = opt(pair(symbol(":"), generate_block_identifier))(s)?;
let (s, z) = many0(generate_item)(s)?; let (s, d) = many0(generate_item)(s)?;
let (s, _) = symbol("end")(s)?; let (s, e) = symbol("end")(s)?;
let (s, v) = opt(preceded(symbol(":"), generate_block_identifier))(s)?; let (s, f) = opt(pair(symbol(":"), generate_block_identifier))(s)?;
Ok(( Ok((
s, s,
GenerateBlock::Multiple(GenerateBlockMultiple { GenerateBlock::Multiple(GenerateBlockMultiple {
nodes: (x, y, z, v), nodes: (a, b, c, d, e, f),
}), }),
)) ))
} }
pub fn generate_item(s: Span) -> IResult<Span, GenerateItem> { pub fn generate_item(s: Span) -> IResult<Span, GenerateItem> {
alt(( alt((
map(module_or_generate_item, |x| GenerateItem::Module(x)), map(module_or_generate_item, |x| {
map(interface_or_generate_item, |x| GenerateItem::Interface(x)), GenerateItem::ModuleOrGenerateItem(x)
map(checker_or_generate_item, |x| GenerateItem::Checker(x)), }),
map(interface_or_generate_item, |x| {
GenerateItem::InterfaceOrGenerateItem(x)
}),
map(checker_or_generate_item, |x| {
GenerateItem::CheckerOrGenerateItem(x)
}),
))(s) ))(s)
} }