Refactoring

This commit is contained in:
dalance 2019-07-10 18:06:33 +09:00
parent 9c4aceb631
commit 26cb4e6818
5 changed files with 545 additions and 235 deletions

View File

@ -36,7 +36,7 @@ A parser library for System Verilog.
| primitive_instance | primitive_strengths | | | |
| primitive_instance | primitive_terminals | | | |
| primitive_instance | primitive_gate_and_switch_types | | | |
| instantiations | module_instantiation | | | |
| instantiations | module_instantiation | x | x | |
| instantiations | interface_instantiation | x | x | |
| instantiations | program_instantiation | x | x | |
| instantiations | checker_instantiation | x | x | |
@ -65,8 +65,8 @@ A parser library for System Verilog.
| specify_section | system_timing_check_commands | | | |
| specify_section | system_timing_check_command_arguments | | | |
| specify_section | system_timing_check_event_definitions | | | |
| expressions | concatenations | | | |
| expressions | subroutine_calls | | | |
| expressions | concatenations | x | x | |
| expressions | subroutine_calls | x | x | |
| expressions | expressions | x | x | |
| expressions | primaries | x | x | |
| expressions | expression_leftside_values | x | x | |

View File

@ -9,40 +9,67 @@ use nom::IResult;
#[derive(Debug)]
pub struct Concatenation<'a> {
pub nodes: (Vec<Expression<'a>>,),
pub nodes: (
Symbol<'a>,
Expression<'a>,
Vec<(Symbol<'a>, Expression<'a>)>,
Symbol<'a>,
),
}
#[derive(Debug)]
pub struct ConstantConcatenation<'a> {
pub nodes: (Vec<ConstantExpression<'a>>,),
pub nodes: (
Symbol<'a>,
ConstantExpression<'a>,
Vec<(Symbol<'a>, ConstantExpression<'a>)>,
Symbol<'a>,
),
}
#[derive(Debug)]
pub struct ConstantMultipleConcatenation<'a> {
pub nodes: (ConstantExpression<'a>, ConstantConcatenation<'a>),
pub nodes: (
Symbol<'a>,
ConstantExpression<'a>,
ConstantConcatenation<'a>,
Symbol<'a>,
),
}
#[derive(Debug)]
pub struct ModulePathConcatenation<'a> {
pub nodes: (Vec<ModulePathExpression<'a>>,),
pub nodes: (
Symbol<'a>,
ModulePathExpression<'a>,
Vec<(Symbol<'a>, ModulePathExpression<'a>)>,
Symbol<'a>,
),
}
#[derive(Debug)]
pub struct ModulePathMultipleConcatenation<'a> {
pub nodes: (ConstantExpression<'a>, ModulePathConcatenation<'a>),
pub nodes: (
Symbol<'a>,
ConstantExpression<'a>,
ModulePathConcatenation<'a>,
Symbol<'a>,
),
}
#[derive(Debug)]
pub struct MultipleConcatenation<'a> {
pub nodes: (Expression<'a>, Concatenation<'a>),
pub nodes: (Symbol<'a>, Expression<'a>, Concatenation<'a>, Symbol<'a>),
}
#[derive(Debug)]
pub struct StreamingConcatenation<'a> {
pub nodes: (
Symbol<'a>,
StreamOperator<'a>,
Option<SliceSize<'a>>,
StreamConcatenation<'a>,
Symbol<'a>,
),
}
@ -53,32 +80,52 @@ pub struct StreamOperator<'a> {
#[derive(Debug)]
pub enum SliceSize<'a> {
Type(SimpleType<'a>),
Expression(ConstantExpression<'a>),
SimpleType(SimpleType<'a>),
ConstantExpression(ConstantExpression<'a>),
}
#[derive(Debug)]
pub struct StreamConcatenation<'a> {
pub nodes: (Vec<StreamExpression<'a>>,),
}
#[derive(Debug)]
pub struct StreamExpression<'a> {
pub nodes: (Expression<'a>, Option<ArrayRangeExpression<'a>>),
}
#[derive(Debug)]
pub struct ArrayRangeExpression<'a> {
pub nodes: (
Expression<'a>,
Option<ArrayRangeOperator<'a>>,
Option<Expression<'a>>,
Symbol<'a>,
StreamExpression<'a>,
Vec<(Symbol<'a>, StreamExpression<'a>)>,
Symbol<'a>,
),
}
#[derive(Debug)]
pub struct ArrayRangeOperator<'a> {
pub nodes: (Symbol<'a>,),
pub struct StreamExpression<'a> {
pub nodes: (
Expression<'a>,
Option<(
Symbol<'a>,
(Symbol<'a>, ArrayRangeExpression<'a>, Symbol<'a>),
)>,
),
}
#[derive(Debug)]
pub enum ArrayRangeExpression<'a> {
Expression(Expression<'a>),
Colon(ArrayRangeExpressionColon<'a>),
PlusColon(ArrayRangeExpressionPlusColon<'a>),
MinusColon(ArrayRangeExpressionMinusColon<'a>),
}
#[derive(Debug)]
pub struct ArrayRangeExpressionColon<'a> {
pub nodes: (Expression<'a>, Symbol<'a>, Expression<'a>),
}
#[derive(Debug)]
pub struct ArrayRangeExpressionPlusColon<'a> {
pub nodes: (Expression<'a>, Symbol<'a>, Expression<'a>),
}
#[derive(Debug)]
pub struct ArrayRangeExpressionMinusColon<'a> {
pub nodes: (Expression<'a>, Symbol<'a>, Expression<'a>),
}
#[derive(Debug)]
@ -89,53 +136,97 @@ pub struct EmptyUnpackedArrayConcatenation<'a> {
// -----------------------------------------------------------------------------
pub fn concatenation(s: Span) -> IResult<Span, Concatenation> {
let (s, x) = brace(separated_nonempty_list(symbol(","), expression))(s)?;
Ok((s, Concatenation { nodes: (x,) }))
let (s, a) = symbol("{")(s)?;
let (s, b) = expression(s)?;
let (s, c) = many0(pair(symbol(","), expression))(s)?;
let (s, d) = symbol("}")(s)?;
Ok((
s,
Concatenation {
nodes: (a, b, c, d),
},
))
}
pub fn constant_concatenation(s: Span) -> IResult<Span, ConstantConcatenation> {
let (s, x) = brace(separated_nonempty_list(symbol(","), constant_expression))(s)?;
Ok((s, ConstantConcatenation { nodes: (x,) }))
let (s, a) = symbol("{")(s)?;
let (s, b) = constant_expression(s)?;
let (s, c) = many0(pair(symbol(","), constant_expression))(s)?;
let (s, d) = symbol("}")(s)?;
Ok((
s,
ConstantConcatenation {
nodes: (a, b, c, d),
},
))
}
pub fn constant_multiple_concatenation(s: Span) -> IResult<Span, ConstantMultipleConcatenation> {
let (s, _) = symbol("{")(s)?;
let (s, x) = constant_expression(s)?;
let (s, y) = constant_concatenation(s)?;
let (s, _) = symbol("}")(s)?;
Ok((s, ConstantMultipleConcatenation { nodes: (x, y) }))
let (s, a) = symbol("{")(s)?;
let (s, b) = constant_expression(s)?;
let (s, c) = constant_concatenation(s)?;
let (s, d) = symbol("}")(s)?;
Ok((
s,
ConstantMultipleConcatenation {
nodes: (a, b, c, d),
},
))
}
pub fn module_path_concatenation(s: Span) -> IResult<Span, ModulePathConcatenation> {
let (s, x) = brace(separated_nonempty_list(symbol(","), module_path_expression))(s)?;
Ok((s, ModulePathConcatenation { nodes: (x,) }))
let (s, a) = symbol("{")(s)?;
let (s, b) = module_path_expression(s)?;
let (s, c) = many0(pair(symbol(","), module_path_expression))(s)?;
let (s, d) = symbol("}")(s)?;
Ok((
s,
ModulePathConcatenation {
nodes: (a, b, c, d),
},
))
}
pub fn module_path_multiple_concatenation(
s: Span,
) -> IResult<Span, ModulePathMultipleConcatenation> {
let (s, _) = symbol("{")(s)?;
let (s, x) = constant_expression(s)?;
let (s, y) = module_path_concatenation(s)?;
let (s, _) = symbol("}")(s)?;
Ok((s, ModulePathMultipleConcatenation { nodes: (x, y) }))
let (s, a) = symbol("{")(s)?;
let (s, b) = constant_expression(s)?;
let (s, c) = module_path_concatenation(s)?;
let (s, d) = symbol("}")(s)?;
Ok((
s,
ModulePathMultipleConcatenation {
nodes: (a, b, c, d),
},
))
}
pub fn multiple_concatenation(s: Span) -> IResult<Span, MultipleConcatenation> {
let (s, _) = symbol("{")(s)?;
let (s, x) = expression(s)?;
let (s, y) = concatenation(s)?;
let (s, _) = symbol("}")(s)?;
Ok((s, MultipleConcatenation { nodes: (x, y) }))
let (s, a) = symbol("{")(s)?;
let (s, b) = expression(s)?;
let (s, c) = concatenation(s)?;
let (s, d) = symbol("}")(s)?;
Ok((
s,
MultipleConcatenation {
nodes: (a, b, c, d),
},
))
}
pub fn streaming_concatenation(s: Span) -> IResult<Span, StreamingConcatenation> {
let (s, _) = symbol("{")(s)?;
let (s, x) = stream_operator(s)?;
let (s, y) = opt(slice_size)(s)?;
let (s, z) = stream_concatenation(s)?;
let (s, _) = symbol("}")(s)?;
Ok((s, StreamingConcatenation { nodes: (x, y, z) }))
let (s, a) = symbol("{")(s)?;
let (s, b) = stream_operator(s)?;
let (s, c) = opt(slice_size)(s)?;
let (s, d) = stream_concatenation(s)?;
let (s, e) = symbol("}")(s)?;
Ok((
s,
StreamingConcatenation {
nodes: (a, b, c, d, e),
},
))
}
pub fn stream_operator(s: Span) -> IResult<Span, StreamOperator> {
@ -147,39 +238,67 @@ pub fn stream_operator(s: Span) -> IResult<Span, StreamOperator> {
pub fn slice_size(s: Span) -> IResult<Span, SliceSize> {
alt((
map(simple_type, |x| SliceSize::Type(x)),
map(constant_expression, |x| SliceSize::Expression(x)),
map(simple_type, |x| SliceSize::SimpleType(x)),
map(constant_expression, |x| SliceSize::ConstantExpression(x)),
))(s)
}
pub fn stream_concatenation(s: Span) -> IResult<Span, StreamConcatenation> {
let (s, x) = brace(separated_nonempty_list(symbol(","), stream_expression))(s)?;
Ok((s, StreamConcatenation { nodes: (x,) }))
let (s, a) = symbol("{")(s)?;
let (s, b) = stream_expression(s)?;
let (s, c) = many0(pair(symbol(","), stream_expression))(s)?;
let (s, d) = symbol("}")(s)?;
Ok((
s,
StreamConcatenation {
nodes: (a, b, c, d),
},
))
}
pub fn stream_expression(s: Span) -> IResult<Span, StreamExpression> {
let (s, x) = expression(s)?;
let (s, y) = opt(preceded(symbol("with"), bracket(array_range_expression)))(s)?;
Ok((s, StreamExpression { nodes: (x, y) }))
let (s, a) = expression(s)?;
let (s, b) = opt(pair(symbol("with"), bracket2(array_range_expression)))(s)?;
Ok((s, StreamExpression { nodes: (a, b) }))
}
pub fn array_range_expression(s: Span) -> IResult<Span, ArrayRangeExpression> {
let (s, x) = expression(s)?;
let (s, y) = opt(pair(array_range_operator, expression))(s)?;
let (y, z) = if let Some((y, z)) = y {
(Some(y), Some(z))
} else {
(None, None)
};
Ok((s, ArrayRangeExpression { nodes: (x, y, z) }))
alt((
map(expression, |x| ArrayRangeExpression::Expression(x)),
array_range_expression_colon,
array_range_expression_plus_colon,
array_range_expression_minus_colon,
))(s)
}
pub fn array_range_operator(s: Span) -> IResult<Span, ArrayRangeOperator> {
alt((
map(symbol(":"), |x| ArrayRangeOperator { nodes: (x,) }),
map(symbol("+:"), |x| ArrayRangeOperator { nodes: (x,) }),
map(symbol("-:"), |x| ArrayRangeOperator { nodes: (x,) }),
))(s)
pub fn array_range_expression_colon(s: Span) -> IResult<Span, ArrayRangeExpression> {
let (s, a) = expression(s)?;
let (s, b) = symbol(":")(s)?;
let (s, c) = expression(s)?;
Ok((
s,
ArrayRangeExpression::Colon(ArrayRangeExpressionColon { nodes: (a, b, c) }),
))
}
pub fn array_range_expression_plus_colon(s: Span) -> IResult<Span, ArrayRangeExpression> {
let (s, a) = expression(s)?;
let (s, b) = symbol("+:")(s)?;
let (s, c) = expression(s)?;
Ok((
s,
ArrayRangeExpression::PlusColon(ArrayRangeExpressionPlusColon { nodes: (a, b, c) }),
))
}
pub fn array_range_expression_minus_colon(s: Span) -> IResult<Span, ArrayRangeExpression> {
let (s, a) = expression(s)?;
let (s, b) = symbol("-:")(s)?;
let (s, c) = expression(s)?;
Ok((
s,
ArrayRangeExpression::MinusColon(ArrayRangeExpressionMinusColon { nodes: (a, b, c) }),
))
}
pub fn empty_unpacked_array_concatenation(

View File

@ -108,7 +108,7 @@ pub enum ConstantParamExpression<'a> {
#[derive(Debug)]
pub enum ParamExpression<'a> {
MintypmaxExpression(MintypmaxExpression<'a>),
DataType(DataType<'a>),
DataType(Box<DataType<'a>>),
Dollar(Symbol<'a>),
}
@ -419,7 +419,7 @@ pub fn param_expression(s: Span) -> IResult<Span, ParamExpression> {
map(mintypmax_expression, |x| {
ParamExpression::MintypmaxExpression(x)
}),
map(data_type, |x| ParamExpression::DataType(x)),
map(data_type, |x| ParamExpression::DataType(Box::new(x))),
))(s)
}

View File

@ -17,28 +17,59 @@ pub struct TfCall<'a> {
pub nodes: (
PsOrHierarchicalTfIdentifier<'a>,
Vec<AttributeInstance<'a>>,
Option<ListOfArguments<'a>>,
Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>,
),
}
#[derive(Debug)]
pub struct SystemTfCall<'a> {
pub enum SystemTfCall<'a> {
ArgOptionl(SystemTfCallArgOptional<'a>),
ArgDataType(SystemTfCallArgDataType<'a>),
ArgExpression(SystemTfCallArgExpression<'a>),
}
#[derive(Debug)]
pub struct SystemTfCallArgOptional<'a> {
pub nodes: (
SystemTfIdentifier<'a>,
Option<ListOfArguments<'a>>,
Option<DataType<'a>>,
Option<Vec<Expression<'a>>>,
Option<ClockingEvent<'a>>,
Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>,
),
}
#[derive(Debug)]
pub struct SystemTfCallArgDataType<'a> {
pub nodes: (
SystemTfIdentifier<'a>,
Symbol<'a>,
DataType<'a>,
Option<(Symbol<'a>, Expression<'a>)>,
Symbol<'a>,
),
}
#[derive(Debug)]
pub struct SystemTfCallArgExpression<'a> {
pub nodes: (
SystemTfIdentifier<'a>,
Symbol<'a>,
Expression<'a>,
Vec<(Symbol<'a>, Option<Expression<'a>>)>,
Option<(Symbol<'a>, Option<ClockingEvent<'a>>)>,
Symbol<'a>,
),
}
#[derive(Debug)]
pub enum SubroutineCall<'a> {
Tf(Box<TfCall<'a>>),
SystemTf(Box<SystemTfCall<'a>>),
Method(Box<MethodCall<'a>>),
Randomize(Box<RandomizeCall<'a>>),
StdRandomize(Box<RandomizeCall<'a>>),
TfCall(Box<TfCall<'a>>),
SystemTfCall(Box<SystemTfCall<'a>>),
MethodCall(Box<MethodCall<'a>>),
Randomize(Box<SubroutineCallRandomize<'a>>),
}
#[derive(Debug)]
pub struct SubroutineCallRandomize<'a> {
pub nodes: (Option<(Symbol<'a>, Symbol<'a>)>, RandomizeCall<'a>),
}
#[derive(Debug)]
@ -47,16 +78,49 @@ pub struct FunctionSubroutineCall<'a> {
}
#[derive(Debug)]
pub struct ListOfArguments<'a> {
pub enum ListOfArguments<'a> {
Ordered(ListOfArgumentsOrdered<'a>),
Named(ListOfArgumentsNamed<'a>),
}
#[derive(Debug)]
pub struct ListOfArgumentsOrdered<'a> {
pub nodes: (
Vec<Expression<'a>>,
Vec<(Identifier<'a>, Option<Expression<'a>>)>,
Option<Expression<'a>>,
Vec<(Symbol<'a>, Option<Expression<'a>>)>,
Vec<(
Symbol<'a>,
Symbol<'a>,
Identifier<'a>,
Symbol<'a>,
Option<Expression<'a>>,
Symbol<'a>,
)>,
),
}
#[derive(Debug)]
pub struct ListOfArgumentsNamed<'a> {
pub nodes: (
Symbol<'a>,
Identifier<'a>,
Symbol<'a>,
Option<Expression<'a>>,
Symbol<'a>,
Vec<(
Symbol<'a>,
Symbol<'a>,
Identifier<'a>,
Symbol<'a>,
Option<Expression<'a>>,
Symbol<'a>,
)>,
),
}
#[derive(Debug)]
pub struct MethodCall<'a> {
pub nodes: (MethodCallRoot<'a>, MethodCallBody<'a>),
pub nodes: (MethodCallRoot<'a>, Symbol<'a>, MethodCallBody<'a>),
}
#[derive(Debug)]
@ -70,7 +134,7 @@ pub struct MethodCallBodyUser<'a> {
pub nodes: (
MethodIdentifier<'a>,
Vec<AttributeInstance<'a>>,
Option<ListOfArguments<'a>>,
Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>,
),
}
@ -85,21 +149,35 @@ pub struct ArrayManipulationCall<'a> {
pub nodes: (
ArrayMethodName<'a>,
Vec<AttributeInstance<'a>>,
Option<ListOfArguments<'a>>,
Option<Expression<'a>>,
Option<(Symbol<'a>, ListOfArguments<'a>, Symbol<'a>)>,
Option<(Symbol<'a>, (Symbol<'a>, Expression<'a>, Symbol<'a>))>,
),
}
#[derive(Debug)]
pub struct RandomizeCall<'a> {
pub nodes: (
Symbol<'a>,
Vec<AttributeInstance<'a>>,
VariableIdentifierList<'a>,
IdentifierList<'a>,
Option<ConstraintBlock<'a>>,
Option<(
Symbol<'a>,
Option<VariableIdentifierListOrNull<'a>>,
Symbol<'a>,
)>,
Option<(
Symbol<'a>,
Option<(Symbol<'a>, Option<IdentifierList<'a>>, Symbol<'a>)>,
ConstraintBlock<'a>,
)>,
),
}
#[derive(Debug)]
pub enum VariableIdentifierListOrNull<'a> {
VariableIdentifierList(VariableIdentifierList<'a>),
Null(Symbol<'a>),
}
#[derive(Debug)]
pub enum MethodCallRoot<'a> {
Primary(Primary<'a>),
@ -109,10 +187,10 @@ pub enum MethodCallRoot<'a> {
#[derive(Debug)]
pub enum ArrayMethodName<'a> {
MethodIdentifier(MethodIdentifier<'a>),
Unique,
And,
Or,
Xor,
Unique(Symbol<'a>),
And(Symbol<'a>),
Or(Symbol<'a>),
Xor(Symbol<'a>),
}
// -----------------------------------------------------------------------------
@ -123,92 +201,131 @@ pub fn constant_function_call(s: Span) -> IResult<Span, ConstantFunctionCall> {
}
pub fn tf_call(s: Span) -> IResult<Span, TfCall> {
let (s, x) = ps_or_hierarchical_tf_identifier(s)?;
let (s, y) = many0(attribute_instance)(s)?;
let (s, z) = opt(paren(list_of_arguments))(s)?;
Ok((s, TfCall { nodes: (x, y, z) }))
let (s, a) = ps_or_hierarchical_tf_identifier(s)?;
let (s, b) = many0(attribute_instance)(s)?;
let (s, c) = opt(paren2(list_of_arguments))(s)?;
Ok((s, TfCall { nodes: (a, b, c) }))
}
pub fn system_tf_call(s: Span) -> IResult<Span, SystemTfCall> {
alt((
system_tf_call_list_of_arguments,
system_tf_call_data_type,
system_tf_call_clocking_event,
system_tf_call_arg_optional,
system_tf_call_arg_data_type,
system_tf_call_arg_expression,
))(s)
}
pub fn system_tf_call_list_of_arguments(s: Span) -> IResult<Span, SystemTfCall> {
let (s, x) = system_tf_identifier(s)?;
let (s, y) = opt(paren(list_of_arguments))(s)?;
pub fn system_tf_call_arg_optional(s: Span) -> IResult<Span, SystemTfCall> {
let (s, a) = system_tf_identifier(s)?;
let (s, b) = opt(paren2(list_of_arguments))(s)?;
Ok((
s,
SystemTfCall {
nodes: (x, y, None, None, None),
},
SystemTfCall::ArgOptionl(SystemTfCallArgOptional { nodes: (a, b) }),
))
}
pub fn system_tf_call_data_type(s: Span) -> IResult<Span, SystemTfCall> {
let (s, x) = system_tf_identifier(s)?;
let (s, _) = symbol("(")(s)?;
let (s, y) = data_type(s)?;
let (s, z) = preceded(symbol(","), expression)(s)?;
let (s, _) = symbol(")")(s)?;
pub fn system_tf_call_arg_data_type(s: Span) -> IResult<Span, SystemTfCall> {
let (s, a) = system_tf_identifier(s)?;
let (s, b) = symbol("(")(s)?;
let (s, c) = data_type(s)?;
let (s, d) = opt(pair(symbol(","), expression))(s)?;
let (s, e) = symbol(")")(s)?;
Ok((
s,
SystemTfCall {
nodes: (x, None, Some(y), Some(vec![z]), None),
},
SystemTfCall::ArgDataType(SystemTfCallArgDataType {
nodes: (a, b, c, d, e),
}),
))
}
pub fn system_tf_call_clocking_event(s: Span) -> IResult<Span, SystemTfCall> {
let (s, x) = system_tf_identifier(s)?;
let (s, _) = symbol("(")(s)?;
let (s, y) = separated_nonempty_list(symbol(","), expression)(s)?;
let (s, z) = opt(preceded(symbol(","), opt(clocking_event)))(s)?;
let (s, _) = symbol(")")(s)?;
let z = if let Some(Some(z)) = z { Some(z) } else { None };
pub fn system_tf_call_arg_expression(s: Span) -> IResult<Span, SystemTfCall> {
let (s, a) = system_tf_identifier(s)?;
let (s, b) = symbol("(")(s)?;
let (s, c) = expression(s)?;
let (s, d) = many0(pair(symbol(","), opt(expression)))(s)?;
let (s, e) = opt(pair(symbol(","), opt(clocking_event)))(s)?;
let (s, f) = symbol(")")(s)?;
Ok((
s,
SystemTfCall {
nodes: (x, None, None, Some(y), z),
},
SystemTfCall::ArgExpression(SystemTfCallArgExpression {
nodes: (a, b, c, d, e, f),
}),
))
}
pub fn subroutine_call(s: Span) -> IResult<Span, SubroutineCall> {
alt((
map(tf_call, |x| SubroutineCall::Tf(Box::new(x))),
map(system_tf_call, |x| SubroutineCall::SystemTf(Box::new(x))),
map(method_call, |x| SubroutineCall::Method(Box::new(x))),
map(
triple(symbol("std"), symbol("::"), randomize_call),
|(_, _, x)| SubroutineCall::StdRandomize(Box::new(x)),
),
map(randomize_call, |x| SubroutineCall::Randomize(Box::new(x))),
map(tf_call, |x| SubroutineCall::TfCall(Box::new(x))),
map(system_tf_call, |x| {
SubroutineCall::SystemTfCall(Box::new(x))
}),
map(method_call, |x| SubroutineCall::MethodCall(Box::new(x))),
subroutine_call_randomize,
))(s)
}
pub fn subroutine_call_randomize(s: Span) -> IResult<Span, SubroutineCall> {
let (s, a) = opt(pair(symbol("std"), symbol("::")))(s)?;
let (s, b) = randomize_call(s)?;
Ok((
s,
SubroutineCall::Randomize(Box::new(SubroutineCallRandomize { nodes: (a, b) })),
))
}
pub fn function_subroutine_call(s: Span) -> IResult<Span, FunctionSubroutineCall> {
map(subroutine_call, |x| FunctionSubroutineCall { nodes: (x,) })(s)
}
pub fn list_of_arguments(s: Span) -> IResult<Span, ListOfArguments> {
let (s, x) = separated_list(symbol(","), expression)(s)?;
let (s, y) = separated_list(
alt((list_of_arguments_ordered, list_of_arguments_named))(s)
}
pub fn list_of_arguments_ordered(s: Span) -> IResult<Span, ListOfArguments> {
let (s, a) = opt(expression)(s)?;
let (s, b) = many0(pair(symbol(","), opt(expression)))(s)?;
let (s, c) = many0(tuple((
symbol(","),
pair(preceded(symbol("."), identifier), paren(opt(expression))),
)(s)?;
Ok((s, ListOfArguments { nodes: (x, y) }))
symbol("."),
identifier,
symbol("("),
opt(expression),
symbol(")"),
)))(s)?;
Ok((
s,
ListOfArguments::Ordered(ListOfArgumentsOrdered { nodes: (a, b, c) }),
))
}
pub fn list_of_arguments_named(s: Span) -> IResult<Span, ListOfArguments> {
let (s, a) = symbol(".")(s)?;
let (s, b) = identifier(s)?;
let (s, c) = symbol("(")(s)?;
let (s, d) = opt(expression)(s)?;
let (s, e) = symbol(")")(s)?;
let (s, f) = many0(tuple((
symbol(","),
symbol("."),
identifier,
symbol("("),
opt(expression),
symbol(")"),
)))(s)?;
Ok((
s,
ListOfArguments::Named(ListOfArgumentsNamed {
nodes: (a, b, c, d, e, f),
}),
))
}
pub fn method_call(s: Span) -> IResult<Span, MethodCall> {
let (s, x) = method_call_root(s)?;
let (s, _) = symbol(".")(s)?;
let (s, y) = method_call_body(s)?;
let (s, a) = method_call_root(s)?;
let (s, b) = symbol(".")(s)?;
let (s, c) = method_call_body(s)?;
Ok((s, MethodCall { nodes: (x, y) }))
Ok((s, MethodCall { nodes: (a, b, c) }))
}
pub fn method_call_body(s: Span) -> IResult<Span, MethodCallBody> {
@ -221,12 +338,12 @@ pub fn method_call_body(s: Span) -> IResult<Span, MethodCallBody> {
}
pub fn method_call_body_user(s: Span) -> IResult<Span, MethodCallBody> {
let (s, x) = method_identifier(s)?;
let (s, y) = many0(attribute_instance)(s)?;
let (s, z) = opt(paren(list_of_arguments))(s)?;
let (s, a) = method_identifier(s)?;
let (s, b) = many0(attribute_instance)(s)?;
let (s, c) = opt(paren2(list_of_arguments))(s)?;
Ok((
s,
MethodCallBody::User(MethodCallBodyUser { nodes: (x, y, z) }),
MethodCallBody::User(MethodCallBodyUser { nodes: (a, b, c) }),
))
}
@ -240,50 +357,44 @@ pub fn built_in_method_call(s: Span) -> IResult<Span, BuiltInMethodCall> {
}
pub fn array_manipulation_call(s: Span) -> IResult<Span, ArrayManipulationCall> {
let (s, x) = array_method_name(s)?;
let (s, y) = many0(attribute_instance)(s)?;
let (s, z) = opt(paren(list_of_arguments))(s)?;
let (s, v) = opt(preceded(symbol("with"), paren(expression)))(s)?;
let (s, a) = array_method_name(s)?;
let (s, b) = many0(attribute_instance)(s)?;
let (s, c) = opt(paren2(list_of_arguments))(s)?;
let (s, d) = opt(pair(symbol("with"), paren2(expression)))(s)?;
Ok((
s,
ArrayManipulationCall {
nodes: (x, y, z, v),
nodes: (a, b, c, d),
},
))
}
pub fn randomize_call(s: Span) -> IResult<Span, RandomizeCall> {
let (s, _) = symbol("randomize")(s)?;
let (s, x) = many0(attribute_instance)(s)?;
let (s, y) = opt(paren(opt(alt((
variable_identifier_list,
map(symbol("null"), |_| VariableIdentifierList {
nodes: (vec![],),
}),
)))))(s)?;
let (s, z) = opt(triple(
let (s, a) = symbol("randomize")(s)?;
let (s, b) = many0(attribute_instance)(s)?;
let (s, c) = opt(paren2(opt(variable_identifier_list_or_null)))(s)?;
let (s, d) = opt(triple(
symbol("with"),
opt(paren(opt(identifier_list))),
opt(paren2(opt(identifier_list))),
constraint_block,
))(s)?;
let y = if let Some(Some(y)) = y {
y
} else {
VariableIdentifierList { nodes: (vec![],) }
};
let (z, v) = if let Some((_, Some(Some(z)), v)) = z {
(z, Some(v))
} else {
(IdentifierList { nodes: (vec![],) }, None)
};
Ok((
s,
RandomizeCall {
nodes: (x, y, z, v),
nodes: (a, b, c, d),
},
))
}
pub fn variable_identifier_list_or_null(s: Span) -> IResult<Span, VariableIdentifierListOrNull> {
alt((
map(variable_identifier_list, |x| {
VariableIdentifierListOrNull::VariableIdentifierList(x)
}),
map(symbol("null"), |x| VariableIdentifierListOrNull::Null(x)),
))(s)
}
pub fn method_call_root(s: Span) -> IResult<Span, MethodCallRoot> {
alt((
map(primary, |x| MethodCallRoot::Primary(x)),
@ -295,10 +406,10 @@ pub fn method_call_root(s: Span) -> IResult<Span, MethodCallRoot> {
pub fn array_method_name(s: Span) -> IResult<Span, ArrayMethodName> {
alt((
map(symbol("unique"), |_| ArrayMethodName::Unique),
map(symbol("and"), |_| ArrayMethodName::And),
map(symbol("or"), |_| ArrayMethodName::Or),
map(symbol("xor"), |_| ArrayMethodName::Xor),
map(symbol("unique"), |x| ArrayMethodName::Unique(x)),
map(symbol("and"), |x| ArrayMethodName::And(x)),
map(symbol("or"), |x| ArrayMethodName::Or(x)),
map(symbol("xor"), |x| ArrayMethodName::Xor(x)),
map(method_identifier, |x| ArrayMethodName::MethodIdentifier(x)),
))(s)
}

View File

@ -2,6 +2,7 @@ use crate::parser::*;
use nom::branch::*;
use nom::combinator::*;
use nom::multi::*;
use nom::sequence::*;
use nom::IResult;
// -----------------------------------------------------------------------------
@ -11,19 +12,44 @@ pub struct ModuleInstantiation<'a> {
pub nodes: (
ModuleIdentifier<'a>,
Option<ParameterValueAssignment<'a>>,
Vec<HierarchicalInstance<'a>>,
HierarchicalInstance<'a>,
Vec<(Symbol<'a>, HierarchicalInstance<'a>)>,
Symbol<'a>,
),
}
#[derive(Debug)]
pub struct ParameterValueAssignment<'a> {
pub nodes: (ListOfParameterAssignments<'a>,),
pub nodes: (
Symbol<'a>,
(
Symbol<'a>,
Option<ListOfParameterAssignments<'a>>,
Symbol<'a>,
),
),
}
#[derive(Debug)]
pub enum ListOfParameterAssignments<'a> {
Ordered(Vec<OrderedParameterAssignment<'a>>),
Named(Vec<NamedParameterAssignment<'a>>),
Ordered(ListOfParameterAssignmentsOrdered<'a>),
Named(ListOfParameterAssignmentsNamed<'a>),
}
#[derive(Debug)]
pub struct ListOfParameterAssignmentsOrdered<'a> {
pub nodes: (
OrderedParameterAssignment<'a>,
Vec<(Symbol<'a>, OrderedParameterAssignment<'a>)>,
),
}
#[derive(Debug)]
pub struct ListOfParameterAssignmentsNamed<'a> {
pub nodes: (
NamedParameterAssignment<'a>,
Vec<(Symbol<'a>, NamedParameterAssignment<'a>)>,
),
}
#[derive(Debug)]
@ -33,12 +59,19 @@ pub struct OrderedParameterAssignment<'a> {
#[derive(Debug)]
pub struct NamedParameterAssignment<'a> {
pub nodes: (ParameterIdentifier<'a>, Option<ParamExpression<'a>>),
pub nodes: (
Symbol<'a>,
ParameterIdentifier<'a>,
(Symbol<'a>, Option<ParamExpression<'a>>, Symbol<'a>),
),
}
#[derive(Debug)]
pub struct HierarchicalInstance<'a> {
pub nodes: (NameOfInstance<'a>, Option<ListOfPortConnections<'a>>),
pub nodes: (
NameOfInstance<'a>,
(Symbol<'a>, Option<ListOfPortConnections<'a>>, Symbol<'a>),
),
}
#[derive(Debug)]
@ -48,8 +81,24 @@ pub struct NameOfInstance<'a> {
#[derive(Debug)]
pub enum ListOfPortConnections<'a> {
Ordered(Vec<OrderedPortConnection<'a>>),
Named(Vec<NamedPortConnection<'a>>),
Ordered(ListOfPortConnectionsOrdered<'a>),
Named(ListOfPortConnectionsNamed<'a>),
}
#[derive(Debug)]
pub struct ListOfPortConnectionsOrdered<'a> {
pub nodes: (
OrderedPortConnection<'a>,
Vec<(Symbol<'a>, OrderedPortConnection<'a>)>,
),
}
#[derive(Debug)]
pub struct ListOfPortConnectionsNamed<'a> {
pub nodes: (
NamedPortConnection<'a>,
Vec<(Symbol<'a>, NamedPortConnection<'a>)>,
),
}
#[derive(Debug)]
@ -67,61 +116,80 @@ pub enum NamedPortConnection<'a> {
pub struct NamedPortConnectionIdentifier<'a> {
pub nodes: (
Vec<AttributeInstance<'a>>,
Symbol<'a>,
PortIdentifier<'a>,
Option<Expression<'a>>,
Option<(Symbol<'a>, Option<Expression<'a>>, Symbol<'a>)>,
),
}
#[derive(Debug)]
pub struct NamedPortConnectionAsterisk<'a> {
pub nodes: (Vec<AttributeInstance<'a>>,),
pub nodes: (Vec<AttributeInstance<'a>>, Symbol<'a>),
}
// -----------------------------------------------------------------------------
pub fn module_instantiation(s: Span) -> IResult<Span, ModuleInstantiation> {
let (s, x) = module_identifier(s)?;
let (s, y) = opt(parameter_value_assignment)(s)?;
let (s, z) = separated_nonempty_list(symbol(","), hierarchical_instance)(s)?;
let (s, _) = symbol(";")(s)?;
Ok((s, ModuleInstantiation { nodes: (x, y, z) }))
let (s, a) = module_identifier(s)?;
let (s, b) = opt(parameter_value_assignment)(s)?;
let (s, c) = hierarchical_instance(s)?;
let (s, d) = many0(pair(symbol(","), hierarchical_instance))(s)?;
let (s, e) = symbol(";")(s)?;
Ok((
s,
ModuleInstantiation {
nodes: (a, b, c, d, e),
},
))
}
pub fn parameter_value_assignment(s: Span) -> IResult<Span, ParameterValueAssignment> {
let (s, _) = symbol("#")(s)?;
let (s, x) = paren(list_of_parameter_assignments)(s)?;
Ok((s, ParameterValueAssignment { nodes: (x,) }))
let (s, a) = symbol("#")(s)?;
let (s, b) = paren2(opt(list_of_parameter_assignments))(s)?;
Ok((s, ParameterValueAssignment { nodes: (a, b) }))
}
pub fn list_of_parameter_assignments(s: Span) -> IResult<Span, ListOfParameterAssignments> {
alt((
map(
separated_nonempty_list(symbol(","), ordered_parameter_assignment),
|x| ListOfParameterAssignments::Ordered(x),
),
map(
separated_nonempty_list(symbol(","), named_parameter_assignment),
|x| ListOfParameterAssignments::Named(x),
),
list_of_parameter_assignments_ordered,
list_of_parameter_assignments_named,
))(s)
}
pub fn list_of_parameter_assignments_ordered(s: Span) -> IResult<Span, ListOfParameterAssignments> {
let (s, a) = ordered_parameter_assignment(s)?;
let (s, b) = many0(pair(symbol(","), ordered_parameter_assignment))(s)?;
Ok((
s,
ListOfParameterAssignments::Ordered(ListOfParameterAssignmentsOrdered { nodes: (a, b) }),
))
}
pub fn list_of_parameter_assignments_named(s: Span) -> IResult<Span, ListOfParameterAssignments> {
let (s, a) = named_parameter_assignment(s)?;
let (s, b) = many0(pair(symbol(","), named_parameter_assignment))(s)?;
Ok((
s,
ListOfParameterAssignments::Named(ListOfParameterAssignmentsNamed { nodes: (a, b) }),
))
}
pub fn ordered_parameter_assignment(s: Span) -> IResult<Span, OrderedParameterAssignment> {
let (s, x) = param_expression(s)?;
Ok((s, OrderedParameterAssignment { nodes: (x,) }))
}
pub fn named_parameter_assignment(s: Span) -> IResult<Span, NamedParameterAssignment> {
let (s, _) = symbol(".")(s)?;
let (s, x) = parameter_identifier(s)?;
let (s, y) = paren(opt(param_expression))(s)?;
Ok((s, NamedParameterAssignment { nodes: (x, y) }))
let (s, a) = symbol(".")(s)?;
let (s, b) = parameter_identifier(s)?;
let (s, c) = paren2(opt(param_expression))(s)?;
Ok((s, NamedParameterAssignment { nodes: (a, b, c) }))
}
pub fn hierarchical_instance(s: Span) -> IResult<Span, HierarchicalInstance> {
let (s, x) = name_of_instance(s)?;
let (s, y) = paren(opt(list_of_port_connections))(s)?;
Ok((s, HierarchicalInstance { nodes: (x, y) }))
let (s, a) = name_of_instance(s)?;
let (s, b) = paren2(opt(list_of_port_connections))(s)?;
Ok((s, HierarchicalInstance { nodes: (a, b) }))
}
pub fn name_of_instance(s: Span) -> IResult<Span, NameOfInstance> {
@ -132,17 +200,29 @@ pub fn name_of_instance(s: Span) -> IResult<Span, NameOfInstance> {
pub fn list_of_port_connections(s: Span) -> IResult<Span, ListOfPortConnections> {
alt((
map(
separated_nonempty_list(symbol(","), ordered_port_connection),
|x| ListOfPortConnections::Ordered(x),
),
map(
separated_nonempty_list(symbol(","), named_port_connection),
|x| ListOfPortConnections::Named(x),
),
list_of_port_connections_ordered,
list_of_port_connections_named,
))(s)
}
pub fn list_of_port_connections_ordered(s: Span) -> IResult<Span, ListOfPortConnections> {
let (s, a) = ordered_port_connection(s)?;
let (s, b) = many0(pair(symbol(","), ordered_port_connection))(s)?;
Ok((
s,
ListOfPortConnections::Ordered(ListOfPortConnectionsOrdered { nodes: (a, b) }),
))
}
pub fn list_of_port_connections_named(s: Span) -> IResult<Span, ListOfPortConnections> {
let (s, a) = named_port_connection(s)?;
let (s, b) = many0(pair(symbol(","), named_port_connection))(s)?;
Ok((
s,
ListOfPortConnections::Named(ListOfPortConnectionsNamed { nodes: (a, b) }),
))
}
pub fn ordered_port_connection(s: Span) -> IResult<Span, OrderedPortConnection> {
let (s, x) = many0(attribute_instance)(s)?;
let (s, y) = opt(expression)(s)?;
@ -157,24 +237,24 @@ pub fn named_port_connection(s: Span) -> IResult<Span, NamedPortConnection> {
}
pub fn named_port_connection_identifier(s: Span) -> IResult<Span, NamedPortConnection> {
let (s, x) = many0(attribute_instance)(s)?;
let (s, _) = symbol(".")(s)?;
let (s, y) = port_identifier(s)?;
let (s, z) = opt(paren(opt(expression)))(s)?;
let z = if let Some(Some(z)) = z { Some(z) } else { None };
let (s, a) = many0(attribute_instance)(s)?;
let (s, b) = symbol(".")(s)?;
let (s, c) = port_identifier(s)?;
let (s, d) = opt(paren2(opt(expression)))(s)?;
Ok((
s,
NamedPortConnection::Identifier(NamedPortConnectionIdentifier { nodes: (x, y, z) }),
NamedPortConnection::Identifier(NamedPortConnectionIdentifier {
nodes: (a, b, c, d),
}),
))
}
pub fn named_port_connection_asterisk(s: Span) -> IResult<Span, NamedPortConnection> {
let (s, x) = many0(attribute_instance)(s)?;
let (s, _) = symbol(".")(s)?;
let (s, _) = symbol("*")(s)?;
let (s, a) = many0(attribute_instance)(s)?;
let (s, b) = symbol(".*")(s)?;
Ok((
s,
NamedPortConnection::Asterisk(NamedPortConnectionAsterisk { nodes: (x,) }),
NamedPortConnection::Asterisk(NamedPortConnectionAsterisk { nodes: (a, b) }),
))
}