diff --git a/src/parser/expressions/concatenations.rs b/src/parser/expressions/concatenations.rs index ff8c6e9..3df100b 100644 --- a/src/parser/expressions/concatenations.rs +++ b/src/parser/expressions/concatenations.rs @@ -1,7 +1,6 @@ use crate::parser::*; use nom::branch::*; use nom::combinator::*; -use nom::multi::*; use nom::sequence::*; use nom::IResult; @@ -9,67 +8,45 @@ use nom::IResult; #[derive(Debug)] pub struct Concatenation<'a> { - pub nodes: ( - Symbol<'a>, - Expression<'a>, - Vec<(Symbol<'a>, Expression<'a>)>, - Symbol<'a>, - ), + pub nodes: (Brace<'a, List, Expression<'a>>>,), } #[derive(Debug)] pub struct ConstantConcatenation<'a> { - pub nodes: ( - Symbol<'a>, - ConstantExpression<'a>, - Vec<(Symbol<'a>, ConstantExpression<'a>)>, - Symbol<'a>, - ), + pub nodes: (Brace<'a, List, ConstantExpression<'a>>>,), } #[derive(Debug)] pub struct ConstantMultipleConcatenation<'a> { - pub nodes: ( - Symbol<'a>, - ConstantExpression<'a>, - ConstantConcatenation<'a>, - Symbol<'a>, - ), + pub nodes: (Brace<'a, (ConstantExpression<'a>, ConstantConcatenation<'a>)>,), } #[derive(Debug)] pub struct ModulePathConcatenation<'a> { - pub nodes: ( - Symbol<'a>, - ModulePathExpression<'a>, - Vec<(Symbol<'a>, ModulePathExpression<'a>)>, - Symbol<'a>, - ), + pub nodes: (Brace<'a, List, ModulePathExpression<'a>>>,), } #[derive(Debug)] pub struct ModulePathMultipleConcatenation<'a> { - pub nodes: ( - Symbol<'a>, - ConstantExpression<'a>, - ModulePathConcatenation<'a>, - Symbol<'a>, - ), + pub nodes: (Brace<'a, (ConstantExpression<'a>, ModulePathConcatenation<'a>)>,), } #[derive(Debug)] pub struct MultipleConcatenation<'a> { - pub nodes: (Symbol<'a>, Expression<'a>, Concatenation<'a>, Symbol<'a>), + pub nodes: (Brace<'a, (Expression<'a>, Concatenation<'a>)>,), } #[derive(Debug)] pub struct StreamingConcatenation<'a> { pub nodes: ( - Symbol<'a>, - StreamOperator<'a>, - Option>, - StreamConcatenation<'a>, - Symbol<'a>, + Brace< + 'a, + ( + StreamOperator<'a>, + Option>, + StreamConcatenation<'a>, + ), + >, ), } @@ -86,22 +63,14 @@ pub enum SliceSize<'a> { #[derive(Debug)] pub struct StreamConcatenation<'a> { - pub nodes: ( - Symbol<'a>, - StreamExpression<'a>, - Vec<(Symbol<'a>, StreamExpression<'a>)>, - Symbol<'a>, - ), + pub nodes: (Brace<'a, List, StreamExpression<'a>>>,), } #[derive(Debug)] pub struct StreamExpression<'a> { pub nodes: ( Expression<'a>, - Option<( - Symbol<'a>, - (Symbol<'a>, ArrayRangeExpression<'a>, Symbol<'a>), - )>, + Option<(Symbol<'a>, Bracket<'a, ArrayRangeExpression<'a>>)>, ), } @@ -136,97 +105,44 @@ pub struct EmptyUnpackedArrayConcatenation<'a> { // ----------------------------------------------------------------------------- pub fn concatenation(s: Span) -> IResult { - 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), - }, - )) + let (s, a) = brace2(list(symbol(","), expression))(s)?; + Ok((s, Concatenation { nodes: (a,) })) } pub fn constant_concatenation(s: Span) -> IResult { - 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), - }, - )) + let (s, a) = brace2(list(symbol(","), constant_expression))(s)?; + Ok((s, ConstantConcatenation { nodes: (a,) })) } pub fn constant_multiple_concatenation(s: Span) -> IResult { - 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), - }, - )) + let (s, a) = brace2(pair(constant_expression, constant_concatenation))(s)?; + Ok((s, ConstantMultipleConcatenation { nodes: (a,) })) } pub fn module_path_concatenation(s: Span) -> IResult { - 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), - }, - )) + let (s, a) = brace2(list(symbol(","), module_path_expression))(s)?; + Ok((s, ModulePathConcatenation { nodes: (a,) })) } pub fn module_path_multiple_concatenation( s: Span, ) -> IResult { - 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), - }, - )) + let (s, a) = brace2(pair(constant_expression, module_path_concatenation))(s)?; + Ok((s, ModulePathMultipleConcatenation { nodes: (a,) })) } pub fn multiple_concatenation(s: Span) -> IResult { - 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), - }, - )) + let (s, a) = brace2(pair(expression, concatenation))(s)?; + Ok((s, MultipleConcatenation { nodes: (a,) })) } pub fn streaming_concatenation(s: Span) -> IResult { - 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), - }, - )) + let (s, a) = brace2(triple( + stream_operator, + opt(slice_size), + stream_concatenation, + ))(s)?; + Ok((s, StreamingConcatenation { nodes: (a,) })) } pub fn stream_operator(s: Span) -> IResult { @@ -244,16 +160,8 @@ pub fn slice_size(s: Span) -> IResult { } pub fn stream_concatenation(s: Span) -> IResult { - 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), - }, - )) + let (s, a) = brace2(list(symbol(","), stream_expression))(s)?; + Ok((s, StreamConcatenation { nodes: (a,) })) } pub fn stream_expression(s: Span) -> IResult { diff --git a/src/parser/expressions/expression_leftside_values.rs b/src/parser/expressions/expression_leftside_values.rs index 70ad807..721f1a8 100644 --- a/src/parser/expressions/expression_leftside_values.rs +++ b/src/parser/expressions/expression_leftside_values.rs @@ -1,8 +1,6 @@ use crate::parser::*; use nom::branch::*; use nom::combinator::*; -use nom::multi::*; -use nom::sequence::*; use nom::IResult; // ----------------------------------------------------------------------------- @@ -21,12 +19,7 @@ pub struct NetLvalueIdentifier<'a> { #[derive(Debug)] pub struct NetLvalueLvalue<'a> { - pub nodes: ( - Symbol<'a>, - NetLvalue<'a>, - Vec<(Symbol<'a>, NetLvalue<'a>)>, - Symbol<'a>, - ), + pub nodes: (Brace<'a, List, NetLvalue<'a>>>,), } #[derive(Debug)] @@ -56,12 +49,7 @@ pub struct VariableLvalueIdentifier<'a> { #[derive(Debug)] pub struct VariableLvalueLvalue<'a> { - pub nodes: ( - Symbol<'a>, - VariableLvalue<'a>, - Vec<(Symbol<'a>, VariableLvalue<'a>)>, - Symbol<'a>, - ), + pub nodes: (Brace<'a, List, VariableLvalue<'a>>>,), } #[derive(Debug)] @@ -103,15 +91,10 @@ pub fn net_lvalue_pattern(s: Span) -> IResult { } pub fn net_lvalue_lvalue(s: Span) -> IResult { - let (s, a) = symbol("{")(s)?; - let (s, b) = net_lvalue(s)?; - let (s, c) = many0(pair(symbol(","), net_lvalue))(s)?; - let (s, d) = symbol("}")(s)?; + let (s, a) = brace2(list(symbol(","), net_lvalue))(s)?; Ok(( s, - NetLvalue::Lvalue(Box::new(NetLvalueLvalue { - nodes: (a, b, c, d), - })), + NetLvalue::Lvalue(Box::new(NetLvalueLvalue { nodes: (a,) })), )) } @@ -146,15 +129,10 @@ pub fn variable_lvalue_pattern(s: Span) -> IResult { } pub fn variable_lvalue_lvalue(s: Span) -> IResult { - let (s, a) = symbol("{")(s)?; - let (s, b) = variable_lvalue(s)?; - let (s, c) = many0(pair(symbol(","), variable_lvalue))(s)?; - let (s, d) = symbol("}")(s)?; + let (s, a) = brace2(list(symbol(","), variable_lvalue))(s)?; Ok(( s, - VariableLvalue::Lvalue(Box::new(VariableLvalueLvalue { - nodes: (a, b, c, d), - })), + VariableLvalue::Lvalue(Box::new(VariableLvalueLvalue { nodes: (a,) })), )) } diff --git a/src/parser/expressions/expressions.rs b/src/parser/expressions/expressions.rs index 77b30f4..38789eb 100644 --- a/src/parser/expressions/expressions.rs +++ b/src/parser/expressions/expressions.rs @@ -153,7 +153,7 @@ pub struct ExpressionUnary<'a> { #[derive(Debug)] pub struct ExpressionOperatorAssignment<'a> { - pub nodes: (Symbol<'a>, OperatorAssignment<'a>, Symbol<'a>), + pub nodes: (Paren<'a, OperatorAssignment<'a>>,), } #[derive(Debug)] @@ -173,13 +173,7 @@ pub struct TaggedUnionExpression<'a> { #[derive(Debug)] pub struct InsideExpression<'a> { - pub nodes: ( - Expression<'a>, - Symbol<'a>, - Symbol<'a>, - Vec>, - Symbol<'a>, - ), + pub nodes: (Expression<'a>, Symbol<'a>, Brace<'a, Vec>>), } #[derive(Debug)] @@ -190,13 +184,7 @@ pub enum ValueRange<'a> { #[derive(Debug)] pub struct ValueRangeBinary<'a> { - pub nodes: ( - Symbol<'a>, - Expression<'a>, - Symbol<'a>, - Expression<'a>, - Symbol<'a>, - ), + pub nodes: (Bracket<'a, (Expression<'a>, Symbol<'a>, Expression<'a>)>,), } #[derive(Debug)] @@ -494,7 +482,7 @@ pub fn expression_operator_assignment(s: Span) -> IResult { let (s, a) = paren2(operator_assignment)(s)?; Ok(( s, - Expression::OperatorAssignment(Box::new(ExpressionOperatorAssignment { nodes: a })), + Expression::OperatorAssignment(Box::new(ExpressionOperatorAssignment { nodes: (a,) })), )) } @@ -521,15 +509,8 @@ pub fn tagged_union_expression(s: Span) -> IResult pub fn inside_expression(s: Span) -> IResult { let (s, a) = expression(s)?; let (s, b) = symbol("inside")(s)?; - let (s, c) = symbol("{")(s)?; - let (s, d) = brace(open_range_list)(s)?; - let (s, e) = symbol("}")(s)?; - Ok(( - s, - InsideExpression { - nodes: (a, b, c, d, e), - }, - )) + let (s, c) = brace2(open_range_list)(s)?; + Ok((s, InsideExpression { nodes: (a, b, c) })) } pub fn value_range(s: Span) -> IResult { @@ -540,17 +521,8 @@ pub fn value_range(s: Span) -> IResult { } pub fn value_range_binary(s: Span) -> IResult { - let (s, a) = symbol("[")(s)?; - let (s, b) = expression(s)?; - let (s, c) = symbol(":")(s)?; - let (s, d) = expression(s)?; - let (s, e) = symbol("]")(s)?; - Ok(( - s, - ValueRange::Binary(ValueRangeBinary { - nodes: (a, b, c, d, e), - }), - )) + let (s, a) = bracket2(triple(expression, symbol(":"), expression))(s)?; + Ok((s, ValueRange::Binary(ValueRangeBinary { nodes: (a,) }))) } pub fn mintypmax_expression(s: Span) -> IResult { diff --git a/src/parser/expressions/primaries.rs b/src/parser/expressions/primaries.rs index 00666bc..9e888fb 100644 --- a/src/parser/expressions/primaries.rs +++ b/src/parser/expressions/primaries.rs @@ -34,7 +34,7 @@ pub struct ConstantPrimaryPsParameter<'a> { pub struct ConstantPrimarySpecparam<'a> { pub nodes: ( SpecparamIdentifier<'a>, - Option<(Symbol<'a>, ConstantRangeExpression<'a>, Symbol<'a>)>, + Option>>, ), } @@ -52,7 +52,7 @@ pub struct ConstantPrimaryEnum<'a> { pub struct ConstantPrimaryConcatenation<'a> { pub nodes: ( ConstantConcatenation<'a>, - Option<(Symbol<'a>, ConstantRangeExpression<'a>, Symbol<'a>)>, + Option>>, ), } @@ -60,13 +60,13 @@ pub struct ConstantPrimaryConcatenation<'a> { pub struct ConstantPrimaryMultipleConcatenation<'a> { pub nodes: ( ConstantMultipleConcatenation<'a>, - Option<(Symbol<'a>, ConstantRangeExpression<'a>, Symbol<'a>)>, + Option>>, ), } #[derive(Debug)] pub struct ConstantPrimaryMintypmaxExpression<'a> { - pub nodes: (Symbol<'a>, ConstantMintypmaxExpression<'a>, Symbol<'a>), + pub nodes: (Paren<'a, ConstantMintypmaxExpression<'a>>,), } #[derive(Debug)] @@ -81,7 +81,7 @@ pub enum ModulePathPrimary<'a> { #[derive(Debug)] pub struct ModulePathPrimaryMintypmax<'a> { - pub nodes: (Symbol<'a>, ModulePathMintypmaxExpression<'a>, Symbol<'a>), + pub nodes: (Paren<'a, ModulePathMintypmaxExpression<'a>>,), } #[derive(Debug)] @@ -114,23 +114,20 @@ pub struct PrimaryHierarchical<'a> { #[derive(Debug)] pub struct PrimaryConcatenation<'a> { - pub nodes: ( - Concatenation<'a>, - Option<(Symbol<'a>, RangeExpression<'a>, Symbol<'a>)>, - ), + pub nodes: (Concatenation<'a>, Option>>), } #[derive(Debug)] pub struct PrimaryMultipleConcatenation<'a> { pub nodes: ( MultipleConcatenation<'a>, - Option<(Symbol<'a>, RangeExpression<'a>, Symbol<'a>)>, + Option>>, ), } #[derive(Debug)] pub struct PrimaryMintypmaxExpression<'a> { - pub nodes: (Symbol<'a>, MintypmaxExpression<'a>, Symbol<'a>), + pub nodes: (Paren<'a, MintypmaxExpression<'a>>,), } #[derive(Debug)] @@ -201,7 +198,7 @@ pub enum ImplicitClassHandle<'a> { #[derive(Debug)] pub struct BitSelect<'a> { - nodes: (Vec<(Symbol<'a>, Expression<'a>, Symbol<'a>)>,), + nodes: (Vec>>,), } #[derive(Debug)] @@ -213,7 +210,7 @@ pub struct Select<'a> { MemberIdentifier<'a>, )>, BitSelect<'a>, - Option<(Symbol<'a>, PartSelectRange<'a>, Symbol<'a>)>, + Option>>, ), } @@ -231,7 +228,7 @@ pub struct NonrangeSelect<'a> { #[derive(Debug)] pub struct ConstantBitSelect<'a> { - nodes: (Vec<(Symbol<'a>, ConstantExpression<'a>, Symbol<'a>)>,), + nodes: (Vec>>,), } #[derive(Debug)] @@ -243,7 +240,7 @@ pub struct ConstantSelect<'a> { MemberIdentifier<'a>, )>, ConstantBitSelect<'a>, - Option<(Symbol<'a>, ConstantPartSelectRange<'a>, Symbol<'a>)>, + Option>>, ), } @@ -252,9 +249,7 @@ pub struct ConstantCast<'a> { pub nodes: ( CastingType<'a>, Symbol<'a>, - Symbol<'a>, - ConstantExpression<'a>, - Symbol<'a>, + Paren<'a, ConstantExpression<'a>>, ), } @@ -265,13 +260,7 @@ pub struct ConstantLetExpression<'a> { #[derive(Debug)] pub struct Cast<'a> { - pub nodes: ( - CastingType<'a>, - Symbol<'a>, - Symbol<'a>, - Expression<'a>, - Symbol<'a>, - ), + pub nodes: (CastingType<'a>, Symbol<'a>, Paren<'a, Expression<'a>>), } // ----------------------------------------------------------------------------- @@ -362,7 +351,7 @@ pub fn constant_primary_mintypmax_expression(s: Span) -> IResult IResult IResult { let (s, a) = paren2(mintypmax_expression)(s)?; Ok(( s, - Primary::MintypmaxExpression(PrimaryMintypmaxExpression { nodes: a }), + Primary::MintypmaxExpression(PrimaryMintypmaxExpression { nodes: (a,) }), )) } @@ -583,13 +572,8 @@ pub fn constant_select(s: Span) -> IResult { pub fn constant_cast(s: Span) -> IResult { let (s, a) = casting_type(s)?; let (s, b) = symbol("'")(s)?; - let (s, (c, d, e)) = paren2(constant_expression)(s)?; - Ok(( - s, - ConstantCast { - nodes: (a, b, c, d, e), - }, - )) + let (s, c) = paren2(constant_expression)(s)?; + Ok((s, ConstantCast { nodes: (a, b, c) })) } pub fn constant_let_expression(s: Span) -> IResult { @@ -600,13 +584,8 @@ pub fn constant_let_expression(s: Span) -> IResult pub fn cast(s: Span) -> IResult { let (s, a) = casting_type(s)?; let (s, b) = symbol("'")(s)?; - let (s, (c, d, e)) = paren2(expression)(s)?; - Ok(( - s, - Cast { - nodes: (a, b, c, d, e), - }, - )) + let (s, c) = paren2(expression)(s)?; + Ok((s, Cast { nodes: (a, b, c) })) } // ----------------------------------------------------------------------------- diff --git a/src/parser/expressions/subroutine_calls.rs b/src/parser/expressions/subroutine_calls.rs index 242e424..6cee508 100644 --- a/src/parser/expressions/subroutine_calls.rs +++ b/src/parser/expressions/subroutine_calls.rs @@ -17,7 +17,7 @@ pub struct TfCall<'a> { pub nodes: ( PsOrHierarchicalTfIdentifier<'a>, Vec>, - Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>, + Option>>, ), } @@ -32,7 +32,7 @@ pub enum SystemTfCall<'a> { pub struct SystemTfCallArgOptional<'a> { pub nodes: ( SystemTfIdentifier<'a>, - Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>, + Option>>, ), } @@ -40,10 +40,7 @@ pub struct SystemTfCallArgOptional<'a> { pub struct SystemTfCallArgDataType<'a> { pub nodes: ( SystemTfIdentifier<'a>, - Symbol<'a>, - DataType<'a>, - Option<(Symbol<'a>, Expression<'a>)>, - Symbol<'a>, + Paren<'a, (DataType<'a>, Option<(Symbol<'a>, Expression<'a>)>)>, ), } @@ -51,11 +48,13 @@ pub struct SystemTfCallArgDataType<'a> { pub struct SystemTfCallArgExpression<'a> { pub nodes: ( SystemTfIdentifier<'a>, - Symbol<'a>, - Expression<'a>, - Vec<(Symbol<'a>, Option>)>, - Option<(Symbol<'a>, Option>)>, - Symbol<'a>, + Paren< + 'a, + ( + List, Option>>, + Option<(Symbol<'a>, Option>)>, + ), + >, ), } @@ -86,15 +85,12 @@ pub enum ListOfArguments<'a> { #[derive(Debug)] pub struct ListOfArgumentsOrdered<'a> { pub nodes: ( - Option>, - Vec<(Symbol<'a>, Option>)>, + List, Option>>, Vec<( Symbol<'a>, Symbol<'a>, Identifier<'a>, - Symbol<'a>, - Option>, - Symbol<'a>, + Paren<'a, Option>>, )>, ), } @@ -104,16 +100,12 @@ pub struct ListOfArgumentsNamed<'a> { pub nodes: ( Symbol<'a>, Identifier<'a>, - Symbol<'a>, - Option>, - Symbol<'a>, + Paren<'a, Option>>, Vec<( Symbol<'a>, Symbol<'a>, Identifier<'a>, - Symbol<'a>, - Option>, - Symbol<'a>, + Paren<'a, Option>>, )>, ), } @@ -134,7 +126,7 @@ pub struct MethodCallBodyUser<'a> { pub nodes: ( MethodIdentifier<'a>, Vec>, - Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>, + Option>>, ), } @@ -149,8 +141,8 @@ pub struct ArrayManipulationCall<'a> { pub nodes: ( ArrayMethodName<'a>, Vec>, - Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>, - Option<(Symbol<'a>, (Symbol<'a>, Expression<'a>, Symbol<'a>))>, + Option>>, + Option<(Symbol<'a>, Paren<'a, Expression<'a>>)>, ), } @@ -159,14 +151,10 @@ pub struct RandomizeCall<'a> { pub nodes: ( Symbol<'a>, Vec>, + Option>>>, Option<( Symbol<'a>, - Option>, - Symbol<'a>, - )>, - Option<( - Symbol<'a>, - Option<(Symbol<'a>, Option>, Symbol<'a>)>, + Option>>>, ConstraintBlock<'a>, )>, ), @@ -226,30 +214,22 @@ pub fn system_tf_call_arg_optional(s: Span) -> IResult { 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)?; + let (s, b) = paren2(pair(data_type, opt(pair(symbol(","), expression))))(s)?; Ok(( s, - SystemTfCall::ArgDataType(SystemTfCallArgDataType { - nodes: (a, b, c, d, e), - }), + SystemTfCall::ArgDataType(SystemTfCallArgDataType { nodes: (a, b) }), )) } 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)?; + let (s, b) = paren2(pair( + list(symbol(","), opt(expression)), + opt(pair(symbol(","), opt(clocking_event))), + ))(s)?; Ok(( s, - SystemTfCall::ArgExpression(SystemTfCallArgExpression { - nodes: (a, b, c, d, e, f), - }), + SystemTfCall::ArgExpression(SystemTfCallArgExpression { nodes: (a, b) }), )) } @@ -282,40 +262,33 @@ pub fn list_of_arguments(s: Span) -> IResult { } 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(( + let (s, a) = list(symbol(","), opt(expression))(s)?; + let (s, b) = many0(tuple(( symbol(","), symbol("."), identifier, - symbol("("), - opt(expression), - symbol(")"), + paren2(opt(expression)), )))(s)?; Ok(( s, - ListOfArguments::Ordered(ListOfArgumentsOrdered { nodes: (a, b, c) }), + ListOfArguments::Ordered(ListOfArgumentsOrdered { nodes: (a, b) }), )) } 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(( + let (s, c) = paren2(opt(expression))(s)?; + let (s, d) = many0(tuple(( symbol(","), symbol("."), identifier, - symbol("("), - opt(expression), - symbol(")"), + paren2(opt(expression)), )))(s)?; Ok(( s, ListOfArguments::Named(ListOfArgumentsNamed { - nodes: (a, b, c, d, e, f), + nodes: (a, b, c, d), }), )) } diff --git a/src/parser/general/attributes.rs b/src/parser/general/attributes.rs index 84a65c3..a9e65cf 100644 --- a/src/parser/general/attributes.rs +++ b/src/parser/general/attributes.rs @@ -1,6 +1,5 @@ use crate::parser::*; use nom::combinator::*; -use nom::multi::*; use nom::sequence::*; use nom::IResult; @@ -8,12 +7,7 @@ use nom::IResult; #[derive(Debug)] pub struct AttributeInstance<'a> { - pub nodes: ( - Symbol<'a>, - AttrSpec<'a>, - Vec<(Symbol<'a>, AttrSpec<'a>)>, - Symbol<'a>, - ), + pub nodes: (Symbol<'a>, List, AttrSpec<'a>>, Symbol<'a>), } #[derive(Debug)] @@ -25,15 +19,9 @@ pub struct AttrSpec<'a> { pub fn attribute_instance(s: Span) -> IResult { let (s, a) = symbol("(*")(s)?; - let (s, b) = attr_spec(s)?; - let (s, c) = many0(pair(symbol(","), attr_spec))(s)?; - let (s, d) = symbol("*)")(s)?; - Ok(( - s, - AttributeInstance { - nodes: (a, b, c, d), - }, - )) + let (s, b) = list(symbol(","), attr_spec)(s)?; + let (s, c) = symbol("*)")(s)?; + Ok((s, AttributeInstance { nodes: (a, b, c) })) } pub fn attr_spec(s: Span) -> IResult { diff --git a/src/parser/instantiations/checker_instantiation.rs b/src/parser/instantiations/checker_instantiation.rs index acfce25..86520f4 100644 --- a/src/parser/instantiations/checker_instantiation.rs +++ b/src/parser/instantiations/checker_instantiation.rs @@ -2,7 +2,6 @@ use crate::parser::*; use nom::branch::*; use nom::combinator::*; use nom::multi::*; -use nom::sequence::*; use nom::IResult; // ----------------------------------------------------------------------------- @@ -12,9 +11,7 @@ pub struct CheckerInstantiation<'a> { pub nodes: ( PsCheckerIdentifier<'a>, NameOfInstance<'a>, - Symbol<'a>, - Option>, - Symbol<'a>, + Paren<'a, Option>>, Symbol<'a>, ), } @@ -27,18 +24,12 @@ pub enum ListOfCheckerPortConnections<'a> { #[derive(Debug)] pub struct ListOfCheckerPortConnectionsOrdered<'a> { - pub nodes: ( - OrderedCheckerPortConnection<'a>, - Vec<(Symbol<'a>, OrderedCheckerPortConnection<'a>)>, - ), + pub nodes: (List, OrderedCheckerPortConnection<'a>>,), } #[derive(Debug)] pub struct ListOfCheckerPortConnectionsNamed<'a> { - pub nodes: ( - NamedCheckerPortConnection<'a>, - Vec<(Symbol<'a>, NamedCheckerPortConnection<'a>)>, - ), + pub nodes: (List, NamedCheckerPortConnection<'a>>,), } #[derive(Debug)] @@ -58,7 +49,7 @@ pub struct NamedCheckerPortConnectionIdentifier<'a> { Vec>, Symbol<'a>, FormalPortIdentifier<'a>, - Option<(Symbol<'a>, Option>, Symbol<'a>)>, + Option>>>, ), } @@ -72,12 +63,12 @@ pub struct NamedCheckerPortConnectionAsterisk<'a> { pub fn checker_instantiation(s: Span) -> IResult { let (s, a) = ps_checker_identifier(s)?; let (s, b) = name_of_instance(s)?; - let (s, (c, d, e)) = paren2(opt(list_of_checker_port_connections))(s)?; - let (s, f) = symbol(";")(s)?; + let (s, c) = paren2(opt(list_of_checker_port_connections))(s)?; + let (s, d) = symbol(";")(s)?; Ok(( s, CheckerInstantiation { - nodes: (a, b, c, d, e, f), + nodes: (a, b, c, d), }, )) } @@ -92,24 +83,20 @@ pub fn list_of_checker_port_connections(s: Span) -> IResult IResult { - let (s, a) = ordered_checker_port_connection(s)?; - let (s, b) = many0(pair(symbol(","), ordered_checker_port_connection))(s)?; + let (s, a) = list(symbol(","), ordered_checker_port_connection)(s)?; Ok(( s, - ListOfCheckerPortConnections::Ordered(ListOfCheckerPortConnectionsOrdered { - nodes: (a, b), - }), + ListOfCheckerPortConnections::Ordered(ListOfCheckerPortConnectionsOrdered { nodes: (a,) }), )) } pub fn list_of_checker_port_connections_named( s: Span, ) -> IResult { - let (s, a) = named_checker_port_connection(s)?; - let (s, b) = many0(pair(symbol(","), named_checker_port_connection))(s)?; + let (s, a) = list(symbol(","), named_checker_port_connection)(s)?; Ok(( s, - ListOfCheckerPortConnections::Named(ListOfCheckerPortConnectionsNamed { nodes: (a, b) }), + ListOfCheckerPortConnections::Named(ListOfCheckerPortConnectionsNamed { nodes: (a,) }), )) } diff --git a/src/parser/instantiations/interface_instantiation.rs b/src/parser/instantiations/interface_instantiation.rs index cc995db..f69d2d2 100644 --- a/src/parser/instantiations/interface_instantiation.rs +++ b/src/parser/instantiations/interface_instantiation.rs @@ -1,7 +1,5 @@ use crate::parser::*; use nom::combinator::*; -use nom::multi::*; -use nom::sequence::*; use nom::IResult; // ----------------------------------------------------------------------------- @@ -11,8 +9,7 @@ pub struct InterfaceInstantiation<'a> { pub nodes: ( InterfaceIdentifier<'a>, Option>, - HierarchicalInstance<'a>, - Vec<(Symbol<'a>, HierarchicalInstance<'a>)>, + List, HierarchicalInstance<'a>>, Symbol<'a>, ), } @@ -22,13 +19,12 @@ pub struct InterfaceInstantiation<'a> { pub fn interface_instantiation(s: Span) -> IResult { let (s, a) = interface_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)?; + let (s, c) = list(symbol(","), hierarchical_instance)(s)?; + let (s, d) = symbol(";")(s)?; Ok(( s, InterfaceInstantiation { - nodes: (a, b, c, d, e), + nodes: (a, b, c, d), }, )) } diff --git a/src/parser/instantiations/module_instantiation.rs b/src/parser/instantiations/module_instantiation.rs index cc20ca6..bd9cb46 100644 --- a/src/parser/instantiations/module_instantiation.rs +++ b/src/parser/instantiations/module_instantiation.rs @@ -2,7 +2,6 @@ use crate::parser::*; use nom::branch::*; use nom::combinator::*; use nom::multi::*; -use nom::sequence::*; use nom::IResult; // ----------------------------------------------------------------------------- @@ -12,8 +11,7 @@ pub struct ModuleInstantiation<'a> { pub nodes: ( ModuleIdentifier<'a>, Option>, - HierarchicalInstance<'a>, - Vec<(Symbol<'a>, HierarchicalInstance<'a>)>, + List, HierarchicalInstance<'a>>, Symbol<'a>, ), } @@ -22,11 +20,7 @@ pub struct ModuleInstantiation<'a> { pub struct ParameterValueAssignment<'a> { pub nodes: ( Symbol<'a>, - ( - Symbol<'a>, - Option>, - Symbol<'a>, - ), + Paren<'a, Option>>, ), } @@ -38,18 +32,12 @@ pub enum ListOfParameterAssignments<'a> { #[derive(Debug)] pub struct ListOfParameterAssignmentsOrdered<'a> { - pub nodes: ( - OrderedParameterAssignment<'a>, - Vec<(Symbol<'a>, OrderedParameterAssignment<'a>)>, - ), + pub nodes: (List, OrderedParameterAssignment<'a>>,), } #[derive(Debug)] pub struct ListOfParameterAssignmentsNamed<'a> { - pub nodes: ( - NamedParameterAssignment<'a>, - Vec<(Symbol<'a>, NamedParameterAssignment<'a>)>, - ), + pub nodes: (List, NamedParameterAssignment<'a>>,), } #[derive(Debug)] @@ -62,7 +50,7 @@ pub struct NamedParameterAssignment<'a> { pub nodes: ( Symbol<'a>, ParameterIdentifier<'a>, - (Symbol<'a>, Option>, Symbol<'a>), + Paren<'a, Option>>, ), } @@ -70,7 +58,7 @@ pub struct NamedParameterAssignment<'a> { pub struct HierarchicalInstance<'a> { pub nodes: ( NameOfInstance<'a>, - (Symbol<'a>, Option>, Symbol<'a>), + Paren<'a, Option>>, ), } @@ -87,18 +75,12 @@ pub enum ListOfPortConnections<'a> { #[derive(Debug)] pub struct ListOfPortConnectionsOrdered<'a> { - pub nodes: ( - OrderedPortConnection<'a>, - Vec<(Symbol<'a>, OrderedPortConnection<'a>)>, - ), + pub nodes: (List, OrderedPortConnection<'a>>,), } #[derive(Debug)] pub struct ListOfPortConnectionsNamed<'a> { - pub nodes: ( - NamedPortConnection<'a>, - Vec<(Symbol<'a>, NamedPortConnection<'a>)>, - ), + pub nodes: (List, NamedPortConnection<'a>>,), } #[derive(Debug)] @@ -118,7 +100,7 @@ pub struct NamedPortConnectionIdentifier<'a> { Vec>, Symbol<'a>, PortIdentifier<'a>, - Option<(Symbol<'a>, Option>, Symbol<'a>)>, + Option>>>, ), } @@ -132,13 +114,12 @@ pub struct NamedPortConnectionAsterisk<'a> { pub fn module_instantiation(s: Span) -> IResult { 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)?; + let (s, c) = list(symbol(","), hierarchical_instance)(s)?; + let (s, d) = symbol(";")(s)?; Ok(( s, ModuleInstantiation { - nodes: (a, b, c, d, e), + nodes: (a, b, c, d), }, )) } @@ -157,20 +138,18 @@ pub fn list_of_parameter_assignments(s: Span) -> IResult IResult { - let (s, a) = ordered_parameter_assignment(s)?; - let (s, b) = many0(pair(symbol(","), ordered_parameter_assignment))(s)?; + let (s, a) = list(symbol(","), ordered_parameter_assignment)(s)?; Ok(( s, - ListOfParameterAssignments::Ordered(ListOfParameterAssignmentsOrdered { nodes: (a, b) }), + ListOfParameterAssignments::Ordered(ListOfParameterAssignmentsOrdered { nodes: (a,) }), )) } 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)?; + let (s, a) = list(symbol(","), named_parameter_assignment)(s)?; Ok(( s, - ListOfParameterAssignments::Named(ListOfParameterAssignmentsNamed { nodes: (a, b) }), + ListOfParameterAssignments::Named(ListOfParameterAssignmentsNamed { nodes: (a,) }), )) } @@ -206,20 +185,18 @@ pub fn list_of_port_connections(s: Span) -> IResult } 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)?; + let (s, a) = list(symbol(","), ordered_port_connection)(s)?; Ok(( s, - ListOfPortConnections::Ordered(ListOfPortConnectionsOrdered { nodes: (a, b) }), + ListOfPortConnections::Ordered(ListOfPortConnectionsOrdered { nodes: (a,) }), )) } 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)?; + let (s, a) = list(symbol(","), named_port_connection)(s)?; Ok(( s, - ListOfPortConnections::Named(ListOfPortConnectionsNamed { nodes: (a, b) }), + ListOfPortConnections::Named(ListOfPortConnectionsNamed { nodes: (a,) }), )) } diff --git a/src/parser/instantiations/program_instantiation.rs b/src/parser/instantiations/program_instantiation.rs index fd1e1cd..22c4289 100644 --- a/src/parser/instantiations/program_instantiation.rs +++ b/src/parser/instantiations/program_instantiation.rs @@ -1,7 +1,5 @@ use crate::parser::*; use nom::combinator::*; -use nom::multi::*; -use nom::sequence::*; use nom::IResult; // ----------------------------------------------------------------------------- @@ -11,8 +9,7 @@ pub struct ProgramInstantiation<'a> { pub nodes: ( ProgramIdentifier<'a>, Option>, - HierarchicalInstance<'a>, - Vec<(Symbol<'a>, HierarchicalInstance<'a>)>, + List, HierarchicalInstance<'a>>, Symbol<'a>, ), } @@ -22,13 +19,12 @@ pub struct ProgramInstantiation<'a> { pub fn program_instantiation(s: Span) -> IResult { let (s, a) = program_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)?; + let (s, c) = list(symbol(","), hierarchical_instance)(s)?; + let (s, d) = symbol(";")(s)?; Ok(( s, ProgramInstantiation { - nodes: (a, b, c, d, e), + nodes: (a, b, c, d), }, )) } diff --git a/src/parser/source_text/library_source_text.rs b/src/parser/source_text/library_source_text.rs index f368c0b..f49c274 100644 --- a/src/parser/source_text/library_source_text.rs +++ b/src/parser/source_text/library_source_text.rs @@ -25,13 +25,8 @@ pub struct LibraryDeclaration<'a> { pub nodes: ( Symbol<'a>, LibraryIdentifier<'a>, - FilePathSpec<'a>, - Vec<(Symbol<'a>, FilePathSpec<'a>)>, - Option<( - Symbol<'a>, - FilePathSpec<'a>, - Vec<(Symbol<'a>, FilePathSpec<'a>)>, - )>, + List, FilePathSpec<'a>>, + Option<(Symbol<'a>, List, FilePathSpec<'a>>)>, Symbol<'a>, ), } @@ -71,18 +66,13 @@ pub fn library_description(s: Span) -> IResult { pub fn library_declaration(s: Span) -> IResult { let (s, a) = symbol("library")(s)?; let (s, b) = library_identifier(s)?; - let (s, c) = file_path_spec(s)?; - let (s, d) = many0(pair(symbol(","), file_path_spec))(s)?; - let (s, e) = opt(triple( - symbol("-incdir"), - file_path_spec, - many0(pair(symbol(","), file_path_spec)), - ))(s)?; - let (s, f) = symbol(";")(s)?; + let (s, c) = list(symbol(","), file_path_spec)(s)?; + let (s, d) = opt(pair(symbol("-incdir"), list(symbol(","), file_path_spec)))(s)?; + let (s, e) = symbol(";")(s)?; Ok(( s, LibraryDeclaration { - nodes: (a, b, c, d, e, f), + nodes: (a, b, c, d, e), }, )) } diff --git a/src/parser/source_text/system_verilog_source_text.rs b/src/parser/source_text/system_verilog_source_text.rs index a62c37c..c173806 100644 --- a/src/parser/source_text/system_verilog_source_text.rs +++ b/src/parser/source_text/system_verilog_source_text.rs @@ -100,9 +100,7 @@ pub struct ModuleDeclarationWildcard<'a> { ModuleKeyword<'a>, Option>, ModuleIdentifier<'a>, - Symbol<'a>, - Symbol<'a>, - Symbol<'a>, + Paren<'a, Symbol<'a>>, Symbol<'a>, Option>, Vec>, @@ -165,9 +163,7 @@ pub struct InterfaceDeclarationWildcard<'a> { Symbol<'a>, Option>, InterfaceIdentifier<'a>, - Symbol<'a>, - Symbol<'a>, - Symbol<'a>, + Paren<'a, Symbol<'a>>, Symbol<'a>, Option>, Vec>, @@ -251,9 +247,7 @@ pub struct ProgramDeclarationWildcard<'a> { Vec>, Symbol<'a>, ProgramIdentifier<'a>, - Symbol<'a>, - Symbol<'a>, - Symbol<'a>, + Paren<'a, Symbol<'a>>, Symbol<'a>, Option>, Vec>, @@ -305,7 +299,7 @@ pub struct CheckerDeclaration<'a> { pub nodes: ( Symbol<'a>, CheckerIdentifier<'a>, - Option<(Symbol<'a>, Option>, Symbol<'a>)>, + Option>>>, Symbol<'a>, Vec<(Vec>, CheckerOrGenerateItem<'a>)>, Symbol<'a>, @@ -324,13 +318,9 @@ pub struct ClassDeclaration<'a> { Option<( Symbol<'a>, ClassType<'a>, - Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>, - )>, - Option<( - Symbol<'a>, - InterfaceClassType<'a>, - Vec<(Symbol<'a>, InterfaceClassType<'a>)>, + Option>>, )>, + Option<(Symbol<'a>, List, InterfaceClassType<'a>>)>, Symbol<'a>, Vec>, Symbol<'a>, @@ -355,11 +345,7 @@ pub struct InterfaceClassDeclaration<'a> { Symbol<'a>, ClassIdentifier<'a>, Option>, - Option<( - Symbol<'a>, - InterfaceClassType<'a>, - Vec<(Symbol<'a>, InterfaceClassType<'a>)>, - )>, + Option<(Symbol<'a>, List, InterfaceClassType<'a>>)>, Symbol<'a>, Vec>, Symbol<'a>, @@ -566,18 +552,16 @@ pub fn module_declaration_wildcard(s: Span) -> IResult let (s, b) = module_keyword(s)?; let (s, c) = opt(lifetime)(s)?; let (s, d) = module_identifier(s)?; - let (s, e) = symbol("(")(s)?; - let (s, f) = symbol(".*")(s)?; - let (s, g) = symbol(")")(s)?; - let (s, h) = symbol(";")(s)?; - let (s, i) = opt(timeunits_declaration)(s)?; - let (s, j) = many0(module_item)(s)?; - let (s, k) = symbol("endmodule")(s)?; - let (s, l) = opt(pair(symbol(":"), module_identifier))(s)?; + let (s, e) = paren2(symbol(".*"))(s)?; + let (s, f) = symbol(";")(s)?; + let (s, g) = opt(timeunits_declaration)(s)?; + let (s, h) = many0(module_item)(s)?; + let (s, i) = symbol("endmodule")(s)?; + let (s, j) = opt(pair(symbol(":"), module_identifier))(s)?; Ok(( s, ModuleDeclaration::Wildcard(ModuleDeclarationWildcard { - nodes: (a, b, c, d, e, f, g, h, i, j, k, l), + nodes: (a, b, c, d, e, f, g, h, i, j), }), )) } @@ -650,18 +634,16 @@ pub fn interface_declaration_wildcard(s: Span) -> IResult IResult IResult { pub fn checker_declaration(s: Span) -> IResult { let (s, a) = symbol("checker")(s)?; let (s, b) = checker_identifier(s)?; - let (s, c) = opt(triple(symbol("("), opt(checker_port_list), symbol(")")))(s)?; + let (s, c) = opt(paren2(opt(checker_port_list)))(s)?; let (s, d) = symbol(";")(s)?; let (s, e) = many0(pair(many0(attribute_instance), checker_or_generate_item))(s)?; let (s, f) = symbol("endchecker")(s)?; @@ -853,12 +833,11 @@ pub fn class_declaration(s: Span) -> IResult { let (s, f) = opt(triple( symbol("extends"), class_type, - opt(triple(symbol("("), list_of_arguments, symbol(")"))), + opt(paren2(list_of_arguments)), ))(s)?; - let (s, g) = opt(triple( + let (s, g) = opt(pair( symbol("implements"), - interface_class_type, - many0(pair(symbol(","), interface_class_type)), + list(symbol(","), interface_class_type), ))(s)?; let (s, h) = symbol(";")(s)?; let (s, i) = many0(class_item)(s)?; @@ -883,10 +862,9 @@ pub fn interface_class_declaration(s: Span) -> IResult { Comment(Comment<'a>), } +#[derive(Debug)] +pub struct Paren<'a, T: 'a> { + pub nodes: (Symbol<'a>, T, Symbol<'a>), +} + +#[derive(Debug)] +pub struct Brace<'a, T: 'a> { + pub nodes: (Symbol<'a>, T, Symbol<'a>), +} + +#[derive(Debug)] +pub struct Bracket<'a, T: 'a> { + pub nodes: (Symbol<'a>, T, Symbol<'a>), +} + +#[derive(Debug)] +pub struct ApostropheBrace<'a, T: 'a> { + pub nodes: (Symbol<'a>, T, Symbol<'a>), +} + +#[derive(Debug)] +pub struct List { + pub nodes: (T, Vec<(U, T)>), +} + // ----------------------------------------------------------------------------- pub fn ws<'a, O, F>(f: F) -> impl Fn(Span<'a>) -> IResult, (O, Vec>)> @@ -37,7 +62,7 @@ pub fn symbol<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult, Symbol<' move |s: Span<'a>| map(ws(tag(t.clone())), |x| Symbol { nodes: x })(s) } -pub fn paren2<'a, O, F>(f: F) -> impl Fn(Span<'a>) -> IResult, (Symbol<'a>, O, Symbol<'a>)> +pub fn paren2<'a, O, F>(f: F) -> impl Fn(Span<'a>) -> IResult, Paren> where F: Fn(Span<'a>) -> IResult, O>, { @@ -45,13 +70,11 @@ where let (s, a) = symbol("(")(s)?; let (s, b) = f(s)?; let (s, c) = symbol(")")(s)?; - Ok((s, (a, b, c))) + Ok((s, Paren { nodes: (a, b, c) })) } } -pub fn bracket2<'a, O, F>( - f: F, -) -> impl Fn(Span<'a>) -> IResult, (Symbol<'a>, O, Symbol<'a>)> +pub fn bracket2<'a, O, F>(f: F) -> impl Fn(Span<'a>) -> IResult, Bracket> where F: Fn(Span<'a>) -> IResult, O>, { @@ -59,7 +82,55 @@ where let (s, a) = symbol("[")(s)?; let (s, b) = f(s)?; let (s, c) = symbol("]")(s)?; - Ok((s, (a, b, c))) + Ok((s, Bracket { nodes: (a, b, c) })) + } +} + +pub fn brace2<'a, O, F>(f: F) -> impl Fn(Span<'a>) -> IResult, Brace> +where + F: Fn(Span<'a>) -> IResult, O>, +{ + move |s: Span<'a>| { + let (s, a) = symbol("{")(s)?; + let (s, b) = f(s)?; + let (s, c) = symbol("}")(s)?; + Ok((s, Brace { nodes: (a, b, c) })) + } +} + +pub fn apostrophe_brace2<'a, O, F>( + f: F, +) -> impl Fn(Span<'a>) -> IResult, ApostropheBrace> +where + F: Fn(Span<'a>) -> IResult, O>, +{ + move |s: Span<'a>| { + let (s, a) = symbol("'{")(s)?; + let (s, b) = f(s)?; + let (s, c) = symbol("}")(s)?; + Ok((s, ApostropheBrace { nodes: (a, b, c) })) + } +} + +pub fn list<'a, O1, O2, F, G>(f: F, g: G) -> impl Fn(Span<'a>) -> IResult, List> +where + F: Fn(Span<'a>) -> IResult, O1>, + G: Fn(Span<'a>) -> IResult, O2>, +{ + move |s: Span<'a>| { + let (s, a) = g(s)?; + let mut s = s.clone(); + let mut ret = Vec::new(); + loop { + if let Ok((t, b)) = f(s) { + let (u, c) = g(t)?; + s = u; + ret.push((b, c)); + } else { + break; + } + } + Ok((s, List { nodes: (a, ret) })) } }