use super::{
super::{combinator::*, expression::*, identifier::*},
*,
};
use crate::ast::*;
pub fn concrete_types(input: &str) -> ParseResult<Type> {
alt((
aggregation_types,
simple_types.map(Type::Simple),
type_ref.map(Type::Named),
))
.parse(input)
}
pub fn aggregation_types(input: &str) -> ParseResult<Type> {
alt((array_type, bag_type, list_type, set_type)).parse(input)
}
pub fn array_type(input: &str) -> ParseResult<Type> {
tuple((
tag("ARRAY"),
bound_spec,
tag("OF"),
opt(tag("OPTIONAL")),
opt(tag("UNIQUE")),
instantiable_type,
))
.map(|(_set, bound, _of, optional, unique, base)| Type::Array {
bound: Some(bound), unique: unique.is_some(),
optional: optional.is_some(),
base: Box::new(base),
})
.parse(input)
}
pub fn bag_type(input: &str) -> ParseResult<Type> {
tuple((tag("BAG"), opt(bound_spec), tag("OF"), instantiable_type))
.map(|(_set, bound, _of, base)| Type::Bag {
bound,
base: Box::new(base),
})
.parse(input)
}
pub fn list_type(input: &str) -> ParseResult<Type> {
tuple((
tag("LIST"),
opt(bound_spec),
tag("OF"),
opt(tag("UNIQUE")),
instantiable_type,
))
.map(|(_set, bound, _of, unique, base)| Type::List {
bound,
unique: unique.is_some(),
base: Box::new(base),
})
.parse(input)
}
pub fn set_type(input: &str) -> ParseResult<Type> {
tuple((tag("SET"), opt(bound_spec), tag("OF"), instantiable_type))
.map(|(_set, bound, _of, base)| Type::Set {
bound,
base: Box::new(base),
})
.parse(input)
}
pub fn bound_1(input: &str) -> ParseResult<Expression> {
numeric_expression(input)
}
pub fn bound_2(input: &str) -> ParseResult<Expression> {
numeric_expression(input)
}
pub fn bound_spec(input: &str) -> ParseResult<Bound> {
tuple((char('['), bound_1, char(':'), bound_2, char(']')))
.map(|(_open, lower, _comma, upper, _close)| Bound { lower, upper })
.parse(input)
}
pub fn instantiable_type(input: &str) -> ParseResult<Type> {
alt((concrete_types, entity_ref.map(Type::Named))).parse(input)
}