From dfbd0718f938448bd5e52a27341653cae4f7ae47 Mon Sep 17 00:00:00 2001 From: dalance Date: Mon, 15 Jul 2019 15:38:14 +0900 Subject: [PATCH] Refactoring --- README.md | 26 +- node_derive/src/lib.rs | 25 +- src/ast/any_node.rs | 144 ++++++++ .../declarations/declaration_assignments.rs | 157 ++++++++- src/parser/declarations/declaration_lists.rs | 70 +++- src/parser/declarations/declaration_ranges.rs | 87 ++++- .../declarations/function_declarations.rs | 181 +++++++++- .../declarations/interface_declarations.rs | 94 +++++- .../declarations/net_and_variable_types.rs | 311 ++++++++++++++++-- src/parser/declarations/task_declarations.rs | 112 ++++++- src/parser/source_text/checker_items.rs | 159 ++++++++- src/parser/source_text/class_items.rs | 298 +++++++++++++++-- .../source_text/configuration_source_text.rs | 210 ++++++++++-- src/parser/source_text/constraints.rs | 260 ++++++++++++--- src/parser/source_text/interface_items.rs | 91 ++++- src/parser/source_text/program_items.rs | 94 +++++- 16 files changed, 2029 insertions(+), 290 deletions(-) diff --git a/README.md b/README.md index 4eb0e7c..958f6f4 100644 --- a/README.md +++ b/README.md @@ -9,26 +9,26 @@ A parser library for System Verilog. | source_text | system_verilog_source_text | x | x | | | 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 | | | | -| source_text | checker_items | | | | -| source_text | class_items | | | | -| source_text | constraints | | | | +| source_text | configuration_source_text | x | x | | +| source_text | interface_items | x | x | | +| source_text | program_items | x | x | | +| source_text | checker_items | x | x | | +| source_text | class_items | x | x | | +| source_text | constraints | x | x | | | source_text | package_items | x | x | | | declaration | module_parameter_declarations | x | x | | | declaration | port_declarations | x | x | | | declaration | type_declarations | x | x | | -| declaration | net_and_variable_types | x | | | +| declaration | net_and_variable_types | x | x | | | declaration | strengths | x | x | x | | declaration | delays | x | x | | -| declaration | declaration_lists | x | | | -| declaration | declaration_assignments | x | | | -| declaration | declaration_ranges | x | | | -| declaration | function_declarations | x | | | -| declaration | task_declarations | x | | | +| declaration | declaration_lists | x | x | | +| declaration | declaration_assignments | x | x | | +| declaration | declaration_ranges | x | x | | +| declaration | function_declarations | x | x | | +| declaration | task_declarations | x | x | | | declaration | block_item_declarations | x | x | | -| declaration | interface_declarations | x | | | +| declaration | interface_declarations | x | x | | | declaration | assertion_declarations | x | | | | declaration | covergroup_declarations | | | | | declaration | let_declarations | | | | diff --git a/node_derive/src/lib.rs b/node_derive/src/lib.rs index 3d77f1a..9886b07 100644 --- a/node_derive/src/lib.rs +++ b/node_derive/src/lib.rs @@ -35,30 +35,9 @@ fn impl_node(ast: &syn::DeriveInput) -> TokenStream { } } } - syn::Data::Struct(ref data) => { - let mut items = quote! {}; - if let syn::Fields::Named(f) = &data.fields { - for f in &f.named { - if let Some(ident) = &f.ident { - if ident.to_string() == "nodes" { - if let syn::Type::Tuple(t) = &f.ty { - for i in 0..t.elems.len() { - let i = syn::Index::from(i); - items = quote! { - #items - let mut nodes : AnyNodes = (&(self.nodes.#i)).into(); - ret.append(&mut nodes.0); - }; - } - } - } - } - } - } + syn::Data::Struct(_) => { quote! { - let mut ret = Vec::new(); - #items - ret.into() + (&(self.nodes)).into() } } _ => { diff --git a/src/ast/any_node.rs b/src/ast/any_node.rs index a25a163..379b83a 100644 --- a/src/ast/any_node.rs +++ b/src/ast/any_node.rs @@ -67,6 +67,18 @@ where } } +impl<'a, T0: 'a> From<&'a (T0,)> for AnyNodes<'a> +where + &'a T0: Into>, +{ + fn from(x: &'a (T0,)) -> Self { + let mut ret = Vec::new(); + let (t0,) = x; + ret.append(&mut t0.into().0); + ret.into() + } +} + impl<'a, T0: 'a, T1: 'a> From<&'a (T0, T1)> for AnyNodes<'a> where &'a T0: Into>, @@ -183,6 +195,138 @@ where } } +impl<'a, T0: 'a, T1: 'a, T2: 'a, T3: 'a, T4: 'a, T5: 'a, T6: 'a, T7: 'a> + From<&'a (T0, T1, T2, T3, T4, T5, T6, T7)> for AnyNodes<'a> +where + &'a T0: Into>, + &'a T1: Into>, + &'a T2: Into>, + &'a T3: Into>, + &'a T4: Into>, + &'a T5: Into>, + &'a T6: Into>, + &'a T7: Into>, +{ + fn from(x: &'a (T0, T1, T2, T3, T4, T5, T6, T7)) -> Self { + let mut ret = Vec::new(); + let (t0, t1, t2, t3, t4, t5, t6, t7) = x; + ret.append(&mut t0.into().0); + ret.append(&mut t1.into().0); + ret.append(&mut t2.into().0); + ret.append(&mut t3.into().0); + ret.append(&mut t4.into().0); + ret.append(&mut t5.into().0); + ret.append(&mut t6.into().0); + ret.append(&mut t7.into().0); + ret.into() + } +} + +impl<'a, T0: 'a, T1: 'a, T2: 'a, T3: 'a, T4: 'a, T5: 'a, T6: 'a, T7: 'a, T8: 'a> + From<&'a (T0, T1, T2, T3, T4, T5, T6, T7, T8)> for AnyNodes<'a> +where + &'a T0: Into>, + &'a T1: Into>, + &'a T2: Into>, + &'a T3: Into>, + &'a T4: Into>, + &'a T5: Into>, + &'a T6: Into>, + &'a T7: Into>, + &'a T8: Into>, +{ + fn from(x: &'a (T0, T1, T2, T3, T4, T5, T6, T7, T8)) -> Self { + let mut ret = Vec::new(); + let (t0, t1, t2, t3, t4, t5, t6, t7, t8) = x; + ret.append(&mut t0.into().0); + ret.append(&mut t1.into().0); + ret.append(&mut t2.into().0); + ret.append(&mut t3.into().0); + ret.append(&mut t4.into().0); + ret.append(&mut t5.into().0); + ret.append(&mut t6.into().0); + ret.append(&mut t7.into().0); + ret.append(&mut t8.into().0); + ret.into() + } +} + +impl<'a, T0: 'a, T1: 'a, T2: 'a, T3: 'a, T4: 'a, T5: 'a, T6: 'a, T7: 'a, T8: 'a, T9: 'a> + From<&'a (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)> for AnyNodes<'a> +where + &'a T0: Into>, + &'a T1: Into>, + &'a T2: Into>, + &'a T3: Into>, + &'a T4: Into>, + &'a T5: Into>, + &'a T6: Into>, + &'a T7: Into>, + &'a T8: Into>, + &'a T9: Into>, +{ + fn from(x: &'a (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)) -> Self { + let mut ret = Vec::new(); + let (t0, t1, t2, t3, t4, t5, t6, t7, t8, t9) = x; + ret.append(&mut t0.into().0); + ret.append(&mut t1.into().0); + ret.append(&mut t2.into().0); + ret.append(&mut t3.into().0); + ret.append(&mut t4.into().0); + ret.append(&mut t5.into().0); + ret.append(&mut t6.into().0); + ret.append(&mut t7.into().0); + ret.append(&mut t8.into().0); + ret.append(&mut t9.into().0); + ret.into() + } +} + +impl< + 'a, + T0: 'a, + T1: 'a, + T2: 'a, + T3: 'a, + T4: 'a, + T5: 'a, + T6: 'a, + T7: 'a, + T8: 'a, + T9: 'a, + T10: 'a, + > From<&'a (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)> for AnyNodes<'a> +where + &'a T0: Into>, + &'a T1: Into>, + &'a T2: Into>, + &'a T3: Into>, + &'a T4: Into>, + &'a T5: Into>, + &'a T6: Into>, + &'a T7: Into>, + &'a T8: Into>, + &'a T9: Into>, + &'a T10: Into>, +{ + fn from(x: &'a (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)) -> Self { + let mut ret = Vec::new(); + let (t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) = x; + ret.append(&mut t0.into().0); + ret.append(&mut t1.into().0); + ret.append(&mut t2.into().0); + ret.append(&mut t3.into().0); + ret.append(&mut t4.into().0); + ret.append(&mut t5.into().0); + ret.append(&mut t6.into().0); + ret.append(&mut t7.into().0); + ret.append(&mut t8.into().0); + ret.append(&mut t9.into().0); + ret.append(&mut t10.into().0); + ret.into() + } +} + impl<'a, T> From<&'a Paren<'a, T>> for AnyNodes<'a> where &'a T: Into>, diff --git a/src/parser/declarations/declaration_assignments.rs b/src/parser/declarations/declaration_assignments.rs index 646d1a3..901f8ab 100644 --- a/src/parser/declarations/declaration_assignments.rs +++ b/src/parser/declarations/declaration_assignments.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; // ----------------------------------------------------------------------------- @@ -37,7 +38,7 @@ pub struct ParamAssignment<'a> { #[derive(Debug, Node)] pub enum SpecparamAssignment<'a> { Mintypmax(SpecparamAssignmentMintypmax<'a>), - PulseControl(PulseControlSpecparam<'a>), + PulseControlSpecparam(PulseControlSpecparam<'a>), } #[derive(Debug, Node)] @@ -167,56 +168,176 @@ pub struct DynamicArrayNew<'a> { pub nodes: ( Symbol<'a>, Bracket<'a, Expression<'a>>, - Option>>, + Option>>, ), } // ----------------------------------------------------------------------------- pub fn defparam_assignment(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = hierarchical_parameter_identifier(s)?; + let (s, b) = symbol("=")(s)?; + let (s, c) = constant_mintypmax_expression(s)?; + Ok((s, DefparamAssignment { nodes: (a, b, c) })) } pub fn net_decl_assignment(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = net_identifier(s)?; + let (s, b) = many0(unpacked_dimension)(s)?; + let (s, c) = opt(pair(symbol("="), expression))(s)?; + Ok((s, NetDeclAssignment { nodes: (a, b, c) })) } pub fn param_assignment(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = parameter_identifier(s)?; + let (s, b) = many0(unpacked_dimension)(s)?; + let (s, c) = opt(pair(symbol("="), constant_param_expression))(s)?; + Ok((s, ParamAssignment { nodes: (a, b, c) })) } pub fn specparam_assignment(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + specparam_assignment_mintypmax, + map(pulse_control_specparam, |x| { + SpecparamAssignment::PulseControlSpecparam(x) + }), + ))(s) +} + +pub fn specparam_assignment_mintypmax(s: Span) -> IResult { + let (s, a) = specparam_identifier(s)?; + let (s, b) = symbol("=")(s)?; + let (s, c) = constant_mintypmax_expression(s)?; + Ok(( + s, + SpecparamAssignment::Mintypmax(SpecparamAssignmentMintypmax { nodes: (a, b, c) }), + )) } pub fn type_assignment(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = type_identifier(s)?; + let (s, b) = opt(pair(symbol("="), data_type))(s)?; + Ok((s, TypeAssignment { nodes: (a, b) })) } pub fn pulse_control_specparam(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + pulse_control_specparam_without_descriptor, + pulse_control_specparam_with_descriptor, + ))(s) +} + +pub fn pulse_control_specparam_without_descriptor(s: Span) -> IResult { + let (s, a) = symbol("PATHPULSE$")(s)?; + let (s, b) = symbol("=")(s)?; + let (s, c) = paren(pair( + reject_limit_value, + opt(pair(symbol(","), error_limit_value)), + ))(s)?; + Ok(( + s, + PulseControlSpecparam::WithoutDescriptor(PulseControlSpecparamWithoutDescriptor { + nodes: (a, b, c), + }), + )) +} + +pub fn pulse_control_specparam_with_descriptor(s: Span) -> IResult { + let (s, a) = symbol("PATHPULSE$")(s)?; + let (s, b) = specify_input_terminal_descriptor(s)?; + let (s, c) = symbol("$")(s)?; + let (s, d) = specify_output_terminal_descriptor(s)?; + let (s, e) = symbol("=")(s)?; + let (s, f) = paren(pair( + reject_limit_value, + opt(pair(symbol(","), error_limit_value)), + ))(s)?; + Ok(( + s, + PulseControlSpecparam::WithDescriptor(PulseControlSpecparamWithDescriptor { + nodes: (a, b, c, d, e, f), + }), + )) } pub fn error_limit_value(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = limit_value(s)?; + Ok((s, ErrorLimitValue { nodes: (a,) })) } pub fn reject_limit_value(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = limit_value(s)?; + Ok((s, RejectLimitValue { nodes: (a,) })) } pub fn limit_value(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = constant_mintypmax_expression(s)?; + Ok((s, LimitValue { nodes: (a,) })) } pub fn variable_decl_assignment(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + variable_decl_assignment_variable, + variable_decl_assignment_dynamic_array, + variable_decl_assignment_class, + ))(s) +} + +pub fn variable_decl_assignment_variable(s: Span) -> IResult { + let (s, a) = variable_identifier(s)?; + let (s, b) = many0(variable_dimension)(s)?; + let (s, c) = opt(pair(symbol("="), expression))(s)?; + Ok(( + s, + VariableDeclAssignment::Variable(VariableDeclAssignmentVariable { nodes: (a, b, c) }), + )) +} + +pub fn variable_decl_assignment_dynamic_array(s: Span) -> IResult { + let (s, a) = dynamic_array_variable_identifier(s)?; + let (s, b) = unsized_dimension(s)?; + let (s, c) = many0(variable_dimension)(s)?; + let (s, d) = opt(pair(symbol("="), dynamic_array_new))(s)?; + Ok(( + s, + VariableDeclAssignment::DynamicArray(VariableDeclAssignmentDynamicArray { + nodes: (a, b, c, d), + }), + )) +} + +pub fn variable_decl_assignment_class(s: Span) -> IResult { + let (s, a) = class_variable_identifier(s)?; + let (s, b) = opt(pair(symbol("="), class_new))(s)?; + Ok(( + s, + VariableDeclAssignment::Class(VariableDeclAssignmentClass { nodes: (a, b) }), + )) } pub fn class_new(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt((class_new_argument, class_new_expression))(s) +} + +pub fn class_new_argument(s: Span) -> IResult { + let (s, a) = opt(class_scope)(s)?; + let (s, b) = symbol("new")(s)?; + let (s, c) = opt(paren(list_of_arguments))(s)?; + Ok((s, ClassNew::Argument(ClassNewArgument { nodes: (a, b, c) }))) +} + +pub fn class_new_expression(s: Span) -> IResult { + let (s, a) = symbol("new")(s)?; + let (s, b) = expression(s)?; + Ok(( + s, + ClassNew::Expression(ClassNewExpression { nodes: (a, b) }), + )) } pub fn dynamic_array_new(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("new")(s)?; + let (s, b) = bracket(expression)(s)?; + let (s, c) = opt(paren(expression))(s)?; + Ok((s, DynamicArrayNew { nodes: (a, b, c) })) } diff --git a/src/parser/declarations/declaration_lists.rs b/src/parser/declarations/declaration_lists.rs index 665dc1f..bda9e77 100644 --- a/src/parser/declarations/declaration_lists.rs +++ b/src/parser/declarations/declaration_lists.rs @@ -1,9 +1,9 @@ use crate::ast::*; use crate::parser::*; -//use nom::branch::*; -//use nom::combinator::*; -use nom::error::*; -use nom::{Err, IResult}; +use nom::combinator::*; +use nom::multi::*; +use nom::sequence::*; +use nom::IResult; // ----------------------------------------------------------------------------- @@ -93,53 +93,89 @@ pub struct ListOfVariablePortIdentifiers<'a> { // ----------------------------------------------------------------------------- pub fn list_of_defparam_assignments(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), defparam_assignment)(s)?; + Ok((s, ListOfDefparamAssignments { nodes: (a,) })) } pub fn list_of_genvar_identifiers(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), genvar_identifier)(s)?; + Ok((s, ListOfGenvarIdentifiers { nodes: (a,) })) } pub fn list_of_interface_identifiers(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list( + symbol(","), + pair(interface_identifier, many0(unpacked_dimension)), + )(s)?; + Ok((s, ListOfInterfaceIdentifiers { nodes: (a,) })) } pub fn list_of_net_decl_assignments(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), net_decl_assignment)(s)?; + Ok((s, ListOfNetDeclAssignments { nodes: (a,) })) } pub fn list_of_param_assignments(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), param_assignment)(s)?; + Ok((s, ListOfParamAssignments { nodes: (a,) })) } pub fn list_of_port_identifiers(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list( + symbol(","), + pair(port_identifier, many0(unpacked_dimension)), + )(s)?; + Ok((s, ListOfPortIdentifiers { nodes: (a,) })) } pub fn list_of_udp_port_identifiers(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), port_identifier)(s)?; + Ok((s, ListOfUdpPortIdentifiers { nodes: (a,) })) } pub fn list_of_specparam_assignments(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), specparam_assignment)(s)?; + Ok((s, ListOfSpecparamAssignments { nodes: (a,) })) } pub fn list_of_tf_variable_identifiers(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list( + symbol(","), + triple( + port_identifier, + many0(variable_dimension), + opt(pair(symbol("="), expression)), + ), + )(s)?; + Ok((s, ListOfTfVariableIdentifiers { nodes: (a,) })) } pub fn list_of_type_assignments(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), type_assignment)(s)?; + Ok((s, ListOfTypeAssignments { nodes: (a,) })) } pub fn list_of_variable_decl_assignments(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), variable_decl_assignment)(s)?; + Ok((s, ListOfVariableDeclAssignments { nodes: (a,) })) } pub fn list_of_variable_identifiers(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list( + symbol(","), + pair(variable_identifier, many0(variable_dimension)), + )(s)?; + Ok((s, ListOfVariableIdentifiers { nodes: (a,) })) } pub fn list_of_variable_port_identifiers(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list( + symbol(","), + triple( + port_identifier, + many0(variable_dimension), + opt(pair(symbol("="), constant_expression)), + ), + )(s)?; + Ok((s, ListOfVariablePortIdentifiers { nodes: (a,) })) } diff --git a/src/parser/declarations/declaration_ranges.rs b/src/parser/declarations/declaration_ranges.rs index c12790d..de78303 100644 --- a/src/parser/declarations/declaration_ranges.rs +++ b/src/parser/declarations/declaration_ranges.rs @@ -1,9 +1,9 @@ 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::sequence::*; +use nom::IResult; // ----------------------------------------------------------------------------- @@ -26,7 +26,7 @@ pub struct UnpackedDimensionExpression<'a> { #[derive(Debug, Node)] pub enum PackedDimension<'a> { Range(PackedDimensionRange<'a>), - Unsized(UnsizedDimension<'a>), + UnsizedDimension(UnsizedDimension<'a>), } #[derive(Debug, Node)] @@ -65,31 +65,94 @@ pub struct QueueDimension<'a> { #[derive(Debug, Node)] pub struct UnsizedDimension<'a> { - pub nodes: ((Symbol<'a>, Symbol<'a>),), + pub nodes: (Symbol<'a>, Symbol<'a>), } // ----------------------------------------------------------------------------- pub fn unpacked_dimension(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt((unpacked_dimension_range, unpacked_dimension_expression))(s) +} + +pub fn unpacked_dimension_range(s: Span) -> IResult { + let (s, a) = bracket(constant_range)(s)?; + Ok(( + s, + UnpackedDimension::Range(UnpackedDimensionRange { nodes: (a,) }), + )) +} + +pub fn unpacked_dimension_expression(s: Span) -> IResult { + let (s, a) = bracket(constant_expression)(s)?; + Ok(( + s, + UnpackedDimension::Expression(UnpackedDimensionExpression { nodes: (a,) }), + )) } pub fn packed_dimension(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + packed_dimension_range, + map(unsized_dimension, |x| PackedDimension::UnsizedDimension(x)), + ))(s) +} + +pub fn packed_dimension_range(s: Span) -> IResult { + let (s, a) = bracket(constant_range)(s)?; + Ok(( + s, + PackedDimension::Range(PackedDimensionRange { nodes: (a,) }), + )) } pub fn associative_dimension(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + associative_dimension_data_type, + associative_dimension_asterisk, + ))(s) +} + +pub fn associative_dimension_data_type(s: Span) -> IResult { + let (s, a) = bracket(data_type)(s)?; + Ok(( + s, + AssociativeDimension::DataType(AssociativeDimensionDataType { nodes: (a,) }), + )) +} + +pub fn associative_dimension_asterisk(s: Span) -> IResult { + let (s, a) = bracket(symbol("*"))(s)?; + Ok(( + s, + AssociativeDimension::Asterisk(AssociativeDimensionAsterisk { nodes: (a,) }), + )) } pub fn variable_dimension(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(unsized_dimension, |x| { + VariableDimension::UnsizedDimension(x) + }), + map(unpacked_dimension, |x| { + VariableDimension::UnpackedDimension(x) + }), + map(associative_dimension, |x| { + VariableDimension::AssociativeDimension(x) + }), + map(queue_dimension, |x| VariableDimension::QueueDimension(x)), + ))(s) } pub fn queue_dimension(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = bracket(pair( + symbol("$"), + opt(pair(symbol(":"), constant_expression)), + ))(s)?; + Ok((s, QueueDimension { nodes: (a,) })) } pub fn unsized_dimension(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("[")(s)?; + let (s, b) = symbol("]")(s)?; + Ok((s, UnsizedDimension { nodes: (a, b) })) } diff --git a/src/parser/declarations/function_declarations.rs b/src/parser/declarations/function_declarations.rs index 64b779a..126d938 100644 --- a/src/parser/declarations/function_declarations.rs +++ b/src/parser/declarations/function_declarations.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; // ----------------------------------------------------------------------------- @@ -57,6 +58,12 @@ pub struct FunctionBodyDeclarationWithPort<'a> { ), } +#[derive(Debug, Node)] +pub enum InterfaceIdentifierOrClassScope<'a> { + InterfaceIdentifier((InterfaceIdentifier<'a>, Symbol<'a>)), + ClassScope(ClassScope<'a>), +} + #[derive(Debug, Node)] pub struct FunctionPrototype<'a> { pub nodes: ( @@ -107,6 +114,7 @@ pub struct DpiImportExportExportFunction<'a> { Option<(CIdentifier<'a>, Symbol<'a>)>, Symbol<'a>, FunctionIdentifier<'a>, + Symbol<'a>, ), } @@ -118,6 +126,7 @@ pub struct DpiImportExportExportTask<'a> { Option<(CIdentifier<'a>, Symbol<'a>)>, Symbol<'a>, TaskIdentifier<'a>, + Symbol<'a>, ), } @@ -151,41 +160,185 @@ pub struct DpiTaskProto<'a> { // ----------------------------------------------------------------------------- pub fn function_data_type_or_implicit(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(data_type_or_void, |x| { + FunctionDataTypeOrImplicit::DataTypeOrVoid(x) + }), + map(implicit_data_type, |x| { + FunctionDataTypeOrImplicit::ImplicitDataType(x) + }), + ))(s) } pub fn function_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("function")(s)?; + let (s, b) = opt(lifetime)(s)?; + let (s, c) = function_body_declaration(s)?; + Ok((s, FunctionDeclaration { nodes: (a, b, c) })) } pub fn function_body_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + function_body_declaration_without_port, + function_body_declaration_with_port, + ))(s) +} + +pub fn function_body_declaration_without_port(s: Span) -> IResult { + let (s, a) = function_data_type_or_implicit(s)?; + let (s, b) = opt(interface_identifier_or_class_scope)(s)?; + let (s, c) = function_identifier(s)?; + let (s, d) = symbol(";")(s)?; + let (s, e) = many0(tf_item_declaration)(s)?; + let (s, f) = many0(function_statement_or_null)(s)?; + let (s, g) = symbol("endfunction")(s)?; + let (s, h) = opt(pair(symbol(":"), function_identifier))(s)?; + Ok(( + s, + FunctionBodyDeclaration::WithoutPort(FunctionBodyDeclarationWithoutPort { + nodes: (a, b, c, d, e, f, g, h), + }), + )) +} + +pub fn function_body_declaration_with_port(s: Span) -> IResult { + let (s, a) = function_data_type_or_implicit(s)?; + let (s, b) = opt(interface_identifier_or_class_scope)(s)?; + let (s, c) = function_identifier(s)?; + let (s, d) = paren(opt(tf_port_list))(s)?; + let (s, e) = symbol(";")(s)?; + let (s, f) = many0(block_item_declaration)(s)?; + let (s, g) = many0(function_statement_or_null)(s)?; + let (s, h) = symbol("endfunction")(s)?; + let (s, i) = opt(pair(symbol(":"), function_identifier))(s)?; + Ok(( + s, + FunctionBodyDeclaration::WithPort(FunctionBodyDeclarationWithPort { + nodes: (a, b, c, d, e, f, g, h, i), + }), + )) +} + +pub fn interface_identifier_or_class_scope( + s: Span, +) -> IResult { + alt(( + map(pair(interface_identifier, symbol(".")), |x| { + InterfaceIdentifierOrClassScope::InterfaceIdentifier(x) + }), + map(class_scope, |x| { + InterfaceIdentifierOrClassScope::ClassScope(x) + }), + ))(s) } pub fn function_prototype(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("function")(s)?; + let (s, b) = data_type_or_void(s)?; + let (s, c) = function_identifier(s)?; + let (s, d) = opt(paren(opt(tf_port_list)))(s)?; + Ok(( + s, + FunctionPrototype { + nodes: (a, b, c, d), + }, + )) } pub fn dpi_import_export(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + dpi_import_export_import_function, + dpi_import_export_import_task, + dpi_import_export_export_function, + dpi_import_export_export_task, + ))(s) +} + +pub fn dpi_import_export_import_function(s: Span) -> IResult { + let (s, a) = symbol("import")(s)?; + let (s, b) = dpi_spec_string(s)?; + let (s, c) = opt(dpi_function_import_property)(s)?; + let (s, d) = opt(pair(c_identifier, symbol("=")))(s)?; + let (s, e) = dpi_function_proto(s)?; + let (s, f) = symbol(";")(s)?; + Ok(( + s, + DpiImportExport::ImportFunction(DpiImportExportImportFunction { + nodes: (a, b, c, d, e, f), + }), + )) +} + +pub fn dpi_import_export_import_task(s: Span) -> IResult { + let (s, a) = symbol("import")(s)?; + let (s, b) = dpi_spec_string(s)?; + let (s, c) = opt(dpi_task_import_property)(s)?; + let (s, d) = opt(pair(c_identifier, symbol("=")))(s)?; + let (s, e) = dpi_task_proto(s)?; + let (s, f) = symbol(";")(s)?; + Ok(( + s, + DpiImportExport::ImportTask(DpiImportExportImportTask { + nodes: (a, b, c, d, e, f), + }), + )) +} + +pub fn dpi_import_export_export_function(s: Span) -> IResult { + let (s, a) = symbol("export")(s)?; + let (s, b) = dpi_spec_string(s)?; + let (s, c) = opt(pair(c_identifier, symbol("=")))(s)?; + let (s, d) = symbol("function")(s)?; + let (s, e) = function_identifier(s)?; + let (s, f) = symbol(";")(s)?; + Ok(( + s, + DpiImportExport::ExportFunction(DpiImportExportExportFunction { + nodes: (a, b, c, d, e, f), + }), + )) +} + +pub fn dpi_import_export_export_task(s: Span) -> IResult { + let (s, a) = symbol("export")(s)?; + let (s, b) = dpi_spec_string(s)?; + let (s, c) = opt(pair(c_identifier, symbol("=")))(s)?; + let (s, d) = symbol("task")(s)?; + let (s, e) = task_identifier(s)?; + let (s, f) = symbol(";")(s)?; + Ok(( + s, + DpiImportExport::ExportTask(DpiImportExportExportTask { + nodes: (a, b, c, d, e, f), + }), + )) } pub fn dpi_spec_string(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("DPI-C"), |x| DpiSpecString::DpiC(x)), + map(symbol("DPI"), |x| DpiSpecString::Dpi(x)), + ))(s) } pub fn dpi_function_import_property(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("context"), |x| DpiFunctionImportProperty::Context(x)), + map(symbol("pure"), |x| DpiFunctionImportProperty::Pure(x)), + ))(s) } pub fn dpi_task_import_property(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("context")(s)?; + Ok((s, DpiTaskImportProperty::Context(a))) } pub fn dpi_function_proto(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = function_prototype(s)?; + Ok((s, DpiFunctionProto { nodes: (a,) })) } pub fn dpi_task_proto(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = task_prototype(s)?; + Ok((s, DpiTaskProto { nodes: (a,) })) } diff --git a/src/parser/declarations/interface_declarations.rs b/src/parser/declarations/interface_declarations.rs index 7e53b9d..70da10d 100644 --- a/src/parser/declarations/interface_declarations.rs +++ b/src/parser/declarations/interface_declarations.rs @@ -1,9 +1,9 @@ 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::IResult; // ----------------------------------------------------------------------------- @@ -24,7 +24,7 @@ pub struct ModportItem<'a> { pub enum ModportPortsDeclaraton<'a> { Simple(ModportPortsDeclaratonSimple<'a>), Tf(ModportPortsDeclaratonTf<'a>), - Clocing(ModportPortsDeclaratonClocking<'a>), + Clocking(ModportPortsDeclaratonClocking<'a>), } #[derive(Debug, Node)] @@ -95,37 +95,103 @@ pub enum ImportExport<'a> { // ----------------------------------------------------------------------------- pub fn modport_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("modport")(s)?; + let (s, b) = list(symbol(","), modport_item)(s)?; + let (s, c) = symbol(";")(s)?; + Ok((s, ModportDeclaration { nodes: (a, b, c) })) } pub fn modport_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = modport_identifier(s)?; + let (s, b) = paren(list(symbol(","), modport_ports_declaration))(s)?; + Ok((s, ModportItem { nodes: (a, b) })) } pub fn modport_ports_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + modport_ports_declaration_simple, + modport_ports_declaration_tf, + modport_ports_declaration_clocking, + ))(s) +} + +pub fn modport_ports_declaration_simple(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = modport_simple_ports_declaration(s)?; + Ok(( + s, + ModportPortsDeclaraton::Simple(ModportPortsDeclaratonSimple { nodes: (a, b) }), + )) +} + +pub fn modport_ports_declaration_tf(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = modport_tf_ports_declaration(s)?; + Ok(( + s, + ModportPortsDeclaraton::Tf(ModportPortsDeclaratonTf { nodes: (a, b) }), + )) +} + +pub fn modport_ports_declaration_clocking(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = modport_clocking_declaration(s)?; + Ok(( + s, + ModportPortsDeclaraton::Clocking(ModportPortsDeclaratonClocking { nodes: (a, b) }), + )) } pub fn modport_clocking_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("clocking")(s)?; + let (s, b) = clocking_identifier(s)?; + Ok((s, ModportClockingDeclaration { nodes: (a, b) })) } pub fn modport_simple_ports_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = port_direction(s)?; + let (s, b) = list(symbol(","), modport_simple_port)(s)?; + Ok((s, ModportSimplePortsDeclaration { nodes: (a, b) })) } pub fn modport_simple_port(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt((modport_simple_port_ordered, modport_simple_port_named))(s) +} + +pub fn modport_simple_port_ordered(s: Span) -> IResult { + let (s, a) = port_identifier(s)?; + Ok(( + s, + ModportSimplePort::Ordered(ModportSimplePortOrdered { nodes: (a,) }), + )) +} + +pub fn modport_simple_port_named(s: Span) -> IResult { + let (s, a) = symbol(".")(s)?; + let (s, b) = port_identifier(s)?; + let (s, c) = paren(opt(expression))(s)?; + Ok(( + s, + ModportSimplePort::Named(ModportSimplePortNamed { nodes: (a, b, c) }), + )) } pub fn modport_tf_ports_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = import_export(s)?; + let (s, b) = list(symbol(","), modport_tf_port)(s)?; + Ok((s, ModportTfPortsDeclaration { nodes: (a, b) })) } pub fn modport_tf_port(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(method_prototype, |x| ModportTfPort::MethodPrototype(x)), + map(tf_identifier, |x| ModportTfPort::TfIdentifier(x)), + ))(s) } pub fn import_export(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("import"), |x| ImportExport::Import(x)), + map(symbol("export"), |x| ImportExport::Export(x)), + ))(s) } diff --git a/src/parser/declarations/net_and_variable_types.rs b/src/parser/declarations/net_and_variable_types.rs index 8f4b584..8dea00d 100644 --- a/src/parser/declarations/net_and_variable_types.rs +++ b/src/parser/declarations/net_and_variable_types.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; // ----------------------------------------------------------------------------- @@ -131,7 +132,7 @@ pub struct EnumBaseTypeVector<'a> { #[derive(Debug, Node)] pub struct EnumBaseTypeType<'a> { - pub nodes: (Identifier<'a>, Option>), + pub nodes: (TypeIdentifier<'a>, Option>), } #[derive(Debug, Node)] @@ -155,7 +156,7 @@ pub struct ClassType<'a> { Option>, Vec<( Symbol<'a>, - Identifier<'a>, + ClassIdentifier<'a>, Option>, )>, ), @@ -249,7 +250,7 @@ pub enum Signing<'a> { #[derive(Debug, Node)] pub enum SimpleType<'a> { IntegerType(IntegerType<'a>), - NonNonIntegerType(IntegerType<'a>), + NonIntegerType(NonIntegerType<'a>), PsTypeIdentifier(PsTypeIdentifier<'a>), PsParameterIdentifier(PsParameterIdentifier<'a>), } @@ -297,89 +298,341 @@ pub struct TypeReferenceDataType<'a> { // ----------------------------------------------------------------------------- pub fn casting_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(simple_type, |x| CastingType::SimpleType(Box::new(x))), + map(constant_primary, |x| { + CastingType::ConstantPrimary(Box::new(x)) + }), + map(signing, |x| CastingType::Signing(Box::new(x))), + map(symbol("string"), |x| CastingType::String(x)), + map(symbol("const"), |x| CastingType::Const(x)), + ))(s) } pub fn data_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + data_type_vector, + data_type_atom, + map(non_integer_type, |x| DataType::NonIntegerType(x)), + data_type_union, + data_type_enum, + map(symbol("string"), |x| DataType::String(x)), + map(symbol("chandle"), |x| DataType::Chandle(x)), + data_type_virtual, + data_type_type, + map(class_type, |x| DataType::ClassType(x)), + map(symbol("event"), |x| DataType::Chandle(x)), + map(ps_covergroup_identifier, |x| { + DataType::PsCovergroupIdentifier(x) + }), + map(type_reference, |x| DataType::TypeReference(Box::new(x))), + ))(s) +} + +pub fn data_type_vector(s: Span) -> IResult { + let (s, a) = integer_vector_type(s)?; + let (s, b) = opt(signing)(s)?; + let (s, c) = many0(packed_dimension)(s)?; + Ok((s, DataType::Vector(DataTypeVector { nodes: (a, b, c) }))) +} + +pub fn data_type_atom(s: Span) -> IResult { + let (s, a) = integer_atom_type(s)?; + let (s, b) = opt(signing)(s)?; + Ok((s, DataType::Atom(DataTypeAtom { nodes: (a, b) }))) +} + +pub fn data_type_union(s: Span) -> IResult { + let (s, a) = struct_union(s)?; + let (s, b) = opt(pair(packed, opt(signing)))(s)?; + let (s, c) = brace(pair(struct_union_member, many0(struct_union_member)))(s)?; + let (s, d) = many0(packed_dimension)(s)?; + Ok(( + s, + DataType::Union(Box::new(DataTypeUnion { + nodes: (a, b, c, d), + })), + )) +} + +pub fn packed(s: Span) -> IResult { + let (s, a) = symbol("packed")(s)?; + Ok((s, Packed { nodes: (a,) })) +} + +pub fn data_type_enum(s: Span) -> IResult { + let (s, a) = symbol("enum")(s)?; + let (s, b) = opt(enum_base_type)(s)?; + let (s, c) = brace(list(symbol(","), enum_name_declaration))(s)?; + let (s, d) = many0(packed_dimension)(s)?; + Ok(( + s, + DataType::Enum(DataTypeEnum { + nodes: (a, b, c, d), + }), + )) +} + +pub fn data_type_virtual(s: Span) -> IResult { + let (s, a) = symbol("virtual")(s)?; + let (s, b) = opt(interface)(s)?; + let (s, c) = interface_identifier(s)?; + let (s, d) = opt(parameter_value_assignment)(s)?; + let (s, e) = opt(pair(symbol("."), modport_identifier))(s)?; + Ok(( + s, + DataType::Virtual(DataTypeVirtual { + nodes: (a, b, c, d, e), + }), + )) +} + +pub fn interface(s: Span) -> IResult { + let (s, a) = symbol("interface")(s)?; + Ok((s, Interface { nodes: (a,) })) +} + +pub fn data_type_type(s: Span) -> IResult { + let (s, a) = opt(package_scope_or_class_scope)(s)?; + let (s, b) = type_identifier(s)?; + let (s, c) = many0(packed_dimension)(s)?; + Ok((s, DataType::Type(DataTypeType { nodes: (a, b, c) }))) } pub fn data_type_or_implicit(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(data_type, |x| DataTypeOrImplicit::DataType(x)), + map(implicit_data_type, |x| { + DataTypeOrImplicit::ImplicitDataType(x) + }), + ))(s) } pub fn implicit_data_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = opt(signing)(s)?; + let (s, b) = many0(packed_dimension)(s)?; + Ok((s, ImplicitDataType { nodes: (a, b) })) } pub fn enum_base_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + enum_base_type_atom, + enum_base_type_vector, + enum_base_type_type, + ))(s) +} + +pub fn enum_base_type_atom(s: Span) -> IResult { + let (s, a) = integer_atom_type(s)?; + let (s, b) = opt(signing)(s)?; + Ok((s, EnumBaseType::Atom(EnumBaseTypeAtom { nodes: (a, b) }))) +} + +pub fn enum_base_type_vector(s: Span) -> IResult { + let (s, a) = integer_vector_type(s)?; + let (s, b) = opt(signing)(s)?; + let (s, c) = opt(packed_dimension)(s)?; + Ok(( + s, + EnumBaseType::Vector(EnumBaseTypeVector { nodes: (a, b, c) }), + )) +} + +pub fn enum_base_type_type(s: Span) -> IResult { + let (s, a) = type_identifier(s)?; + let (s, b) = opt(packed_dimension)(s)?; + Ok((s, EnumBaseType::Type(EnumBaseTypeType { nodes: (a, b) }))) } pub fn enum_name_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = enum_identifier(s)?; + let (s, b) = opt(bracket(pair( + integral_number, + opt(pair(symbol(":"), integral_number)), + )))(s)?; + let (s, c) = opt(pair(symbol("="), constant_expression))(s)?; + Ok((s, EnumNameDeclaration { nodes: (a, b, c) })) } pub fn class_scope(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = class_type(s)?; + let (s, b) = symbol("::")(s)?; + Ok((s, ClassScope { nodes: (a, b) })) } pub fn class_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = ps_class_identifier(s)?; + let (s, b) = opt(parameter_value_assignment)(s)?; + let (s, c) = many0(triple( + symbol("::"), + class_identifier, + opt(parameter_value_assignment), + ))(s)?; + Ok((s, ClassType { nodes: (a, b, c) })) } pub fn integer_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(integer_vector_type, |x| IntegerType::IntegerVectorType(x)), + map(integer_atom_type, |x| IntegerType::IntegerAtomType(x)), + ))(s) } pub fn integer_atom_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("byte"), |x| IntegerAtomType::Byte(x)), + map(symbol("shortint"), |x| IntegerAtomType::Shortint(x)), + map(symbol("int"), |x| IntegerAtomType::Int(x)), + map(symbol("longint"), |x| IntegerAtomType::Longint(x)), + map(symbol("integer"), |x| IntegerAtomType::Integer(x)), + map(symbol("time"), |x| IntegerAtomType::Time(x)), + ))(s) } pub fn integer_vector_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("bit"), |x| IntegerVectorType::Bit(x)), + map(symbol("logic"), |x| IntegerVectorType::Logic(x)), + map(symbol("reg"), |x| IntegerVectorType::Reg(x)), + ))(s) } pub fn non_integer_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("shortreal"), |x| NonIntegerType::Shortreal(x)), + map(symbol("realtime"), |x| NonIntegerType::Realtime(x)), + map(symbol("real"), |x| NonIntegerType::Real(x)), + ))(s) } pub fn net_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("supply0"), |x| NetType::Supply0(x)), + map(symbol("supply1"), |x| NetType::Supply1(x)), + map(symbol("tri"), |x| NetType::Tri(x)), + map(symbol("triand"), |x| NetType::Triand(x)), + map(symbol("trior"), |x| NetType::Trior(x)), + map(symbol("trireg"), |x| NetType::Trireg(x)), + map(symbol("tri0"), |x| NetType::Tri0(x)), + map(symbol("tri1"), |x| NetType::Tri1(x)), + map(symbol("uwire"), |x| NetType::Uwire(x)), + map(symbol("wire"), |x| NetType::Wire(x)), + map(symbol("wand"), |x| NetType::Wand(x)), + map(symbol("wor"), |x| NetType::Wor(x)), + ))(s) } pub fn net_port_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + net_port_type_data_type, + map(net_type_identifier, |x| NetPortType::NetTypeIdentifier(x)), + net_port_type_interconnect, + ))(s) +} + +pub fn net_port_type_data_type(s: Span) -> IResult { + let (s, a) = opt(net_type)(s)?; + let (s, b) = data_type_or_implicit(s)?; + Ok(( + s, + NetPortType::DataType(NetPortTypeDataType { nodes: (a, b) }), + )) +} + +pub fn net_port_type_interconnect(s: Span) -> IResult { + let (s, a) = symbol("interconnect")(s)?; + let (s, b) = implicit_data_type(s)?; + Ok(( + s, + NetPortType::Interconnect(NetPortTypeInterconnect { nodes: (a, b) }), + )) } pub fn variable_port_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = var_data_type(s)?; + Ok((s, VariablePortType { nodes: (a,) })) } pub fn var_data_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(data_type, |x| VarDataType::DataType(x)), + var_data_type_var, + ))(s) +} + +pub fn var_data_type_var(s: Span) -> IResult { + let (s, a) = symbol("var")(s)?; + let (s, b) = data_type_or_implicit(s)?; + Ok((s, VarDataType::Var(VarDataTypeVar { nodes: (a, b) }))) } pub fn signing(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("signed"), |x| Signing::Signed(x)), + map(symbol("unsigned"), |x| Signing::Unsigned(x)), + ))(s) } pub fn simple_type(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(integer_type, |x| SimpleType::IntegerType(x)), + map(non_integer_type, |x| SimpleType::NonIntegerType(x)), + map(ps_type_identifier, |x| SimpleType::PsTypeIdentifier(x)), + map(ps_parameter_identifier, |x| { + SimpleType::PsParameterIdentifier(x) + }), + ))(s) } pub fn struct_union_member(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = opt(random_qualifier)(s)?; + let (s, c) = data_type_or_void(s)?; + let (s, d) = list_of_variable_decl_assignments(s)?; + let (s, e) = symbol(";")(s)?; + Ok(( + s, + StructUnionMember { + nodes: (a, b, c, d, e), + }, + )) } pub fn data_type_or_void(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(data_type, |x| DataTypeOrVoid::DataType(x)), + map(symbol("void"), |x| DataTypeOrVoid::Void(x)), + ))(s) } pub fn struct_union(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("struct"), |x| StructUnion::Struct(x)), + map(pair(symbol("union"), symbol("tagged")), |x| { + StructUnion::UnionTagged(x) + }), + map(symbol("union"), |x| StructUnion::Union(x)), + ))(s) } pub fn type_reference(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt((type_reference_expression, type_reference_data_type))(s) +} + +pub fn type_reference_expression(s: Span) -> IResult { + let (s, a) = symbol("type")(s)?; + let (s, b) = paren(expression)(s)?; + Ok(( + s, + TypeReference::Expression(TypeReferenceExpression { nodes: (a, b) }), + )) +} + +pub fn type_reference_data_type(s: Span) -> IResult { + let (s, a) = symbol("type")(s)?; + let (s, b) = paren(data_type)(s)?; + Ok(( + s, + TypeReference::DataType(TypeReferenceDataType { nodes: (a, b) }), + )) } diff --git a/src/parser/declarations/task_declarations.rs b/src/parser/declarations/task_declarations.rs index 2c5390b..a668c25 100644 --- a/src/parser/declarations/task_declarations.rs +++ b/src/parser/declarations/task_declarations.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; // ----------------------------------------------------------------------------- @@ -40,16 +41,11 @@ pub struct TaskBodyDeclarationWithPort<'a> { Symbol<'a>, Vec>, Vec>, + Symbol<'a>, Option<(Symbol<'a>, TaskIdentifier<'a>)>, ), } -#[derive(Debug, Node)] -pub enum InterfaceIdentifierOrClassScope<'a> { - InterfaceIdentifier((InterfaceIdentifier<'a>, Symbol<'a>)), - ClassScope(ClassScope<'a>), -} - #[derive(Debug, Node)] pub enum TfItemDeclaration<'a> { BlockItemDeclaration(BlockItemDeclaration<'a>), @@ -106,33 +102,113 @@ pub struct TaskPrototype<'a> { // ----------------------------------------------------------------------------- pub fn task_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("task")(s)?; + let (s, b) = opt(lifetime)(s)?; + let (s, c) = task_body_declaration(s)?; + Ok((s, TaskDeclaration { nodes: (a, b, c) })) } pub fn task_body_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + task_body_declaration_without_port, + task_body_declaration_with_port, + ))(s) +} + +pub fn task_body_declaration_without_port(s: Span) -> IResult { + let (s, a) = opt(interface_identifier_or_class_scope)(s)?; + let (s, b) = task_identifier(s)?; + let (s, c) = symbol(";")(s)?; + let (s, d) = many0(tf_item_declaration)(s)?; + let (s, e) = many0(statement_or_null)(s)?; + let (s, f) = symbol("endtask")(s)?; + let (s, g) = opt(pair(symbol(":"), task_identifier))(s)?; + Ok(( + s, + TaskBodyDeclaration::WithoutPort(TaskBodyDeclarationWithoutPort { + nodes: (a, b, c, d, e, f, g), + }), + )) +} + +pub fn task_body_declaration_with_port(s: Span) -> IResult { + let (s, a) = opt(interface_identifier_or_class_scope)(s)?; + let (s, b) = task_identifier(s)?; + let (s, c) = paren(opt(tf_port_list))(s)?; + let (s, d) = symbol(";")(s)?; + let (s, e) = many0(block_item_declaration)(s)?; + let (s, f) = many0(statement_or_null)(s)?; + let (s, g) = symbol("endtask")(s)?; + let (s, h) = opt(pair(symbol(":"), task_identifier))(s)?; + Ok(( + s, + TaskBodyDeclaration::WithPort(TaskBodyDeclarationWithPort { + nodes: (a, b, c, d, e, f, g, h), + }), + )) } pub fn tf_item_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(block_item_declaration, |x| { + TfItemDeclaration::BlockItemDeclaration(x) + }), + map(tf_port_declaration, |x| { + TfItemDeclaration::TfPortDeclaration(x) + }), + ))(s) } pub fn tf_port_list(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), tf_port_item)(s)?; + Ok((s, TfPortList { nodes: (a,) })) } pub fn tf_port_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = opt(tf_port_direction)(s)?; + let (s, c) = opt(var)(s)?; + let (s, d) = data_type_or_implicit(s)?; + let (s, e) = opt(triple( + port_identifier, + many0(variable_dimension), + opt(pair(symbol(":"), expression)), + ))(s)?; + Ok(( + s, + TfPortItem { + nodes: (a, b, c, d, e), + }, + )) } pub fn tf_port_direction(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(port_direction, |x| TfPortDirection::PortDirection(x)), + map(pair(symbol("const"), symbol("ref")), |x| { + TfPortDirection::ConstRef(x) + }), + ))(s) } pub fn tf_port_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = tf_port_direction(s)?; + let (s, c) = opt(var)(s)?; + let (s, d) = data_type_or_implicit(s)?; + let (s, e) = list_of_tf_variable_identifiers(s)?; + let (s, f) = symbol(";")(s)?; + Ok(( + s, + TfPortDeclaration { + nodes: (a, b, c, d, e, f), + }, + )) } pub fn task_prototype(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("task")(s)?; + let (s, b) = task_identifier(s)?; + let (s, c) = opt(paren(opt(tf_port_list)))(s)?; + Ok((s, TaskPrototype { nodes: (a, b, c) })) } diff --git a/src/parser/source_text/checker_items.rs b/src/parser/source_text/checker_items.rs index f979073..68bbe33 100644 --- a/src/parser/source_text/checker_items.rs +++ b/src/parser/source_text/checker_items.rs @@ -1,15 +1,16 @@ 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; // ----------------------------------------------------------------------------- #[derive(Debug, Node)] pub struct CheckerPortList<'a> { - pub nodes: (Vec>,), + pub nodes: (List, CheckerPortItem<'a>>,), } #[derive(Debug, Node)] @@ -20,7 +21,7 @@ pub struct CheckerPortItem<'a> { PropertyFormalType<'a>, FormalPortIdentifier<'a>, Vec>, - Option>, + Option<(Symbol<'a>, PropertyActualArg<'a>)>, ), } @@ -51,7 +52,7 @@ pub enum CheckerOrGenerateItemDeclaration<'a> { GenvarDeclaration(GenvarDeclaration<'a>), ClockingDeclaration(ClockingDeclaration<'a>), Clocking(CheckerOrGenerateItemDeclarationClocking<'a>), - Expression(CheckerOrGenerateItemDeclarationExpression<'a>), + Disable(CheckerOrGenerateItemDeclarationDisable<'a>), Empty(Symbol<'a>), } @@ -62,17 +63,23 @@ pub struct CheckerOrGenerateItemDeclarationData<'a> { #[derive(Debug, Node)] pub struct Rand<'a> { - pub nodes: (Symbol<'a>), + pub nodes: (Symbol<'a>,), } #[derive(Debug, Node)] pub struct CheckerOrGenerateItemDeclarationClocking<'a> { - pub nodes: (ClockingIdentifier<'a>,), + pub nodes: (Symbol<'a>, Symbol<'a>, ClockingIdentifier<'a>, Symbol<'a>), } #[derive(Debug, Node)] -pub struct CheckerOrGenerateItemDeclarationExpression<'a> { - pub nodes: (ExpressionOrDist<'a>,), +pub struct CheckerOrGenerateItemDeclarationDisable<'a> { + pub nodes: ( + Symbol<'a>, + Symbol<'a>, + Symbol<'a>, + ExpressionOrDist<'a>, + Symbol<'a>, + ), } #[derive(Debug, Node)] @@ -86,27 +93,145 @@ pub enum CheckerGenerateItem<'a> { // ----------------------------------------------------------------------------- pub fn checker_port_list(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), checker_port_item)(s)?; + Ok((s, CheckerPortList { nodes: (a,) })) } pub fn checker_port_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = opt(checker_port_direction)(s)?; + let (s, c) = property_formal_type(s)?; + let (s, d) = formal_port_identifier(s)?; + let (s, e) = many0(variable_dimension)(s)?; + let (s, f) = opt(pair(symbol("="), property_actual_arg))(s)?; + Ok(( + s, + CheckerPortItem { + nodes: (a, b, c, d, e, f), + }, + )) } pub fn checker_port_direction(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("input"), |x| CheckerPortDirection::Input(x)), + map(symbol("output"), |x| CheckerPortDirection::Output(x)), + ))(s) } pub fn checker_or_generate_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(checker_or_generate_item_declaration, |x| { + CheckerOrGenerateItem::CheckerOrGenerateItemDeclaration(x) + }), + map(initial_construct, |x| { + CheckerOrGenerateItem::InitialConstruct(x) + }), + map(always_construct, |x| { + CheckerOrGenerateItem::AlwaysConstruct(x) + }), + map(final_construct, |x| { + CheckerOrGenerateItem::FinalConstruct(x) + }), + map(assertion_item, |x| CheckerOrGenerateItem::AssertionItem(x)), + map(continuous_assign, |x| { + CheckerOrGenerateItem::ContinuousAssign(x) + }), + map(checker_generate_item, |x| { + CheckerOrGenerateItem::CheckerGenerateItem(x) + }), + ))(s) } pub fn checker_or_generate_item_declaration( s: Span, ) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + checker_or_generate_item_declaration_data, + map(function_declaration, |x| { + CheckerOrGenerateItemDeclaration::FunctionDeclaration(x) + }), + map(checker_declaration, |x| { + CheckerOrGenerateItemDeclaration::CheckerDeclaration(x) + }), + map(assertion_item_declaration, |x| { + CheckerOrGenerateItemDeclaration::AssertionItemDeclaration(x) + }), + map(covergroup_declaration, |x| { + CheckerOrGenerateItemDeclaration::CovergroupDeclaration(x) + }), + map(genvar_declaration, |x| { + CheckerOrGenerateItemDeclaration::GenvarDeclaration(x) + }), + map(clocking_declaration, |x| { + CheckerOrGenerateItemDeclaration::ClockingDeclaration(x) + }), + checker_or_generate_item_declaration_clocking, + checker_or_generate_item_declaration_disable, + map(symbol(";"), |x| CheckerOrGenerateItemDeclaration::Empty(x)), + ))(s) +} + +pub fn checker_or_generate_item_declaration_data( + s: Span, +) -> IResult { + let (s, a) = opt(rand)(s)?; + let (s, b) = data_declaration(s)?; + Ok(( + s, + CheckerOrGenerateItemDeclaration::Data(CheckerOrGenerateItemDeclarationData { + nodes: (a, b), + }), + )) +} + +pub fn rand(s: Span) -> IResult { + let (s, a) = symbol("rand")(s)?; + Ok((s, Rand { nodes: (a,) })) +} + +pub fn checker_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, + CheckerOrGenerateItemDeclaration::Clocking(CheckerOrGenerateItemDeclarationClocking { + nodes: (a, b, c, d), + }), + )) +} + +pub fn checker_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, + CheckerOrGenerateItemDeclaration::Disable(CheckerOrGenerateItemDeclarationDisable { + nodes: (a, b, c, d, e), + }), + )) } pub fn checker_generate_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(loop_generate_construct, |x| { + CheckerGenerateItem::LoopGenerateConstruct(Box::new(x)) + }), + map(conditional_generate_construct, |x| { + CheckerGenerateItem::ConditionalGenerateConstruct(Box::new(x)) + }), + map(generate_region, |x| CheckerGenerateItem::GenerateRegion(x)), + map(elaboration_system_task, |x| { + CheckerGenerateItem::ElaborationSystemTask(x) + }), + ))(s) } diff --git a/src/parser/source_text/class_items.rs b/src/parser/source_text/class_items.rs index 70b602e..b398924 100644 --- a/src/parser/source_text/class_items.rs +++ b/src/parser/source_text/class_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; // ----------------------------------------------------------------------------- @@ -14,8 +15,8 @@ pub enum ClassItem<'a> { Constraint(ClassItemConstraint<'a>), Declaration(ClassItemDeclaration<'a>), Covergroup(ClassItemCovergroup<'a>), - LocalParameterDeclaration(LocalParameterDeclaration<'a>), - ParameterDeclaration(ParameterDeclaration<'a>), + LocalParameterDeclaration((LocalParameterDeclaration<'a>, Symbol<'a>)), + ParameterDeclaration((ParameterDeclaration<'a>, Symbol<'a>)), Empty(Symbol<'a>), } @@ -58,10 +59,12 @@ pub struct ClassPropertyNonConst<'a> { #[derive(Debug, Node)] pub struct ClassPropertyConst<'a> { pub nodes: ( + Symbol<'a>, Vec>, DataType<'a>, ConstIdentifier<'a>, - Option>, + Option<(Symbol<'a>, ConstantExpression<'a>)>, + Symbol<'a>, ), } @@ -87,27 +90,47 @@ pub struct ClassMethodFunction<'a> { #[derive(Debug, Node)] pub struct ClassMethodPureVirtual<'a> { - pub nodes: (Vec>, MethodPrototype<'a>), + pub nodes: ( + Symbol<'a>, + Symbol<'a>, + Vec>, + MethodPrototype<'a>, + Symbol<'a>, + ), } #[derive(Debug, Node)] pub struct ClassMethodExternMethod<'a> { - pub nodes: (Vec>, MethodPrototype<'a>), + pub nodes: ( + Symbol<'a>, + Vec>, + MethodPrototype<'a>, + Symbol<'a>, + ), } #[derive(Debug, Node)] pub struct ClassMethodConstructor<'a> { - pub nodes: (Vec>, ClassConstructorPrototype<'a>), + pub nodes: (Vec>, ClassConstructorDeclaration<'a>), } #[derive(Debug, Node)] pub struct ClassMethodExternConstructor<'a> { - pub nodes: (Vec>, ClassConstructorPrototype<'a>), + pub nodes: ( + Symbol<'a>, + Vec>, + ClassConstructorPrototype<'a>, + ), } #[derive(Debug, Node)] pub struct ClassConstructorPrototype<'a> { - pub nodes: (Option>,), + pub nodes: ( + Symbol<'a>, + Symbol<'a>, + Option>>>, + Symbol<'a>, + ), } #[derive(Debug, Node)] @@ -138,7 +161,7 @@ pub enum RandomQualifier<'a> { #[derive(Debug, Node)] pub enum MethodQualifier<'a> { Virtual(Symbol<'a>), - PureVirtual(Symbol<'a>), + PureVirtual((Symbol<'a>, Symbol<'a>)), ClassItemQualifier(ClassItemQualifier<'a>), } @@ -151,12 +174,22 @@ pub enum MethodPrototype<'a> { #[derive(Debug, Node)] pub struct ClassConstructorDeclaration<'a> { pub nodes: ( + Symbol<'a>, Option>, - Option>>, + Symbol<'a>, + Option>>>, + Symbol<'a>, Vec>, - Option>>, + Option<( + Symbol<'a>, + Symbol<'a>, + Symbol<'a>, + Option>>, + Symbol<'a>, + )>, Vec>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, New<'a>)>, ), } @@ -168,45 +201,256 @@ pub struct New<'a> { // ----------------------------------------------------------------------------- pub fn class_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + class_item_property, + class_item_method, + class_item_constraint, + class_item_declaration, + class_item_covergroup, + map(pair(local_parameter_declaration, symbol(";")), |x| { + ClassItem::LocalParameterDeclaration(x) + }), + map(pair(parameter_declaration, symbol(";")), |x| { + ClassItem::ParameterDeclaration(x) + }), + map(symbol(";"), |x| ClassItem::Empty(x)), + ))(s) +} + +pub fn class_item_property(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = class_property(s)?; + Ok((s, ClassItem::Property(ClassItemProperty { nodes: (a, b) }))) +} + +pub fn class_item_method(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = class_method(s)?; + Ok((s, ClassItem::Method(ClassItemMethod { nodes: (a, b) }))) +} + +pub fn class_item_constraint(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = class_constraint(s)?; + Ok(( + s, + ClassItem::Constraint(ClassItemConstraint { nodes: (a, b) }), + )) +} + +pub fn class_item_declaration(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = class_declaration(s)?; + Ok(( + s, + ClassItem::Declaration(ClassItemDeclaration { nodes: (a, b) }), + )) +} + +pub fn class_item_covergroup(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = covergroup_declaration(s)?; + Ok(( + s, + ClassItem::Covergroup(ClassItemCovergroup { nodes: (a, b) }), + )) } pub fn class_property(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt((class_property_non_const, class_property_const))(s) +} + +pub fn class_property_non_const(s: Span) -> IResult { + let (s, a) = many0(property_qualifier)(s)?; + let (s, b) = data_declaration(s)?; + Ok(( + s, + ClassProperty::NonConst(ClassPropertyNonConst { nodes: (a, b) }), + )) +} + +pub fn class_property_const(s: Span) -> IResult { + let (s, a) = symbol("const")(s)?; + let (s, b) = many0(class_item_qualifier)(s)?; + let (s, c) = data_type(s)?; + let (s, d) = const_identifier(s)?; + let (s, e) = opt(pair(symbol("="), constant_expression))(s)?; + let (s, f) = symbol(";")(s)?; + Ok(( + s, + ClassProperty::Const(ClassPropertyConst { + nodes: (a, b, c, d, e, f), + }), + )) } pub fn class_method(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + class_method_task, + class_method_function, + class_method_pure_virtual, + class_method_extern_method, + class_method_constructor, + class_method_extern_constructor, + ))(s) +} + +pub fn class_method_task(s: Span) -> IResult { + let (s, a) = many0(method_qualifier)(s)?; + let (s, b) = task_declaration(s)?; + Ok((s, ClassMethod::Task(ClassMethodTask { nodes: (a, b) }))) +} + +pub fn class_method_function(s: Span) -> IResult { + let (s, a) = many0(method_qualifier)(s)?; + let (s, b) = function_declaration(s)?; + Ok(( + s, + ClassMethod::Function(ClassMethodFunction { nodes: (a, b) }), + )) +} + +pub fn class_method_pure_virtual(s: Span) -> IResult { + let (s, a) = symbol("pure")(s)?; + let (s, b) = symbol("virtual")(s)?; + let (s, c) = many0(class_item_qualifier)(s)?; + let (s, d) = method_prototype(s)?; + let (s, e) = symbol(";")(s)?; + Ok(( + s, + ClassMethod::PureVirtual(ClassMethodPureVirtual { + nodes: (a, b, c, d, e), + }), + )) +} + +pub fn class_method_extern_method(s: Span) -> IResult { + let (s, a) = symbol("extern")(s)?; + let (s, b) = many0(method_qualifier)(s)?; + let (s, c) = method_prototype(s)?; + let (s, d) = symbol(";")(s)?; + Ok(( + s, + ClassMethod::ExternMethod(ClassMethodExternMethod { + nodes: (a, b, c, d), + }), + )) +} + +pub fn class_method_constructor(s: Span) -> IResult { + let (s, a) = many0(method_qualifier)(s)?; + let (s, b) = class_constructor_declaration(s)?; + Ok(( + s, + ClassMethod::Constructor(ClassMethodConstructor { nodes: (a, b) }), + )) +} + +pub fn class_method_extern_constructor(s: Span) -> IResult { + let (s, a) = symbol("extern")(s)?; + let (s, b) = many0(method_qualifier)(s)?; + let (s, c) = class_constructor_prototype(s)?; + Ok(( + s, + ClassMethod::ExternConstructor(ClassMethodExternConstructor { nodes: (a, b, c) }), + )) } pub fn class_constructor_prototype(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("function")(s)?; + let (s, b) = symbol("new")(s)?; + let (s, c) = opt(paren(opt(tf_port_list)))(s)?; + let (s, d) = symbol(";")(s)?; + Ok(( + s, + ClassConstructorPrototype { + nodes: (a, b, c, d), + }, + )) } pub fn class_constraint(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(constraint_prototype, |x| { + ClassConstraint::ConstraintPrototype(x) + }), + map(constraint_declaration, |x| { + ClassConstraint::ConstraintDeclaration(x) + }), + ))(s) } pub fn class_item_qualifier(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("static"), |x| ClassItemQualifier::Static(x)), + map(symbol("protected"), |x| ClassItemQualifier::Protected(x)), + map(symbol("local"), |x| ClassItemQualifier::Local(x)), + ))(s) } pub fn property_qualifier(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(random_qualifier, |x| PropertyQualifier::RandomQualifier(x)), + map(class_item_qualifier, |x| { + PropertyQualifier::ClassItemQualifier(x) + }), + ))(s) } pub fn random_qualifier(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("randc"), |x| RandomQualifier::Randc(x)), + map(symbol("rand"), |x| RandomQualifier::Rand(x)), + ))(s) } pub fn method_qualifier(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(pair(symbol("pure"), symbol("virtual")), |x| { + MethodQualifier::PureVirtual(x) + }), + map(symbol("virtual"), |x| MethodQualifier::Virtual(x)), + map(class_item_qualifier, |x| { + MethodQualifier::ClassItemQualifier(x) + }), + ))(s) } pub fn method_prototype(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(task_prototype, |x| MethodPrototype::TaskPrototype(x)), + map(function_prototype, |x| { + MethodPrototype::FunctionPrototype(x) + }), + ))(s) } pub fn class_constructor_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("function")(s)?; + let (s, b) = opt(class_scope)(s)?; + let (s, c) = symbol("new")(s)?; + let (s, d) = opt(paren(opt(tf_port_list)))(s)?; + let (s, e) = symbol(";")(s)?; + let (s, f) = many0(block_item_declaration)(s)?; + let (s, g) = opt(tuple(( + symbol("super"), + symbol("."), + symbol("new"), + opt(paren(list_of_arguments)), + symbol(";"), + )))(s)?; + let (s, h) = many0(function_statement_or_null)(s)?; + let (s, i) = symbol("end")(s)?; + let (s, j) = opt(pair(symbol(":"), new))(s)?; + Ok(( + s, + ClassConstructorDeclaration { + nodes: (a, b, c, d, e, f, g, h, i, j), + }, + )) +} + +pub fn new(s: Span) -> IResult { + let (s, a) = symbol("new")(s)?; + Ok((s, New { nodes: (a,) })) } diff --git a/src/parser/source_text/configuration_source_text.rs b/src/parser/source_text/configuration_source_text.rs index 97d7cee..ac53555 100644 --- a/src/parser/source_text/configuration_source_text.rs +++ b/src/parser/source_text/configuration_source_text.rs @@ -1,26 +1,37 @@ 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; // ----------------------------------------------------------------------------- #[derive(Debug, Node)] pub struct ConfigDeclaration<'a> { pub nodes: ( + Symbol<'a>, ConfigIdentifier<'a>, - Vec>, + Symbol<'a>, + Vec<(LocalParameterDeclaration<'a>, Symbol<'a>)>, DesignStatement<'a>, Vec>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, ConfigIdentifier<'a>)>, ), } #[derive(Debug, Node)] pub struct DesignStatement<'a> { - pub nodes: (Vec<(Option>, CellIdentifier<'a>)>,), + pub nodes: ( + Symbol<'a>, + Vec<( + Option<(LibraryIdentifier<'a>, Symbol<'a>)>, + CellIdentifier<'a>, + )>, + Symbol<'a>, + ), } #[derive(Debug, Node)] @@ -34,27 +45,27 @@ pub enum ConfigRuleStatement<'a> { #[derive(Debug, Node)] pub struct ConfigRuleStatementDefault<'a> { - pub nodes: (DefaultClause<'a>, LiblistClause<'a>), + pub nodes: (DefaultClause<'a>, LiblistClause<'a>, Symbol<'a>), } #[derive(Debug, Node)] pub struct ConfigRuleStatementInstLib<'a> { - pub nodes: (InstClause<'a>, LiblistClause<'a>), + pub nodes: (InstClause<'a>, LiblistClause<'a>, Symbol<'a>), } #[derive(Debug, Node)] pub struct ConfigRuleStatementInstUse<'a> { - pub nodes: (InstClause<'a>, UseClause<'a>), + pub nodes: (InstClause<'a>, UseClause<'a>, Symbol<'a>), } #[derive(Debug, Node)] pub struct ConfigRuleStatementCellLib<'a> { - pub nodes: (CellClause<'a>, LiblistClause<'a>), + pub nodes: (CellClause<'a>, LiblistClause<'a>, Symbol<'a>), } #[derive(Debug, Node)] pub struct ConfigRuleStatementCellUse<'a> { - pub nodes: (CellClause<'a>, UseClause<'a>), + pub nodes: (CellClause<'a>, UseClause<'a>, Symbol<'a>), } #[derive(Debug, Node)] @@ -64,22 +75,29 @@ pub struct DefaultClause<'a> { #[derive(Debug, Node)] pub struct InstClause<'a> { - pub nodes: (InstName<'a>,), + pub nodes: (Symbol<'a>, InstName<'a>), } #[derive(Debug, Node)] pub struct InstName<'a> { - pub nodes: (TopmoduleIdentifier<'a>, Vec>), + pub nodes: ( + TopmoduleIdentifier<'a>, + Vec<(Symbol<'a>, InstanceIdentifier<'a>)>, + ), } #[derive(Debug, Node)] pub struct CellClause<'a> { - pub nodes: (Option>, CellIdentifier<'a>), + pub nodes: ( + Symbol<'a>, + Option<(LibraryIdentifier<'a>, Symbol<'a>)>, + CellIdentifier<'a>, + ), } #[derive(Debug, Node)] pub struct LiblistClause<'a> { - pub nodes: (Vec>,), + pub nodes: (Symbol<'a>, Vec>), } #[derive(Debug, Node)] @@ -92,24 +110,30 @@ pub enum UseClause<'a> { #[derive(Debug, Node)] pub struct UseClauseCell<'a> { pub nodes: ( - Option>, + Symbol<'a>, + Option<(LibraryIdentifier<'a>, Symbol<'a>)>, CellIdentifier<'a>, - Option>, + Option<(Symbol<'a>, Config<'a>)>, ), } #[derive(Debug, Node)] pub struct UseClauseNamed<'a> { - pub nodes: (Vec>, Option>), + pub nodes: ( + Symbol<'a>, + List, NamedParameterAssignment<'a>>, + Option<(Symbol<'a>, Config<'a>)>, + ), } #[derive(Debug, Node)] pub struct UseClauseCellNamed<'a> { pub nodes: ( - Option>, + Symbol<'a>, + Option<(LibraryIdentifier<'a>, Symbol<'a>)>, CellIdentifier<'a>, - Vec>, - Option>, + List, NamedParameterAssignment<'a>>, + Option<(Symbol<'a>, Config<'a>)>, ), } @@ -121,37 +145,161 @@ pub struct Config<'a> { // ----------------------------------------------------------------------------- pub fn config_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("config")(s)?; + let (s, b) = config_identifier(s)?; + let (s, c) = symbol(";")(s)?; + let (s, d) = many0(pair(local_parameter_declaration, symbol(";")))(s)?; + let (s, e) = design_statement(s)?; + let (s, f) = many0(config_rule_statement)(s)?; + let (s, g) = symbol("endconfig")(s)?; + let (s, h) = opt(pair(symbol(":"), config_identifier))(s)?; + Ok(( + s, + ConfigDeclaration { + nodes: (a, b, c, d, e, f, g, h), + }, + )) } pub fn design_statement(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("design")(s)?; + let (s, b) = many0(pair( + opt(pair(library_identifier, symbol("."))), + cell_identifier, + ))(s)?; + let (s, c) = symbol(";")(s)?; + Ok((s, DesignStatement { nodes: (a, b, c) })) } pub fn config_rule_statement(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + config_rule_statement_default, + config_rule_statement_inst_lib, + config_rule_statement_inst_use, + config_rule_statement_cell_lib, + config_rule_statement_cell_use, + ))(s) +} + +pub fn config_rule_statement_default(s: Span) -> IResult { + let (s, a) = default_clause(s)?; + let (s, b) = liblist_clause(s)?; + let (s, c) = symbol(";")(s)?; + Ok(( + s, + ConfigRuleStatement::Default(ConfigRuleStatementDefault { nodes: (a, b, c) }), + )) +} + +pub fn config_rule_statement_inst_lib(s: Span) -> IResult { + let (s, a) = inst_clause(s)?; + let (s, b) = liblist_clause(s)?; + let (s, c) = symbol(";")(s)?; + Ok(( + s, + ConfigRuleStatement::InstLib(ConfigRuleStatementInstLib { nodes: (a, b, c) }), + )) +} + +pub fn config_rule_statement_inst_use(s: Span) -> IResult { + let (s, a) = inst_clause(s)?; + let (s, b) = use_clause(s)?; + let (s, c) = symbol(";")(s)?; + Ok(( + s, + ConfigRuleStatement::InstUse(ConfigRuleStatementInstUse { nodes: (a, b, c) }), + )) +} + +pub fn config_rule_statement_cell_lib(s: Span) -> IResult { + let (s, a) = cell_clause(s)?; + let (s, b) = liblist_clause(s)?; + let (s, c) = symbol(";")(s)?; + Ok(( + s, + ConfigRuleStatement::CellLib(ConfigRuleStatementCellLib { nodes: (a, b, c) }), + )) +} + +pub fn config_rule_statement_cell_use(s: Span) -> IResult { + let (s, a) = cell_clause(s)?; + let (s, b) = use_clause(s)?; + let (s, c) = symbol(";")(s)?; + Ok(( + s, + ConfigRuleStatement::CellUse(ConfigRuleStatementCellUse { nodes: (a, b, c) }), + )) } pub fn default_clause(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("default")(s)?; + Ok((s, DefaultClause { nodes: (a,) })) } pub fn inst_clause(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("instance")(s)?; + let (s, b) = inst_name(s)?; + Ok((s, InstClause { nodes: (a, b) })) } pub fn inst_name(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = topmodule_identifier(s)?; + let (s, b) = many0(pair(symbol("."), instance_identifier))(s)?; + Ok((s, InstName { nodes: (a, b) })) } pub fn cell_clause(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("cell")(s)?; + let (s, b) = opt(pair(library_identifier, symbol(".")))(s)?; + let (s, c) = cell_identifier(s)?; + Ok((s, CellClause { nodes: (a, b, c) })) } pub fn liblist_clause(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("liblist")(s)?; + let (s, b) = many0(library_identifier)(s)?; + Ok((s, LiblistClause { nodes: (a, b) })) } pub fn use_clause(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt((use_clause_cell, use_clause_named, use_clause_cell_named))(s) +} + +pub fn use_clause_cell(s: Span) -> IResult { + let (s, a) = symbol("use")(s)?; + let (s, b) = opt(pair(library_identifier, symbol(".")))(s)?; + let (s, c) = cell_identifier(s)?; + let (s, d) = opt(pair(symbol(":"), config))(s)?; + Ok(( + s, + UseClause::Cell(UseClauseCell { + nodes: (a, b, c, d), + }), + )) +} + +pub fn use_clause_named(s: Span) -> IResult { + let (s, a) = symbol("use")(s)?; + let (s, b) = list(symbol(","), named_parameter_assignment)(s)?; + let (s, c) = opt(pair(symbol(":"), config))(s)?; + Ok((s, UseClause::Named(UseClauseNamed { nodes: (a, b, c) }))) +} + +pub fn use_clause_cell_named(s: Span) -> IResult { + let (s, a) = symbol("use")(s)?; + let (s, b) = opt(pair(library_identifier, symbol(".")))(s)?; + let (s, c) = cell_identifier(s)?; + let (s, d) = list(symbol(","), named_parameter_assignment)(s)?; + let (s, e) = opt(pair(symbol(":"), config))(s)?; + Ok(( + s, + UseClause::CellNamed(UseClauseCellNamed { + nodes: (a, b, c, d, e), + }), + )) +} + +pub fn config(s: Span) -> IResult { + let (s, a) = symbol("config")(s)?; + Ok((s, Config { nodes: (a,) })) } diff --git a/src/parser/source_text/constraints.rs b/src/parser/source_text/constraints.rs index 602a4bf..54c6789 100644 --- a/src/parser/source_text/constraints.rs +++ b/src/parser/source_text/constraints.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,6 +12,7 @@ use nom::{Err, IResult}; pub struct ConstraintDeclaration<'a> { pub nodes: ( Option>, + Symbol<'a>, ConstraintIdentifier<'a>, ConstraintBlock<'a>, ), @@ -23,7 +25,7 @@ pub struct Static<'a> { #[derive(Debug, Node)] pub struct ConstraintBlock<'a> { - pub nodes: (Vec>,), + pub nodes: (Brace<'a, Vec>>,), } #[derive(Debug, Node)] @@ -34,12 +36,18 @@ pub enum ConstraintBlockItem<'a> { #[derive(Debug, Node)] pub struct ConstraintBlockItemSolve<'a> { - pub nodes: (SolveBeforeList<'a>, SolveBeforeList<'a>), + pub nodes: ( + Symbol<'a>, + SolveBeforeList<'a>, + Symbol<'a>, + SolveBeforeList<'a>, + Symbol<'a>, + ), } #[derive(Debug, Node)] pub struct SolveBeforeList<'a> { - pub nodes: (Vec>,), + pub nodes: (List, ConstraintPrimary<'a>>,), } #[derive(Debug, Node)] @@ -54,7 +62,7 @@ pub struct ConstraintPrimary<'a> { #[derive(Debug, Node)] pub enum ConstraintExpression<'a> { Expression(ConstraintExpressionExpression<'a>), - UniquenessConstraint(UniquenessConstraint<'a>), + UniquenessConstraint((UniquenessConstraint<'a>, Symbol<'a>)), Arrow(ConstraintExpressionArrow<'a>), If(ConstraintExpressionIf<'a>), Foreach(ConstraintExpressionForeach<'a>), @@ -63,7 +71,7 @@ pub enum ConstraintExpression<'a> { #[derive(Debug, Node)] pub struct ConstraintExpressionExpression<'a> { - pub nodes: (Option>, ExpressionOrDist<'a>), + pub nodes: (Option>, ExpressionOrDist<'a>, Symbol<'a>), } #[derive(Debug, Node)] @@ -73,47 +81,58 @@ pub struct Soft<'a> { #[derive(Debug, Node)] pub struct ConstraintExpressionArrow<'a> { - pub nodes: (Expression<'a>, ConstraintSet<'a>), + pub nodes: (Expression<'a>, Symbol<'a>, ConstraintSet<'a>), } #[derive(Debug, Node)] pub struct ConstraintExpressionIf<'a> { - pub nodes: (Expression<'a>, ConstraintSet<'a>, Option>), + pub nodes: ( + Symbol<'a>, + Paren<'a, Expression<'a>>, + ConstraintSet<'a>, + Option<(Symbol<'a>, ConstraintSet<'a>)>, + ), } #[derive(Debug, Node)] pub struct ConstraintExpressionForeach<'a> { pub nodes: ( - PsOrHierarchicalArrayIdentifier<'a>, - LoopVariables<'a>, + Symbol<'a>, + Paren< + 'a, + ( + PsOrHierarchicalArrayIdentifier<'a>, + Bracket<'a, LoopVariables<'a>>, + ), + >, ConstraintSet<'a>, ), } #[derive(Debug, Node)] pub struct ConstraintExpressionDisable<'a> { - pub nodes: (ConstraintPrimary<'a>,), + pub nodes: (Symbol<'a>, Symbol<'a>, ConstraintPrimary<'a>, Symbol<'a>), } #[derive(Debug, Node)] pub struct UniquenessConstraint<'a> { - pub nodes: (OpenRangeList<'a>,), + pub nodes: (Symbol<'a>, Brace<'a, OpenRangeList<'a>>), } #[derive(Debug, Node)] pub enum ConstraintSet<'a> { ConstraintExpression(Box>), - Bracket(ConstraintSetBracket<'a>), + Brace(ConstraintSetBrace<'a>), } #[derive(Debug, Node)] -pub struct ConstraintSetBracket<'a> { - pub nodes: (Vec>,), +pub struct ConstraintSetBrace<'a> { + pub nodes: (Brace<'a, Vec>>,), } #[derive(Debug, Node)] pub struct DistList<'a> { - pub nodes: (Vec>,), + pub nodes: (List, DistItem<'a>>,), } #[derive(Debug, Node)] @@ -129,12 +148,12 @@ pub enum DistWeight<'a> { #[derive(Debug, Node)] pub struct DistWeightEqual<'a> { - pub nodes: (Expression<'a>,), + pub nodes: (Symbol<'a>, Expression<'a>), } #[derive(Debug, Node)] pub struct DistWeightDivide<'a> { - pub nodes: (Expression<'a>,), + pub nodes: (Symbol<'a>, Expression<'a>), } #[derive(Debug, Node)] @@ -142,7 +161,9 @@ pub struct ConstraintPrototype<'a> { pub nodes: ( Option>, Option>, + Symbol<'a>, ConstraintIdentifier<'a>, + Symbol<'a>, ), } @@ -156,6 +177,7 @@ pub enum ConstraintPrototypeQualifier<'a> { pub struct ExternConstraintDeclaration<'a> { pub nodes: ( Option>, + Symbol<'a>, ClassScope<'a>, ConstraintIdentifier<'a>, ConstraintBlock<'a>, @@ -164,67 +186,231 @@ pub struct ExternConstraintDeclaration<'a> { #[derive(Debug, Node)] pub struct IdentifierList<'a> { - pub nodes: (Vec>,), + pub nodes: (List, Identifier<'a>>,), } // ----------------------------------------------------------------------------- pub fn constraint_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = opt(r#static)(s)?; + let (s, b) = symbol("constraint")(s)?; + let (s, c) = constraint_identifier(s)?; + let (s, d) = constraint_block(s)?; + Ok(( + s, + ConstraintDeclaration { + nodes: (a, b, c, d), + }, + )) +} + +pub fn r#static(s: Span) -> IResult { + let (s, a) = symbol("static")(s)?; + Ok((s, Static { nodes: (a,) })) } pub fn constraint_block(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = brace(many0(constraint_block_item))(s)?; + Ok((s, ConstraintBlock { nodes: (a,) })) } pub fn constraint_block_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + constraint_block_item_solve, + map(constraint_expression, |x| { + ConstraintBlockItem::ConstraintExpression(x) + }), + ))(s) +} + +pub fn constraint_block_item_solve(s: Span) -> IResult { + let (s, a) = symbol("solve")(s)?; + let (s, b) = solve_before_list(s)?; + let (s, c) = symbol("before")(s)?; + let (s, d) = solve_before_list(s)?; + let (s, e) = symbol(";")(s)?; + Ok(( + s, + ConstraintBlockItem::Solve(ConstraintBlockItemSolve { + nodes: (a, b, c, d, e), + }), + )) } pub fn solve_before_list(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), constraint_primary)(s)?; + Ok((s, SolveBeforeList { nodes: (a,) })) } pub fn constraint_primary(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = opt(implicit_class_handle_or_class_scope)(s)?; + let (s, b) = hierarchical_identifier(s)?; + let (s, c) = select(s)?; + Ok((s, ConstraintPrimary { nodes: (a, b, c) })) } pub fn constraint_expression(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + constraint_expression_expression, + map(pair(uniqueness_constraint, symbol(";")), |x| { + ConstraintExpression::UniquenessConstraint(x) + }), + constraint_expression_arrow, + constraint_expression_if, + constraint_expression_foreach, + constraint_expression_disable, + ))(s) +} + +pub fn constraint_expression_expression(s: Span) -> IResult { + let (s, a) = opt(soft)(s)?; + let (s, b) = expression_or_dist(s)?; + let (s, c) = symbol(";")(s)?; + Ok(( + s, + ConstraintExpression::Expression(ConstraintExpressionExpression { nodes: (a, b, c) }), + )) +} + +pub fn soft(s: Span) -> IResult { + let (s, a) = symbol("soft")(s)?; + Ok((s, Soft { nodes: (a,) })) +} + +pub fn constraint_expression_arrow(s: Span) -> IResult { + let (s, a) = expression(s)?; + let (s, b) = symbol("->")(s)?; + let (s, c) = constraint_set(s)?; + Ok(( + s, + ConstraintExpression::Arrow(ConstraintExpressionArrow { nodes: (a, b, c) }), + )) +} + +pub fn constraint_expression_if(s: Span) -> IResult { + let (s, a) = symbol("if")(s)?; + let (s, b) = paren(expression)(s)?; + let (s, c) = constraint_set(s)?; + let (s, d) = opt(pair(symbol("else"), constraint_set))(s)?; + Ok(( + s, + ConstraintExpression::If(ConstraintExpressionIf { + nodes: (a, b, c, d), + }), + )) +} + +pub fn constraint_expression_foreach(s: Span) -> IResult { + let (s, a) = symbol("foreach")(s)?; + let (s, b) = paren(pair( + ps_or_hierarchical_array_identifier, + bracket(loop_variables), + ))(s)?; + let (s, c) = constraint_set(s)?; + Ok(( + s, + ConstraintExpression::Foreach(ConstraintExpressionForeach { nodes: (a, b, c) }), + )) +} + +pub fn constraint_expression_disable(s: Span) -> IResult { + let (s, a) = symbol("disable")(s)?; + let (s, b) = symbol("soft")(s)?; + let (s, c) = constraint_primary(s)?; + let (s, d) = symbol(";")(s)?; + Ok(( + s, + ConstraintExpression::Disable(ConstraintExpressionDisable { + nodes: (a, b, c, d), + }), + )) } pub fn uniqueness_constraint(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("unique")(s)?; + let (s, b) = brace(open_range_list)(s)?; + Ok((s, UniquenessConstraint { nodes: (a, b) })) } pub fn constraint_set(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(constraint_expression, |x| { + ConstraintSet::ConstraintExpression(Box::new(x)) + }), + constraint_set_brace, + ))(s) +} + +pub fn constraint_set_brace(s: Span) -> IResult { + let (s, a) = brace(many0(constraint_expression))(s)?; + Ok((s, ConstraintSet::Brace(ConstraintSetBrace { nodes: (a,) }))) } pub fn dist_list(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), dist_item)(s)?; + Ok((s, DistList { nodes: (a,) })) } pub fn dist_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = value_range(s)?; + let (s, b) = opt(dist_weight)(s)?; + Ok((s, DistItem { nodes: (a, b) })) } pub fn dist_weight(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt((dist_weight_equal, dist_weight_divide))(s) +} + +pub fn dist_weight_equal(s: Span) -> IResult { + let (s, a) = symbol(":=")(s)?; + let (s, b) = expression(s)?; + Ok((s, DistWeight::Equal(DistWeightEqual { nodes: (a, b) }))) +} + +pub fn dist_weight_divide(s: Span) -> IResult { + let (s, a) = symbol(":/")(s)?; + let (s, b) = expression(s)?; + Ok((s, DistWeight::Divide(DistWeightDivide { nodes: (a, b) }))) } pub fn constraint_prototype(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = opt(constraint_prototype_qualifier)(s)?; + let (s, b) = opt(r#static)(s)?; + let (s, c) = symbol("constraint")(s)?; + let (s, d) = constraint_identifier(s)?; + let (s, e) = symbol(";")(s)?; + Ok(( + s, + ConstraintPrototype { + nodes: (a, b, c, d, e), + }, + )) } pub fn constraint_prototype_qualifier(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("extern"), |x| { + ConstraintPrototypeQualifier::Extern(x) + }), + map(symbol("pure"), |x| ConstraintPrototypeQualifier::Pure(x)), + ))(s) } pub fn extern_constraint_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = opt(r#static)(s)?; + let (s, b) = symbol("constraint")(s)?; + let (s, c) = class_scope(s)?; + let (s, d) = constraint_identifier(s)?; + let (s, e) = constraint_block(s)?; + Ok(( + s, + ExternConstraintDeclaration { + nodes: (a, b, c, d, e), + }, + )) } pub fn identifier_list(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = list(symbol(","), identifier)(s)?; + Ok((s, IdentifierList { nodes: (a,) })) } diff --git a/src/parser/source_text/interface_items.rs b/src/parser/source_text/interface_items.rs index b620b81..e8c72f8 100644 --- a/src/parser/source_text/interface_items.rs +++ b/src/parser/source_text/interface_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; // ----------------------------------------------------------------------------- @@ -31,17 +32,17 @@ pub enum ExternTfDeclaration<'a> { #[derive(Debug, Node)] pub struct ExternTfDeclarationMethod<'a> { - pub nodes: (MethodPrototype<'a>), + pub nodes: (Symbol<'a>, MethodPrototype<'a>, Symbol<'a>), } #[derive(Debug, Node)] pub struct ExternTfDeclarationTask<'a> { - pub nodes: (TaskPrototype<'a>), + pub nodes: (Symbol<'a>, Symbol<'a>, TaskPrototype<'a>, Symbol<'a>), } #[derive(Debug, Node)] pub enum InterfaceItem<'a> { - PortDeclaration(PortDeclaration<'a>), + PortDeclaration((PortDeclaration<'a>, Symbol<'a>)), NonPortInterfaceItem(NonPortInterfaceItem<'a>), } @@ -58,17 +59,85 @@ pub enum NonPortInterfaceItem<'a> { // ----------------------------------------------------------------------------- pub fn interface_or_generate_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + interface_or_generate_item_module, + interface_or_generate_item_extern, + ))(s) +} + +pub fn interface_or_generate_item_module(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = module_common_item(s)?; + Ok(( + s, + InterfaceOrGenerateItem::Module(InterfaceOrGenerateItemModule { nodes: (a, b) }), + )) +} + +pub fn interface_or_generate_item_extern(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = extern_tf_declaration(s)?; + Ok(( + s, + InterfaceOrGenerateItem::Extern(InterfaceOrGenerateItemExtern { nodes: (a, b) }), + )) } pub fn extern_tf_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt((extern_tf_declaration_method, extern_tf_declaration_task))(s) +} + +pub fn extern_tf_declaration_method(s: Span) -> IResult { + let (s, a) = symbol("extern")(s)?; + let (s, b) = method_prototype(s)?; + let (s, c) = symbol(";")(s)?; + Ok(( + s, + ExternTfDeclaration::Method(ExternTfDeclarationMethod { nodes: (a, b, c) }), + )) +} + +pub fn extern_tf_declaration_task(s: Span) -> IResult { + let (s, a) = symbol("extern")(s)?; + let (s, b) = symbol("forkjoin")(s)?; + let (s, c) = task_prototype(s)?; + let (s, d) = symbol(";")(s)?; + Ok(( + s, + ExternTfDeclaration::Task(ExternTfDeclarationTask { + nodes: (a, b, c, d), + }), + )) } pub fn interface_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(pair(port_declaration, symbol(";")), |x| { + InterfaceItem::PortDeclaration(x) + }), + map(non_port_interface_item, |x| { + InterfaceItem::NonPortInterfaceItem(x) + }), + ))(s) } pub fn non_port_interface_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(generate_region, |x| NonPortInterfaceItem::GenerateRegion(x)), + map(interface_or_generate_item, |x| { + NonPortInterfaceItem::InterfaceOrGenerateItem(x) + }), + map(program_declaration, |x| { + NonPortInterfaceItem::ProgramDeclaration(x) + }), + map(modport_declaration, |x| { + NonPortInterfaceItem::ModportDeclaration(x) + }), + map(interface_declaration, |x| { + NonPortInterfaceItem::InterfaceDeclaration(x) + }), + map(timeunits_declaration, |x| { + NonPortInterfaceItem::TimeunitsDeclaration(x) + }), + ))(s) } diff --git a/src/parser/source_text/program_items.rs b/src/parser/source_text/program_items.rs index b105a71..174bc15 100644 --- a/src/parser/source_text/program_items.rs +++ b/src/parser/source_text/program_items.rs @@ -1,15 +1,16 @@ 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; // ----------------------------------------------------------------------------- #[derive(Debug, Node)] pub enum ProgramItem<'a> { - PortDeclaration(PortDeclaration<'a>), + PortDeclaration((PortDeclaration<'a>, Symbol<'a>)), NonPortProgramItem(NonPortProgramItem<'a>), } @@ -54,7 +55,7 @@ pub struct NonPortProgramItemAssertion<'a> { #[derive(Debug, Node)] pub enum ProgramGenerateItem<'a> { - LoopGenerateConstuct(LoopGenerateConstruct<'a>), + LoopGenerateConstruct(LoopGenerateConstruct<'a>), ConditionalGenerateConstruct(ConditionalGenerateConstruct<'a>), GenerateRegion(GenerateRegion<'a>), ElaborationSystemTask(ElaborationSystemTask<'a>), @@ -63,13 +64,88 @@ pub enum ProgramGenerateItem<'a> { // ----------------------------------------------------------------------------- pub fn program_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(pair(port_declaration, symbol(";")), |x| { + ProgramItem::PortDeclaration(x) + }), + map(non_port_program_item, |x| { + ProgramItem::NonPortProgramItem(x) + }), + ))(s) } pub fn non_port_program_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + non_port_program_item_assign, + non_port_program_item_module, + non_port_program_item_initial, + non_port_program_item_final, + non_port_program_item_assertion, + map(timeunits_declaration, |x| { + NonPortProgramItem::TimeunitsDeclaration(x) + }), + map(program_generate_item, |x| { + NonPortProgramItem::ProgramGenerateItem(x) + }), + ))(s) +} + +pub fn non_port_program_item_assign(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = continuous_assign(s)?; + Ok(( + s, + NonPortProgramItem::Assign(NonPortProgramItemAssign { nodes: (a, b) }), + )) +} + +pub fn non_port_program_item_module(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = module_or_generate_item_declaration(s)?; + Ok(( + s, + NonPortProgramItem::Module(NonPortProgramItemModule { nodes: (a, b) }), + )) +} + +pub fn non_port_program_item_initial(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = initial_construct(s)?; + Ok(( + s, + NonPortProgramItem::Initial(NonPortProgramItemInitial { nodes: (a, b) }), + )) +} + +pub fn non_port_program_item_final(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = final_construct(s)?; + Ok(( + s, + NonPortProgramItem::Final(NonPortProgramItemFinal { nodes: (a, b) }), + )) +} + +pub fn non_port_program_item_assertion(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = concurrent_assertion_item(s)?; + Ok(( + s, + NonPortProgramItem::Assertion(NonPortProgramItemAssertion { nodes: (a, b) }), + )) } pub fn program_generate_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(loop_generate_construct, |x| { + ProgramGenerateItem::LoopGenerateConstruct(x) + }), + map(conditional_generate_construct, |x| { + ProgramGenerateItem::ConditionalGenerateConstruct(x) + }), + map(generate_region, |x| ProgramGenerateItem::GenerateRegion(x)), + map(elaboration_system_task, |x| { + ProgramGenerateItem::ElaborationSystemTask(x) + }), + ))(s) }