From 7b95c76fd49c035348a850c482264b5604a633c7 Mon Sep 17 00:00:00 2001 From: dalance Date: Wed, 3 Jul 2019 16:00:19 +0900 Subject: [PATCH] Add concatenations --- src/concatenations.rs | 213 ++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/lvalues.rs | 1 + src/primaries.rs | 11 ++- src/util.rs | 84 ++--------------- 5 files changed, 230 insertions(+), 80 deletions(-) create mode 100644 src/concatenations.rs diff --git a/src/concatenations.rs b/src/concatenations.rs new file mode 100644 index 0000000..ceff5b8 --- /dev/null +++ b/src/concatenations.rs @@ -0,0 +1,213 @@ +use crate::expressions::*; +use crate::util::*; +use nom::branch::*; +use nom::bytes::complete::*; +use nom::combinator::*; +use nom::multi::*; +use nom::sequence::*; +use nom::IResult; + +// ----------------------------------------------------------------------------- + +#[derive(Debug)] +pub struct Concatenation<'a> { + pub expression: Vec>, +} + +#[derive(Debug)] +pub struct ConstantConcatenation<'a> { + pub expression: Vec>, +} + +#[derive(Debug)] +pub struct ConstantMultipleConcatenation<'a> { + pub expression: ConstantExpression<'a>, + pub concatenation: ConstantConcatenation<'a>, +} + +#[derive(Debug)] +pub struct ModulePathConcatenation<'a> { + pub expression: Vec>, +} + +#[derive(Debug)] +pub struct ModulePathMultipleConcatenation<'a> { + pub expression: ConstantExpression<'a>, + pub concatenation: ModulePathConcatenation<'a>, +} + +#[derive(Debug)] +pub struct MultipleConcatenation<'a> { + pub expression: Expression<'a>, + pub concatenation: Concatenation<'a>, +} + +#[derive(Debug)] +pub struct StreamingConcatenation<'a> { + pub operator: &'a str, + pub size: Option>, + pub concatenation: StreamConcatenation<'a>, +} + +#[derive(Debug)] +pub enum SliceSize<'a> { + Type(SimpleType<'a>), + Expression(ConstantExpression<'a>), +} + +#[derive(Debug)] +pub struct StreamConcatenation<'a> { + pub expression: Vec>, +} + +#[derive(Debug)] +pub struct StreamExpression<'a> { + pub expression: Expression<'a>, + pub with: Option>, +} + +#[derive(Debug)] +pub struct ArrayRangeExpression<'a> { + pub arg0: Expression<'a>, + pub operator: Option<&'a str>, + pub arg1: Option>, +} + +// ----------------------------------------------------------------------------- + +pub fn concatenation(s: &str) -> IResult<&str, Concatenation> { + let (s, _) = tag("{")(s)?; + let (s, expression) = separated_nonempty_list(sp(tag(",")), sp(expression))(s)?; + let (s, _) = sp(tag("}"))(s)?; + Ok((s, Concatenation { expression })) +} + +pub fn constant_concatenation(s: &str) -> IResult<&str, ConstantConcatenation> { + let (s, _) = tag("{")(s)?; + let (s, expression) = separated_nonempty_list(sp(tag(",")), sp(constant_expression))(s)?; + let (s, _) = sp(tag("}"))(s)?; + Ok((s, ConstantConcatenation { expression })) +} + +pub fn constant_multiple_concatenation(s: &str) -> IResult<&str, ConstantMultipleConcatenation> { + let (s, _) = tag("{")(s)?; + let (s, expression) = sp(constant_expression)(s)?; + let (s, concatenation) = sp(constant_concatenation)(s)?; + let (s, _) = sp(tag("}"))(s)?; + Ok(( + s, + ConstantMultipleConcatenation { + expression, + concatenation, + }, + )) +} + +pub fn module_path_concatenation(s: &str) -> IResult<&str, ModulePathConcatenation> { + let (s, _) = tag("{")(s)?; + let (s, expression) = separated_nonempty_list(sp(tag(",")), sp(module_path_expression))(s)?; + let (s, _) = sp(tag("}"))(s)?; + Ok((s, ModulePathConcatenation { expression })) +} + +pub fn module_path_multiple_concatenation( + s: &str, +) -> IResult<&str, ModulePathMultipleConcatenation> { + let (s, _) = tag("{")(s)?; + let (s, expression) = sp(constant_expression)(s)?; + let (s, concatenation) = sp(module_path_concatenation)(s)?; + let (s, _) = sp(tag("}"))(s)?; + Ok(( + s, + ModulePathMultipleConcatenation { + expression, + concatenation, + }, + )) +} + +pub fn multiple_concatenation(s: &str) -> IResult<&str, MultipleConcatenation> { + let (s, _) = tag("{")(s)?; + let (s, expression) = sp(expression)(s)?; + let (s, concatenation) = sp(concatenation)(s)?; + let (s, _) = sp(tag("}"))(s)?; + Ok(( + s, + MultipleConcatenation { + expression, + concatenation, + }, + )) +} + +pub fn streaming_concatenation(s: &str) -> IResult<&str, StreamingConcatenation> { + let (s, _) = tag("{")(s)?; + let (s, operator) = sp(stream_operator)(s)?; + let (s, size) = opt(sp(slice_size))(s)?; + let (s, concatenation) = sp(stream_concatenation)(s)?; + let (s, _) = sp(tag("}"))(s)?; + Ok(( + s, + StreamingConcatenation { + operator, + size, + concatenation, + }, + )) +} + +pub fn stream_operator(s: &str) -> IResult<&str, &str> { + alt((tag(">>"), tag("<<")))(s) +} + +pub fn slice_size(s: &str) -> IResult<&str, SliceSize> { + alt(( + map(simple_type, |x| SliceSize::Type(x)), + map(constant_expression, |x| SliceSize::Expression(x)), + ))(s) +} + +pub fn stream_concatenation(s: &str) -> IResult<&str, StreamConcatenation> { + let (s, _) = tag("{")(s)?; + let (s, expression) = separated_nonempty_list(sp(tag(",")), sp(stream_expression))(s)?; + let (s, _) = sp(tag("}"))(s)?; + Ok((s, StreamConcatenation { expression })) +} + +pub fn stream_expression(s: &str) -> IResult<&str, StreamExpression> { + let (s, expression) = expression(s)?; + let (s, with) = opt(preceded( + sp(tag("with")), + delimited(sp(tag("[")), array_range_expression, sp(tag("]"))), + ))(s)?; + Ok((s, StreamExpression { expression, with })) +} + +pub fn array_range_expression(s: &str) -> IResult<&str, ArrayRangeExpression> { + let (s, arg0) = expression(s)?; + let (s, x) = opt(pair( + alt((sp(tag(":")), sp(tag("+:")), sp(tag("-:")))), + sp(expression), + ))(s)?; + let (operator, arg1) = if let Some((x, y)) = x { + (Some(x), Some(y)) + } else { + (None, None) + }; + Ok(( + s, + ArrayRangeExpression { + arg0, + operator, + arg1, + }, + )) +} + +pub fn empty_unpacked_array_concatenation(s: &str) -> IResult<&str, ()> { + let (s, _) = tag("{")(s)?; + let (s, _) = sp(tag("}"))(s)?; + Ok((s, ())) +} + +// ----------------------------------------------------------------------------- diff --git a/src/lib.rs b/src/lib.rs index 14e759b..f23138f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ pub mod attributes; pub mod comments; +pub mod concatenations; pub mod expressions; pub mod identifiers; pub mod lvalues; diff --git a/src/lvalues.rs b/src/lvalues.rs index e7cb211..f841a1a 100644 --- a/src/lvalues.rs +++ b/src/lvalues.rs @@ -1,3 +1,4 @@ +use crate::concatenations::*; use crate::identifiers::*; use crate::primaries::*; use crate::util::*; diff --git a/src/primaries.rs b/src/primaries.rs index 8980d63..96cd9a3 100644 --- a/src/primaries.rs +++ b/src/primaries.rs @@ -1,3 +1,4 @@ +use crate::concatenations::*; use crate::expressions::*; use crate::identifiers::*; use crate::numbers::*; @@ -58,13 +59,13 @@ pub struct ConstantPrimaryEnum<'a> { #[derive(Debug)] pub struct ConstantPrimaryConcatenation<'a> { - concatenation: Concatenation<'a>, + concatenation: ConstantConcatenation<'a>, range: Option>, } #[derive(Debug)] pub struct ConstantPrimaryMultipleConcatenation<'a> { - concatenation: MultipleConcatenation<'a>, + concatenation: ConstantMultipleConcatenation<'a>, range: Option>, } @@ -82,7 +83,7 @@ pub enum ModulePathPrimary<'a> { pub enum Primary<'a> { PrimaryLiteral(PrimaryLiteral<'a>), Hierarchical(PrimaryHierarchical<'a>), - EmptyUnpackedArrayConcatenation(EmptyUnpackedArrayConcatenation<'a>), + EmptyUnpackedArrayConcatenation, Concatenation(PrimaryConcatenation<'a>), MultipleConcatenation(PrimaryMultipleConcatenation<'a>), FunctionSubroutineCall(SubroutineCall<'a>), @@ -339,8 +340,8 @@ pub fn primary(s: &str) -> IResult<&str, Primary> { alt(( map(primary_literal, |x| Primary::PrimaryLiteral(x)), primary_hierarchical, - map(empty_unpacked_array_concatenation, |x| { - Primary::EmptyUnpackedArrayConcatenation(x) + map(empty_unpacked_array_concatenation, |_| { + Primary::EmptyUnpackedArrayConcatenation }), primary_concatenation, map(function_subroutine_call, |x| { diff --git a/src/util.rs b/src/util.rs index 6f65fd5..5975940 100644 --- a/src/util.rs +++ b/src/util.rs @@ -17,21 +17,6 @@ where // ----------------------------------------------------------------------------- -#[derive(Debug)] -pub struct Concatenation<'a> { - pub raw: Vec<&'a str>, -} - -#[derive(Debug)] -pub struct MultipleConcatenation<'a> { - pub raw: Vec<&'a str>, -} - -#[derive(Debug)] -pub struct ConstantLetExpression<'a> { - pub raw: Vec<&'a str>, -} - #[derive(Debug)] pub struct CastingType<'a> { pub raw: Vec<&'a str>, @@ -52,31 +37,11 @@ pub struct TypeReference<'a> { pub raw: Vec<&'a str>, } -#[derive(Debug)] -pub struct ModulePathConcatenation<'a> { - pub raw: Vec<&'a str>, -} - -#[derive(Debug)] -pub struct ModulePathMultipleConcatenation<'a> { - pub raw: Vec<&'a str>, -} - -#[derive(Debug)] -pub struct EmptyUnpackedArrayConcatenation<'a> { - pub raw: Vec<&'a str>, -} - #[derive(Debug)] pub struct AssignmentPatternExpression<'a> { pub raw: Vec<&'a str>, } -#[derive(Debug)] -pub struct StreamingConcatenation<'a> { - pub raw: Vec<&'a str>, -} - #[derive(Debug)] pub struct SequenceMethodCall<'a> { pub raw: Vec<&'a str>, @@ -127,22 +92,15 @@ pub struct ConstraintBlock<'a> { pub raw: Vec<&'a str>, } +#[derive(Debug)] +pub struct SimpleType<'a> { + pub raw: Vec<&'a str>, +} + pub fn class_scope(s: &str) -> IResult<&str, Scope> { Ok((s, Scope::ClassScope)) } -pub fn constant_concatenation(s: &str) -> IResult<&str, Concatenation> { - Ok((s, Concatenation { raw: vec![] })) -} - -pub fn constant_multiple_concatenation(s: &str) -> IResult<&str, MultipleConcatenation> { - Ok((s, MultipleConcatenation { raw: vec![] })) -} - -pub fn constant_let_expression(s: &str) -> IResult<&str, ConstantLetExpression> { - Ok((s, ConstantLetExpression { raw: vec![] })) -} - pub fn casting_type(s: &str) -> IResult<&str, CastingType> { Ok((s, CastingType { raw: vec![] })) } @@ -161,38 +119,10 @@ pub fn type_reference(s: &str) -> IResult<&str, TypeReference> { Ok((s, TypeReference { raw: vec![] })) } -pub fn module_path_concatenation(s: &str) -> IResult<&str, ModulePathConcatenation> { - Ok((s, ModulePathConcatenation { raw: vec![] })) -} - -pub fn module_path_multiple_concatenation( - s: &str, -) -> IResult<&str, ModulePathMultipleConcatenation> { - Ok((s, ModulePathMultipleConcatenation { raw: vec![] })) -} - -pub fn empty_unpacked_array_concatenation( - s: &str, -) -> IResult<&str, EmptyUnpackedArrayConcatenation> { - Ok((s, EmptyUnpackedArrayConcatenation { raw: vec![] })) -} - -pub fn concatenation(s: &str) -> IResult<&str, Concatenation> { - Ok((s, Concatenation { raw: vec![] })) -} - -pub fn multiple_concatenation(s: &str) -> IResult<&str, MultipleConcatenation> { - Ok((s, MultipleConcatenation { raw: vec![] })) -} - pub fn assignment_pattern_expression(s: &str) -> IResult<&str, AssignmentPatternExpression> { Ok((s, AssignmentPatternExpression { raw: vec![] })) } -pub fn streaming_concatenation(s: &str) -> IResult<&str, StreamingConcatenation> { - Ok((s, StreamingConcatenation { raw: vec![] })) -} - pub fn sequence_method_call(s: &str) -> IResult<&str, SequenceMethodCall> { Ok((s, SequenceMethodCall { raw: vec![] })) } @@ -244,3 +174,7 @@ pub fn variable_identifier_list(s: &str) -> IResult<&str, Vec> { pub fn identifier_list(s: &str) -> IResult<&str, Vec> { Ok((s, vec![])) } + +pub fn simple_type(s: &str) -> IResult<&str, SimpleType> { + Ok((s, SimpleType { raw: vec![] })) +}