From 164016ef802e1920c81d361cb41046407cb4dbc4 Mon Sep 17 00:00:00 2001 From: dalance Date: Mon, 15 Jul 2019 01:52:09 +0900 Subject: [PATCH] Refactoring --- README.md | 4 +- src/parser/declarations/port_declarations.rs | 6 +- src/parser/source_text/module_items.rs | 348 ++++++++++++++++-- .../module_parameters_and_ports.rs | 312 ++++++++++++++-- 4 files changed, 602 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 385a7fe..4eb0e7c 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ A parser library for System Verilog. | --------------------------------- | ------------------------------------- | ---- | ------ | ---- | | source_text | library_source_text | x | x | x | | source_text | system_verilog_source_text | x | x | | -| source_text | module_parameters_and_ports | | | | -| source_text | module_items | | | | +| source_text | module_parameters_and_ports | x | x | | +| source_text | module_items | x | x | | | source_text | configuration_source_text | | | | | source_text | interface_items | | | | | source_text | program_items | | | | diff --git a/src/parser/declarations/port_declarations.rs b/src/parser/declarations/port_declarations.rs index 9814fbc..032bb26 100644 --- a/src/parser/declarations/port_declarations.rs +++ b/src/parser/declarations/port_declarations.rs @@ -72,14 +72,14 @@ pub struct RefDeclaration<'a> { // ----------------------------------------------------------------------------- -pub fn inout_declaratrion(s: Span) -> IResult { +pub fn inout_declaration(s: Span) -> IResult { let (s, a) = symbol("inout")(s)?; let (s, b) = net_port_type(s)?; let (s, c) = list_of_port_identifiers(s)?; Ok((s, InoutDeclaration { nodes: (a, b, c) })) } -pub fn input_declaratrion(s: Span) -> IResult { +pub fn input_declaration(s: Span) -> IResult { alt((input_declaration_net, input_declaration_variable))(s) } @@ -103,7 +103,7 @@ pub fn input_declaration_variable(s: Span) -> IResult { )) } -pub fn output_declaratrion(s: Span) -> IResult { +pub fn output_declaration(s: Span) -> IResult { alt((output_declaration_net, output_declaration_variable))(s) } diff --git a/src/parser/source_text/module_items.rs b/src/parser/source_text/module_items.rs index 966f236..ee022da 100644 --- a/src/parser/source_text/module_items.rs +++ b/src/parser/source_text/module_items.rs @@ -1,9 +1,10 @@ use crate::ast::*; use crate::parser::*; -//use nom::branch::*; -//use nom::combinator::*; -use nom::error::*; -use nom::{Err, IResult}; +use nom::branch::*; +use nom::combinator::*; +use nom::multi::*; +use nom::sequence::*; +use nom::IResult; // ----------------------------------------------------------------------------- @@ -17,22 +18,38 @@ pub enum ElaborationSystemTask<'a> { #[derive(Debug, Node)] pub struct ElaborationSystemTaskFatal<'a> { - pub nodes: (Option<(FinishNumber<'a>, Option>)>,), + pub nodes: ( + Symbol<'a>, + Option, Option<(Symbol<'a>, ListOfArguments<'a>)>)>>, + Symbol<'a>, + ), } #[derive(Debug, Node)] pub struct ElaborationSystemTaskError<'a> { - pub nodes: (Option>>,), + pub nodes: ( + Symbol<'a>, + Option>>>, + Symbol<'a>, + ), } #[derive(Debug, Node)] pub struct ElaborationSystemTaskWarning<'a> { - pub nodes: (Option>>,), + pub nodes: ( + Symbol<'a>, + Option>>>, + Symbol<'a>, + ), } #[derive(Debug, Node)] pub struct ElaborationSystemTaskInfo<'a> { - pub nodes: (Option>>,), + pub nodes: ( + Symbol<'a>, + Option>>>, + Symbol<'a>, + ), } #[derive(Debug, Node)] @@ -61,7 +78,7 @@ pub enum ModuleCommonItem<'a> { #[derive(Debug, Node)] pub enum ModuleItem<'a> { - PortDeclaratoin(PortDeclaration<'a>), + PortDeclaration((PortDeclaration<'a>, Symbol<'a>)), NonPortModuleItem(NonPortModuleItem<'a>), } @@ -105,17 +122,23 @@ pub enum ModuleOrGenerateItemDeclaration<'a> { GenvarDeclaration(GenvarDeclaration<'a>), ClockingDeclaration(ClockingDeclaration<'a>), Clocking(ModuleOrGenerateItemDeclarationClocking<'a>), - Expression(ModuleOrGenerateItemDeclarationExpression<'a>), + Disable(ModuleOrGenerateItemDeclarationDisable<'a>), } #[derive(Debug, Node)] pub struct ModuleOrGenerateItemDeclarationClocking<'a> { - pub nodes: (ClockingIdentifier<'a>), + pub nodes: (Symbol<'a>, Symbol<'a>, ClockingIdentifier<'a>, Symbol<'a>), } #[derive(Debug, Node)] -pub struct ModuleOrGenerateItemDeclarationExpression<'a> { - pub nodes: (ExpressionOrDist<'a>), +pub struct ModuleOrGenerateItemDeclarationDisable<'a> { + pub nodes: ( + Symbol<'a>, + Symbol<'a>, + Symbol<'a>, + ExpressionOrDist<'a>, + Symbol<'a>, + ), } #[derive(Debug, Node)] @@ -137,7 +160,7 @@ pub struct NonPortModuleItemSpecparam<'a> { #[derive(Debug, Node)] pub struct ParameterOverride<'a> { - pub nodes: (ListOfDefparamAssignments<'a>,), + pub nodes: (Symbol<'a>, ListOfDefparamAssignments<'a>, Symbol<'a>), } #[derive(Debug, Node)] @@ -149,15 +172,22 @@ pub enum BindDirective<'a> { #[derive(Debug, Node)] pub struct BindDirectiveScope<'a> { pub nodes: ( + Symbol<'a>, BindTargetScope<'a>, - Option>, + Option<(Symbol<'a>, BindTargetInstanceList<'a>)>, BindInstantiation<'a>, + Symbol<'a>, ), } #[derive(Debug, Node)] pub struct BindDirectiveInstance<'a> { - pub nodes: (BindTargetInstanceList<'a>, BindInstantiation<'a>), + pub nodes: ( + Symbol<'a>, + BindTargetInstance<'a>, + BindInstantiation<'a>, + Symbol<'a>, + ), } #[derive(Debug, Node)] @@ -173,7 +203,7 @@ pub struct BindTargetInstance<'a> { #[derive(Debug, Node)] pub struct BindTargetInstanceList<'a> { - pub nodes: (Vec>,), + pub nodes: (List, BindTargetInstance<'a>>,), } #[derive(Debug, Node)] @@ -187,55 +217,313 @@ pub enum BindInstantiation<'a> { // ----------------------------------------------------------------------------- pub fn elaboration_system_task(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + elaboration_system_task_fatal, + elaboration_system_task_error, + elaboration_system_task_warning, + elaboration_system_task_info, + ))(s) +} + +pub fn elaboration_system_task_fatal(s: Span) -> IResult { + let (s, a) = symbol("$fatal")(s)?; + let (s, b) = opt(paren(pair( + finish_number, + opt(pair(symbol(","), list_of_arguments)), + )))(s)?; + let (s, c) = symbol(";")(s)?; + Ok(( + s, + ElaborationSystemTask::Fatal(ElaborationSystemTaskFatal { nodes: (a, b, c) }), + )) +} + +pub fn elaboration_system_task_error(s: Span) -> IResult { + let (s, a) = symbol("$error")(s)?; + let (s, b) = opt(paren(opt(list_of_arguments)))(s)?; + let (s, c) = symbol(";")(s)?; + Ok(( + s, + ElaborationSystemTask::Error(ElaborationSystemTaskError { nodes: (a, b, c) }), + )) +} + +pub fn elaboration_system_task_warning(s: Span) -> IResult { + let (s, a) = symbol("$warning")(s)?; + let (s, b) = opt(paren(opt(list_of_arguments)))(s)?; + let (s, c) = symbol(";")(s)?; + Ok(( + s, + ElaborationSystemTask::Warning(ElaborationSystemTaskWarning { nodes: (a, b, c) }), + )) +} + +pub fn elaboration_system_task_info(s: Span) -> IResult { + let (s, a) = symbol("$info")(s)?; + let (s, b) = opt(paren(opt(list_of_arguments)))(s)?; + let (s, c) = symbol(";")(s)?; + Ok(( + s, + ElaborationSystemTask::Info(ElaborationSystemTaskInfo { nodes: (a, b, c) }), + )) } pub fn finish_number(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("0"), |x| FinishNumber::Zero(x)), + map(symbol("1"), |x| FinishNumber::One(x)), + map(symbol("2"), |x| FinishNumber::Two(x)), + ))(s) } pub fn module_common_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(module_or_generate_item_declaration, |x| { + ModuleCommonItem::ModuleOrGenerateItemDeclaration(x) + }), + map(interface_instantiation, |x| { + ModuleCommonItem::InterfaceInstantiation(x) + }), + map(program_instantiation, |x| { + ModuleCommonItem::ProgramInstantiation(x) + }), + map(assertion_item, |x| ModuleCommonItem::AssertionItem(x)), + map(bind_directive, |x| ModuleCommonItem::BindDirective(x)), + map(continuous_assign, |x| ModuleCommonItem::ContinuousAssign(x)), + map(net_alias, |x| ModuleCommonItem::NetAlias(x)), + map(initial_construct, |x| ModuleCommonItem::InitialConstruct(x)), + map(final_construct, |x| ModuleCommonItem::FinalConstruct(x)), + map(always_construct, |x| ModuleCommonItem::AlwaysConstruct(x)), + map(loop_generate_construct, |x| { + ModuleCommonItem::LoopGenerateConstruct(Box::new(x)) + }), + map(conditional_generate_construct, |x| { + ModuleCommonItem::ConditionalGenerateConstruct(Box::new(x)) + }), + map(elaboration_system_task, |x| { + ModuleCommonItem::ElaborationSystemTask(x) + }), + ))(s) } pub fn module_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(pair(port_declaration, symbol(";")), |x| { + ModuleItem::PortDeclaration(x) + }), + map(non_port_module_item, |x| ModuleItem::NonPortModuleItem(x)), + ))(s) } pub fn module_or_generate_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + module_or_generate_item_parameter, + module_or_generate_item_gate, + module_or_generate_item_udp, + module_or_generate_item_module, + module_or_generate_item_module_item, + ))(s) +} + +pub fn module_or_generate_item_parameter(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = parameter_override(s)?; + Ok(( + s, + ModuleOrGenerateItem::Parameter(ModuleOrGenerateItemParameter { nodes: (a, b) }), + )) +} + +pub fn module_or_generate_item_gate(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = gate_instantiation(s)?; + Ok(( + s, + ModuleOrGenerateItem::Gate(ModuleOrGenerateItemGate { nodes: (a, b) }), + )) +} + +pub fn module_or_generate_item_udp(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = udp_instantiation(s)?; + Ok(( + s, + ModuleOrGenerateItem::Udp(ModuleOrGenerateItemUdp { nodes: (a, b) }), + )) +} + +pub fn module_or_generate_item_module(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = module_instantiation(s)?; + Ok(( + s, + ModuleOrGenerateItem::Module(ModuleOrGenerateItemModule { nodes: (a, b) }), + )) +} + +pub fn module_or_generate_item_module_item(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = module_common_item(s)?; + Ok(( + s, + ModuleOrGenerateItem::ModuleItem(Box::new(ModuleOrGenerateItemModuleItem { + nodes: (a, b), + })), + )) } pub fn module_or_generate_item_declaration( s: Span, ) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(package_or_generate_item_declaration, |x| { + ModuleOrGenerateItemDeclaration::PackageOrGenerateItemDeclaration(x) + }), + map(genvar_declaration, |x| { + ModuleOrGenerateItemDeclaration::GenvarDeclaration(x) + }), + map(clocking_declaration, |x| { + ModuleOrGenerateItemDeclaration::ClockingDeclaration(x) + }), + module_or_generate_item_declaration_clocking, + module_or_generate_item_declaration_disable, + ))(s) +} + +pub fn module_or_generate_item_declaration_clocking( + s: Span, +) -> IResult { + let (s, a) = symbol("default")(s)?; + let (s, b) = symbol("clocking")(s)?; + let (s, c) = clocking_identifier(s)?; + let (s, d) = symbol(";")(s)?; + Ok(( + s, + ModuleOrGenerateItemDeclaration::Clocking(ModuleOrGenerateItemDeclarationClocking { + nodes: (a, b, c, d), + }), + )) +} + +pub fn module_or_generate_item_declaration_disable( + s: Span, +) -> IResult { + let (s, a) = symbol("default")(s)?; + let (s, b) = symbol("disable")(s)?; + let (s, c) = symbol("iff")(s)?; + let (s, d) = expression_or_dist(s)?; + let (s, e) = symbol(";")(s)?; + Ok(( + s, + ModuleOrGenerateItemDeclaration::Disable(ModuleOrGenerateItemDeclarationDisable { + nodes: (a, b, c, d, e), + }), + )) } pub fn non_port_module_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(generate_region, |x| NonPortModuleItem::GenerateRegion(x)), + map(module_or_generate_item, |x| { + NonPortModuleItem::ModuleOrGenerateItem(x) + }), + map(specify_block, |x| NonPortModuleItem::SpecifyBlock(x)), + non_port_module_item_specparam, + map(program_declaration, |x| { + NonPortModuleItem::ProgramDeclaration(x) + }), + map(module_declaration, |x| { + NonPortModuleItem::ModuleDeclaration(x) + }), + map(interface_declaration, |x| { + NonPortModuleItem::InterfaceDeclaration(x) + }), + map(timeunits_declaration, |x| { + NonPortModuleItem::TimeunitsDeclaration(x) + }), + ))(s) +} + +pub fn non_port_module_item_specparam(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = specparam_declaration(s)?; + Ok(( + s, + NonPortModuleItem::Specparam(NonPortModuleItemSpecparam { nodes: (a, b) }), + )) } pub fn parameter_override(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("defparam")(s)?; + let (s, b) = list_of_defparam_assignments(s)?; + let (s, c) = symbol(";")(s)?; + Ok((s, ParameterOverride { nodes: (a, b, c) })) } pub fn bind_directive(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt((bind_directive_scope, bind_directive_instance))(s) +} + +pub fn bind_directive_scope(s: Span) -> IResult { + let (s, a) = symbol("bind")(s)?; + let (s, b) = bind_target_scope(s)?; + let (s, c) = opt(pair(symbol(":"), bind_target_instance_list))(s)?; + let (s, d) = bind_instantiation(s)?; + let (s, e) = symbol(";")(s)?; + Ok(( + s, + BindDirective::Scope(BindDirectiveScope { + nodes: (a, b, c, d, e), + }), + )) +} + +pub fn bind_directive_instance(s: Span) -> IResult { + let (s, a) = symbol("bind")(s)?; + let (s, b) = bind_target_instance(s)?; + let (s, c) = bind_instantiation(s)?; + let (s, d) = symbol(";")(s)?; + Ok(( + s, + BindDirective::Instance(BindDirectiveInstance { + nodes: (a, b, c, d), + }), + )) } pub fn bind_target_scope(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(module_identifier, |x| BindTargetScope::ModuleIdentifier(x)), + map(interface_identifier, |x| { + BindTargetScope::InterfaceIdentifier(x) + }), + ))(s) } pub fn bind_target_instance(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = hierarchical_identifier(s)?; + let (s, b) = constant_bit_select(s)?; + Ok((s, BindTargetInstance { nodes: (a, b) })) } pub fn bind_target_instance_list(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), bind_target_instance)(s)?; + Ok((s, BindTargetInstanceList { nodes: (a,) })) } pub fn bind_instantiation(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(program_instantiation, |x| { + BindInstantiation::ProgramInstantiation(x) + }), + map(module_instantiation, |x| { + BindInstantiation::ModuleInstantiation(x) + }), + map(interface_instantiation, |x| { + BindInstantiation::InterfaceInstantiation(x) + }), + map(checker_instantiation, |x| { + BindInstantiation::CheckerInstantiation(x) + }), + ))(s) } diff --git a/src/parser/source_text/module_parameters_and_ports.rs b/src/parser/source_text/module_parameters_and_ports.rs index 4f54c1d..b130f24 100644 --- a/src/parser/source_text/module_parameters_and_ports.rs +++ b/src/parser/source_text/module_parameters_and_ports.rs @@ -1,9 +1,10 @@ use crate::ast::*; use crate::parser::*; -//use nom::branch::*; -//use nom::combinator::*; -use nom::error::*; -use nom::{Err, IResult}; +use nom::branch::*; +use nom::combinator::*; +use nom::multi::*; +use nom::sequence::*; +use nom::IResult; // ----------------------------------------------------------------------------- @@ -11,20 +12,29 @@ use nom::{Err, IResult}; pub enum ParameterPortList<'a> { Assignment(ParameterPortListAssignment<'a>), Declaration(ParameterPortListDeclaration<'a>), - Empty(Symbol<'a>), + Empty((Symbol<'a>, Symbol<'a>, Symbol<'a>)), } #[derive(Debug, Node)] pub struct ParameterPortListAssignment<'a> { pub nodes: ( - ListOfParamAssignments<'a>, - Vec>, + Symbol<'a>, + Paren< + 'a, + ( + ListOfParamAssignments<'a>, + Vec<(Symbol<'a>, ParameterPortDeclaration<'a>)>, + ), + >, ), } #[derive(Debug, Node)] pub struct ParameterPortListDeclaration<'a> { - pub nodes: (Vec>,), + pub nodes: ( + Symbol<'a>, + Paren<'a, List, ParameterPortDeclaration<'a>>>, + ), } #[derive(Debug, Node)] @@ -42,17 +52,19 @@ pub struct ParameterPortDeclarationParamList<'a> { #[derive(Debug, Node)] pub struct ParameterPortDeclarationTypeList<'a> { - pub nodes: (ListOfTypeAssignments<'a>,), + pub nodes: (Symbol<'a>, ListOfTypeAssignments<'a>), } #[derive(Debug, Node)] pub struct ListOfPorts<'a> { - pub nodes: (Vec>,), + pub nodes: (Paren<'a, List, Port<'a>>>,), } #[derive(Debug, Node)] pub struct ListOfPortDeclarations<'a> { - pub nodes: (Option>, AnsiPortDeclaration<'a>)>>,), + pub nodes: ( + Paren<'a, Option, (Vec>, AnsiPortDeclaration<'a>)>>>, + ), } #[derive(Debug, Node)] @@ -102,18 +114,22 @@ pub struct PortNonNamed<'a> { #[derive(Debug, Node)] pub struct PortNamed<'a> { - pub nodes: (PortIdentifier<'a>, Option>), + pub nodes: ( + Symbol<'a>, + PortIdentifier<'a>, + Paren<'a, Option>>, + ), } #[derive(Debug, Node)] pub enum PortExpression<'a> { PortReference(PortReference<'a>), - Bracket(PortExpressionBracket<'a>), + Brace(PortExpressionBrace<'a>), } #[derive(Debug, Node)] -pub struct PortExpressionBracket<'a> { - pub nodes: (Vec>,), +pub struct PortExpressionBrace<'a> { + pub nodes: (Brace<'a, List, PortReference<'a>>>,), } #[derive(Debug, Node)] @@ -147,12 +163,15 @@ pub enum InterfacePortHeader<'a> { #[derive(Debug, Node)] pub struct InterfacePortHeaderIdentifier<'a> { - pub nodes: (InterfaceIdentifier<'a>, Option>), + pub nodes: ( + InterfaceIdentifier<'a>, + Option<(Symbol<'a>, ModportIdentifier<'a>)>, + ), } #[derive(Debug, Node)] pub struct InterfacePortHeaderInterface<'a> { - pub nodes: (Option>,), + pub nodes: (Symbol<'a>, Option<(Symbol<'a>, ModportIdentifier<'a>)>), } #[derive(Debug, Node)] @@ -173,7 +192,7 @@ pub struct AnsiPortDeclarationNet<'a> { Option>, PortIdentifier<'a>, Vec>, - Option>, + Option<(Symbol<'a>, ConstantExpression<'a>)>, ), } @@ -183,7 +202,7 @@ pub struct AnsiPortDeclarationVariable<'a> { Option>, PortIdentifier<'a>, Vec>, - Option>, + Option<(Symbol<'a>, ConstantExpression<'a>)>, ), } @@ -191,61 +210,288 @@ pub struct AnsiPortDeclarationVariable<'a> { pub struct AnsiPortDeclarationParen<'a> { pub nodes: ( Option>, + Symbol<'a>, PortIdentifier<'a>, - Option>, + Paren<'a, Option>>, ), } // ----------------------------------------------------------------------------- pub fn parameter_port_list(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + parameter_port_list_assignment, + parameter_port_list_declaration, + parameter_port_list_empty, + ))(s) +} + +pub fn parameter_port_list_assignment(s: Span) -> IResult { + let (s, a) = symbol("#")(s)?; + let (s, b) = paren(pair( + list_of_param_assignments, + many0(pair(symbol(","), parameter_port_declaration)), + ))(s)?; + Ok(( + s, + ParameterPortList::Assignment(ParameterPortListAssignment { nodes: (a, b) }), + )) +} + +pub fn parameter_port_list_declaration(s: Span) -> IResult { + let (s, a) = symbol("#")(s)?; + let (s, b) = paren(list(symbol(","), parameter_port_declaration))(s)?; + Ok(( + s, + ParameterPortList::Declaration(ParameterPortListDeclaration { nodes: (a, b) }), + )) +} + +pub fn parameter_port_list_empty(s: Span) -> IResult { + let (s, a) = symbol("#")(s)?; + let (s, b) = symbol("(")(s)?; + let (s, c) = symbol(")")(s)?; + Ok((s, ParameterPortList::Empty((a, b, c)))) } pub fn parameter_port_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(parameter_declaration, |x| { + ParameterPortDeclaration::ParameterDeclaration(x) + }), + map(local_parameter_declaration, |x| { + ParameterPortDeclaration::LocalParameterDeclaration(x) + }), + parameter_port_declaration_param_list, + parameter_port_declaration_type_list, + ))(s) +} + +pub fn parameter_port_declaration_param_list(s: Span) -> IResult { + let (s, a) = data_type(s)?; + let (s, b) = list_of_param_assignments(s)?; + Ok(( + s, + ParameterPortDeclaration::ParamList(ParameterPortDeclarationParamList { nodes: (a, b) }), + )) +} + +pub fn parameter_port_declaration_type_list(s: Span) -> IResult { + let (s, a) = symbol("type")(s)?; + let (s, b) = list_of_type_assignments(s)?; + Ok(( + s, + ParameterPortDeclaration::TypeList(ParameterPortDeclarationTypeList { nodes: (a, b) }), + )) } pub fn list_of_ports(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = paren(list(symbol(","), port))(s)?; + Ok((s, ListOfPorts { nodes: (a,) })) } pub fn list_of_port_declarations(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = paren(opt(list( + symbol(","), + pair(many0(attribute_instance), ansi_port_declaration), + )))(s)?; + Ok((s, ListOfPortDeclarations { nodes: (a,) })) } pub fn port_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + port_declaration_inout, + port_declaration_input, + port_declaration_output, + port_declaration_ref, + port_declaration_interface, + ))(s) +} + +pub fn port_declaration_inout(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = inout_declaration(s)?; + Ok(( + s, + PortDeclaration::Inout(PortDeclarationInout { nodes: (a, b) }), + )) +} + +pub fn port_declaration_input(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = input_declaration(s)?; + Ok(( + s, + PortDeclaration::Input(PortDeclarationInput { nodes: (a, b) }), + )) +} + +pub fn port_declaration_output(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = output_declaration(s)?; + Ok(( + s, + PortDeclaration::Output(PortDeclarationOutput { nodes: (a, b) }), + )) +} + +pub fn port_declaration_ref(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = ref_declaration(s)?; + Ok(( + s, + PortDeclaration::Ref(PortDeclarationRef { nodes: (a, b) }), + )) +} + +pub fn port_declaration_interface(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = interface_port_declaration(s)?; + Ok(( + s, + PortDeclaration::Interface(PortDeclarationInterface { nodes: (a, b) }), + )) } pub fn port(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt((port_non_named, port_named))(s) +} + +pub fn port_non_named(s: Span) -> IResult { + let (s, a) = opt(port_expression)(s)?; + Ok((s, Port::NonNamed(PortNonNamed { nodes: (a,) }))) +} + +pub fn port_named(s: Span) -> IResult { + let (s, a) = symbol(".")(s)?; + let (s, b) = port_identifier(s)?; + let (s, c) = paren(opt(port_expression))(s)?; + Ok((s, Port::Named(PortNamed { nodes: (a, b, c) }))) } pub fn port_expression(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(port_reference, |x| PortExpression::PortReference(x)), + port_expressio_named, + ))(s) +} + +pub fn port_expressio_named(s: Span) -> IResult { + let (s, a) = brace(list(symbol(","), port_reference))(s)?; + Ok(( + s, + PortExpression::Brace(PortExpressionBrace { nodes: (a,) }), + )) } pub fn port_reference(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = port_identifier(s)?; + let (s, b) = constant_select(s)?; + Ok((s, PortReference { nodes: (a, b) })) } pub fn port_direction(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("input"), |x| PortDirection::Input(x)), + map(symbol("output"), |x| PortDirection::Output(x)), + map(symbol("inout"), |x| PortDirection::Inout(x)), + map(symbol("ref"), |x| PortDirection::Ref(x)), + ))(s) } pub fn net_port_header(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = opt(port_direction)(s)?; + let (s, b) = net_port_type(s)?; + Ok((s, NetPortHeader { nodes: (a, b) })) } pub fn variable_port_header(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = opt(port_direction)(s)?; + let (s, b) = variable_port_type(s)?; + Ok((s, VariablePortHeader { nodes: (a, b) })) } pub fn interface_port_header(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + interface_port_header_identifier, + interface_port_header_interface, + ))(s) +} + +pub fn interface_port_header_identifier(s: Span) -> IResult { + let (s, a) = interface_identifier(s)?; + let (s, b) = opt(pair(symbol("."), modport_identifier))(s)?; + Ok(( + s, + InterfacePortHeader::Identifier(InterfacePortHeaderIdentifier { nodes: (a, b) }), + )) +} + +pub fn interface_port_header_interface(s: Span) -> IResult { + let (s, a) = symbol("interface")(s)?; + let (s, b) = opt(pair(symbol("."), modport_identifier))(s)?; + Ok(( + s, + InterfacePortHeader::Interface(InterfacePortHeaderInterface { nodes: (a, b) }), + )) } pub fn ansi_port_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + ansi_port_declaration_net, + ansi_port_declaration_port, + ansi_port_declaration_paren, + ))(s) +} + +pub fn ansi_port_declaration_net(s: Span) -> IResult { + let (s, a) = opt(net_port_header_or_interface_port_header)(s)?; + let (s, b) = port_identifier(s)?; + let (s, c) = many0(unpacked_dimension)(s)?; + let (s, d) = opt(pair(symbol("="), constant_expression))(s)?; + Ok(( + s, + AnsiPortDeclaration::Net(AnsiPortDeclarationNet { + nodes: (a, b, c, d), + }), + )) +} + +pub fn net_port_header_or_interface_port_header( + s: Span, +) -> IResult { + alt(( + map(net_port_header, |x| { + NetPortHeaderOrInterfacePortHeader::NetPortHeader(x) + }), + map(interface_port_header, |x| { + NetPortHeaderOrInterfacePortHeader::InterfacePortHeader(x) + }), + ))(s) +} + +pub fn ansi_port_declaration_port(s: Span) -> IResult { + let (s, a) = opt(variable_port_header)(s)?; + let (s, b) = port_identifier(s)?; + let (s, c) = many0(variable_dimension)(s)?; + let (s, d) = opt(pair(symbol("="), constant_expression))(s)?; + Ok(( + s, + AnsiPortDeclaration::Variable(AnsiPortDeclarationVariable { + nodes: (a, b, c, d), + }), + )) +} + +pub fn ansi_port_declaration_paren(s: Span) -> IResult { + let (s, a) = opt(port_direction)(s)?; + let (s, b) = symbol(".")(s)?; + let (s, c) = port_identifier(s)?; + let (s, d) = paren(opt(expression))(s)?; + Ok(( + s, + AnsiPortDeclaration::Paren(AnsiPortDeclarationParen { + nodes: (a, b, c, d), + }), + )) }