diff --git a/README.md b/README.md index db2c9c7..ac7f9a6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,78 @@ # sv-parser A parser library for System Verilog. + +## Implementation Status + +| Category | Module | Type | Parser | Test | +| --------------------------------- | ------------------------------------- | ---- | ------ | ---- | +| source_text | library_source_text | x | x | x | +| source_text | system_verilog_source_text | x | x | | +| source_text | module_parameters_and_ports | | | | +| source_text | module_items | | | | +| source_text | configuration_source_text | | | | +| source_text | interface_items | | | | +| source_text | program_items | | | | +| source_text | checker_items | | | | +| source_text | class_items | | | | +| source_text | constraints | | | | +| source_text | package_items | | | | +| declaration | module_parameter_declarations | | | | +| declaration | port_declarations | | | | +| declaration | type_declarations | | | | +| declaration | net_and_variable_types | | | | +| declaration | strengths | | | | +| declaration | delays | | | | +| declaration | declaration_lists | | | | +| declaration | declaration_assignments | | | | +| declaration | declaration_ranges | | | | +| declaration | function_declarations | | | | +| declaration | task_declarations | | | | +| declaration | block_item_declarations | | | | +| declaration | interface_declarations | | | | +| declaration | assertion_declarations | | | | +| declaration | covergroup_declarations | | | | +| declaration | let_declarations | | | | +| primitive_instance | primitive_instantiation_and_instances | | | | +| primitive_instance | primitive_strengths | | | | +| primitive_instance | primitive_terminals | | | | +| primitive_instance | primitive_gate_and_switch_types | | | | +| instantiations | module_instantiation | | | | +| instantiations | interface_instantiation | x | x | | +| instantiations | program_instantiation | x | x | | +| instantiations | checker_instantiation | x | x | | +| instantiations | generated_instantiation | | | | +| udp_declaration_and_instantiation | udp_declaration | | | | +| udp_declaration_and_instantiation | udp_ports | | | | +| udp_declaration_and_instantiation | udp_body | | | | +| udp_declaration_and_instantiation | udp_instantiation | | | | +| behavioral_statements | continuous_assignment_and_net_alias | | | | +| behavioral_statements | procedural_blocks_and_assignments | | | | +| behavioral_statements | parallel_and_sequential_blocks | | | | +| behavioral_statements | statements | | | | +| behavioral_statements | timing_control_statements | | | | +| behavioral_statements | conditional_statements | | | | +| behavioral_statements | case_statements | | | | +| behavioral_statements | patterns | | | | +| behavioral_statements | looping_statements | | | | +| behavioral_statements | subroutine_call_statements | | | | +| behavioral_statements | assertion_statements | | | | +| behavioral_statements | clocking_block | | | | +| behavioral_statements | randsequence | | | | +| specify_section | specify_block_declaration | | | | +| specify_section | specify_path_declarations | | | | +| specify_section | specify_block_terminals | | | | +| specify_section | specify_path_delays | | | | +| 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 | expressions | x | x | | +| expressions | primaries | x | x | | +| expressions | expression_leftside_values | x | x | | +| expressions | operators | x | x | x | +| expressions | numbers | x | x | x | +| expressions | strings | x | x | x | +| general | attributes | x | x | x | +| general | comments | x | x | x | +| general | identifiers | x | x | x | diff --git a/src/parser/behavioral_statements/looping_statements.rs b/src/parser/behavioral_statements/looping_statements.rs index 53b72cc..20c5e70 100644 --- a/src/parser/behavioral_statements/looping_statements.rs +++ b/src/parser/behavioral_statements/looping_statements.rs @@ -76,9 +76,9 @@ pub struct Var {} #[derive(Debug)] pub enum ForStepAssignment<'a> { - Operator(OperatorAssignment<'a>), - IncOrDec(IncOrDecExpression<'a>), - Subroutine(SubroutineCall<'a>), + OperatorAssignment(OperatorAssignment<'a>), + IncOrDecExpression(IncOrDecExpression<'a>), + FunctionSubroutineCall(FunctionSubroutineCall<'a>), } #[derive(Debug)] @@ -212,10 +212,14 @@ pub fn for_step(s: Span) -> IResult> { pub fn for_step_assignment(s: Span) -> IResult { alt(( - map(operator_assignment, |x| ForStepAssignment::Operator(x)), - map(inc_or_dec_expression, |x| ForStepAssignment::IncOrDec(x)), + map(operator_assignment, |x| { + ForStepAssignment::OperatorAssignment(x) + }), + map(inc_or_dec_expression, |x| { + ForStepAssignment::IncOrDecExpression(x) + }), map(function_subroutine_call, |x| { - ForStepAssignment::Subroutine(x) + ForStepAssignment::FunctionSubroutineCall(x) }), ))(s) } diff --git a/src/parser/behavioral_statements/patterns.rs b/src/parser/behavioral_statements/patterns.rs index 9bca1a7..1b93b10 100644 --- a/src/parser/behavioral_statements/patterns.rs +++ b/src/parser/behavioral_statements/patterns.rs @@ -59,6 +59,11 @@ pub enum AssignmentPatternExpressionType<'a> { TypeReference(TypeReference<'a>), } +#[derive(Debug)] +pub struct ConstantAssignmentPatternExpression<'a> { + pub nodes: (AssignmentPatternExpression<'a>,), +} + #[derive(Debug)] pub struct AssignmentPatternNetLvalue<'a> { pub nodes: (Vec>,), @@ -178,8 +183,9 @@ pub fn assignment_pattern_expression_type( pub fn constant_assignment_pattern_expression( s: Span, -) -> IResult { - assignment_pattern_expression(s) +) -> IResult { + let (s, a) = assignment_pattern_expression(s)?; + Ok((s, ConstantAssignmentPatternExpression { nodes: (a,) })) } pub fn assignment_pattern_net_lvalue(s: Span) -> IResult { diff --git a/src/parser/behavioral_statements/subroutine_call_statements.rs b/src/parser/behavioral_statements/subroutine_call_statements.rs index d4ceea3..e051dfe 100644 --- a/src/parser/behavioral_statements/subroutine_call_statements.rs +++ b/src/parser/behavioral_statements/subroutine_call_statements.rs @@ -9,7 +9,7 @@ use nom::IResult; #[derive(Debug)] pub enum SubroutineCallStatement<'a> { SubroutineCall(SubroutineCall<'a>), - FunctionSubroutineCall(SubroutineCall<'a>), + FunctionSubroutineCall(FunctionSubroutineCall<'a>), } // ----------------------------------------------------------------------------- @@ -21,7 +21,7 @@ pub fn subroutine_call_statement(s: Span) -> IResult { #[derive(Debug)] pub struct FunctionDeclaration<'a> { - pub nodes: (Option, FunctionBodyDeclaration<'a>), + pub nodes: (Option>, FunctionBodyDeclaration<'a>), } #[derive(Debug)] diff --git a/src/parser/declarations/net_and_variable_types.rs b/src/parser/declarations/net_and_variable_types.rs index ae12a9f..165b721 100644 --- a/src/parser/declarations/net_and_variable_types.rs +++ b/src/parser/declarations/net_and_variable_types.rs @@ -287,6 +287,10 @@ pub fn class_scope(s: Span) -> IResult { Err(Err::Error(make_error(s, ErrorKind::Fix))) } +pub fn class_type(s: Span) -> IResult { + Err(Err::Error(make_error(s, ErrorKind::Fix))) +} + pub fn integer_type(s: Span) -> IResult { Err(Err::Error(make_error(s, ErrorKind::Fix))) } diff --git a/src/parser/declarations/task_declarations.rs b/src/parser/declarations/task_declarations.rs index 9f3bd2c..db66f0c 100644 --- a/src/parser/declarations/task_declarations.rs +++ b/src/parser/declarations/task_declarations.rs @@ -8,7 +8,7 @@ use nom::{Err, IResult}; #[derive(Debug)] pub struct TaskDeclaration<'a> { - pub nodes: (Option, TaskBodyDeclaration<'a>), + pub nodes: (Option>, TaskBodyDeclaration<'a>), } #[derive(Debug)] diff --git a/src/parser/declarations/type_declarations.rs b/src/parser/declarations/type_declarations.rs index 2357ef0..02e1f5a 100644 --- a/src/parser/declarations/type_declarations.rs +++ b/src/parser/declarations/type_declarations.rs @@ -20,7 +20,7 @@ pub struct DataDeclarationVariable<'a> { pub nodes: ( Option, Option, - Option, + Option>, DataTypeOrImplicit<'a>, ListOfVariableDeclAssignments<'a>, ), @@ -176,9 +176,9 @@ pub struct NetTypeDeclarationNetType<'a> { } #[derive(Debug)] -pub enum Lifetime { - Static, - Automatic, +pub enum Lifetime<'a> { + Static(Symbol<'a>), + Automatic(Symbol<'a>), } // ----------------------------------------------------------------------------- @@ -429,7 +429,7 @@ pub fn net_type_declaration_net_type(s: Span) -> IResult IResult { alt(( - map(symbol("static"), |_| Lifetime::Static), - map(symbol("automatic"), |_| Lifetime::Automatic), + map(symbol("static"), |x| Lifetime::Static(x)), + map(symbol("automatic"), |x| Lifetime::Automatic(x)), ))(s) } diff --git a/src/parser/expressions/concatenations.rs b/src/parser/expressions/concatenations.rs index 8b80a73..a19f46e 100644 --- a/src/parser/expressions/concatenations.rs +++ b/src/parser/expressions/concatenations.rs @@ -81,6 +81,11 @@ pub struct ArrayRangeOperator<'a> { pub nodes: (Symbol<'a>,), } +#[derive(Debug)] +pub struct EmptyUnpackedArrayConcatenation<'a> { + pub nodes: (Symbol<'a>, Symbol<'a>), +} + // ----------------------------------------------------------------------------- pub fn concatenation(s: Span) -> IResult { @@ -177,10 +182,12 @@ pub fn array_range_operator(s: Span) -> IResult { ))(s) } -pub fn empty_unpacked_array_concatenation(s: Span) -> IResult { - let (s, _) = symbol("{")(s)?; - let (s, _) = symbol("}")(s)?; - Ok((s, ())) +pub fn empty_unpacked_array_concatenation( + s: Span, +) -> IResult { + let (s, a) = symbol("{")(s)?; + let (s, b) = symbol("}")(s)?; + Ok((s, EmptyUnpackedArrayConcatenation { nodes: (a, b) })) } // ----------------------------------------------------------------------------- diff --git a/src/parser/expressions/expression_leftside_values.rs b/src/parser/expressions/expression_leftside_values.rs index 8ceb1f6..70ad807 100644 --- a/src/parser/expressions/expression_leftside_values.rs +++ b/src/parser/expressions/expression_leftside_values.rs @@ -9,9 +9,9 @@ use nom::IResult; #[derive(Debug)] pub enum NetLvalue<'a> { - Identifier(Box>), - Lvalue(Box>>), - Pattern(Box>), + Identifier(NetLvalueIdentifier<'a>), + Lvalue(Box>), + Pattern(NetLvaluePattern<'a>), } #[derive(Debug)] @@ -19,6 +19,16 @@ pub struct NetLvalueIdentifier<'a> { pub nodes: (PsOrHierarchicalNetIdentifier<'a>, ConstantSelect<'a>), } +#[derive(Debug)] +pub struct NetLvalueLvalue<'a> { + pub nodes: ( + Symbol<'a>, + NetLvalue<'a>, + Vec<(Symbol<'a>, NetLvalue<'a>)>, + Symbol<'a>, + ), +} + #[derive(Debug)] pub struct NetLvaluePattern<'a> { pub nodes: ( @@ -29,10 +39,10 @@ pub struct NetLvaluePattern<'a> { #[derive(Debug)] pub enum VariableLvalue<'a> { - Identifier(Box>), - Lvalue(Box>>), - Pattern(Box>), - Concatenation(Box>), + Identifier(VariableLvalueIdentifier<'a>), + Lvalue(Box>), + Pattern(VariableLvaluePattern<'a>), + StreamingConcatenation(StreamingConcatenation<'a>), } #[derive(Debug)] @@ -44,6 +54,16 @@ pub struct VariableLvalueIdentifier<'a> { ), } +#[derive(Debug)] +pub struct VariableLvalueLvalue<'a> { + pub nodes: ( + Symbol<'a>, + VariableLvalue<'a>, + Vec<(Symbol<'a>, VariableLvalue<'a>)>, + Symbol<'a>, + ), +} + #[derive(Debug)] pub struct VariableLvaluePattern<'a> { pub nodes: ( @@ -57,7 +77,7 @@ pub struct NonrangeVariableLvalue<'a> { pub nodes: ( Option>, HierarchicalVariableIdentifier<'a>, - Select<'a>, + NonrangeSelect<'a>, ), } @@ -68,36 +88,31 @@ pub fn net_lvalue(s: Span) -> IResult { } pub fn net_lvalue_identifier(s: Span) -> IResult { - let (s, x) = ps_or_hierarchical_net_identifier(s)?; - let (s, y) = constant_select(s)?; + let (s, a) = ps_or_hierarchical_net_identifier(s)?; + let (s, b) = constant_select(s)?; Ok(( s, - NetLvalue::Identifier(Box::new(NetLvalueIdentifier { nodes: (x, y) })), + NetLvalue::Identifier(NetLvalueIdentifier { nodes: (a, b) }), )) } pub fn net_lvalue_pattern(s: Span) -> IResult { - let (s, x) = opt(assignment_pattern_expression_type)(s)?; - let (s, y) = assignment_pattern_net_lvalue(s)?; - Ok(( - s, - NetLvalue::Pattern(Box::new(NetLvaluePattern { nodes: (x, y) })), - )) + let (s, a) = opt(assignment_pattern_expression_type)(s)?; + let (s, b) = assignment_pattern_net_lvalue(s)?; + Ok((s, NetLvalue::Pattern(NetLvaluePattern { nodes: (a, b) }))) } pub fn net_lvalue_lvalue(s: Span) -> IResult { - let (s, _) = symbol("{")(s)?; - let (s, x) = net_lvalue(s)?; - let (s, y) = many0(preceded(symbol(","), net_lvalue))(s)?; - let (s, _) = symbol("}")(s)?; - - let mut ret = Vec::new(); - ret.push(x); - for y in y { - ret.push(y); - } - - Ok((s, NetLvalue::Lvalue(Box::new(ret)))) + 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)?; + Ok(( + s, + NetLvalue::Lvalue(Box::new(NetLvalueLvalue { + nodes: (a, b, c, d), + })), + )) } pub fn variable_lvalue(s: Span) -> IResult { @@ -106,99 +121,56 @@ pub fn variable_lvalue(s: Span) -> IResult { variable_lvalue_lvalue, variable_lvalue_pattern, map(streaming_concatenation, |x| { - VariableLvalue::Concatenation(Box::new(x)) + VariableLvalue::StreamingConcatenation(x) }), ))(s) } pub fn variable_lvalue_identifier(s: Span) -> IResult { - let (s, x) = opt(implicit_class_handle_or_package_scope)(s)?; - let (s, y) = hierarchical_variable_identifier(s)?; - let (s, z) = select(s)?; + let (s, a) = opt(implicit_class_handle_or_package_scope)(s)?; + let (s, b) = hierarchical_variable_identifier(s)?; + let (s, c) = select(s)?; Ok(( s, - VariableLvalue::Identifier(Box::new(VariableLvalueIdentifier { nodes: (x, y, z) })), + VariableLvalue::Identifier(VariableLvalueIdentifier { nodes: (a, b, c) }), )) } pub fn variable_lvalue_pattern(s: Span) -> IResult { - let (s, x) = opt(assignment_pattern_expression_type)(s)?; - let (s, y) = assignment_pattern_variable_lvalue(s)?; + let (s, a) = opt(assignment_pattern_expression_type)(s)?; + let (s, b) = assignment_pattern_variable_lvalue(s)?; Ok(( s, - VariableLvalue::Pattern(Box::new(VariableLvaluePattern { nodes: (x, y) })), + VariableLvalue::Pattern(VariableLvaluePattern { nodes: (a, b) }), )) } pub fn variable_lvalue_lvalue(s: Span) -> IResult { - let (s, _) = symbol("{")(s)?; - let (s, x) = variable_lvalue(s)?; - let (s, y) = many0(preceded(symbol(","), variable_lvalue))(s)?; - let (s, _) = symbol("}")(s)?; - - let mut ret = Vec::new(); - ret.push(x); - for y in y { - ret.push(y); - } - - Ok((s, VariableLvalue::Lvalue(Box::new(ret)))) + 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)?; + Ok(( + s, + VariableLvalue::Lvalue(Box::new(VariableLvalueLvalue { + nodes: (a, b, c, d), + })), + )) } pub fn nonrange_variable_lvalue(s: Span) -> IResult { - let (s, x) = opt(implicit_class_handle_or_package_scope)(s)?; - let (s, y) = hierarchical_variable_identifier(s)?; - let (s, z) = nonrange_select(s)?; - Ok((s, NonrangeVariableLvalue { nodes: (x, y, z) })) + let (s, a) = opt(implicit_class_handle_or_package_scope)(s)?; + let (s, b) = hierarchical_variable_identifier(s)?; + let (s, c) = nonrange_select(s)?; + Ok((s, NonrangeVariableLvalue { nodes: (a, b, c) })) } // ----------------------------------------------------------------------------- #[cfg(test)] mod tests { - use super::*; + //use super::*; #[test] - fn test() { - //assert_eq!( - // format!("{:?}", all_consuming(net_lvalue)(Span::new("a"))), - // "Ok((\"\", Identifier(NetLvalueIdentifier { identifier: ScopedIdentifier { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"a\" } } }, select: ConstantSelect { member: None, bit_select: [], part_select_range: None } })))" - //); - //assert_eq!( - // format!("{:?}", all_consuming(net_lvalue)(Span::new("a[1][2]"))), - // "Ok((\"\", Identifier(NetLvalueIdentifier { identifier: ScopedIdentifier { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"a\" } } }, select: ConstantSelect { member: None, bit_select: [Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"1\"))))), Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"2\")))))], part_select_range: None } })))" - //); - //assert_eq!( - // format!("{:?}", all_consuming(net_lvalue)(Span::new("a[1][10:5]"))), - // "Ok((\"\", Identifier(NetLvalueIdentifier { identifier: ScopedIdentifier { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"a\" } } }, select: ConstantSelect { member: None, bit_select: [Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"1\")))))], part_select_range: Some(Range((Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"10\"))))), Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"5\")))))))) } })))" - //); - //assert_eq!( - // format!("{:?}", all_consuming(net_lvalue)(Span::new("{a, b[1], c}"))), - // "Ok((\"\", Lvalue([Identifier(NetLvalueIdentifier { identifier: ScopedIdentifier { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"a\" } } }, select: ConstantSelect { member: None, bit_select: [], part_select_range: None } }), Identifier(NetLvalueIdentifier { identifier: ScopedIdentifier { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"b\" } } }, select: ConstantSelect { member: None, bit_select: [Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"1\")))))], part_select_range: None } }), Identifier(NetLvalueIdentifier { identifier: ScopedIdentifier { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"c\" } } }, select: ConstantSelect { member: None, bit_select: [], part_select_range: None } })])))" - //); - //assert_eq!( - // format!("{:?}", all_consuming(variable_lvalue)(Span::new("a"))), - // "Ok((\"\", Identifier(VariableLvalueIdentifier { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"a\" } }, select: Select { member: None, bit_select: [], part_select_range: None } })))" - //); - //assert_eq!( - // format!("{:?}", all_consuming(variable_lvalue)(Span::new("a[1][2]"))), - // "Ok((\"\", Identifier(VariableLvalueIdentifier { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"a\" } }, select: Select { member: None, bit_select: [Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"1\"))))), Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"2\")))))], part_select_range: None } })))" - //); - //assert_eq!( - // format!("{:?}", all_consuming(variable_lvalue)(Span::new("a[1][10:5]"))), - // "Ok((\"\", Identifier(VariableLvalueIdentifier { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"a\" } }, select: Select { member: None, bit_select: [Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"1\")))))], part_select_range: Some(Range((Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"10\"))))), Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"5\")))))))) } })))" - //); - //assert_eq!( - // format!("{:?}", all_consuming(variable_lvalue)(Span::new("{a, b[1], c}"))), - // "Ok((\"\", Lvalue([Identifier(VariableLvalueIdentifier { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"a\" } }, select: Select { member: None, bit_select: [], part_select_range: None } }), Identifier(VariableLvalueIdentifier { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"b\" } }, select: Select { member: None, bit_select: [Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"1\")))))], part_select_range: None } }), Identifier(VariableLvalueIdentifier { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"c\" } }, select: Select { member: None, bit_select: [], part_select_range: None } })])))" - //); - //assert_eq!( - // format!("{:?}", all_consuming(nonrange_variable_lvalue)(Span::new("a"))), - // "Ok((\"\", NonrangeVariableLvalue { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"a\" } }, select: Select { member: None, bit_select: [], part_select_range: None } }))" - //); - //assert_eq!( - // format!("{:?}", all_consuming(nonrange_variable_lvalue)(Span::new("a[1][2]"))), - // "Ok((\"\", NonrangeVariableLvalue { scope: None, identifier: HierarchicalIdentifier { hierarchy: [], identifier: Identifier { raw: \"a\" } }, select: Select { member: None, bit_select: [Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"1\"))))), Nullary(PrimaryLiteral(Number(IntegralNumber(UnsignedNumber(\"2\")))))], part_select_range: None } }))" - //); - } + fn test() {} } diff --git a/src/parser/expressions/expressions.rs b/src/parser/expressions/expressions.rs index d426c37..f5df308 100644 --- a/src/parser/expressions/expressions.rs +++ b/src/parser/expressions/expressions.rs @@ -34,15 +34,17 @@ pub struct IncOrDecExpressionSuffix<'a> { pub struct ConditionalExpression<'a> { pub nodes: ( CondPredicate<'a>, + Symbol<'a>, Vec>, Expression<'a>, + Symbol<'a>, Expression<'a>, ), } #[derive(Debug)] pub enum ConstantExpression<'a> { - Nullary(Box>), + ConstantPrimary(Box>), Unary(Box>), Binary(Box>), Ternary(Box>), @@ -71,8 +73,10 @@ pub struct ConstantExpressionBinary<'a> { pub struct ConstantExpressionTernary<'a> { pub nodes: ( ConstantExpression<'a>, + Symbol<'a>, Vec>, ConstantExpression<'a>, + Symbol<'a>, ConstantExpression<'a>, ), } @@ -80,70 +84,66 @@ pub struct ConstantExpressionTernary<'a> { #[derive(Debug)] pub enum ConstantMintypmaxExpression<'a> { Unary(ConstantExpression<'a>), - Ternary( - ( - ConstantExpression<'a>, - ConstantExpression<'a>, - ConstantExpression<'a>, - ), + Ternary(ConstantMintypmaxExpressionTernary<'a>), +} + +#[derive(Debug)] +pub struct ConstantMintypmaxExpressionTernary<'a> { + pub nodes: ( + ConstantExpression<'a>, + Symbol<'a>, + ConstantExpression<'a>, + Symbol<'a>, + ConstantExpression<'a>, ), } #[derive(Debug)] pub enum ConstantParamExpression<'a> { - Mintypmax(ConstantMintypmaxExpression<'a>), + ConstantMintypmaxExpression(ConstantMintypmaxExpression<'a>), DataType(DataType<'a>), - Dollar, + Dollar(Symbol<'a>), } #[derive(Debug)] pub enum ParamExpression<'a> { - Mintypmax(MintypmaxExpression<'a>), + MintypmaxExpression(MintypmaxExpression<'a>), DataType(DataType<'a>), - Dollar, + Dollar(Symbol<'a>), } #[derive(Debug)] pub enum ConstantRangeExpression<'a> { - Expression(ConstantExpression<'a>), - PartSelectRange(ConstantPartSelectRange<'a>), + ConstantExpression(ConstantExpression<'a>), + ConstantPartSelectRange(ConstantPartSelectRange<'a>), } #[derive(Debug)] pub enum ConstantPartSelectRange<'a> { - Range(ConstantRange<'a>), - IndexedRange(ConstantIndexedRange<'a>), + ConstantRange(ConstantRange<'a>), + ConstantIndexedRange(ConstantIndexedRange<'a>), } #[derive(Debug)] pub struct ConstantRange<'a> { - pub nodes: (ConstantExpression<'a>, ConstantExpression<'a>), + pub nodes: (ConstantExpression<'a>, Symbol<'a>, ConstantExpression<'a>), } #[derive(Debug)] pub struct ConstantIndexedRange<'a> { - pub nodes: ( - ConstantExpression<'a>, - ConstantIndexedRangeOperator<'a>, - ConstantExpression<'a>, - ), -} - -#[derive(Debug)] -pub struct ConstantIndexedRangeOperator<'a> { - pub nodes: (Symbol<'a>,), + pub nodes: (ConstantExpression<'a>, Symbol<'a>, ConstantExpression<'a>), } #[derive(Debug)] pub enum Expression<'a> { - Nullary(Box>), + Primary(Box>), Unary(Box>), - IncOrDec(Box>), - Assignment(Box>), + IncOrDecExpression(Box>), + OperatorAssignment(Box>), Binary(Box>), - Conditional(Box>), - Inside(Box>), - TaggedUnion(Box>), + ConditionalExpression(Box>), + InsideExpression(Box>), + TaggedUnionExpression(Box>), } #[derive(Debug)] @@ -151,6 +151,11 @@ pub struct ExpressionUnary<'a> { pub nodes: (UnaryOperator<'a>, Vec>, Primary<'a>), } +#[derive(Debug)] +pub struct ExpressionOperatorAssignment<'a> { + pub nodes: (Symbol<'a>, OperatorAssignment<'a>, Symbol<'a>), +} + #[derive(Debug)] pub struct ExpressionBinary<'a> { pub nodes: ( @@ -163,42 +168,72 @@ pub struct ExpressionBinary<'a> { #[derive(Debug)] pub struct TaggedUnionExpression<'a> { - pub nodes: (MemberIdentifier<'a>, Option>), + pub nodes: (Symbol<'a>, MemberIdentifier<'a>, Option>), } #[derive(Debug)] pub struct InsideExpression<'a> { - pub nodes: (Expression<'a>, Vec>), + pub nodes: ( + Expression<'a>, + Symbol<'a>, + Symbol<'a>, + Vec>, + Symbol<'a>, + ), } #[derive(Debug)] pub enum ValueRange<'a> { - Unary(Expression<'a>), - Binary((Expression<'a>, Expression<'a>)), + Expression(Expression<'a>), + Binary(ValueRangeBinary<'a>), +} + +#[derive(Debug)] +pub struct ValueRangeBinary<'a> { + pub nodes: ( + Symbol<'a>, + Expression<'a>, + Symbol<'a>, + Expression<'a>, + Symbol<'a>, + ), } #[derive(Debug)] pub enum MintypmaxExpression<'a> { - Unary(Expression<'a>), - Ternary((Expression<'a>, Expression<'a>, Expression<'a>)), + Expression(Expression<'a>), + Ternary(MintypmaxExpressionTernary<'a>), +} + +#[derive(Debug)] +pub struct MintypmaxExpressionTernary<'a> { + pub nodes: ( + Expression<'a>, + Symbol<'a>, + Expression<'a>, + Symbol<'a>, + Expression<'a>, + ), } #[derive(Debug)] pub struct ModulePathConditionalExpression<'a> { pub nodes: ( ModulePathExpression<'a>, + Symbol<'a>, Vec>, ModulePathExpression<'a>, + Symbol<'a>, ModulePathExpression<'a>, ), } #[derive(Debug)] pub enum ModulePathExpression<'a> { - Nullary(Box>), + ModulePathPrimary(Box>), Unary(Box>), Binary(Box>), - Conditional(Box>), + ModulePathConditionalExpression(Box>), } #[derive(Debug)] @@ -222,20 +257,35 @@ pub struct ModulePathExpressionBinary<'a> { #[derive(Debug)] pub enum ModulePathMintypmaxExpression<'a> { - Unary(ModulePathExpression<'a>), - Ternary( - ( - ModulePathExpression<'a>, - ModulePathExpression<'a>, - ModulePathExpression<'a>, - ), + ModulePathExpression(ModulePathExpression<'a>), + Ternary(ModulePathMintypmaxExpressionTernary<'a>), +} + +#[derive(Debug)] +pub struct ModulePathMintypmaxExpressionTernary<'a> { + pub nodes: ( + ModulePathExpression<'a>, + Symbol<'a>, + ModulePathExpression<'a>, + Symbol<'a>, + ModulePathExpression<'a>, ), } #[derive(Debug)] pub enum PartSelectRange<'a> { - Range((ConstantExpression<'a>, ConstantExpression<'a>)), - IndexedRange((Expression<'a>, Symbol<'a>, ConstantExpression<'a>)), + ConstantRange(ConstantRange<'a>), + IndexedRange(IndexedRange<'a>), +} + +#[derive(Debug)] +pub struct IndexedRange<'a> { + pub nodes: (Expression<'a>, Symbol<'a>, ConstantExpression<'a>), +} + +#[derive(Debug)] +pub struct GenvarExpression<'a> { + pub nodes: (ConstantExpression<'a>,), } // ----------------------------------------------------------------------------- @@ -245,36 +295,36 @@ pub fn inc_or_dec_expression(s: Span) -> IResult { } pub fn inc_or_dec_expression_prefix(s: Span) -> IResult { - let (s, x) = inc_or_dec_operator(s)?; - let (s, y) = many0(attribute_instance)(s)?; - let (s, z) = variable_lvalue(s)?; + let (s, a) = inc_or_dec_operator(s)?; + let (s, b) = many0(attribute_instance)(s)?; + let (s, c) = variable_lvalue(s)?; Ok(( s, - IncOrDecExpression::Prefix(IncOrDecExpressionPrefix { nodes: (x, y, z) }), + IncOrDecExpression::Prefix(IncOrDecExpressionPrefix { nodes: (a, b, c) }), )) } pub fn inc_or_dec_expression_suffix(s: Span) -> IResult { - let (s, x) = variable_lvalue(s)?; - let (s, y) = many0(attribute_instance)(s)?; - let (s, z) = inc_or_dec_operator(s)?; + let (s, a) = variable_lvalue(s)?; + let (s, b) = many0(attribute_instance)(s)?; + let (s, c) = inc_or_dec_operator(s)?; Ok(( s, - IncOrDecExpression::Suffix(IncOrDecExpressionSuffix { nodes: (x, y, z) }), + IncOrDecExpression::Suffix(IncOrDecExpressionSuffix { nodes: (a, b, c) }), )) } pub fn conditional_expression(s: Span) -> IResult { - let (s, x) = cond_predicate(s)?; - let (s, _) = symbol("?")(s)?; - let (s, y) = many0(attribute_instance)(s)?; - let (s, z) = expression(s)?; - let (s, _) = symbol(":")(s)?; - let (s, v) = expression(s)?; + let (s, a) = cond_predicate(s)?; + let (s, b) = symbol("?")(s)?; + let (s, c) = many0(attribute_instance)(s)?; + let (s, d) = expression(s)?; + let (s, e) = symbol(":")(s)?; + let (s, f) = expression(s)?; Ok(( s, ConditionalExpression { - nodes: (x, y, z, v), + nodes: (a, b, c, d, e, f), }, )) } @@ -282,7 +332,7 @@ pub fn conditional_expression(s: Span) -> IResult { pub fn constant_expression(s: Span) -> IResult { alt(( map(constant_primary, |x| { - ConstantExpression::Nullary(Box::new(x)) + ConstantExpression::ConstantPrimary(Box::new(x)) }), constant_expression_unary, constant_expression_binary, @@ -291,39 +341,39 @@ pub fn constant_expression(s: Span) -> IResult { } pub fn constant_expression_unary(s: Span) -> IResult { - let (s, x) = unary_operator(s)?; - let (s, y) = many0(attribute_instance)(s)?; - let (s, z) = constant_primary(s)?; + let (s, a) = unary_operator(s)?; + let (s, b) = many0(attribute_instance)(s)?; + let (s, c) = constant_primary(s)?; Ok(( s, - ConstantExpression::Unary(Box::new(ConstantExpressionUnary { nodes: (x, y, z) })), + ConstantExpression::Unary(Box::new(ConstantExpressionUnary { nodes: (a, b, c) })), )) } pub fn constant_expression_binary(s: Span) -> IResult { - let (s, x) = constant_expression(s)?; - let (s, y) = binary_operator(s)?; - let (s, z) = many0(attribute_instance)(s)?; - let (s, v) = constant_expression(s)?; + let (s, a) = constant_expression(s)?; + let (s, b) = binary_operator(s)?; + let (s, c) = many0(attribute_instance)(s)?; + let (s, d) = constant_expression(s)?; Ok(( s, ConstantExpression::Binary(Box::new(ConstantExpressionBinary { - nodes: (x, y, z, v), + nodes: (a, b, c, d), })), )) } pub fn constant_expression_ternary(s: Span) -> IResult { - let (s, x) = constant_expression(s)?; - let (s, _) = symbol("?")(s)?; - let (s, y) = many0(attribute_instance)(s)?; - let (s, z) = constant_expression(s)?; - let (s, _) = symbol(":")(s)?; - let (s, v) = constant_expression(s)?; + let (s, a) = constant_expression(s)?; + let (s, b) = symbol("?")(s)?; + let (s, c) = many0(attribute_instance)(s)?; + let (s, d) = constant_expression(s)?; + let (s, e) = symbol(":")(s)?; + let (s, f) = constant_expression(s)?; Ok(( s, ConstantExpression::Ternary(Box::new(ConstantExpressionTernary { - nodes: (x, y, z, v), + nodes: (a, b, c, d, e, f), })), )) } @@ -340,19 +390,24 @@ pub fn constant_mintypmax_expression(s: Span) -> IResult IResult { - let (s, x) = constant_expression(s)?; - let (s, _) = symbol(":")(s)?; - let (s, y) = constant_expression(s)?; - let (s, _) = symbol(":")(s)?; - let (s, z) = constant_expression(s)?; - Ok((s, ConstantMintypmaxExpression::Ternary((x, y, z)))) + let (s, a) = constant_expression(s)?; + let (s, b) = symbol(":")(s)?; + let (s, c) = constant_expression(s)?; + let (s, d) = symbol(":")(s)?; + let (s, e) = constant_expression(s)?; + Ok(( + s, + ConstantMintypmaxExpression::Ternary(ConstantMintypmaxExpressionTernary { + nodes: (a, b, c, d, e), + }), + )) } pub fn constant_param_expression(s: Span) -> IResult { alt(( - map(symbol("$"), |_| ConstantParamExpression::Dollar), + map(symbol("$"), |x| ConstantParamExpression::Dollar(x)), map(constant_mintypmax_expression, |x| { - ConstantParamExpression::Mintypmax(x) + ConstantParamExpression::ConstantMintypmaxExpression(x) }), map(data_type, |x| ConstantParamExpression::DataType(x)), ))(s) @@ -360,8 +415,10 @@ pub fn constant_param_expression(s: Span) -> IResult IResult { alt(( - map(symbol("$"), |_| ParamExpression::Dollar), - map(mintypmax_expression, |x| ParamExpression::Mintypmax(x)), + map(symbol("$"), |x| ParamExpression::Dollar(x)), + map(mintypmax_expression, |x| { + ParamExpression::MintypmaxExpression(x) + }), map(data_type, |x| ParamExpression::DataType(x)), ))(s) } @@ -369,54 +426,56 @@ pub fn param_expression(s: Span) -> IResult { pub fn constant_range_expression(s: Span) -> IResult { alt(( map(constant_part_select_range, |x| { - ConstantRangeExpression::PartSelectRange(x) + ConstantRangeExpression::ConstantPartSelectRange(x) }), map(constant_expression, |x| { - ConstantRangeExpression::Expression(x) + ConstantRangeExpression::ConstantExpression(x) }), ))(s) } pub fn constant_part_select_range(s: Span) -> IResult { alt(( - map(constant_range, |x| ConstantPartSelectRange::Range(x)), + map(constant_range, |x| { + ConstantPartSelectRange::ConstantRange(x) + }), map(constant_indexed_range, |x| { - ConstantPartSelectRange::IndexedRange(x) + ConstantPartSelectRange::ConstantIndexedRange(x) }), ))(s) } pub fn constant_range(s: Span) -> IResult { - let (s, x) = constant_expression(s)?; - let (s, _) = symbol(":")(s)?; - let (s, y) = constant_expression(s)?; - Ok((s, ConstantRange { nodes: (x, y) })) + let (s, a) = constant_expression(s)?; + let (s, b) = symbol(":")(s)?; + let (s, c) = constant_expression(s)?; + Ok((s, ConstantRange { nodes: (a, b, c) })) } pub fn constant_indexed_range(s: Span) -> IResult { - let (s, x) = constant_expression(s)?; - let (s, y) = map(alt((symbol("+:"), symbol("-:"))), |x| { - ConstantIndexedRangeOperator { nodes: (x,) } - })(s)?; - let (s, z) = constant_expression(s)?; - Ok((s, ConstantIndexedRange { nodes: (x, y, z) })) + let (s, a) = constant_expression(s)?; + let (s, b) = alt((symbol("+:"), symbol("-:")))(s)?; + let (s, c) = constant_expression(s)?; + Ok((s, ConstantIndexedRange { nodes: (a, b, c) })) } pub fn expression(s: Span) -> IResult { alt(( - map(primary, |x| Expression::Nullary(Box::new(x))), + map(primary, |x| Expression::Primary(Box::new(x))), expression_unary, - map(inc_or_dec_expression, |x| Expression::IncOrDec(Box::new(x))), - map(paren(operator_assignment), |x| { - Expression::Assignment(Box::new(x)) + map(inc_or_dec_expression, |x| { + Expression::IncOrDecExpression(Box::new(x)) }), + expression_operator_assignment, expression_binary, map(conditional_expression, |x| { - Expression::Conditional(Box::new(x)) + Expression::ConditionalExpression(Box::new(x)) + }), + map(inside_expression, |x| { + Expression::InsideExpression(Box::new(x)) }), - map(inside_expression, |x| Expression::Inside(Box::new(x))), map(tagged_union_expression, |x| { - Expression::TaggedUnion(Box::new(x)) + Expression::TaggedUnionExpression(Box::new(x)) }), ))(s) } @@ -431,78 +490,103 @@ pub fn expression_unary(s: Span) -> IResult { )) } +pub fn expression_operator_assignment(s: Span) -> IResult { + let (s, a) = paren2(operator_assignment)(s)?; + Ok(( + s, + Expression::OperatorAssignment(Box::new(ExpressionOperatorAssignment { nodes: a })), + )) +} + pub fn expression_binary(s: Span) -> IResult { - let (s, x) = expression(s)?; - let (s, y) = binary_operator(s)?; - let (s, z) = many0(attribute_instance)(s)?; - let (s, v) = expression(s)?; + let (s, a) = expression(s)?; + let (s, b) = binary_operator(s)?; + let (s, c) = many0(attribute_instance)(s)?; + let (s, d) = expression(s)?; Ok(( s, Expression::Binary(Box::new(ExpressionBinary { - nodes: (x, y, z, v), + nodes: (a, b, c, d), })), )) } pub fn tagged_union_expression(s: Span) -> IResult { - let (s, _) = symbol("tagged")(s)?; - let (s, x) = member_identifier(s)?; - let (s, y) = opt(expression)(s)?; - Ok((s, TaggedUnionExpression { nodes: (x, y) })) + let (s, a) = symbol("tagged")(s)?; + let (s, b) = member_identifier(s)?; + let (s, c) = opt(expression)(s)?; + Ok((s, TaggedUnionExpression { nodes: (a, b, c) })) } pub fn inside_expression(s: Span) -> IResult { - let (s, x) = expression(s)?; - let (s, _) = symbol("inside")(s)?; - let (s, y) = brace(open_range_list)(s)?; - Ok((s, InsideExpression { nodes: (x, y) })) + 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), + }, + )) } pub fn value_range(s: Span) -> IResult { alt(( value_range_binary, - map(expression, |x| ValueRange::Unary(x)), + map(expression, |x| ValueRange::Expression(x)), ))(s) } pub fn value_range_binary(s: Span) -> IResult { - let (s, _) = symbol("[")(s)?; - let (s, x) = expression(s)?; - let (s, _) = symbol(":")(s)?; - let (s, y) = expression(s)?; - let (s, _) = symbol("]")(s)?; - Ok((s, ValueRange::Binary((x, y)))) + 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), + }), + )) } pub fn mintypmax_expression(s: Span) -> IResult { alt(( mintypmax_expression_ternary, - map(expression, |x| MintypmaxExpression::Unary(x)), + map(expression, |x| MintypmaxExpression::Expression(x)), ))(s) } pub fn mintypmax_expression_ternary(s: Span) -> IResult { - let (s, x) = expression(s)?; - let (s, _) = symbol(":")(s)?; - let (s, y) = expression(s)?; - let (s, _) = symbol(":")(s)?; - let (s, z) = expression(s)?; - Ok((s, MintypmaxExpression::Ternary((x, y, z)))) + let (s, a) = expression(s)?; + let (s, b) = symbol(":")(s)?; + let (s, c) = expression(s)?; + let (s, d) = symbol(":")(s)?; + let (s, e) = expression(s)?; + Ok(( + s, + MintypmaxExpression::Ternary(MintypmaxExpressionTernary { + nodes: (a, b, c, d, e), + }), + )) } pub fn module_path_conditional_expression( s: Span, ) -> IResult { - let (s, x) = module_path_expression(s)?; - let (s, _) = symbol("?")(s)?; - let (s, y) = many0(attribute_instance)(s)?; - let (s, z) = module_path_expression(s)?; - let (s, _) = symbol(":")(s)?; - let (s, v) = module_path_expression(s)?; + let (s, a) = module_path_expression(s)?; + let (s, b) = symbol("?")(s)?; + let (s, c) = many0(attribute_instance)(s)?; + let (s, d) = module_path_expression(s)?; + let (s, e) = symbol(":")(s)?; + let (s, f) = module_path_expression(s)?; Ok(( s, ModulePathConditionalExpression { - nodes: (x, y, z, v), + nodes: (a, b, c, d, e, f), }, )) } @@ -510,35 +594,35 @@ pub fn module_path_conditional_expression( pub fn module_path_expression(s: Span) -> IResult { alt(( map(module_path_primary, |x| { - ModulePathExpression::Nullary(Box::new(x)) + ModulePathExpression::ModulePathPrimary(Box::new(x)) }), module_path_expression_unary, module_path_expression_binary, map(module_path_conditional_expression, |x| { - ModulePathExpression::Conditional(Box::new(x)) + ModulePathExpression::ModulePathConditionalExpression(Box::new(x)) }), ))(s) } pub fn module_path_expression_unary(s: Span) -> IResult { - let (s, x) = unary_module_path_operator(s)?; - let (s, y) = many0(attribute_instance)(s)?; - let (s, z) = module_path_primary(s)?; + let (s, a) = unary_module_path_operator(s)?; + let (s, b) = many0(attribute_instance)(s)?; + let (s, c) = module_path_primary(s)?; Ok(( s, - ModulePathExpression::Unary(Box::new(ModulePathExpressionUnary { nodes: (x, y, z) })), + ModulePathExpression::Unary(Box::new(ModulePathExpressionUnary { nodes: (a, b, c) })), )) } pub fn module_path_expression_binary(s: Span) -> IResult { - let (s, x) = module_path_expression(s)?; - let (s, y) = binary_module_path_operator(s)?; - let (s, z) = many0(attribute_instance)(s)?; - let (s, v) = module_path_expression(s)?; + let (s, a) = module_path_expression(s)?; + let (s, b) = binary_module_path_operator(s)?; + let (s, c) = many0(attribute_instance)(s)?; + let (s, d) = module_path_expression(s)?; Ok(( s, ModulePathExpression::Binary(Box::new(ModulePathExpressionBinary { - nodes: (x, y, z, v), + nodes: (a, b, c, d), })), )) } @@ -547,7 +631,7 @@ pub fn module_path_mintypmax_expression(s: Span) -> IResult IResult IResult { - let (s, x) = module_path_expression(s)?; - let (s, _) = symbol(":")(s)?; - let (s, y) = module_path_expression(s)?; - let (s, _) = symbol(":")(s)?; - let (s, z) = module_path_expression(s)?; - Ok((s, ModulePathMintypmaxExpression::Ternary((x, y, z)))) + let (s, a) = module_path_expression(s)?; + let (s, b) = symbol(":")(s)?; + let (s, c) = module_path_expression(s)?; + let (s, d) = symbol(":")(s)?; + let (s, e) = module_path_expression(s)?; + Ok(( + s, + ModulePathMintypmaxExpression::Ternary(ModulePathMintypmaxExpressionTernary { + nodes: (a, b, c, d, e), + }), + )) } pub fn part_select_range(s: Span) -> IResult { - alt((range, indexed_range))(s) + alt(( + map(constant_range, |x| PartSelectRange::ConstantRange(x)), + map(indexed_range, |x| PartSelectRange::IndexedRange(x)), + ))(s) } -pub fn range(s: Span) -> IResult { - let (s, x) = constant_expression(s)?; - let (s, _) = symbol(":")(s)?; - let (s, y) = constant_expression(s)?; - Ok((s, PartSelectRange::Range((x, y)))) +pub fn indexed_range(s: Span) -> IResult { + let (s, a) = expression(s)?; + let (s, b) = alt((symbol("+:"), symbol("-:")))(s)?; + let (s, c) = constant_expression(s)?; + Ok((s, IndexedRange { nodes: (a, b, c) })) } -pub fn indexed_range(s: Span) -> IResult { - let (s, x) = expression(s)?; - let (s, y) = alt((symbol("+:"), symbol("-:")))(s)?; - let (s, z) = constant_expression(s)?; - Ok((s, PartSelectRange::IndexedRange((x, y, z)))) -} - -pub fn genvar_expression(s: Span) -> IResult { - constant_expression(s) +pub fn genvar_expression(s: Span) -> IResult { + let (s, a) = constant_expression(s)?; + Ok((s, GenvarExpression { nodes: (a,) })) } // ----------------------------------------------------------------------------- @@ -591,22 +677,5 @@ pub fn genvar_expression(s: Span) -> IResult { mod tests { #[test] - fn test() { - // TODO after operator_assignment - //assert_eq!( - // format!( - // "{:?}", - // all_consuming(expression)("(a:b:c) + (d:e:f)") - // ), - // "" - //); - // TODO after operator_assignment - //assert_eq!( - // format!( - // "{:?}", - // all_consuming(expression)("val - (32'd 50: 32'd 75: 32'd 100)") - // ), - // "" - //); - } + fn test() {} } diff --git a/src/parser/expressions/primaries.rs b/src/parser/expressions/primaries.rs index 72411e6..00666bc 100644 --- a/src/parser/expressions/primaries.rs +++ b/src/parser/expressions/primaries.rs @@ -2,7 +2,6 @@ use crate::parser::*; use nom::branch::*; use nom::combinator::*; use nom::multi::*; -use nom::sequence::*; use nom::IResult; // ----------------------------------------------------------------------------- @@ -12,18 +11,18 @@ pub enum ConstantPrimary<'a> { PrimaryLiteral(PrimaryLiteral<'a>), PsParameter(ConstantPrimaryPsParameter<'a>), Specparam(ConstantPrimarySpecparam<'a>), - Genvar(GenvarIdentifier<'a>), + GenvarIdentifier(GenvarIdentifier<'a>), FormalPort(ConstantPrimaryFormalPort<'a>), Enum(ConstantPrimaryEnum<'a>), Concatenation(ConstantPrimaryConcatenation<'a>), MultipleConcatenation(ConstantPrimaryMultipleConcatenation<'a>), - FunctionCall(SubroutineCall<'a>), - LetExpression(LetExpression<'a>), - MintypmaxExpression(ConstantMintypmaxExpression<'a>), - Cast(ConstantCast<'a>), - AssignmentPatternExpression(AssignmentPatternExpression<'a>), + ConstantFunctionCall(ConstantFunctionCall<'a>), + ConstantLetExpression(ConstantLetExpression<'a>), + MintypmaxExpression(ConstantPrimaryMintypmaxExpression<'a>), + ConstantCast(ConstantCast<'a>), + ConstantAssignmentPatternExpression(ConstantAssignmentPatternExpression<'a>), TypeReference(TypeReference<'a>), - Null, + Null(Symbol<'a>), } #[derive(Debug)] @@ -33,7 +32,10 @@ pub struct ConstantPrimaryPsParameter<'a> { #[derive(Debug)] pub struct ConstantPrimarySpecparam<'a> { - pub nodes: (SpecparamIdentifier<'a>, Option>), + pub nodes: ( + SpecparamIdentifier<'a>, + Option<(Symbol<'a>, ConstantRangeExpression<'a>, Symbol<'a>)>, + ), } #[derive(Debug)] @@ -50,7 +52,7 @@ pub struct ConstantPrimaryEnum<'a> { pub struct ConstantPrimaryConcatenation<'a> { pub nodes: ( ConstantConcatenation<'a>, - Option>, + Option<(Symbol<'a>, ConstantRangeExpression<'a>, Symbol<'a>)>, ), } @@ -58,58 +60,53 @@ pub struct ConstantPrimaryConcatenation<'a> { pub struct ConstantPrimaryMultipleConcatenation<'a> { pub nodes: ( ConstantMultipleConcatenation<'a>, - Option>, + Option<(Symbol<'a>, ConstantRangeExpression<'a>, Symbol<'a>)>, ), } +#[derive(Debug)] +pub struct ConstantPrimaryMintypmaxExpression<'a> { + pub nodes: (Symbol<'a>, ConstantMintypmaxExpression<'a>, Symbol<'a>), +} + #[derive(Debug)] pub enum ModulePathPrimary<'a> { Number(Number<'a>), Identifier(Identifier<'a>), ModulePathConcatenation(ModulePathConcatenation<'a>), ModulePathMultipleConcatenation(ModulePathMultipleConcatenation<'a>), - FunctionSubroutineCall(SubroutineCall<'a>), - ModulePathMintypmaxExpression(ModulePathMintypmaxExpression<'a>), + FunctionSubroutineCall(FunctionSubroutineCall<'a>), + Mintypmax(ModulePathPrimaryMintypmax<'a>), +} + +#[derive(Debug)] +pub struct ModulePathPrimaryMintypmax<'a> { + pub nodes: (Symbol<'a>, ModulePathMintypmaxExpression<'a>, Symbol<'a>), } #[derive(Debug)] pub enum Primary<'a> { PrimaryLiteral(PrimaryLiteral<'a>), Hierarchical(PrimaryHierarchical<'a>), - EmptyUnpackedArrayConcatenation, + EmptyUnpackedArrayConcatenation(EmptyUnpackedArrayConcatenation<'a>), Concatenation(PrimaryConcatenation<'a>), MultipleConcatenation(PrimaryMultipleConcatenation<'a>), - FunctionSubroutineCall(SubroutineCall<'a>), + FunctionSubroutineCall(FunctionSubroutineCall<'a>), LetExpression(LetExpression<'a>), - MintypmaxExpression(MintypmaxExpression<'a>), + MintypmaxExpression(PrimaryMintypmaxExpression<'a>), Cast(Cast<'a>), AssignmentPatternExpression(AssignmentPatternExpression<'a>), StreamingConcatenation(StreamingConcatenation<'a>), SequenceMethodCall(SequenceMethodCall<'a>), - This(This<'a>), - Dollar(Dollar<'a>), - Null(Null<'a>), -} - -#[derive(Debug)] -pub struct This<'a> { - pub nodes: (Symbol<'a>,), -} - -#[derive(Debug)] -pub struct Dollar<'a> { - pub nodes: (Symbol<'a>,), -} - -#[derive(Debug)] -pub struct Null<'a> { - pub nodes: (Symbol<'a>,), + This(Symbol<'a>), + Dollar(Symbol<'a>), + Null(Symbol<'a>), } #[derive(Debug)] pub struct PrimaryHierarchical<'a> { pub nodes: ( - Option>, + Option>, HierarchicalIdentifier<'a>, Select<'a>, ), @@ -117,27 +114,43 @@ pub struct PrimaryHierarchical<'a> { #[derive(Debug)] pub struct PrimaryConcatenation<'a> { - pub nodes: (Concatenation<'a>, Option>), + pub nodes: ( + Concatenation<'a>, + Option<(Symbol<'a>, RangeExpression<'a>, Symbol<'a>)>, + ), } #[derive(Debug)] pub struct PrimaryMultipleConcatenation<'a> { - pub nodes: (MultipleConcatenation<'a>, Option>), + pub nodes: ( + MultipleConcatenation<'a>, + Option<(Symbol<'a>, RangeExpression<'a>, Symbol<'a>)>, + ), } #[derive(Debug)] -pub enum PrimaryHierarchicalQualifier<'a> { +pub struct PrimaryMintypmaxExpression<'a> { + pub nodes: (Symbol<'a>, MintypmaxExpression<'a>, Symbol<'a>), +} + +#[derive(Debug)] +pub enum ClassQualifierOrPackageScope<'a> { ClassQualifier(ClassQualifier<'a>), PackageScope(PackageScope<'a>), } #[derive(Debug)] pub struct ClassQualifier<'a> { - pub nodes: (Option, Option>), + pub nodes: ( + Option>, + Option>, + ), } #[derive(Debug)] -pub struct Local {} +pub struct Local<'a> { + pub nodes: (Symbol<'a>,), +} #[derive(Debug)] pub enum RangeExpression<'a> { @@ -155,8 +168,18 @@ pub enum PrimaryLiteral<'a> { #[derive(Debug)] pub enum TimeLiteral<'a> { - UnsignedTimeLiteral(UnsignedTimeLiteral<'a>), - FixedPointTimeLiteral(FixedPointTimeLiteral<'a>), + Unsigned(TimeLiteralUnsigned<'a>), + FixedPoint(TimeLiteralFixedPoint<'a>), +} + +#[derive(Debug)] +pub struct TimeLiteralUnsigned<'a> { + pub nodes: (UnsignedNumber<'a>, TimeUnit<'a>), +} + +#[derive(Debug)] +pub struct TimeLiteralFixedPoint<'a> { + pub nodes: (FixedPointNumber<'a>, TimeUnit<'a>), } #[derive(Debug)] @@ -170,155 +193,177 @@ pub enum TimeUnit<'a> { } #[derive(Debug)] -pub enum ImplicitClassHandle { - This, - Super, - ThisSuper, +pub enum ImplicitClassHandle<'a> { + This(Symbol<'a>), + Super(Symbol<'a>), + ThisSuper((Symbol<'a>, Symbol<'a>, Symbol<'a>)), } #[derive(Debug)] pub struct BitSelect<'a> { - nodes: (Vec>,), -} - -#[derive(Debug)] -pub struct UnsignedTimeLiteral<'a> { - pub nodes: (UnsignedNumber<'a>, TimeUnit<'a>), -} - -#[derive(Debug)] -pub struct FixedPointTimeLiteral<'a> { - pub nodes: (FixedPointNumber<'a>, TimeUnit<'a>), + nodes: (Vec<(Symbol<'a>, Expression<'a>, Symbol<'a>)>,), } #[derive(Debug)] pub struct Select<'a> { pub nodes: ( - Option>, + Option<( + Vec<(Symbol<'a>, MemberIdentifier<'a>, BitSelect<'a>)>, + Symbol<'a>, + MemberIdentifier<'a>, + )>, + BitSelect<'a>, + Option<(Symbol<'a>, PartSelectRange<'a>, Symbol<'a>)>, + ), +} + +#[derive(Debug)] +pub struct NonrangeSelect<'a> { + pub nodes: ( + Option<( + Vec<(Symbol<'a>, MemberIdentifier<'a>, BitSelect<'a>)>, + Symbol<'a>, + MemberIdentifier<'a>, + )>, BitSelect<'a>, - Option>, ), } #[derive(Debug)] pub struct ConstantBitSelect<'a> { - nodes: (Vec>,), + nodes: (Vec<(Symbol<'a>, ConstantExpression<'a>, Symbol<'a>)>,), } #[derive(Debug)] pub struct ConstantSelect<'a> { pub nodes: ( - Option>, + Option<( + Vec<(Symbol<'a>, MemberIdentifier<'a>, ConstantBitSelect<'a>)>, + Symbol<'a>, + MemberIdentifier<'a>, + )>, ConstantBitSelect<'a>, - Option>, + Option<(Symbol<'a>, ConstantPartSelectRange<'a>, Symbol<'a>)>, ), } -#[derive(Debug)] -pub struct SelectMember<'a> { - pub nodes: ( - Vec<(MemberIdentifier<'a>, BitSelect<'a>)>, - MemberIdentifier<'a>, - ), -} - -#[derive(Debug)] -pub struct Cast<'a> { - pub nodes: (CastingType<'a>, Expression<'a>), -} - #[derive(Debug)] pub struct ConstantCast<'a> { - pub nodes: (CastingType<'a>, ConstantExpression<'a>), + pub nodes: ( + CastingType<'a>, + Symbol<'a>, + Symbol<'a>, + ConstantExpression<'a>, + Symbol<'a>, + ), +} + +#[derive(Debug)] +pub struct ConstantLetExpression<'a> { + pub nodes: (LetExpression<'a>,), +} + +#[derive(Debug)] +pub struct Cast<'a> { + pub nodes: ( + CastingType<'a>, + Symbol<'a>, + Symbol<'a>, + Expression<'a>, + Symbol<'a>, + ), } // ----------------------------------------------------------------------------- pub fn constant_primary(s: Span) -> IResult { alt(( - map(symbol("null"), |_| ConstantPrimary::Null), map(primary_literal, |x| ConstantPrimary::PrimaryLiteral(x)), constant_primary_ps_parameter, constant_primary_specparam, - map(genvar_identifier, |x| ConstantPrimary::Genvar(x)), + map(genvar_identifier, |x| ConstantPrimary::GenvarIdentifier(x)), constant_primary_formal_port, constant_primary_enum, constant_primary_concatenation, constant_primary_multiple_concatenation, - map(constant_function_call, |x| ConstantPrimary::FunctionCall(x)), + map(constant_function_call, |x| { + ConstantPrimary::ConstantFunctionCall(x) + }), map(constant_let_expression, |x| { - ConstantPrimary::LetExpression(x) + ConstantPrimary::ConstantLetExpression(x) }), - map(paren(constant_mintypmax_expression), |x| { - ConstantPrimary::MintypmaxExpression(x) - }), - map(constant_cast, |x| ConstantPrimary::Cast(x)), + constant_primary_mintypmax_expression, + map(constant_cast, |x| ConstantPrimary::ConstantCast(x)), map(constant_assignment_pattern_expression, |x| { - ConstantPrimary::AssignmentPatternExpression(x) + ConstantPrimary::ConstantAssignmentPatternExpression(x) }), map(type_reference, |x| ConstantPrimary::TypeReference(x)), + map(symbol("null"), |x| ConstantPrimary::Null(x)), ))(s) } pub fn constant_primary_ps_parameter(s: Span) -> IResult { - let (s, x) = ps_parameter_identifier(s)?; - let (s, y) = constant_select(s)?; + let (s, a) = ps_parameter_identifier(s)?; + let (s, b) = constant_select(s)?; Ok(( s, - ConstantPrimary::PsParameter(ConstantPrimaryPsParameter { nodes: (x, y) }), + ConstantPrimary::PsParameter(ConstantPrimaryPsParameter { nodes: (a, b) }), )) } pub fn constant_primary_specparam(s: Span) -> IResult { - let (s, x) = specparam_identifier(s)?; - let (s, y) = opt(bracket(constant_range_expression))(s)?; + let (s, a) = specparam_identifier(s)?; + let (s, b) = opt(bracket2(constant_range_expression))(s)?; Ok(( s, - ConstantPrimary::Specparam(ConstantPrimarySpecparam { nodes: (x, y) }), + ConstantPrimary::Specparam(ConstantPrimarySpecparam { nodes: (a, b) }), )) } pub fn constant_primary_formal_port(s: Span) -> IResult { - let (s, x) = formal_port_identifier(s)?; - let (s, y) = constant_select(s)?; + let (s, a) = formal_port_identifier(s)?; + let (s, b) = constant_select(s)?; Ok(( s, - ConstantPrimary::FormalPort(ConstantPrimaryFormalPort { nodes: (x, y) }), + ConstantPrimary::FormalPort(ConstantPrimaryFormalPort { nodes: (a, b) }), )) } pub fn constant_primary_enum(s: Span) -> IResult { - let (s, x) = package_scope_or_class_scope(s)?; - let (s, y) = enum_identifier(s)?; + let (s, a) = package_scope_or_class_scope(s)?; + let (s, b) = enum_identifier(s)?; Ok(( s, - ConstantPrimary::Enum(ConstantPrimaryEnum { nodes: (x, y) }), + ConstantPrimary::Enum(ConstantPrimaryEnum { nodes: (a, b) }), )) } pub fn constant_primary_concatenation(s: Span) -> IResult { - let (s, x) = constant_concatenation(s)?; - let (s, y) = opt(bracket(constant_range_expression))(s)?; + let (s, a) = constant_concatenation(s)?; + let (s, b) = opt(bracket2(constant_range_expression))(s)?; Ok(( s, - ConstantPrimary::Concatenation(ConstantPrimaryConcatenation { nodes: (x, y) }), + ConstantPrimary::Concatenation(ConstantPrimaryConcatenation { nodes: (a, b) }), )) } pub fn constant_primary_multiple_concatenation(s: Span) -> IResult { - let (s, x) = constant_multiple_concatenation(s)?; - let (s, y) = opt(bracket(constant_range_expression))(s)?; + let (s, a) = constant_multiple_concatenation(s)?; + let (s, b) = opt(bracket2(constant_range_expression))(s)?; Ok(( s, ConstantPrimary::MultipleConcatenation(ConstantPrimaryMultipleConcatenation { - nodes: (x, y), + nodes: (a, b), }), )) } pub fn constant_primary_mintypmax_expression(s: Span) -> IResult { - let (s, x) = paren(constant_mintypmax_expression)(s)?; - Ok((s, ConstantPrimary::MintypmaxExpression(x))) + let (s, a) = paren2(constant_mintypmax_expression)(s)?; + Ok(( + s, + ConstantPrimary::MintypmaxExpression(ConstantPrimaryMintypmaxExpression { nodes: a }), + )) } pub fn module_path_primary(s: Span) -> IResult { @@ -334,27 +379,31 @@ pub fn module_path_primary(s: Span) -> IResult { map(function_subroutine_call, |x| { ModulePathPrimary::FunctionSubroutineCall(x) }), - map(paren(module_path_mintypmax_expression), |x| { - ModulePathPrimary::ModulePathMintypmaxExpression(x) - }), + module_path_primary_mintypmax_expression, ))(s) } +pub fn module_path_primary_mintypmax_expression(s: Span) -> IResult { + let (s, a) = paren2(module_path_mintypmax_expression)(s)?; + Ok(( + s, + ModulePathPrimary::Mintypmax(ModulePathPrimaryMintypmax { nodes: a }), + )) +} + pub fn primary(s: Span) -> IResult { alt(( map(primary_literal, |x| Primary::PrimaryLiteral(x)), primary_hierarchical, - map(empty_unpacked_array_concatenation, |_| { - Primary::EmptyUnpackedArrayConcatenation + map(empty_unpacked_array_concatenation, |x| { + Primary::EmptyUnpackedArrayConcatenation(x) }), primary_concatenation, map(rec(function_subroutine_call, REC_PRIMARY), |x| { Primary::FunctionSubroutineCall(x) }), map(let_expression, |x| Primary::LetExpression(x)), - map(paren(mintypmax_expression), |x| { - Primary::MintypmaxExpression(x) - }), + primary_mintypmax_expression, map(cast, |x| Primary::Cast(x)), map(assignment_pattern_expression, |x| { Primary::AssignmentPatternExpression(x) @@ -363,58 +412,66 @@ pub fn primary(s: Span) -> IResult { Primary::StreamingConcatenation(x) }), map(sequence_method_call, |x| Primary::SequenceMethodCall(x)), - map(symbol("this"), |x| Primary::This(This { nodes: (x,) })), - map(symbol("$"), |x| Primary::Dollar(Dollar { nodes: (x,) })), - map(symbol("null"), |x| Primary::Null(Null { nodes: (x,) })), + map(symbol("this"), |x| Primary::This(x)), + map(symbol("$"), |x| Primary::Dollar(x)), + map(symbol("null"), |x| Primary::Null(x)), ))(s) } pub fn primary_hierarchical(s: Span) -> IResult { - let (s, x) = opt(primary_hierarchical_qualifier)(s)?; - let (s, y) = hierarchical_identifier(s)?; - let (s, z) = select(s)?; + let (s, a) = opt(class_qualifier_or_package_scope)(s)?; + let (s, b) = hierarchical_identifier(s)?; + let (s, c) = select(s)?; Ok(( s, - Primary::Hierarchical(PrimaryHierarchical { nodes: (x, y, z) }), + Primary::Hierarchical(PrimaryHierarchical { nodes: (a, b, c) }), )) } pub fn primary_concatenation(s: Span) -> IResult { - let (s, x) = concatenation(s)?; - let (s, y) = opt(range_expression)(s)?; + let (s, a) = concatenation(s)?; + let (s, b) = opt(bracket2(range_expression))(s)?; Ok(( s, - Primary::Concatenation(PrimaryConcatenation { nodes: (x, y) }), + Primary::Concatenation(PrimaryConcatenation { nodes: (a, b) }), )) } pub fn primary_multiple_concatenation(s: Span) -> IResult { - let (s, x) = multiple_concatenation(s)?; - let (s, y) = opt(range_expression)(s)?; + let (s, a) = multiple_concatenation(s)?; + let (s, b) = opt(bracket2(range_expression))(s)?; Ok(( s, - Primary::MultipleConcatenation(PrimaryMultipleConcatenation { nodes: (x, y) }), + Primary::MultipleConcatenation(PrimaryMultipleConcatenation { nodes: (a, b) }), )) } -pub fn primary_hierarchical_qualifier(s: Span) -> IResult { +pub fn primary_mintypmax_expression(s: Span) -> IResult { + let (s, a) = paren2(mintypmax_expression)(s)?; + Ok(( + s, + Primary::MintypmaxExpression(PrimaryMintypmaxExpression { nodes: a }), + )) +} + +pub fn class_qualifier_or_package_scope(s: Span) -> IResult { alt(( map(class_qualifier, |x| { - PrimaryHierarchicalQualifier::ClassQualifier(x) + ClassQualifierOrPackageScope::ClassQualifier(x) }), map(package_scope, |x| { - PrimaryHierarchicalQualifier::PackageScope(x) + ClassQualifierOrPackageScope::PackageScope(x) }), ))(s) } pub fn class_qualifier(s: Span) -> IResult { - let (s, x) = opt(symbol("local::"))(s)?; - let (s, y) = opt(implicit_class_handle_or_class_scope)(s)?; + let (s, a) = opt(symbol("local::"))(s)?; + let (s, b) = opt(implicit_class_handle_or_class_scope)(s)?; Ok(( s, ClassQualifier { - nodes: (x.map(|_| Local {}), y), + nodes: (a.map(|x| Local { nodes: (x,) }), b), }, )) } @@ -438,24 +495,24 @@ pub fn primary_literal(s: Span) -> IResult { } pub fn time_literal(s: Span) -> IResult { - alt((unsigned_time_literal, fixed_point_time_literal))(s) + alt((time_literal_unsigned, time_literal_fixed_point))(s) } -pub fn unsigned_time_literal(s: Span) -> IResult { - let (s, x) = unsigned_number(s)?; - let (s, y) = time_unit(s)?; +pub fn time_literal_unsigned(s: Span) -> IResult { + let (s, a) = unsigned_number(s)?; + let (s, b) = time_unit(s)?; Ok(( s, - TimeLiteral::UnsignedTimeLiteral(UnsignedTimeLiteral { nodes: (x, y) }), + TimeLiteral::Unsigned(TimeLiteralUnsigned { nodes: (a, b) }), )) } -pub fn fixed_point_time_literal(s: Span) -> IResult { - let (s, x) = fixed_point_number(s)?; - let (s, y) = time_unit(s)?; +pub fn time_literal_fixed_point(s: Span) -> IResult { + let (s, a) = fixed_point_number(s)?; + let (s, b) = time_unit(s)?; Ok(( s, - TimeLiteral::FixedPointTimeLiteral(FixedPointTimeLiteral { nodes: (x, y) }), + TimeLiteral::FixedPoint(TimeLiteralFixedPoint { nodes: (a, b) }), )) } @@ -473,95 +530,83 @@ pub fn time_unit(s: Span) -> IResult { pub fn implicit_class_handle(s: Span) -> IResult { alt(( map( - tuple((symbol("this"), symbol("."), symbol("super"))), - |_| ImplicitClassHandle::ThisSuper, + triple(symbol("this"), symbol("."), symbol("super")), + |(x, y, z)| ImplicitClassHandle::ThisSuper((x, y, z)), ), - map(symbol("this"), |_| ImplicitClassHandle::This), - map(symbol("super"), |_| ImplicitClassHandle::Super), + map(symbol("this"), |x| ImplicitClassHandle::This(x)), + map(symbol("super"), |x| ImplicitClassHandle::Super(x)), ))(s) } pub fn bit_select(s: Span) -> IResult { - let (s, x) = many0(bracket(expression))(s)?; - Ok((s, BitSelect { nodes: (x,) })) + let (s, a) = many0(bracket2(expression))(s)?; + Ok((s, BitSelect { nodes: (a,) })) } pub fn select(s: Span) -> IResult { - let (s, x) = opt(pair( - many0(preceded(symbol("."), pair(member_identifier, bit_select))), - preceded(symbol("."), member_identifier), + let (s, a) = opt(triple( + many0(triple(symbol("."), member_identifier, bit_select)), + symbol("."), + member_identifier, ))(s)?; - let (s, y) = bit_select(s)?; - let (s, z) = opt(bracket(part_select_range))(s)?; - - let x = if let Some((x, y)) = x { - Some(SelectMember { nodes: (x, y) }) - } else { - None - }; - - Ok((s, Select { nodes: (x, y, z) })) + let (s, b) = bit_select(s)?; + let (s, c) = opt(bracket2(part_select_range))(s)?; + Ok((s, Select { nodes: (a, b, c) })) } -pub fn nonrange_select(s: Span) -> IResult { - let (s, x) = opt(pair( - many0(preceded(symbol("."), pair(member_identifier, bit_select))), - preceded(symbol("."), member_identifier), +pub fn nonrange_select(s: Span) -> IResult { + let (s, a) = opt(triple( + many0(triple(symbol("."), member_identifier, bit_select)), + symbol("."), + member_identifier, ))(s)?; - let (s, y) = bit_select(s)?; + let (s, b) = bit_select(s)?; + Ok((s, NonrangeSelect { nodes: (a, b) })) +} - let x = if let Some((x, y)) = x { - Some(SelectMember { nodes: (x, y) }) - } else { - None - }; +pub fn constant_bit_select(s: Span) -> IResult { + let (s, a) = many0(bracket2(constant_expression))(s)?; + Ok((s, ConstantBitSelect { nodes: (a,) })) +} +pub fn constant_select(s: Span) -> IResult { + let (s, a) = opt(triple( + many0(triple(symbol("."), member_identifier, constant_bit_select)), + symbol("."), + member_identifier, + ))(s)?; + let (s, b) = constant_bit_select(s)?; + let (s, c) = opt(bracket2(constant_part_select_range))(s)?; + Ok((s, ConstantSelect { nodes: (a, b, c) })) +} + +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, - Select { - nodes: (x, y, None), + ConstantCast { + nodes: (a, b, c, d, e), }, )) } -pub fn constant_bit_select(s: Span) -> IResult { - let (s, x) = many0(bracket(constant_expression))(s)?; - Ok((s, ConstantBitSelect { nodes: (x,) })) -} - -pub fn constant_select(s: Span) -> IResult { - let (s, x) = opt(pair( - many0(preceded(symbol("."), pair(member_identifier, bit_select))), - preceded(symbol("."), member_identifier), - ))(s)?; - let (s, y) = constant_bit_select(s)?; - let (s, z) = opt(bracket(constant_part_select_range))(s)?; - - let x = if let Some((x, y)) = x { - Some(SelectMember { nodes: (x, y) }) - } else { - None - }; - - Ok((s, ConstantSelect { nodes: (x, y, z) })) -} - -pub fn constant_cast(s: Span) -> IResult { - let (s, x) = casting_type(s)?; - let (s, _) = symbol("'")(s)?; - let (s, y) = paren(constant_expression)(s)?; - Ok((s, ConstantCast { nodes: (x, y) })) -} - -pub fn constant_let_expression(s: Span) -> IResult { - let_expression(s) +pub fn constant_let_expression(s: Span) -> IResult { + let (s, a) = let_expression(s)?; + Ok((s, ConstantLetExpression { nodes: (a,) })) } pub fn cast(s: Span) -> IResult { - let (s, x) = casting_type(s)?; - let (s, _) = symbol("'")(s)?; - let (s, y) = paren(expression)(s)?; - Ok((s, Cast { nodes: (x, y) })) + 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), + }, + )) } // ----------------------------------------------------------------------------- diff --git a/src/parser/expressions/strings.rs b/src/parser/expressions/strings.rs index 1540b78..723525c 100644 --- a/src/parser/expressions/strings.rs +++ b/src/parser/expressions/strings.rs @@ -20,12 +20,12 @@ pub fn string_literal(s: Span) -> IResult { } pub fn string_literal_impl(s: Span) -> IResult { - let (s, _) = tag("\"")(s)?; - let (s, x) = many1(pair(is_not("\\\""), opt(pair(tag("\\"), take(1usize)))))(s)?; - let (s, _) = tag("\"")(s)?; + let (s, a) = tag("\"")(s)?; + let (s, b) = many1(pair(is_not("\\\""), opt(pair(tag("\\"), take(1usize)))))(s)?; + let (s, c) = tag("\"")(s)?; let mut ret = None; - for (x, y) in x { + for (x, y) in b { ret = if let Some(ret) = ret { Some(concat(ret, x).unwrap()) } else { @@ -45,9 +45,11 @@ pub fn string_literal_impl(s: Span) -> IResult { } } - let ret = ret.unwrap(); + let b = ret.unwrap(); + let a = concat(a, b).unwrap(); + let a = concat(a, c).unwrap(); - Ok((s, ret)) + Ok((s, a)) } // ----------------------------------------------------------------------------- diff --git a/src/parser/expressions/subroutine_calls.rs b/src/parser/expressions/subroutine_calls.rs index eb386a4..e23929d 100644 --- a/src/parser/expressions/subroutine_calls.rs +++ b/src/parser/expressions/subroutine_calls.rs @@ -7,6 +7,11 @@ use nom::IResult; // ----------------------------------------------------------------------------- +#[derive(Debug)] +pub struct ConstantFunctionCall<'a> { + pub nodes: (FunctionSubroutineCall<'a>,), +} + #[derive(Debug)] pub struct TfCall<'a> { pub nodes: ( @@ -36,6 +41,11 @@ pub enum SubroutineCall<'a> { StdRandomize(Box>), } +#[derive(Debug)] +pub struct FunctionSubroutineCall<'a> { + pub nodes: (SubroutineCall<'a>,), +} + #[derive(Debug)] pub struct ListOfArguments<'a> { pub nodes: ( @@ -49,17 +59,10 @@ pub struct MethodCall<'a> { pub nodes: (MethodCallRoot<'a>, MethodCallBody<'a>), } -#[derive(Debug)] -pub enum MethodCallRoot<'a> { - Primary(Primary<'a>), - ImplicitClassHandle(ImplicitClassHandle), -} - #[derive(Debug)] pub enum MethodCallBody<'a> { User(MethodCallBodyUser<'a>), - Array(ArrayManipulationCall<'a>), - Randomize(RandomizeCall<'a>), + BuiltInMethodCall(BuiltInMethodCall<'a>), } #[derive(Debug)] @@ -71,6 +74,12 @@ pub struct MethodCallBodyUser<'a> { ), } +#[derive(Debug)] +pub enum BuiltInMethodCall<'a> { + ArrayManipulationCall(ArrayManipulationCall<'a>), + RandomizeCall(RandomizeCall<'a>), +} + #[derive(Debug)] pub struct ArrayManipulationCall<'a> { pub nodes: ( @@ -91,6 +100,12 @@ pub struct RandomizeCall<'a> { ), } +#[derive(Debug)] +pub enum MethodCallRoot<'a> { + Primary(Primary<'a>), + ImplicitClassHandle(ImplicitClassHandle<'a>), +} + #[derive(Debug)] pub enum ArrayMethodName<'a> { MethodIdentifier(MethodIdentifier<'a>), @@ -102,8 +117,9 @@ pub enum ArrayMethodName<'a> { // ----------------------------------------------------------------------------- -pub fn constant_function_call(s: Span) -> IResult { - function_subroutine_call(s) +pub fn constant_function_call(s: Span) -> IResult { + let (s, a) = function_subroutine_call(s)?; + Ok((s, ConstantFunctionCall { nodes: (a,) })) } pub fn tf_call(s: Span) -> IResult { @@ -167,15 +183,15 @@ pub fn subroutine_call(s: Span) -> IResult { map(system_tf_call, |x| SubroutineCall::SystemTf(Box::new(x))), map(method_call, |x| SubroutineCall::Method(Box::new(x))), map( - tuple((symbol("std"), symbol("::"), randomize_call)), + triple(symbol("std"), symbol("::"), randomize_call), |(_, _, x)| SubroutineCall::StdRandomize(Box::new(x)), ), map(randomize_call, |x| SubroutineCall::Randomize(Box::new(x))), ))(s) } -pub fn function_subroutine_call(s: Span) -> IResult { - subroutine_call(s) +pub fn function_subroutine_call(s: Span) -> IResult { + map(subroutine_call, |x| FunctionSubroutineCall { nodes: (x,) })(s) } pub fn list_of_arguments(s: Span) -> IResult { @@ -196,7 +212,12 @@ pub fn method_call(s: Span) -> IResult { } pub fn method_call_body(s: Span) -> IResult { - alt((method_call_body_user, built_in_method_call))(s) + alt(( + method_call_body_user, + map(built_in_method_call, |x| { + MethodCallBody::BuiltInMethodCall(x) + }), + ))(s) } pub fn method_call_body_user(s: Span) -> IResult { @@ -209,10 +230,12 @@ pub fn method_call_body_user(s: Span) -> IResult { )) } -pub fn built_in_method_call(s: Span) -> IResult { +pub fn built_in_method_call(s: Span) -> IResult { alt(( - map(array_manipulation_call, |x| MethodCallBody::Array(x)), - map(randomize_call, |x| MethodCallBody::Randomize(x)), + map(array_manipulation_call, |x| { + BuiltInMethodCall::ArrayManipulationCall(x) + }), + map(randomize_call, |x| BuiltInMethodCall::RandomizeCall(x)), ))(s) } @@ -238,11 +261,11 @@ pub fn randomize_call(s: Span) -> IResult { nodes: (vec![],), }), )))))(s)?; - let (s, z) = opt(tuple(( + let (s, z) = opt(triple( symbol("with"), opt(paren(opt(identifier_list))), constraint_block, - )))(s)?; + ))(s)?; let y = if let Some(Some(y)) = y { y } else { diff --git a/src/parser/general/attributes.rs b/src/parser/general/attributes.rs index 392f6cd..84a65c3 100644 --- a/src/parser/general/attributes.rs +++ b/src/parser/general/attributes.rs @@ -8,27 +8,38 @@ use nom::IResult; #[derive(Debug)] pub struct AttributeInstance<'a> { - pub nodes: (Vec>,), + pub nodes: ( + Symbol<'a>, + AttrSpec<'a>, + Vec<(Symbol<'a>, AttrSpec<'a>)>, + Symbol<'a>, + ), } #[derive(Debug)] pub struct AttrSpec<'a> { - pub nodes: (Identifier<'a>, Option>), + pub nodes: (Identifier<'a>, Option<(Symbol<'a>, ConstantExpression<'a>)>), } // ----------------------------------------------------------------------------- pub fn attribute_instance(s: Span) -> IResult { - let (s, _) = symbol("(*")(s)?; - let (s, x) = separated_nonempty_list(symbol(","), attr_spec)(s)?; - let (s, _) = symbol("*)")(s)?; - Ok((s, AttributeInstance { nodes: (x,) })) + 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), + }, + )) } pub fn attr_spec(s: Span) -> IResult { - let (s, x) = identifier(s)?; - let (s, y) = opt(preceded(symbol("="), constant_expression))(s)?; - Ok((s, AttrSpec { nodes: (x, y) })) + let (s, a) = identifier(s)?; + let (s, b) = opt(pair(symbol("="), constant_expression))(s)?; + Ok((s, AttrSpec { nodes: (a, b) })) } // ----------------------------------------------------------------------------- diff --git a/src/parser/general/comments.rs b/src/parser/general/comments.rs index 9093a69..ce7e5f0 100644 --- a/src/parser/general/comments.rs +++ b/src/parser/general/comments.rs @@ -17,19 +17,19 @@ pub fn comment(s: Span) -> IResult { } pub fn one_line_comment(s: Span) -> IResult { - let (s, x) = tag("//")(s)?; - let (s, y) = is_not("\n")(s)?; - let x = concat(x, y).unwrap(); - Ok((s, Comment { nodes: (x,) })) + let (s, a) = tag("//")(s)?; + let (s, b) = is_not("\n")(s)?; + let a = concat(a, b).unwrap(); + Ok((s, Comment { nodes: (a,) })) } pub fn block_comment(s: Span) -> IResult { - let (s, x) = tag("/*")(s)?; - let (s, y) = is_not("*/")(s)?; - let (s, z) = tag("*/")(s)?; - let x = concat(x, y).unwrap(); - let x = concat(x, z).unwrap(); - Ok((s, Comment { nodes: (x,) })) + let (s, a) = tag("/*")(s)?; + let (s, b) = is_not("*/")(s)?; + let (s, c) = tag("*/")(s)?; + let a = concat(a, b).unwrap(); + let a = concat(a, c).unwrap(); + Ok((s, Comment { nodes: (a,) })) } // ----------------------------------------------------------------------------- diff --git a/src/parser/general/identifiers.rs b/src/parser/general/identifiers.rs index 632650c..1e7f601 100644 --- a/src/parser/general/identifiers.rs +++ b/src/parser/general/identifiers.rs @@ -497,20 +497,20 @@ pub struct VariableIdentifier<'a> { #[derive(Debug)] pub enum ImplicitClassHandleOrClassScopeOrPackageScope<'a> { - ImplicitClassHandle(ImplicitClassHandle), + ImplicitClassHandle(ImplicitClassHandle<'a>), ClassScope(ClassScope<'a>), PackageScope(PackageScope<'a>), } #[derive(Debug)] pub enum ImplicitClassHandleOrPackageScope<'a> { - ImplicitClassHandle(ImplicitClassHandle), + ImplicitClassHandle(ImplicitClassHandle<'a>), PackageScope(PackageScope<'a>), } #[derive(Debug)] pub enum ImplicitClassHandleOrClassScope<'a> { - ImplicitClassHandle(ImplicitClassHandle), + ImplicitClassHandle(ImplicitClassHandle<'a>), ClassScope(ClassScope<'a>), } diff --git a/src/parser/instantiations/checker_instantiation.rs b/src/parser/instantiations/checker_instantiation.rs index 5cdd620..acfce25 100644 --- a/src/parser/instantiations/checker_instantiation.rs +++ b/src/parser/instantiations/checker_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,14 +12,33 @@ pub struct CheckerInstantiation<'a> { pub nodes: ( PsCheckerIdentifier<'a>, NameOfInstance<'a>, + Symbol<'a>, Option>, + Symbol<'a>, + Symbol<'a>, ), } #[derive(Debug)] pub enum ListOfCheckerPortConnections<'a> { - Ordered(Vec>), - Named(Vec>), + Ordered(ListOfCheckerPortConnectionsOrdered<'a>), + Named(ListOfCheckerPortConnectionsNamed<'a>), +} + +#[derive(Debug)] +pub struct ListOfCheckerPortConnectionsOrdered<'a> { + pub nodes: ( + OrderedCheckerPortConnection<'a>, + Vec<(Symbol<'a>, OrderedCheckerPortConnection<'a>)>, + ), +} + +#[derive(Debug)] +pub struct ListOfCheckerPortConnectionsNamed<'a> { + pub nodes: ( + NamedCheckerPortConnection<'a>, + Vec<(Symbol<'a>, NamedCheckerPortConnection<'a>)>, + ), } #[derive(Debug)] @@ -36,39 +56,63 @@ pub enum NamedCheckerPortConnection<'a> { pub struct NamedCheckerPortConnectionIdentifier<'a> { pub nodes: ( Vec>, + Symbol<'a>, FormalPortIdentifier<'a>, - Option>, + Option<(Symbol<'a>, Option>, Symbol<'a>)>, ), } #[derive(Debug)] pub struct NamedCheckerPortConnectionAsterisk<'a> { - pub nodes: (Vec>,), + pub nodes: (Vec>, Symbol<'a>), } // ----------------------------------------------------------------------------- pub fn checker_instantiation(s: Span) -> IResult { - let (s, x) = ps_checker_identifier(s)?; - let (s, y) = name_of_instance(s)?; - let (s, z) = paren(opt(list_of_checker_port_connections))(s)?; - let (s, _) = symbol(";")(s)?; - Ok((s, CheckerInstantiation { nodes: (x, y, z) })) + 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)?; + Ok(( + s, + CheckerInstantiation { + nodes: (a, b, c, d, e, f), + }, + )) } pub fn list_of_checker_port_connections(s: Span) -> IResult { alt(( - map( - separated_nonempty_list(symbol(","), ordered_checker_port_connection), - |x| ListOfCheckerPortConnections::Ordered(x), - ), - map( - separated_nonempty_list(symbol(","), named_checker_port_connection), - |x| ListOfCheckerPortConnections::Named(x), - ), + list_of_checker_port_connections_ordered, + list_of_checker_port_connections_named, ))(s) } +pub fn list_of_checker_port_connections_ordered( + s: Span, +) -> IResult { + let (s, a) = ordered_checker_port_connection(s)?; + let (s, b) = many0(pair(symbol(","), ordered_checker_port_connection))(s)?; + Ok(( + s, + ListOfCheckerPortConnections::Ordered(ListOfCheckerPortConnectionsOrdered { + nodes: (a, b), + }), + )) +} + +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)?; + Ok(( + s, + ListOfCheckerPortConnections::Named(ListOfCheckerPortConnectionsNamed { nodes: (a, b) }), + )) +} + pub fn ordered_checker_port_connection(s: Span) -> IResult { let (s, x) = many0(attribute_instance)(s)?; let (s, y) = opt(property_actual_arg)(s)?; @@ -85,15 +129,14 @@ pub fn named_checker_port_connection(s: Span) -> IResult IResult { - let (s, x) = many0(attribute_instance)(s)?; - let (s, _) = symbol(".")(s)?; - let (s, y) = formal_port_identifier(s)?; - let (s, z) = opt(paren(opt(property_actual_arg)))(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) = formal_port_identifier(s)?; + let (s, d) = opt(paren2(opt(property_actual_arg)))(s)?; Ok(( s, NamedCheckerPortConnection::Identifier(NamedCheckerPortConnectionIdentifier { - nodes: (x, y, z), + nodes: (a, b, c, d), }), )) } @@ -101,12 +144,11 @@ pub fn named_checker_port_connection_identifier( pub fn named_checker_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, - NamedCheckerPortConnection::Asterisk(NamedCheckerPortConnectionAsterisk { nodes: (x,) }), + NamedCheckerPortConnection::Asterisk(NamedCheckerPortConnectionAsterisk { nodes: (a, b) }), )) } diff --git a/src/parser/instantiations/generated_instantiation.rs b/src/parser/instantiations/generated_instantiation.rs index 70d7e71..8864549 100644 --- a/src/parser/instantiations/generated_instantiation.rs +++ b/src/parser/instantiations/generated_instantiation.rs @@ -16,7 +16,7 @@ pub struct GenerateRegion<'a> { pub struct LoopGenerateConstruct<'a> { pub nodes: ( GenvarInitialization<'a>, - ConstantExpression<'a>, + GenvarExpression<'a>, GenvarIteration<'a>, GenerateBlock<'a>, ), @@ -42,7 +42,7 @@ pub struct GenvarIterationAssignment<'a> { pub nodes: ( GenvarIdentifier<'a>, AssignmentOperator<'a>, - ConstantExpression<'a>, + GenvarExpression<'a>, ), } diff --git a/src/parser/instantiations/interface_instantiation.rs b/src/parser/instantiations/interface_instantiation.rs index ca04986..cc995db 100644 --- a/src/parser/instantiations/interface_instantiation.rs +++ b/src/parser/instantiations/interface_instantiation.rs @@ -1,6 +1,7 @@ use crate::parser::*; use nom::combinator::*; use nom::multi::*; +use nom::sequence::*; use nom::IResult; // ----------------------------------------------------------------------------- @@ -10,18 +11,26 @@ pub struct InterfaceInstantiation<'a> { pub nodes: ( InterfaceIdentifier<'a>, Option>, - Vec>, + HierarchicalInstance<'a>, + Vec<(Symbol<'a>, HierarchicalInstance<'a>)>, + Symbol<'a>, ), } // ----------------------------------------------------------------------------- pub fn interface_instantiation(s: Span) -> IResult { - let (s, x) = interface_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, InterfaceInstantiation { nodes: (x, y, z) })) + 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)?; + Ok(( + s, + InterfaceInstantiation { + nodes: (a, b, c, d, e), + }, + )) } // ----------------------------------------------------------------------------- diff --git a/src/parser/instantiations/program_instantiation.rs b/src/parser/instantiations/program_instantiation.rs index 97ab812..fd1e1cd 100644 --- a/src/parser/instantiations/program_instantiation.rs +++ b/src/parser/instantiations/program_instantiation.rs @@ -1,6 +1,7 @@ use crate::parser::*; use nom::combinator::*; use nom::multi::*; +use nom::sequence::*; use nom::IResult; // ----------------------------------------------------------------------------- @@ -10,18 +11,26 @@ pub struct ProgramInstantiation<'a> { pub nodes: ( ProgramIdentifier<'a>, Option>, - Vec>, + HierarchicalInstance<'a>, + Vec<(Symbol<'a>, HierarchicalInstance<'a>)>, + Symbol<'a>, ), } // ----------------------------------------------------------------------------- pub fn program_instantiation(s: Span) -> IResult { - let (s, x) = program_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, ProgramInstantiation { nodes: (x, y, z) })) + 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)?; + Ok(( + s, + ProgramInstantiation { + nodes: (a, b, c, d, e), + }, + )) } // ----------------------------------------------------------------------------- diff --git a/src/parser/source_text/class_items.rs b/src/parser/source_text/class_items.rs index 4d3ec47..043e05a 100644 --- a/src/parser/source_text/class_items.rs +++ b/src/parser/source_text/class_items.rs @@ -200,6 +200,10 @@ pub fn method_qualifier(s: Span) -> IResult { Err(Err::Error(make_error(s, ErrorKind::Fix))) } +pub fn method_prototype(s: Span) -> IResult { + Err(Err::Error(make_error(s, ErrorKind::Fix))) +} + pub fn class_constructor_declaration(s: Span) -> IResult { Err(Err::Error(make_error(s, ErrorKind::Fix))) } diff --git a/src/parser/source_text/library_source_text.rs b/src/parser/source_text/library_source_text.rs index 28eb2c3..f368c0b 100644 --- a/src/parser/source_text/library_source_text.rs +++ b/src/parser/source_text/library_source_text.rs @@ -73,11 +73,11 @@ pub fn library_declaration(s: Span) -> IResult { 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(tuple(( + let (s, e) = opt(triple( symbol("-incdir"), file_path_spec, many0(pair(symbol(","), file_path_spec)), - )))(s)?; + ))(s)?; let (s, f) = symbol(";")(s)?; Ok(( s, @@ -94,6 +94,7 @@ pub fn include_statement(s: Span) -> IResult { Ok((s, IncludeStatement { nodes: (a, b, c) })) } +//TODO support non literal path pub fn file_path_spec(s: Span) -> IResult { let (s, a) = string_literal(s)?; Ok((s, FilePathSpec { nodes: (a,) })) diff --git a/src/parser/source_text/system_verilog_source_text.rs b/src/parser/source_text/system_verilog_source_text.rs index 68a134d..a62c37c 100644 --- a/src/parser/source_text/system_verilog_source_text.rs +++ b/src/parser/source_text/system_verilog_source_text.rs @@ -1,8 +1,9 @@ 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,12 +38,13 @@ pub struct DescriptionBindDirective<'a> { pub struct ModuleNonansiHeader<'a> { pub nodes: ( Vec>, - ModuleKeyword, - Option, + ModuleKeyword<'a>, + Option>, ModuleIdentifier<'a>, Vec>, Option>, ListOfPorts<'a>, + Symbol<'a>, ), } @@ -50,12 +52,13 @@ pub struct ModuleNonansiHeader<'a> { pub struct ModuleAnsiHeader<'a> { pub nodes: ( Vec>, - ModuleKeyword, - Option, + ModuleKeyword<'a>, + Option>, ModuleIdentifier<'a>, Vec>, Option>, Option>, + Symbol<'a>, ), } @@ -74,7 +77,8 @@ pub struct ModuleDeclarationNonansi<'a> { ModuleNonansiHeader<'a>, Option>, Vec>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, ModuleIdentifier<'a>)>, ), } @@ -84,7 +88,8 @@ pub struct ModuleDeclarationAnsi<'a> { ModuleAnsiHeader<'a>, Option>, Vec>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, ModuleIdentifier<'a>)>, ), } @@ -92,29 +97,34 @@ pub struct ModuleDeclarationAnsi<'a> { pub struct ModuleDeclarationWildcard<'a> { pub nodes: ( Vec>, - ModuleKeyword, - Option, + ModuleKeyword<'a>, + Option>, ModuleIdentifier<'a>, + Symbol<'a>, + Symbol<'a>, + Symbol<'a>, + Symbol<'a>, Option>, Vec>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, ModuleIdentifier<'a>)>, ), } #[derive(Debug)] pub struct ModuleDeclarationExternNonansi<'a> { - pub nodes: (ModuleNonansiHeader<'a>,), + pub nodes: (Symbol<'a>, ModuleNonansiHeader<'a>), } #[derive(Debug)] pub struct ModuleDeclarationExternAnsi<'a> { - pub nodes: (ModuleAnsiHeader<'a>,), + pub nodes: (Symbol<'a>, ModuleAnsiHeader<'a>), } #[derive(Debug)] -pub enum ModuleKeyword { - Module, - Macromodule, +pub enum ModuleKeyword<'a> { + Module(Symbol<'a>), + Macromodule(Symbol<'a>), } #[derive(Debug)] @@ -132,7 +142,8 @@ pub struct InterfaceDeclarationNonansi<'a> { InterfaceNonansiHeader<'a>, Option>, Vec>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, InterfaceIdentifier<'a>)>, ), } @@ -142,7 +153,8 @@ pub struct InterfaceDeclarationAnsi<'a> { InterfaceAnsiHeader<'a>, Option>, Vec>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, InterfaceIdentifier<'a>)>, ), } @@ -150,32 +162,41 @@ pub struct InterfaceDeclarationAnsi<'a> { pub struct InterfaceDeclarationWildcard<'a> { pub nodes: ( Vec>, + Symbol<'a>, + Option>, InterfaceIdentifier<'a>, + Symbol<'a>, + Symbol<'a>, + Symbol<'a>, + Symbol<'a>, Option>, Vec>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, InterfaceIdentifier<'a>)>, ), } #[derive(Debug)] pub struct InterfaceDeclarationExternNonansi<'a> { - pub nodes: (InterfaceNonansiHeader<'a>,), + pub nodes: (Symbol<'a>, InterfaceNonansiHeader<'a>), } #[derive(Debug)] pub struct InterfaceDeclarationExternAnsi<'a> { - pub nodes: (InterfaceAnsiHeader<'a>,), + pub nodes: (Symbol<'a>, InterfaceAnsiHeader<'a>), } #[derive(Debug)] pub struct InterfaceNonansiHeader<'a> { pub nodes: ( Vec>, - Option, + Symbol<'a>, + Option>, InterfaceIdentifier<'a>, Vec>, Option>, ListOfPorts<'a>, + Symbol<'a>, ), } @@ -183,11 +204,13 @@ pub struct InterfaceNonansiHeader<'a> { pub struct InterfaceAnsiHeader<'a> { pub nodes: ( Vec>, - Option, + Symbol<'a>, + Option>, InterfaceIdentifier<'a>, Vec>, Option>, Option>, + Symbol<'a>, ), } @@ -206,7 +229,8 @@ pub struct ProgramDeclarationNonansi<'a> { ProgramNonansiHeader<'a>, Option>, Vec>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, ProgramIdentifier<'a>)>, ), } @@ -216,7 +240,8 @@ pub struct ProgramDeclarationAnsi<'a> { ProgramAnsiHeader<'a>, Option>, Vec>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, ProgramIdentifier<'a>)>, ), } @@ -224,32 +249,40 @@ pub struct ProgramDeclarationAnsi<'a> { pub struct ProgramDeclarationWildcard<'a> { pub nodes: ( Vec>, + Symbol<'a>, ProgramIdentifier<'a>, + Symbol<'a>, + Symbol<'a>, + Symbol<'a>, + Symbol<'a>, Option>, Vec>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, ProgramIdentifier<'a>)>, ), } #[derive(Debug)] pub struct ProgramDeclarationExternNonansi<'a> { - pub nodes: (ProgramNonansiHeader<'a>,), + pub nodes: (Symbol<'a>, ProgramNonansiHeader<'a>), } #[derive(Debug)] pub struct ProgramDeclarationExternAnsi<'a> { - pub nodes: (ProgramAnsiHeader<'a>,), + pub nodes: (Symbol<'a>, ProgramAnsiHeader<'a>), } #[derive(Debug)] pub struct ProgramNonansiHeader<'a> { pub nodes: ( Vec>, - Option, + Symbol<'a>, + Option>, ProgramIdentifier<'a>, Vec>, Option>, ListOfPorts<'a>, + Symbol<'a>, ), } @@ -257,40 +290,58 @@ pub struct ProgramNonansiHeader<'a> { pub struct ProgramAnsiHeader<'a> { pub nodes: ( Vec>, - Option, + Symbol<'a>, + Option>, ProgramIdentifier<'a>, Vec>, Option>, Option>, + Symbol<'a>, ), } #[derive(Debug)] pub struct CheckerDeclaration<'a> { pub nodes: ( + Symbol<'a>, CheckerIdentifier<'a>, - Option>, + Option<(Symbol<'a>, Option>, Symbol<'a>)>, + Symbol<'a>, Vec<(Vec>, CheckerOrGenerateItem<'a>)>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, CheckerIdentifier<'a>)>, ), } #[derive(Debug)] pub struct ClassDeclaration<'a> { pub nodes: ( - Option, - Option, + Option>, + Symbol<'a>, + Option>, ClassIdentifier<'a>, Option>, - Option<(ClassType<'a>, Option>)>, - Option>>, + Option<( + Symbol<'a>, + ClassType<'a>, + Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>, + )>, + Option<( + Symbol<'a>, + InterfaceClassType<'a>, + Vec<(Symbol<'a>, InterfaceClassType<'a>)>, + )>, + Symbol<'a>, Vec>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, ClassIdentifier<'a>)>, ), } #[derive(Debug)] -pub struct Virtual {} +pub struct Virtual<'a> { + pub nodes: (Symbol<'a>,), +} #[derive(Debug)] pub struct InterfaceClassType<'a> { @@ -300,11 +351,19 @@ pub struct InterfaceClassType<'a> { #[derive(Debug)] pub struct InterfaceClassDeclaration<'a> { pub nodes: ( + Symbol<'a>, + Symbol<'a>, ClassIdentifier<'a>, Option>, - Option>>, + Option<( + Symbol<'a>, + InterfaceClassType<'a>, + Vec<(Symbol<'a>, InterfaceClassType<'a>)>, + )>, + Symbol<'a>, Vec>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, ClassIdentifier<'a>)>, ), } @@ -312,9 +371,9 @@ pub struct InterfaceClassDeclaration<'a> { pub enum InterfaceClassItem<'a> { TypeDeclaration(TypeDeclaration<'a>), Method(InterfaceClassItemMethod<'a>), - LocalParameterDeclaration(LocalParameterDeclaration<'a>), - ParameterDeclaration(ParameterDeclaration<'a>), - Empty, + LocalParameterDeclaration((LocalParameterDeclaration<'a>, Symbol<'a>)), + ParameterDeclaration((ParameterDeclaration<'a>, Symbol<'a>)), + Null(Symbol<'a>), } #[derive(Debug)] @@ -324,18 +383,21 @@ pub struct InterfaceClassItemMethod<'a> { #[derive(Debug)] pub struct InterfaceClassMethod<'a> { - pub nodes: (MethodPrototype<'a>,), + pub nodes: (Symbol<'a>, Symbol<'a>, MethodPrototype<'a>, Symbol<'a>), } #[derive(Debug)] pub struct PackageDeclaration<'a> { pub nodes: ( Vec>, - Option, + Symbol<'a>, + Option>, PackageIdentifier<'a>, + Symbol<'a>, Option>, Vec<(Vec>, PackageItem<'a>)>, - Option>, + Symbol<'a>, + Option<(Symbol<'a>, PackageIdentifier<'a>)>, ), } @@ -349,102 +411,635 @@ pub enum TimeunitsDeclaration<'a> { #[derive(Debug)] pub struct TimeunitsDeclarationTimeunit<'a> { - pub nodes: (TimeLiteral<'a>, Option>), + pub nodes: ( + Symbol<'a>, + TimeLiteral<'a>, + Option<(Symbol<'a>, TimeLiteral<'a>)>, + Symbol<'a>, + ), } #[derive(Debug)] pub struct TimeunitsDeclarationTimeprecision<'a> { - pub nodes: (TimeLiteral<'a>,), + pub nodes: (Symbol<'a>, TimeLiteral<'a>, Symbol<'a>), } #[derive(Debug)] pub struct TimeunitsDeclarationTimeunitTimeprecision<'a> { - pub nodes: (TimeLiteral<'a>, TimeLiteral<'a>), + pub nodes: ( + Symbol<'a>, + TimeLiteral<'a>, + Symbol<'a>, + Symbol<'a>, + TimeLiteral<'a>, + Symbol<'a>, + ), } #[derive(Debug)] pub struct TimeunitsDeclarationTimeprecisionTimeunit<'a> { - pub nodes: (TimeLiteral<'a>, TimeLiteral<'a>), + pub nodes: ( + Symbol<'a>, + TimeLiteral<'a>, + Symbol<'a>, + Symbol<'a>, + TimeLiteral<'a>, + Symbol<'a>, + ), } // ----------------------------------------------------------------------------- pub fn source_text(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = opt(timeunits_declaration)(s)?; + let (s, b) = many0(description)(s)?; + Ok((s, SourceText { nodes: (a, b) })) } pub fn description(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(module_declaration, |x| Description::ModuleDeclaration(x)), + map(udp_declaration, |x| Description::UdpDeclaration(x)), + map(interface_declaration, |x| { + Description::InterfaceDeclaration(x) + }), + map(program_declaration, |x| Description::ProgramDeclaration(x)), + map(package_declaration, |x| Description::PackageDeclaration(x)), + description_package_item, + description_bind_directive, + map(config_declaration, |x| Description::ConfigDeclaration(x)), + ))(s) +} + +pub fn description_package_item(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = package_item(s)?; + Ok(( + s, + Description::PackageItem(DescriptionPackageItem { nodes: (a, b) }), + )) +} + +pub fn description_bind_directive(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = bind_directive(s)?; + Ok(( + s, + Description::BindDirective(DescriptionBindDirective { nodes: (a, b) }), + )) } pub fn module_nonansi_header(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = module_keyword(s)?; + let (s, c) = opt(lifetime)(s)?; + let (s, d) = module_identifier(s)?; + let (s, e) = many0(package_import_declaration)(s)?; + let (s, f) = opt(parameter_port_list)(s)?; + let (s, g) = list_of_ports(s)?; + let (s, h) = symbol(";")(s)?; + Ok(( + s, + ModuleNonansiHeader { + nodes: (a, b, c, d, e, f, g, h), + }, + )) } pub fn module_ansi_header(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = module_keyword(s)?; + let (s, c) = opt(lifetime)(s)?; + let (s, d) = module_identifier(s)?; + let (s, e) = many0(package_import_declaration)(s)?; + let (s, f) = opt(parameter_port_list)(s)?; + let (s, g) = opt(list_of_port_declarations)(s)?; + let (s, h) = symbol(";")(s)?; + Ok(( + s, + ModuleAnsiHeader { + nodes: (a, b, c, d, e, f, g, h), + }, + )) } pub fn module_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + module_declaration_nonansi, + module_declaration_ansi, + module_declaration_wildcard, + module_declaration_extern_nonansi, + module_declaration_extern_ansi, + ))(s) +} + +pub fn module_declaration_nonansi(s: Span) -> IResult { + let (s, a) = module_nonansi_header(s)?; + let (s, b) = opt(timeunits_declaration)(s)?; + let (s, c) = many0(module_item)(s)?; + let (s, d) = symbol("endmodule")(s)?; + let (s, e) = opt(pair(symbol(":"), module_identifier))(s)?; + Ok(( + s, + ModuleDeclaration::Nonansi(ModuleDeclarationNonansi { + nodes: (a, b, c, d, e), + }), + )) +} + +pub fn module_declaration_ansi(s: Span) -> IResult { + let (s, a) = module_ansi_header(s)?; + let (s, b) = opt(timeunits_declaration)(s)?; + let (s, c) = many0(non_port_module_item)(s)?; + let (s, d) = symbol("endmodule")(s)?; + let (s, e) = opt(pair(symbol(":"), module_identifier))(s)?; + Ok(( + s, + ModuleDeclaration::Ansi(ModuleDeclarationAnsi { + nodes: (a, b, c, d, e), + }), + )) +} + +pub fn module_declaration_wildcard(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + 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)?; + Ok(( + s, + ModuleDeclaration::Wildcard(ModuleDeclarationWildcard { + nodes: (a, b, c, d, e, f, g, h, i, j, k, l), + }), + )) +} + +pub fn module_declaration_extern_nonansi(s: Span) -> IResult { + let (s, a) = symbol("extern")(s)?; + let (s, b) = module_nonansi_header(s)?; + Ok(( + s, + ModuleDeclaration::ExternNonansi(ModuleDeclarationExternNonansi { nodes: (a, b) }), + )) +} + +pub fn module_declaration_extern_ansi(s: Span) -> IResult { + let (s, a) = symbol("extern")(s)?; + let (s, b) = module_ansi_header(s)?; + Ok(( + s, + ModuleDeclaration::ExternAnsi(ModuleDeclarationExternAnsi { nodes: (a, b) }), + )) } pub fn module_keyword(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("module"), |x| ModuleKeyword::Module(x)), + map(symbol("macromodule"), |x| ModuleKeyword::Macromodule(x)), + ))(s) } pub fn interface_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + interface_declaration_nonansi, + interface_declaration_ansi, + interface_declaration_wildcard, + interface_declaration_extern_nonansi, + interface_declaration_extern_ansi, + ))(s) +} + +pub fn interface_declaration_nonansi(s: Span) -> IResult { + let (s, a) = interface_nonansi_header(s)?; + let (s, b) = opt(timeunits_declaration)(s)?; + let (s, c) = many0(interface_item)(s)?; + let (s, d) = symbol("endinterface")(s)?; + let (s, e) = opt(pair(symbol(":"), interface_identifier))(s)?; + Ok(( + s, + InterfaceDeclaration::Nonansi(InterfaceDeclarationNonansi { + nodes: (a, b, c, d, e), + }), + )) +} + +pub fn interface_declaration_ansi(s: Span) -> IResult { + let (s, a) = interface_ansi_header(s)?; + let (s, b) = opt(timeunits_declaration)(s)?; + let (s, c) = many0(non_port_interface_item)(s)?; + let (s, d) = symbol("endinterface")(s)?; + let (s, e) = opt(pair(symbol(":"), interface_identifier))(s)?; + Ok(( + s, + InterfaceDeclaration::Ansi(InterfaceDeclarationAnsi { + nodes: (a, b, c, d, e), + }), + )) +} + +pub fn interface_declaration_wildcard(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = symbol("interface")(s)?; + let (s, c) = opt(lifetime)(s)?; + let (s, d) = interface_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(interface_item)(s)?; + let (s, k) = symbol("endinterface")(s)?; + let (s, l) = opt(pair(symbol(":"), interface_identifier))(s)?; + Ok(( + s, + InterfaceDeclaration::Wildcard(InterfaceDeclarationWildcard { + nodes: (a, b, c, d, e, f, g, h, i, j, k, l), + }), + )) +} + +pub fn interface_declaration_extern_nonansi(s: Span) -> IResult { + let (s, a) = symbol("extern")(s)?; + let (s, b) = interface_nonansi_header(s)?; + Ok(( + s, + InterfaceDeclaration::ExternNonansi(InterfaceDeclarationExternNonansi { nodes: (a, b) }), + )) +} + +pub fn interface_declaration_extern_ansi(s: Span) -> IResult { + let (s, a) = symbol("extern")(s)?; + let (s, b) = interface_ansi_header(s)?; + Ok(( + s, + InterfaceDeclaration::ExternAnsi(InterfaceDeclarationExternAnsi { nodes: (a, b) }), + )) } pub fn interface_nonansi_header(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = symbol("interface")(s)?; + let (s, c) = opt(lifetime)(s)?; + let (s, d) = interface_identifier(s)?; + let (s, e) = many0(package_import_declaration)(s)?; + let (s, f) = opt(parameter_port_list)(s)?; + let (s, g) = list_of_ports(s)?; + let (s, h) = symbol(";")(s)?; + Ok(( + s, + InterfaceNonansiHeader { + nodes: (a, b, c, d, e, f, g, h), + }, + )) } pub fn interface_ansi_header(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = symbol("interface")(s)?; + let (s, c) = opt(lifetime)(s)?; + let (s, d) = interface_identifier(s)?; + let (s, e) = many0(package_import_declaration)(s)?; + let (s, f) = opt(parameter_port_list)(s)?; + let (s, g) = opt(list_of_port_declarations)(s)?; + let (s, h) = symbol(";")(s)?; + Ok(( + s, + InterfaceAnsiHeader { + nodes: (a, b, c, d, e, f, g, h), + }, + )) } pub fn program_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + program_declaration_nonansi, + program_declaration_ansi, + program_declaration_wildcard, + program_declaration_extern_nonansi, + program_declaration_extern_ansi, + ))(s) +} + +pub fn program_declaration_nonansi(s: Span) -> IResult { + let (s, a) = program_nonansi_header(s)?; + let (s, b) = opt(timeunits_declaration)(s)?; + let (s, c) = many0(program_item)(s)?; + let (s, d) = symbol("endprogram")(s)?; + let (s, e) = opt(pair(symbol(":"), program_identifier))(s)?; + Ok(( + s, + ProgramDeclaration::Nonansi(ProgramDeclarationNonansi { + nodes: (a, b, c, d, e), + }), + )) +} + +pub fn program_declaration_ansi(s: Span) -> IResult { + let (s, a) = program_ansi_header(s)?; + let (s, b) = opt(timeunits_declaration)(s)?; + let (s, c) = many0(non_port_program_item)(s)?; + let (s, d) = symbol("endprogram")(s)?; + let (s, e) = opt(pair(symbol(":"), program_identifier))(s)?; + Ok(( + s, + ProgramDeclaration::Ansi(ProgramDeclarationAnsi { + nodes: (a, b, c, d, e), + }), + )) +} + +pub fn program_declaration_wildcard(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = symbol("program")(s)?; + let (s, c) = program_identifier(s)?; + let (s, d) = symbol("(")(s)?; + let (s, e) = symbol(".*")(s)?; + let (s, f) = symbol(")")(s)?; + let (s, g) = symbol(";")(s)?; + let (s, h) = opt(timeunits_declaration)(s)?; + let (s, i) = many0(program_item)(s)?; + let (s, j) = symbol("endprogram")(s)?; + let (s, k) = opt(pair(symbol(":"), program_identifier))(s)?; + Ok(( + s, + ProgramDeclaration::Wildcard(ProgramDeclarationWildcard { + nodes: (a, b, c, d, e, f, g, h, i, j, k), + }), + )) +} + +pub fn program_declaration_extern_nonansi(s: Span) -> IResult { + let (s, a) = symbol("extern")(s)?; + let (s, b) = program_nonansi_header(s)?; + Ok(( + s, + ProgramDeclaration::ExternNonansi(ProgramDeclarationExternNonansi { nodes: (a, b) }), + )) +} + +pub fn program_declaration_extern_ansi(s: Span) -> IResult { + let (s, a) = symbol("extern")(s)?; + let (s, b) = program_ansi_header(s)?; + Ok(( + s, + ProgramDeclaration::ExternAnsi(ProgramDeclarationExternAnsi { nodes: (a, b) }), + )) } pub fn program_nonansi_header(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = symbol("prgogram")(s)?; + let (s, c) = opt(lifetime)(s)?; + let (s, d) = program_identifier(s)?; + let (s, e) = many0(package_import_declaration)(s)?; + let (s, f) = opt(parameter_port_list)(s)?; + let (s, g) = list_of_ports(s)?; + let (s, h) = symbol(";")(s)?; + Ok(( + s, + ProgramNonansiHeader { + nodes: (a, b, c, d, e, f, g, h), + }, + )) } pub fn program_ansi_header(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = symbol("program")(s)?; + let (s, c) = opt(lifetime)(s)?; + let (s, d) = program_identifier(s)?; + let (s, e) = many0(package_import_declaration)(s)?; + let (s, f) = opt(parameter_port_list)(s)?; + let (s, g) = opt(list_of_port_declarations)(s)?; + let (s, h) = symbol(";")(s)?; + Ok(( + s, + ProgramAnsiHeader { + nodes: (a, b, c, d, e, f, g, h), + }, + )) } pub fn checker_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + 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, d) = symbol(";")(s)?; + let (s, e) = many0(pair(many0(attribute_instance), checker_or_generate_item))(s)?; + let (s, f) = symbol("endchecker")(s)?; + let (s, g) = opt(pair(symbol(":"), checker_identifier))(s)?; + Ok(( + s, + CheckerDeclaration { + nodes: (a, b, c, d, e, f, g), + }, + )) } pub fn class_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = opt(map(symbol("virtual"), |x| Virtual { nodes: (x,) }))(s)?; + let (s, b) = symbol("class")(s)?; + let (s, c) = opt(lifetime)(s)?; + let (s, d) = class_identifier(s)?; + let (s, e) = opt(parameter_port_list)(s)?; + let (s, f) = opt(triple( + symbol("extends"), + class_type, + opt(triple(symbol("("), list_of_arguments, symbol(")"))), + ))(s)?; + let (s, g) = opt(triple( + symbol("implements"), + interface_class_type, + many0(pair(symbol(","), interface_class_type)), + ))(s)?; + let (s, h) = symbol(";")(s)?; + let (s, i) = many0(class_item)(s)?; + let (s, j) = symbol("endclass")(s)?; + let (s, k) = opt(pair(symbol(":"), class_identifier))(s)?; + Ok(( + s, + ClassDeclaration { + nodes: (a, b, c, d, e, f, g, h, i, j, k), + }, + )) } pub fn interface_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)?; + Ok((s, InterfaceClassType { nodes: (a, b) })) } pub fn interface_class_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("interface")(s)?; + let (s, b) = symbol("class")(s)?; + let (s, c) = class_identifier(s)?; + let (s, d) = opt(parameter_port_list)(s)?; + let (s, e) = opt(triple( + symbol("extends"), + interface_class_type, + many0(pair(symbol(","), interface_class_type)), + ))(s)?; + let (s, f) = symbol(";")(s)?; + let (s, g) = many0(interface_class_item)(s)?; + let (s, h) = symbol("endclass")(s)?; + let (s, i) = opt(pair(symbol(":"), class_identifier))(s)?; + Ok(( + s, + InterfaceClassDeclaration { + nodes: (a, b, c, d, e, f, g, h, i), + }, + )) } pub fn interface_class_item(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(type_declaration, |x| InterfaceClassItem::TypeDeclaration(x)), + interface_class_item_method, + map(pair(local_parameter_declaration, symbol(";")), |x| { + InterfaceClassItem::LocalParameterDeclaration(x) + }), + map(pair(parameter_declaration, symbol(";")), |x| { + InterfaceClassItem::ParameterDeclaration(x) + }), + map(symbol(";"), |x| InterfaceClassItem::Null(x)), + ))(s) +} + +pub fn interface_class_item_method(s: Span) -> IResult { + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = interface_class_method(s)?; + Ok(( + s, + InterfaceClassItem::Method(InterfaceClassItemMethod { nodes: (a, b) }), + )) } pub fn interface_class_method(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = symbol("pure")(s)?; + let (s, b) = symbol("virtual")(s)?; + let (s, c) = method_prototype(s)?; + let (s, d) = symbol(";")(s)?; + Ok(( + s, + InterfaceClassMethod { + nodes: (a, b, c, d), + }, + )) } pub fn package_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + let (s, a) = many0(attribute_instance)(s)?; + let (s, b) = symbol("package")(s)?; + let (s, c) = opt(lifetime)(s)?; + let (s, d) = package_identifier(s)?; + let (s, e) = symbol(";")(s)?; + let (s, f) = opt(timeunits_declaration)(s)?; + let (s, g) = many0(pair(many0(attribute_instance), package_item))(s)?; + let (s, h) = symbol("endpackage")(s)?; + let (s, i) = opt(pair(symbol(":"), package_identifier))(s)?; + Ok(( + s, + PackageDeclaration { + nodes: (a, b, c, d, e, f, g, h, i), + }, + )) } -pub fn timeunit_declaration(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) +pub fn timeunits_declaration(s: Span) -> IResult { + alt(( + timeunits_declaration_timeunit_timeprecision, + timeunits_declaration_timeunit, + timeunits_declaration_timeprecision_timeunit, + timeunits_declaration_timeprecision, + ))(s) +} + +pub fn timeunits_declaration_timeunit(s: Span) -> IResult { + let (s, a) = symbol("timeunit")(s)?; + let (s, b) = time_literal(s)?; + let (s, c) = opt(pair(symbol("/"), time_literal))(s)?; + let (s, d) = symbol(";")(s)?; + Ok(( + s, + TimeunitsDeclaration::Timeunit(TimeunitsDeclarationTimeunit { + nodes: (a, b, c, d), + }), + )) +} + +pub fn timeunits_declaration_timeprecision(s: Span) -> IResult { + let (s, a) = symbol("timeprecision")(s)?; + let (s, b) = time_literal(s)?; + let (s, c) = symbol(";")(s)?; + Ok(( + s, + TimeunitsDeclaration::Timeprecision(TimeunitsDeclarationTimeprecision { nodes: (a, b, c) }), + )) +} + +pub fn timeunits_declaration_timeunit_timeprecision( + s: Span, +) -> IResult { + let (s, a) = symbol("timeunit")(s)?; + let (s, b) = time_literal(s)?; + let (s, c) = symbol(";")(s)?; + let (s, d) = symbol("timeprecision")(s)?; + let (s, e) = time_literal(s)?; + let (s, f) = symbol(";")(s)?; + Ok(( + s, + TimeunitsDeclaration::TimeunitTimeprecision(TimeunitsDeclarationTimeunitTimeprecision { + nodes: (a, b, c, d, e, f), + }), + )) +} + +pub fn timeunits_declaration_timeprecision_timeunit( + s: Span, +) -> IResult { + let (s, a) = symbol("timeprecision")(s)?; + let (s, b) = time_literal(s)?; + let (s, c) = symbol(";")(s)?; + let (s, d) = symbol("timeunit")(s)?; + let (s, e) = time_literal(s)?; + let (s, f) = symbol(";")(s)?; + Ok(( + s, + TimeunitsDeclaration::TimeprecisionTimeunit(TimeunitsDeclarationTimeprecisionTimeunit { + nodes: (a, b, c, d, e, f), + }), + )) +} + +// ----------------------------------------------------------------------------- + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_timeunits_declaration() { + parser_test!(timeunits_declaration, "timeunit 1.0ps;", Ok((_, _))); + parser_test!(timeunits_declaration, "timeunit 1.0ps / 20ms;", Ok((_, _))); + parser_test!(timeunits_declaration, "timeprecision 10.0fs;", Ok((_, _))); + parser_test!( + timeunits_declaration, + "timeunit 10.0fs; timeprecision 20s;", + Ok((_, _)) + ); + parser_test!( + timeunits_declaration, + "timeprecision 10.0fs; timeunit 20s \n;", + Ok((_, _)) + ); + } } diff --git a/src/parser/utils.rs b/src/parser/utils.rs index 22a2123..da981ea 100644 --- a/src/parser/utils.rs +++ b/src/parser/utils.rs @@ -37,15 +37,41 @@ 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>)> +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, (a, b, c))) + } +} + +pub fn bracket2<'a, O, F>( + f: F, +) -> impl Fn(Span<'a>) -> IResult, (Symbol<'a>, O, Symbol<'a>)> +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, (a, b, c))) + } +} + pub fn paren<'a, O, F>(f: F) -> impl Fn(Span<'a>) -> IResult, O> where F: Fn(Span<'a>) -> IResult, O>, { move |s: Span<'a>| { let (s, _) = symbol("(")(s)?; - let (s, x) = f(s)?; + let (s, b) = f(s)?; let (s, _) = symbol(")")(s)?; - Ok((s, x)) + Ok((s, b)) } } @@ -55,9 +81,9 @@ where { move |s: Span<'a>| { let (s, _) = symbol("[")(s)?; - let (s, x) = f(s)?; + let (s, b) = f(s)?; let (s, _) = symbol("]")(s)?; - Ok((s, x)) + Ok((s, b)) } } @@ -100,6 +126,24 @@ where } } +pub fn triple<'a, O1, O2, O3, F, G, H>( + f: F, + g: G, + h: H, +) -> impl Fn(Span<'a>) -> IResult, (O1, O2, O3)> +where + F: Fn(Span<'a>) -> IResult, O1>, + G: Fn(Span<'a>) -> IResult, O2>, + H: Fn(Span<'a>) -> IResult, O3>, +{ + move |s: Span<'a>| { + let (s, x) = f(s)?; + let (s, y) = g(s)?; + let (s, z) = h(s)?; + Ok((s, (x, y, z))) + } +} + // ----------------------------------------------------------------------------- pub fn white_space(s: Span) -> IResult {