From 26cb4e681860f8939a7f6a33e54bdce1bcd01337 Mon Sep 17 00:00:00 2001 From: dalance Date: Wed, 10 Jul 2019 18:06:33 +0900 Subject: [PATCH] Refactoring --- README.md | 6 +- src/parser/expressions/concatenations.rs | 261 ++++++++++---- src/parser/expressions/expressions.rs | 4 +- src/parser/expressions/subroutine_calls.rs | 327 ++++++++++++------ .../instantiations/module_instantiation.rs | 182 +++++++--- 5 files changed, 545 insertions(+), 235 deletions(-) diff --git a/README.md b/README.md index ac7f9a6..d65a349 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ A parser library for System Verilog. | primitive_instance | primitive_strengths | | | | | primitive_instance | primitive_terminals | | | | | primitive_instance | primitive_gate_and_switch_types | | | | -| instantiations | module_instantiation | | | | +| instantiations | module_instantiation | x | x | | | instantiations | interface_instantiation | x | x | | | instantiations | program_instantiation | x | x | | | instantiations | checker_instantiation | x | x | | @@ -65,8 +65,8 @@ A parser library for System Verilog. | specify_section | system_timing_check_commands | | | | | specify_section | system_timing_check_command_arguments | | | | | specify_section | system_timing_check_event_definitions | | | | -| expressions | concatenations | | | | -| expressions | subroutine_calls | | | | +| expressions | concatenations | x | x | | +| expressions | subroutine_calls | x | x | | | expressions | expressions | x | x | | | expressions | primaries | x | x | | | expressions | expression_leftside_values | x | x | | diff --git a/src/parser/expressions/concatenations.rs b/src/parser/expressions/concatenations.rs index a19f46e..ff8c6e9 100644 --- a/src/parser/expressions/concatenations.rs +++ b/src/parser/expressions/concatenations.rs @@ -9,40 +9,67 @@ use nom::IResult; #[derive(Debug)] pub struct Concatenation<'a> { - pub nodes: (Vec>,), + pub nodes: ( + Symbol<'a>, + Expression<'a>, + Vec<(Symbol<'a>, Expression<'a>)>, + Symbol<'a>, + ), } #[derive(Debug)] pub struct ConstantConcatenation<'a> { - pub nodes: (Vec>,), + pub nodes: ( + Symbol<'a>, + ConstantExpression<'a>, + Vec<(Symbol<'a>, ConstantExpression<'a>)>, + Symbol<'a>, + ), } #[derive(Debug)] pub struct ConstantMultipleConcatenation<'a> { - pub nodes: (ConstantExpression<'a>, ConstantConcatenation<'a>), + pub nodes: ( + Symbol<'a>, + ConstantExpression<'a>, + ConstantConcatenation<'a>, + Symbol<'a>, + ), } #[derive(Debug)] pub struct ModulePathConcatenation<'a> { - pub nodes: (Vec>,), + pub nodes: ( + Symbol<'a>, + ModulePathExpression<'a>, + Vec<(Symbol<'a>, ModulePathExpression<'a>)>, + Symbol<'a>, + ), } #[derive(Debug)] pub struct ModulePathMultipleConcatenation<'a> { - pub nodes: (ConstantExpression<'a>, ModulePathConcatenation<'a>), + pub nodes: ( + Symbol<'a>, + ConstantExpression<'a>, + ModulePathConcatenation<'a>, + Symbol<'a>, + ), } #[derive(Debug)] pub struct MultipleConcatenation<'a> { - pub nodes: (Expression<'a>, Concatenation<'a>), + pub nodes: (Symbol<'a>, Expression<'a>, Concatenation<'a>, Symbol<'a>), } #[derive(Debug)] pub struct StreamingConcatenation<'a> { pub nodes: ( + Symbol<'a>, StreamOperator<'a>, Option>, StreamConcatenation<'a>, + Symbol<'a>, ), } @@ -53,32 +80,52 @@ pub struct StreamOperator<'a> { #[derive(Debug)] pub enum SliceSize<'a> { - Type(SimpleType<'a>), - Expression(ConstantExpression<'a>), + SimpleType(SimpleType<'a>), + ConstantExpression(ConstantExpression<'a>), } #[derive(Debug)] pub struct StreamConcatenation<'a> { - pub nodes: (Vec>,), -} - -#[derive(Debug)] -pub struct StreamExpression<'a> { - pub nodes: (Expression<'a>, Option>), -} - -#[derive(Debug)] -pub struct ArrayRangeExpression<'a> { pub nodes: ( - Expression<'a>, - Option>, - Option>, + Symbol<'a>, + StreamExpression<'a>, + Vec<(Symbol<'a>, StreamExpression<'a>)>, + Symbol<'a>, ), } #[derive(Debug)] -pub struct ArrayRangeOperator<'a> { - pub nodes: (Symbol<'a>,), +pub struct StreamExpression<'a> { + pub nodes: ( + Expression<'a>, + Option<( + Symbol<'a>, + (Symbol<'a>, ArrayRangeExpression<'a>, Symbol<'a>), + )>, + ), +} + +#[derive(Debug)] +pub enum ArrayRangeExpression<'a> { + Expression(Expression<'a>), + Colon(ArrayRangeExpressionColon<'a>), + PlusColon(ArrayRangeExpressionPlusColon<'a>), + MinusColon(ArrayRangeExpressionMinusColon<'a>), +} + +#[derive(Debug)] +pub struct ArrayRangeExpressionColon<'a> { + pub nodes: (Expression<'a>, Symbol<'a>, Expression<'a>), +} + +#[derive(Debug)] +pub struct ArrayRangeExpressionPlusColon<'a> { + pub nodes: (Expression<'a>, Symbol<'a>, Expression<'a>), +} + +#[derive(Debug)] +pub struct ArrayRangeExpressionMinusColon<'a> { + pub nodes: (Expression<'a>, Symbol<'a>, Expression<'a>), } #[derive(Debug)] @@ -89,53 +136,97 @@ pub struct EmptyUnpackedArrayConcatenation<'a> { // ----------------------------------------------------------------------------- pub fn concatenation(s: Span) -> IResult { - let (s, x) = brace(separated_nonempty_list(symbol(","), expression))(s)?; - Ok((s, Concatenation { nodes: (x,) })) + let (s, a) = symbol("{")(s)?; + let (s, b) = expression(s)?; + let (s, c) = many0(pair(symbol(","), expression))(s)?; + let (s, d) = symbol("}")(s)?; + Ok(( + s, + Concatenation { + nodes: (a, b, c, d), + }, + )) } pub fn constant_concatenation(s: Span) -> IResult { - let (s, x) = brace(separated_nonempty_list(symbol(","), constant_expression))(s)?; - Ok((s, ConstantConcatenation { nodes: (x,) })) + let (s, a) = symbol("{")(s)?; + let (s, b) = constant_expression(s)?; + let (s, c) = many0(pair(symbol(","), constant_expression))(s)?; + let (s, d) = symbol("}")(s)?; + Ok(( + s, + ConstantConcatenation { + nodes: (a, b, c, d), + }, + )) } pub fn constant_multiple_concatenation(s: Span) -> IResult { - let (s, _) = symbol("{")(s)?; - let (s, x) = constant_expression(s)?; - let (s, y) = constant_concatenation(s)?; - let (s, _) = symbol("}")(s)?; - Ok((s, ConstantMultipleConcatenation { nodes: (x, y) })) + let (s, a) = symbol("{")(s)?; + let (s, b) = constant_expression(s)?; + let (s, c) = constant_concatenation(s)?; + let (s, d) = symbol("}")(s)?; + Ok(( + s, + ConstantMultipleConcatenation { + nodes: (a, b, c, d), + }, + )) } pub fn module_path_concatenation(s: Span) -> IResult { - let (s, x) = brace(separated_nonempty_list(symbol(","), module_path_expression))(s)?; - Ok((s, ModulePathConcatenation { nodes: (x,) })) + let (s, a) = symbol("{")(s)?; + let (s, b) = module_path_expression(s)?; + let (s, c) = many0(pair(symbol(","), module_path_expression))(s)?; + let (s, d) = symbol("}")(s)?; + Ok(( + s, + ModulePathConcatenation { + nodes: (a, b, c, d), + }, + )) } pub fn module_path_multiple_concatenation( s: Span, ) -> IResult { - let (s, _) = symbol("{")(s)?; - let (s, x) = constant_expression(s)?; - let (s, y) = module_path_concatenation(s)?; - let (s, _) = symbol("}")(s)?; - Ok((s, ModulePathMultipleConcatenation { nodes: (x, y) })) + let (s, a) = symbol("{")(s)?; + let (s, b) = constant_expression(s)?; + let (s, c) = module_path_concatenation(s)?; + let (s, d) = symbol("}")(s)?; + Ok(( + s, + ModulePathMultipleConcatenation { + nodes: (a, b, c, d), + }, + )) } pub fn multiple_concatenation(s: Span) -> IResult { - let (s, _) = symbol("{")(s)?; - let (s, x) = expression(s)?; - let (s, y) = concatenation(s)?; - let (s, _) = symbol("}")(s)?; - Ok((s, MultipleConcatenation { nodes: (x, y) })) + let (s, a) = symbol("{")(s)?; + let (s, b) = expression(s)?; + let (s, c) = concatenation(s)?; + let (s, d) = symbol("}")(s)?; + Ok(( + s, + MultipleConcatenation { + nodes: (a, b, c, d), + }, + )) } pub fn streaming_concatenation(s: Span) -> IResult { - let (s, _) = symbol("{")(s)?; - let (s, x) = stream_operator(s)?; - let (s, y) = opt(slice_size)(s)?; - let (s, z) = stream_concatenation(s)?; - let (s, _) = symbol("}")(s)?; - Ok((s, StreamingConcatenation { nodes: (x, y, z) })) + let (s, a) = symbol("{")(s)?; + let (s, b) = stream_operator(s)?; + let (s, c) = opt(slice_size)(s)?; + let (s, d) = stream_concatenation(s)?; + let (s, e) = symbol("}")(s)?; + Ok(( + s, + StreamingConcatenation { + nodes: (a, b, c, d, e), + }, + )) } pub fn stream_operator(s: Span) -> IResult { @@ -147,39 +238,67 @@ pub fn stream_operator(s: Span) -> IResult { pub fn slice_size(s: Span) -> IResult { alt(( - map(simple_type, |x| SliceSize::Type(x)), - map(constant_expression, |x| SliceSize::Expression(x)), + map(simple_type, |x| SliceSize::SimpleType(x)), + map(constant_expression, |x| SliceSize::ConstantExpression(x)), ))(s) } pub fn stream_concatenation(s: Span) -> IResult { - let (s, x) = brace(separated_nonempty_list(symbol(","), stream_expression))(s)?; - Ok((s, StreamConcatenation { nodes: (x,) })) + let (s, a) = symbol("{")(s)?; + let (s, b) = stream_expression(s)?; + let (s, c) = many0(pair(symbol(","), stream_expression))(s)?; + let (s, d) = symbol("}")(s)?; + Ok(( + s, + StreamConcatenation { + nodes: (a, b, c, d), + }, + )) } pub fn stream_expression(s: Span) -> IResult { - let (s, x) = expression(s)?; - let (s, y) = opt(preceded(symbol("with"), bracket(array_range_expression)))(s)?; - Ok((s, StreamExpression { nodes: (x, y) })) + let (s, a) = expression(s)?; + let (s, b) = opt(pair(symbol("with"), bracket2(array_range_expression)))(s)?; + Ok((s, StreamExpression { nodes: (a, b) })) } pub fn array_range_expression(s: Span) -> IResult { - let (s, x) = expression(s)?; - let (s, y) = opt(pair(array_range_operator, expression))(s)?; - let (y, z) = if let Some((y, z)) = y { - (Some(y), Some(z)) - } else { - (None, None) - }; - Ok((s, ArrayRangeExpression { nodes: (x, y, z) })) + alt(( + map(expression, |x| ArrayRangeExpression::Expression(x)), + array_range_expression_colon, + array_range_expression_plus_colon, + array_range_expression_minus_colon, + ))(s) } -pub fn array_range_operator(s: Span) -> IResult { - alt(( - map(symbol(":"), |x| ArrayRangeOperator { nodes: (x,) }), - map(symbol("+:"), |x| ArrayRangeOperator { nodes: (x,) }), - map(symbol("-:"), |x| ArrayRangeOperator { nodes: (x,) }), - ))(s) +pub fn array_range_expression_colon(s: Span) -> IResult { + let (s, a) = expression(s)?; + let (s, b) = symbol(":")(s)?; + let (s, c) = expression(s)?; + Ok(( + s, + ArrayRangeExpression::Colon(ArrayRangeExpressionColon { nodes: (a, b, c) }), + )) +} + +pub fn array_range_expression_plus_colon(s: Span) -> IResult { + let (s, a) = expression(s)?; + let (s, b) = symbol("+:")(s)?; + let (s, c) = expression(s)?; + Ok(( + s, + ArrayRangeExpression::PlusColon(ArrayRangeExpressionPlusColon { nodes: (a, b, c) }), + )) +} + +pub fn array_range_expression_minus_colon(s: Span) -> IResult { + let (s, a) = expression(s)?; + let (s, b) = symbol("-:")(s)?; + let (s, c) = expression(s)?; + Ok(( + s, + ArrayRangeExpression::MinusColon(ArrayRangeExpressionMinusColon { nodes: (a, b, c) }), + )) } pub fn empty_unpacked_array_concatenation( diff --git a/src/parser/expressions/expressions.rs b/src/parser/expressions/expressions.rs index f5df308..77b30f4 100644 --- a/src/parser/expressions/expressions.rs +++ b/src/parser/expressions/expressions.rs @@ -108,7 +108,7 @@ pub enum ConstantParamExpression<'a> { #[derive(Debug)] pub enum ParamExpression<'a> { MintypmaxExpression(MintypmaxExpression<'a>), - DataType(DataType<'a>), + DataType(Box>), Dollar(Symbol<'a>), } @@ -419,7 +419,7 @@ pub fn param_expression(s: Span) -> IResult { map(mintypmax_expression, |x| { ParamExpression::MintypmaxExpression(x) }), - map(data_type, |x| ParamExpression::DataType(x)), + map(data_type, |x| ParamExpression::DataType(Box::new(x))), ))(s) } diff --git a/src/parser/expressions/subroutine_calls.rs b/src/parser/expressions/subroutine_calls.rs index e23929d..242e424 100644 --- a/src/parser/expressions/subroutine_calls.rs +++ b/src/parser/expressions/subroutine_calls.rs @@ -17,28 +17,59 @@ pub struct TfCall<'a> { pub nodes: ( PsOrHierarchicalTfIdentifier<'a>, Vec>, - Option>, + Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>, ), } #[derive(Debug)] -pub struct SystemTfCall<'a> { +pub enum SystemTfCall<'a> { + ArgOptionl(SystemTfCallArgOptional<'a>), + ArgDataType(SystemTfCallArgDataType<'a>), + ArgExpression(SystemTfCallArgExpression<'a>), +} + +#[derive(Debug)] +pub struct SystemTfCallArgOptional<'a> { pub nodes: ( SystemTfIdentifier<'a>, - Option>, - Option>, - Option>>, - Option>, + Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>, + ), +} + +#[derive(Debug)] +pub struct SystemTfCallArgDataType<'a> { + pub nodes: ( + SystemTfIdentifier<'a>, + Symbol<'a>, + DataType<'a>, + Option<(Symbol<'a>, Expression<'a>)>, + Symbol<'a>, + ), +} + +#[derive(Debug)] +pub struct SystemTfCallArgExpression<'a> { + pub nodes: ( + SystemTfIdentifier<'a>, + Symbol<'a>, + Expression<'a>, + Vec<(Symbol<'a>, Option>)>, + Option<(Symbol<'a>, Option>)>, + Symbol<'a>, ), } #[derive(Debug)] pub enum SubroutineCall<'a> { - Tf(Box>), - SystemTf(Box>), - Method(Box>), - Randomize(Box>), - StdRandomize(Box>), + TfCall(Box>), + SystemTfCall(Box>), + MethodCall(Box>), + Randomize(Box>), +} + +#[derive(Debug)] +pub struct SubroutineCallRandomize<'a> { + pub nodes: (Option<(Symbol<'a>, Symbol<'a>)>, RandomizeCall<'a>), } #[derive(Debug)] @@ -47,16 +78,49 @@ pub struct FunctionSubroutineCall<'a> { } #[derive(Debug)] -pub struct ListOfArguments<'a> { +pub enum ListOfArguments<'a> { + Ordered(ListOfArgumentsOrdered<'a>), + Named(ListOfArgumentsNamed<'a>), +} + +#[derive(Debug)] +pub struct ListOfArgumentsOrdered<'a> { pub nodes: ( - Vec>, - Vec<(Identifier<'a>, Option>)>, + Option>, + Vec<(Symbol<'a>, Option>)>, + Vec<( + Symbol<'a>, + Symbol<'a>, + Identifier<'a>, + Symbol<'a>, + Option>, + Symbol<'a>, + )>, + ), +} + +#[derive(Debug)] +pub struct ListOfArgumentsNamed<'a> { + pub nodes: ( + Symbol<'a>, + Identifier<'a>, + Symbol<'a>, + Option>, + Symbol<'a>, + Vec<( + Symbol<'a>, + Symbol<'a>, + Identifier<'a>, + Symbol<'a>, + Option>, + Symbol<'a>, + )>, ), } #[derive(Debug)] pub struct MethodCall<'a> { - pub nodes: (MethodCallRoot<'a>, MethodCallBody<'a>), + pub nodes: (MethodCallRoot<'a>, Symbol<'a>, MethodCallBody<'a>), } #[derive(Debug)] @@ -70,7 +134,7 @@ pub struct MethodCallBodyUser<'a> { pub nodes: ( MethodIdentifier<'a>, Vec>, - Option>, + Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>, ), } @@ -85,21 +149,35 @@ pub struct ArrayManipulationCall<'a> { pub nodes: ( ArrayMethodName<'a>, Vec>, - Option>, - Option>, + Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>, + Option<(Symbol<'a>, (Symbol<'a>, Expression<'a>, Symbol<'a>))>, ), } #[derive(Debug)] pub struct RandomizeCall<'a> { pub nodes: ( + Symbol<'a>, Vec>, - VariableIdentifierList<'a>, - IdentifierList<'a>, - Option>, + Option<( + Symbol<'a>, + Option>, + Symbol<'a>, + )>, + Option<( + Symbol<'a>, + Option<(Symbol<'a>, Option>, Symbol<'a>)>, + ConstraintBlock<'a>, + )>, ), } +#[derive(Debug)] +pub enum VariableIdentifierListOrNull<'a> { + VariableIdentifierList(VariableIdentifierList<'a>), + Null(Symbol<'a>), +} + #[derive(Debug)] pub enum MethodCallRoot<'a> { Primary(Primary<'a>), @@ -109,10 +187,10 @@ pub enum MethodCallRoot<'a> { #[derive(Debug)] pub enum ArrayMethodName<'a> { MethodIdentifier(MethodIdentifier<'a>), - Unique, - And, - Or, - Xor, + Unique(Symbol<'a>), + And(Symbol<'a>), + Or(Symbol<'a>), + Xor(Symbol<'a>), } // ----------------------------------------------------------------------------- @@ -123,92 +201,131 @@ pub fn constant_function_call(s: Span) -> IResult { } pub fn tf_call(s: Span) -> IResult { - let (s, x) = ps_or_hierarchical_tf_identifier(s)?; - let (s, y) = many0(attribute_instance)(s)?; - let (s, z) = opt(paren(list_of_arguments))(s)?; - Ok((s, TfCall { nodes: (x, y, z) })) + let (s, a) = ps_or_hierarchical_tf_identifier(s)?; + let (s, b) = many0(attribute_instance)(s)?; + let (s, c) = opt(paren2(list_of_arguments))(s)?; + Ok((s, TfCall { nodes: (a, b, c) })) } pub fn system_tf_call(s: Span) -> IResult { alt(( - system_tf_call_list_of_arguments, - system_tf_call_data_type, - system_tf_call_clocking_event, + system_tf_call_arg_optional, + system_tf_call_arg_data_type, + system_tf_call_arg_expression, ))(s) } -pub fn system_tf_call_list_of_arguments(s: Span) -> IResult { - let (s, x) = system_tf_identifier(s)?; - let (s, y) = opt(paren(list_of_arguments))(s)?; +pub fn system_tf_call_arg_optional(s: Span) -> IResult { + let (s, a) = system_tf_identifier(s)?; + let (s, b) = opt(paren2(list_of_arguments))(s)?; Ok(( s, - SystemTfCall { - nodes: (x, y, None, None, None), - }, + SystemTfCall::ArgOptionl(SystemTfCallArgOptional { nodes: (a, b) }), )) } -pub fn system_tf_call_data_type(s: Span) -> IResult { - let (s, x) = system_tf_identifier(s)?; - let (s, _) = symbol("(")(s)?; - let (s, y) = data_type(s)?; - let (s, z) = preceded(symbol(","), expression)(s)?; - let (s, _) = symbol(")")(s)?; +pub fn system_tf_call_arg_data_type(s: Span) -> IResult { + let (s, a) = system_tf_identifier(s)?; + let (s, b) = symbol("(")(s)?; + let (s, c) = data_type(s)?; + let (s, d) = opt(pair(symbol(","), expression))(s)?; + let (s, e) = symbol(")")(s)?; Ok(( s, - SystemTfCall { - nodes: (x, None, Some(y), Some(vec![z]), None), - }, + SystemTfCall::ArgDataType(SystemTfCallArgDataType { + nodes: (a, b, c, d, e), + }), )) } -pub fn system_tf_call_clocking_event(s: Span) -> IResult { - let (s, x) = system_tf_identifier(s)?; - let (s, _) = symbol("(")(s)?; - let (s, y) = separated_nonempty_list(symbol(","), expression)(s)?; - let (s, z) = opt(preceded(symbol(","), opt(clocking_event)))(s)?; - let (s, _) = symbol(")")(s)?; - let z = if let Some(Some(z)) = z { Some(z) } else { None }; +pub fn system_tf_call_arg_expression(s: Span) -> IResult { + let (s, a) = system_tf_identifier(s)?; + let (s, b) = symbol("(")(s)?; + let (s, c) = expression(s)?; + let (s, d) = many0(pair(symbol(","), opt(expression)))(s)?; + let (s, e) = opt(pair(symbol(","), opt(clocking_event)))(s)?; + let (s, f) = symbol(")")(s)?; Ok(( s, - SystemTfCall { - nodes: (x, None, None, Some(y), z), - }, + SystemTfCall::ArgExpression(SystemTfCallArgExpression { + nodes: (a, b, c, d, e, f), + }), )) } pub fn subroutine_call(s: Span) -> IResult { alt(( - map(tf_call, |x| SubroutineCall::Tf(Box::new(x))), - map(system_tf_call, |x| SubroutineCall::SystemTf(Box::new(x))), - map(method_call, |x| SubroutineCall::Method(Box::new(x))), - map( - triple(symbol("std"), symbol("::"), randomize_call), - |(_, _, x)| SubroutineCall::StdRandomize(Box::new(x)), - ), - map(randomize_call, |x| SubroutineCall::Randomize(Box::new(x))), + map(tf_call, |x| SubroutineCall::TfCall(Box::new(x))), + map(system_tf_call, |x| { + SubroutineCall::SystemTfCall(Box::new(x)) + }), + map(method_call, |x| SubroutineCall::MethodCall(Box::new(x))), + subroutine_call_randomize, ))(s) } +pub fn subroutine_call_randomize(s: Span) -> IResult { + let (s, a) = opt(pair(symbol("std"), symbol("::")))(s)?; + let (s, b) = randomize_call(s)?; + Ok(( + s, + SubroutineCall::Randomize(Box::new(SubroutineCallRandomize { nodes: (a, b) })), + )) +} + pub fn function_subroutine_call(s: Span) -> IResult { map(subroutine_call, |x| FunctionSubroutineCall { nodes: (x,) })(s) } pub fn list_of_arguments(s: Span) -> IResult { - let (s, x) = separated_list(symbol(","), expression)(s)?; - let (s, y) = separated_list( + alt((list_of_arguments_ordered, list_of_arguments_named))(s) +} + +pub fn list_of_arguments_ordered(s: Span) -> IResult { + let (s, a) = opt(expression)(s)?; + let (s, b) = many0(pair(symbol(","), opt(expression)))(s)?; + let (s, c) = many0(tuple(( symbol(","), - pair(preceded(symbol("."), identifier), paren(opt(expression))), - )(s)?; - Ok((s, ListOfArguments { nodes: (x, y) })) + symbol("."), + identifier, + symbol("("), + opt(expression), + symbol(")"), + )))(s)?; + Ok(( + s, + ListOfArguments::Ordered(ListOfArgumentsOrdered { nodes: (a, b, c) }), + )) +} + +pub fn list_of_arguments_named(s: Span) -> IResult { + let (s, a) = symbol(".")(s)?; + let (s, b) = identifier(s)?; + let (s, c) = symbol("(")(s)?; + let (s, d) = opt(expression)(s)?; + let (s, e) = symbol(")")(s)?; + let (s, f) = many0(tuple(( + symbol(","), + symbol("."), + identifier, + symbol("("), + opt(expression), + symbol(")"), + )))(s)?; + Ok(( + s, + ListOfArguments::Named(ListOfArgumentsNamed { + nodes: (a, b, c, d, e, f), + }), + )) } pub fn method_call(s: Span) -> IResult { - let (s, x) = method_call_root(s)?; - let (s, _) = symbol(".")(s)?; - let (s, y) = method_call_body(s)?; + let (s, a) = method_call_root(s)?; + let (s, b) = symbol(".")(s)?; + let (s, c) = method_call_body(s)?; - Ok((s, MethodCall { nodes: (x, y) })) + Ok((s, MethodCall { nodes: (a, b, c) })) } pub fn method_call_body(s: Span) -> IResult { @@ -221,12 +338,12 @@ pub fn method_call_body(s: Span) -> IResult { } pub fn method_call_body_user(s: Span) -> IResult { - let (s, x) = method_identifier(s)?; - let (s, y) = many0(attribute_instance)(s)?; - let (s, z) = opt(paren(list_of_arguments))(s)?; + let (s, a) = method_identifier(s)?; + let (s, b) = many0(attribute_instance)(s)?; + let (s, c) = opt(paren2(list_of_arguments))(s)?; Ok(( s, - MethodCallBody::User(MethodCallBodyUser { nodes: (x, y, z) }), + MethodCallBody::User(MethodCallBodyUser { nodes: (a, b, c) }), )) } @@ -240,50 +357,44 @@ pub fn built_in_method_call(s: Span) -> IResult { } pub fn array_manipulation_call(s: Span) -> IResult { - let (s, x) = array_method_name(s)?; - let (s, y) = many0(attribute_instance)(s)?; - let (s, z) = opt(paren(list_of_arguments))(s)?; - let (s, v) = opt(preceded(symbol("with"), paren(expression)))(s)?; + let (s, a) = array_method_name(s)?; + let (s, b) = many0(attribute_instance)(s)?; + let (s, c) = opt(paren2(list_of_arguments))(s)?; + let (s, d) = opt(pair(symbol("with"), paren2(expression)))(s)?; Ok(( s, ArrayManipulationCall { - nodes: (x, y, z, v), + nodes: (a, b, c, d), }, )) } pub fn randomize_call(s: Span) -> IResult { - let (s, _) = symbol("randomize")(s)?; - let (s, x) = many0(attribute_instance)(s)?; - let (s, y) = opt(paren(opt(alt(( - variable_identifier_list, - map(symbol("null"), |_| VariableIdentifierList { - nodes: (vec![],), - }), - )))))(s)?; - let (s, z) = opt(triple( + let (s, a) = symbol("randomize")(s)?; + let (s, b) = many0(attribute_instance)(s)?; + let (s, c) = opt(paren2(opt(variable_identifier_list_or_null)))(s)?; + let (s, d) = opt(triple( symbol("with"), - opt(paren(opt(identifier_list))), + opt(paren2(opt(identifier_list))), constraint_block, ))(s)?; - let y = if let Some(Some(y)) = y { - y - } else { - VariableIdentifierList { nodes: (vec![],) } - }; - let (z, v) = if let Some((_, Some(Some(z)), v)) = z { - (z, Some(v)) - } else { - (IdentifierList { nodes: (vec![],) }, None) - }; Ok(( s, RandomizeCall { - nodes: (x, y, z, v), + nodes: (a, b, c, d), }, )) } +pub fn variable_identifier_list_or_null(s: Span) -> IResult { + alt(( + map(variable_identifier_list, |x| { + VariableIdentifierListOrNull::VariableIdentifierList(x) + }), + map(symbol("null"), |x| VariableIdentifierListOrNull::Null(x)), + ))(s) +} + pub fn method_call_root(s: Span) -> IResult { alt(( map(primary, |x| MethodCallRoot::Primary(x)), @@ -295,10 +406,10 @@ pub fn method_call_root(s: Span) -> IResult { pub fn array_method_name(s: Span) -> IResult { alt(( - map(symbol("unique"), |_| ArrayMethodName::Unique), - map(symbol("and"), |_| ArrayMethodName::And), - map(symbol("or"), |_| ArrayMethodName::Or), - map(symbol("xor"), |_| ArrayMethodName::Xor), + map(symbol("unique"), |x| ArrayMethodName::Unique(x)), + map(symbol("and"), |x| ArrayMethodName::And(x)), + map(symbol("or"), |x| ArrayMethodName::Or(x)), + map(symbol("xor"), |x| ArrayMethodName::Xor(x)), map(method_identifier, |x| ArrayMethodName::MethodIdentifier(x)), ))(s) } diff --git a/src/parser/instantiations/module_instantiation.rs b/src/parser/instantiations/module_instantiation.rs index 63ac64f..cc20ca6 100644 --- a/src/parser/instantiations/module_instantiation.rs +++ b/src/parser/instantiations/module_instantiation.rs @@ -2,6 +2,7 @@ use crate::parser::*; use nom::branch::*; use nom::combinator::*; use nom::multi::*; +use nom::sequence::*; use nom::IResult; // ----------------------------------------------------------------------------- @@ -11,19 +12,44 @@ pub struct ModuleInstantiation<'a> { pub nodes: ( ModuleIdentifier<'a>, Option>, - Vec>, + HierarchicalInstance<'a>, + Vec<(Symbol<'a>, HierarchicalInstance<'a>)>, + Symbol<'a>, ), } #[derive(Debug)] pub struct ParameterValueAssignment<'a> { - pub nodes: (ListOfParameterAssignments<'a>,), + pub nodes: ( + Symbol<'a>, + ( + Symbol<'a>, + Option>, + Symbol<'a>, + ), + ), } #[derive(Debug)] pub enum ListOfParameterAssignments<'a> { - Ordered(Vec>), - Named(Vec>), + Ordered(ListOfParameterAssignmentsOrdered<'a>), + Named(ListOfParameterAssignmentsNamed<'a>), +} + +#[derive(Debug)] +pub struct ListOfParameterAssignmentsOrdered<'a> { + pub nodes: ( + OrderedParameterAssignment<'a>, + Vec<(Symbol<'a>, OrderedParameterAssignment<'a>)>, + ), +} + +#[derive(Debug)] +pub struct ListOfParameterAssignmentsNamed<'a> { + pub nodes: ( + NamedParameterAssignment<'a>, + Vec<(Symbol<'a>, NamedParameterAssignment<'a>)>, + ), } #[derive(Debug)] @@ -33,12 +59,19 @@ pub struct OrderedParameterAssignment<'a> { #[derive(Debug)] pub struct NamedParameterAssignment<'a> { - pub nodes: (ParameterIdentifier<'a>, Option>), + pub nodes: ( + Symbol<'a>, + ParameterIdentifier<'a>, + (Symbol<'a>, Option>, Symbol<'a>), + ), } #[derive(Debug)] pub struct HierarchicalInstance<'a> { - pub nodes: (NameOfInstance<'a>, Option>), + pub nodes: ( + NameOfInstance<'a>, + (Symbol<'a>, Option>, Symbol<'a>), + ), } #[derive(Debug)] @@ -48,8 +81,24 @@ pub struct NameOfInstance<'a> { #[derive(Debug)] pub enum ListOfPortConnections<'a> { - Ordered(Vec>), - Named(Vec>), + Ordered(ListOfPortConnectionsOrdered<'a>), + Named(ListOfPortConnectionsNamed<'a>), +} + +#[derive(Debug)] +pub struct ListOfPortConnectionsOrdered<'a> { + pub nodes: ( + OrderedPortConnection<'a>, + Vec<(Symbol<'a>, OrderedPortConnection<'a>)>, + ), +} + +#[derive(Debug)] +pub struct ListOfPortConnectionsNamed<'a> { + pub nodes: ( + NamedPortConnection<'a>, + Vec<(Symbol<'a>, NamedPortConnection<'a>)>, + ), } #[derive(Debug)] @@ -67,61 +116,80 @@ pub enum NamedPortConnection<'a> { pub struct NamedPortConnectionIdentifier<'a> { pub nodes: ( Vec>, + Symbol<'a>, PortIdentifier<'a>, - Option>, + Option<(Symbol<'a>, Option>, Symbol<'a>)>, ), } #[derive(Debug)] pub struct NamedPortConnectionAsterisk<'a> { - pub nodes: (Vec>,), + pub nodes: (Vec>, Symbol<'a>), } // ----------------------------------------------------------------------------- pub fn module_instantiation(s: Span) -> IResult { - let (s, x) = module_identifier(s)?; - let (s, y) = opt(parameter_value_assignment)(s)?; - let (s, z) = separated_nonempty_list(symbol(","), hierarchical_instance)(s)?; - let (s, _) = symbol(";")(s)?; - Ok((s, ModuleInstantiation { nodes: (x, y, z) })) + let (s, a) = module_identifier(s)?; + let (s, b) = opt(parameter_value_assignment)(s)?; + let (s, c) = hierarchical_instance(s)?; + let (s, d) = many0(pair(symbol(","), hierarchical_instance))(s)?; + let (s, e) = symbol(";")(s)?; + Ok(( + s, + ModuleInstantiation { + nodes: (a, b, c, d, e), + }, + )) } pub fn parameter_value_assignment(s: Span) -> IResult { - let (s, _) = symbol("#")(s)?; - let (s, x) = paren(list_of_parameter_assignments)(s)?; - Ok((s, ParameterValueAssignment { nodes: (x,) })) + let (s, a) = symbol("#")(s)?; + let (s, b) = paren2(opt(list_of_parameter_assignments))(s)?; + Ok((s, ParameterValueAssignment { nodes: (a, b) })) } pub fn list_of_parameter_assignments(s: Span) -> IResult { alt(( - map( - separated_nonempty_list(symbol(","), ordered_parameter_assignment), - |x| ListOfParameterAssignments::Ordered(x), - ), - map( - separated_nonempty_list(symbol(","), named_parameter_assignment), - |x| ListOfParameterAssignments::Named(x), - ), + list_of_parameter_assignments_ordered, + list_of_parameter_assignments_named, ))(s) } +pub fn list_of_parameter_assignments_ordered(s: Span) -> IResult { + let (s, a) = ordered_parameter_assignment(s)?; + let (s, b) = many0(pair(symbol(","), ordered_parameter_assignment))(s)?; + Ok(( + s, + ListOfParameterAssignments::Ordered(ListOfParameterAssignmentsOrdered { nodes: (a, b) }), + )) +} + +pub fn list_of_parameter_assignments_named(s: Span) -> IResult { + let (s, a) = named_parameter_assignment(s)?; + let (s, b) = many0(pair(symbol(","), named_parameter_assignment))(s)?; + Ok(( + s, + ListOfParameterAssignments::Named(ListOfParameterAssignmentsNamed { nodes: (a, b) }), + )) +} + pub fn ordered_parameter_assignment(s: Span) -> IResult { let (s, x) = param_expression(s)?; Ok((s, OrderedParameterAssignment { nodes: (x,) })) } pub fn named_parameter_assignment(s: Span) -> IResult { - let (s, _) = symbol(".")(s)?; - let (s, x) = parameter_identifier(s)?; - let (s, y) = paren(opt(param_expression))(s)?; - Ok((s, NamedParameterAssignment { nodes: (x, y) })) + let (s, a) = symbol(".")(s)?; + let (s, b) = parameter_identifier(s)?; + let (s, c) = paren2(opt(param_expression))(s)?; + Ok((s, NamedParameterAssignment { nodes: (a, b, c) })) } pub fn hierarchical_instance(s: Span) -> IResult { - let (s, x) = name_of_instance(s)?; - let (s, y) = paren(opt(list_of_port_connections))(s)?; - Ok((s, HierarchicalInstance { nodes: (x, y) })) + let (s, a) = name_of_instance(s)?; + let (s, b) = paren2(opt(list_of_port_connections))(s)?; + Ok((s, HierarchicalInstance { nodes: (a, b) })) } pub fn name_of_instance(s: Span) -> IResult { @@ -132,17 +200,29 @@ pub fn name_of_instance(s: Span) -> IResult { pub fn list_of_port_connections(s: Span) -> IResult { alt(( - map( - separated_nonempty_list(symbol(","), ordered_port_connection), - |x| ListOfPortConnections::Ordered(x), - ), - map( - separated_nonempty_list(symbol(","), named_port_connection), - |x| ListOfPortConnections::Named(x), - ), + list_of_port_connections_ordered, + list_of_port_connections_named, ))(s) } +pub fn list_of_port_connections_ordered(s: Span) -> IResult { + let (s, a) = ordered_port_connection(s)?; + let (s, b) = many0(pair(symbol(","), ordered_port_connection))(s)?; + Ok(( + s, + ListOfPortConnections::Ordered(ListOfPortConnectionsOrdered { nodes: (a, b) }), + )) +} + +pub fn list_of_port_connections_named(s: Span) -> IResult { + let (s, a) = named_port_connection(s)?; + let (s, b) = many0(pair(symbol(","), named_port_connection))(s)?; + Ok(( + s, + ListOfPortConnections::Named(ListOfPortConnectionsNamed { nodes: (a, b) }), + )) +} + pub fn ordered_port_connection(s: Span) -> IResult { let (s, x) = many0(attribute_instance)(s)?; let (s, y) = opt(expression)(s)?; @@ -157,24 +237,24 @@ pub fn named_port_connection(s: Span) -> IResult { } pub fn named_port_connection_identifier(s: Span) -> IResult { - let (s, x) = many0(attribute_instance)(s)?; - let (s, _) = symbol(".")(s)?; - let (s, y) = port_identifier(s)?; - let (s, z) = opt(paren(opt(expression)))(s)?; - let z = if let Some(Some(z)) = z { Some(z) } else { None }; + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = symbol(".")(s)?; + let (s, c) = port_identifier(s)?; + let (s, d) = opt(paren2(opt(expression)))(s)?; Ok(( s, - NamedPortConnection::Identifier(NamedPortConnectionIdentifier { nodes: (x, y, z) }), + NamedPortConnection::Identifier(NamedPortConnectionIdentifier { + nodes: (a, b, c, d), + }), )) } pub fn named_port_connection_asterisk(s: Span) -> IResult { - let (s, x) = many0(attribute_instance)(s)?; - let (s, _) = symbol(".")(s)?; - let (s, _) = symbol("*")(s)?; + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = symbol(".*")(s)?; Ok(( s, - NamedPortConnection::Asterisk(NamedPortConnectionAsterisk { nodes: (x,) }), + NamedPortConnection::Asterisk(NamedPortConnectionAsterisk { nodes: (a, b) }), )) }