feat: Accept slices as types (#507)

* fix: Avoid emitting redundant parentheses

Fixes #493

* Update lrgrammar

* feat: Accept slices as types

* Update lrgrammar
This commit is contained in:
Markus Westerlind 2020-02-29 11:27:24 +01:00 committed by GitHub
parent 3746d5e3b9
commit c3e1cda5c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 6521 additions and 6443 deletions

View File

@ -57,6 +57,7 @@ impl FreeVariables for repr::TypeRepr {
fn free_variables(&self, type_parameters: &[TypeParameter]) -> Vec<TypeParameter> {
match self {
repr::TypeRepr::Tuple(tys) => tys.free_variables(type_parameters),
repr::TypeRepr::Slice(ty) => ty.free_variables(type_parameters),
repr::TypeRepr::Nominal(data) | repr::TypeRepr::TraitObject(data) => {
data.free_variables(type_parameters)
}

View File

@ -200,6 +200,9 @@ pub enum TypeRef {
// (T1, T2)
Tuple(Vec<TypeRef>),
// [T]
Slice(Box<TypeRef>),
// Foo<'a, 'b, T1, T2>, Foo::Bar, etc
Nominal {
path: Path,
@ -1008,6 +1011,7 @@ impl Display for TypeRef {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
match *self {
TypeRef::Tuple(ref types) => write!(fmt, "({})", Sep(", ", types)),
TypeRef::Slice(ref ty) => write!(fmt, "[{}]", ty),
TypeRef::Nominal {
ref path,
ref types,
@ -1076,6 +1080,7 @@ impl TypeRef {
TypeRef::Tuple(ref types) => {
TypeRepr::Tuple(types.iter().map(TypeRef::type_repr).collect())
}
TypeRef::Slice(ref ty) => TypeRepr::Slice(Box::new(ty.type_repr())),
TypeRef::Nominal {
ref path,
ref types,

View File

@ -182,6 +182,7 @@ pub enum InlinedSymbol {
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum TypeRepr {
Tuple(Vec<TypeRepr>),
Slice(Box<TypeRepr>),
Nominal(NominalTypeRepr),
Associated {
type_parameter: Atom,
@ -239,6 +240,7 @@ impl TypeRepr {
TypeRepr::Tuple(types) => {
TypeRepr::Tuple(types.iter().map(|t| t.bottom_up(op)).collect())
}
TypeRepr::Slice(ty) => TypeRepr::Slice(Box::new(ty.bottom_up(op))),
TypeRepr::Nominal(NominalTypeRepr { path, types }) => {
TypeRepr::Nominal(NominalTypeRepr {
path: path.clone(),
@ -304,6 +306,7 @@ impl TypeRepr {
self.bottom_up(&mut |t| match t {
TypeRepr::Tuple { .. }
| TypeRepr::Slice { .. }
| TypeRepr::Nominal { .. }
| TypeRepr::Associated { .. }
| TypeRepr::TraitObject { .. }
@ -511,6 +514,7 @@ impl Display for TypeRepr {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
match *self {
TypeRepr::Tuple(ref types) => write!(fmt, "({})", Sep(", ", types)),
TypeRepr::Slice(ref ty) => write!(fmt, "[{}]", ty),
TypeRepr::Nominal(ref data) => write!(fmt, "{}", data),
TypeRepr::Associated {
ref type_parameter,

View File

@ -233,6 +233,9 @@ impl MacroExpander {
) -> TypeRef {
match *type_ref {
TypeRef::Tuple(ref trs) => TypeRef::Tuple(self.macro_expand_type_refs(args, trs)),
TypeRef::Slice(ref tr) => {
TypeRef::Slice(Box::new(self.macro_expand_type_ref(args, tr)))
}
TypeRef::Nominal {
ref path,
ref types,

View File

@ -248,6 +248,7 @@ impl<'grammar> TypeInferencer<'grammar> {
.collect::<Result<_, _>>()?;
Ok(TypeRepr::Tuple(types))
}
TypeRef::Slice(ref ty) => self.type_ref(ty).map(|ty| TypeRepr::Slice(Box::new(ty))),
TypeRef::Nominal {
ref path,
ref types,

View File

@ -239,6 +239,9 @@ TypeRef: TypeRef = {
"(" <Comma<TypeRef>> ")" =>
TypeRef::Tuple(<>),
"[" <TypeRef> "]" =>
TypeRef::Slice(Box::new(<>)),
"#" <Symbol> "#" => {
TypeRef::OfSymbol(<>.kind)
},

File diff suppressed because it is too large Load Diff