ceylon / ceylon.ast

Apache License 2.0
18 stars 3 forks source link

compilation units #53

Closed lucaswerkmeister closed 10 years ago

lucaswerkmeister commented 10 years ago

There are three kinds of compilation unit:

  • A regular compilation unit contains a list of toplevel type, value, or function definitions.
  • A module descriptor […] contains a module declaration. The file must be named module.ceylon.
  • A package descriptor […] contains a package declaration. The file must be named package.ceylon.

[…]

Import* (ModuleDescriptor | PackageDescriptor | Declaration*)

The following approach would seem natural, I think:

However, since regular compilation unit is probably going to be the most used type by far, and it’s also probably what most people think of when they hear “compilation unit”, I’m considering something like this instead:

lucaswerkmeister commented 10 years ago

I’ll almost certainly not use RegularCompilationUnit. It’s far too long, considering that “regular” really doesn’t mean anything.

gavinking commented 10 years ago

@lucaswerkmeister I'm not sure I would do it like this. The current restriction that you can't mix descriptors and declarations might not last forever. I don't see why you need to distinguish between the different kinds of compilation unit.

lucaswerkmeister commented 10 years ago

So you would rather have CompilationUnit(Declaration[] declarations, ModuleDescriptor? mod, PackageDescriptor? dec)? I don’t like that… it would mean that at the moment, we’d have to assert that e. g. a module descriptor doesn’t also have declarations. So far, I’ve only used very few asserts in the node initializers, and instead represented almost every restriction through the type system.

If the language changes, then ceylon.ast will have to change as well… I wouldn’t try to anticipate every possible language change. Unless there are concrete plans for this change?

We could still have a single class CompilationUnit(Import[] imports, Declaration[]|ModuleDescriptor|PackageDescriptor content). But that doesn’t feel good to me either – at least at the moment, the three kinds of compilation units are completely separate, so I don’t see why they should be mashed together like this.

lucaswerkmeister commented 10 years ago

I’m going to implement

now.

lucaswerkmeister commented 10 years ago

Done. The abstract AnyCompilationUnit has separate fields for declarations and descriptors; this way, AST consumers can use them in such a way that they won’t break when it’s allowed to put mix compilation unit contents freely (they can just ignore that subtypes of AnyCompilationUnit exist).