Add concatenations

This commit is contained in:
dalance 2019-07-03 16:00:19 +09:00
parent 2f5396642f
commit 7b95c76fd4
5 changed files with 230 additions and 80 deletions

213
src/concatenations.rs Normal file
View File

@ -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<Expression<'a>>,
}
#[derive(Debug)]
pub struct ConstantConcatenation<'a> {
pub expression: Vec<ConstantExpression<'a>>,
}
#[derive(Debug)]
pub struct ConstantMultipleConcatenation<'a> {
pub expression: ConstantExpression<'a>,
pub concatenation: ConstantConcatenation<'a>,
}
#[derive(Debug)]
pub struct ModulePathConcatenation<'a> {
pub expression: Vec<ModulePathExpression<'a>>,
}
#[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<SliceSize<'a>>,
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<StreamExpression<'a>>,
}
#[derive(Debug)]
pub struct StreamExpression<'a> {
pub expression: Expression<'a>,
pub with: Option<ArrayRangeExpression<'a>>,
}
#[derive(Debug)]
pub struct ArrayRangeExpression<'a> {
pub arg0: Expression<'a>,
pub operator: Option<&'a str>,
pub arg1: Option<Expression<'a>>,
}
// -----------------------------------------------------------------------------
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, ()))
}
// -----------------------------------------------------------------------------

View File

@ -1,5 +1,6 @@
pub mod attributes; pub mod attributes;
pub mod comments; pub mod comments;
pub mod concatenations;
pub mod expressions; pub mod expressions;
pub mod identifiers; pub mod identifiers;
pub mod lvalues; pub mod lvalues;

View File

@ -1,3 +1,4 @@
use crate::concatenations::*;
use crate::identifiers::*; use crate::identifiers::*;
use crate::primaries::*; use crate::primaries::*;
use crate::util::*; use crate::util::*;

View File

@ -1,3 +1,4 @@
use crate::concatenations::*;
use crate::expressions::*; use crate::expressions::*;
use crate::identifiers::*; use crate::identifiers::*;
use crate::numbers::*; use crate::numbers::*;
@ -58,13 +59,13 @@ pub struct ConstantPrimaryEnum<'a> {
#[derive(Debug)] #[derive(Debug)]
pub struct ConstantPrimaryConcatenation<'a> { pub struct ConstantPrimaryConcatenation<'a> {
concatenation: Concatenation<'a>, concatenation: ConstantConcatenation<'a>,
range: Option<ConstantRangeExpression<'a>>, range: Option<ConstantRangeExpression<'a>>,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct ConstantPrimaryMultipleConcatenation<'a> { pub struct ConstantPrimaryMultipleConcatenation<'a> {
concatenation: MultipleConcatenation<'a>, concatenation: ConstantMultipleConcatenation<'a>,
range: Option<ConstantRangeExpression<'a>>, range: Option<ConstantRangeExpression<'a>>,
} }
@ -82,7 +83,7 @@ pub enum ModulePathPrimary<'a> {
pub enum Primary<'a> { pub enum Primary<'a> {
PrimaryLiteral(PrimaryLiteral<'a>), PrimaryLiteral(PrimaryLiteral<'a>),
Hierarchical(PrimaryHierarchical<'a>), Hierarchical(PrimaryHierarchical<'a>),
EmptyUnpackedArrayConcatenation(EmptyUnpackedArrayConcatenation<'a>), EmptyUnpackedArrayConcatenation,
Concatenation(PrimaryConcatenation<'a>), Concatenation(PrimaryConcatenation<'a>),
MultipleConcatenation(PrimaryMultipleConcatenation<'a>), MultipleConcatenation(PrimaryMultipleConcatenation<'a>),
FunctionSubroutineCall(SubroutineCall<'a>), FunctionSubroutineCall(SubroutineCall<'a>),
@ -339,8 +340,8 @@ pub fn primary(s: &str) -> IResult<&str, Primary> {
alt(( alt((
map(primary_literal, |x| Primary::PrimaryLiteral(x)), map(primary_literal, |x| Primary::PrimaryLiteral(x)),
primary_hierarchical, primary_hierarchical,
map(empty_unpacked_array_concatenation, |x| { map(empty_unpacked_array_concatenation, |_| {
Primary::EmptyUnpackedArrayConcatenation(x) Primary::EmptyUnpackedArrayConcatenation
}), }),
primary_concatenation, primary_concatenation,
map(function_subroutine_call, |x| { map(function_subroutine_call, |x| {

View File

@ -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)] #[derive(Debug)]
pub struct CastingType<'a> { pub struct CastingType<'a> {
pub raw: Vec<&'a str>, pub raw: Vec<&'a str>,
@ -52,31 +37,11 @@ pub struct TypeReference<'a> {
pub raw: Vec<&'a str>, 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)] #[derive(Debug)]
pub struct AssignmentPatternExpression<'a> { pub struct AssignmentPatternExpression<'a> {
pub raw: Vec<&'a str>, pub raw: Vec<&'a str>,
} }
#[derive(Debug)]
pub struct StreamingConcatenation<'a> {
pub raw: Vec<&'a str>,
}
#[derive(Debug)] #[derive(Debug)]
pub struct SequenceMethodCall<'a> { pub struct SequenceMethodCall<'a> {
pub raw: Vec<&'a str>, pub raw: Vec<&'a str>,
@ -127,22 +92,15 @@ pub struct ConstraintBlock<'a> {
pub raw: Vec<&'a str>, 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> { pub fn class_scope(s: &str) -> IResult<&str, Scope> {
Ok((s, Scope::ClassScope)) 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> { pub fn casting_type(s: &str) -> IResult<&str, CastingType> {
Ok((s, CastingType { raw: vec![] })) Ok((s, CastingType { raw: vec![] }))
} }
@ -161,38 +119,10 @@ pub fn type_reference(s: &str) -> IResult<&str, TypeReference> {
Ok((s, TypeReference { raw: vec![] })) 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> { pub fn assignment_pattern_expression(s: &str) -> IResult<&str, AssignmentPatternExpression> {
Ok((s, AssignmentPatternExpression { raw: vec![] })) 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> { pub fn sequence_method_call(s: &str) -> IResult<&str, SequenceMethodCall> {
Ok((s, SequenceMethodCall { raw: vec![] })) Ok((s, SequenceMethodCall { raw: vec![] }))
} }
@ -244,3 +174,7 @@ pub fn variable_identifier_list(s: &str) -> IResult<&str, Vec<Identifier>> {
pub fn identifier_list(s: &str) -> IResult<&str, Vec<Identifier>> { pub fn identifier_list(s: &str) -> IResult<&str, Vec<Identifier>> {
Ok((s, vec![])) Ok((s, vec![]))
} }
pub fn simple_type(s: &str) -> IResult<&str, SimpleType> {
Ok((s, SimpleType { raw: vec![] }))
}