Facilities for parsing and formatting type expression-like Strings into Ceylon metamodels.

Type expressions

The TypeParser can be used to parse an expression into a Type. It can be configured to allow unqualified names using Imports (which function rather like the import statement), but by default uses full-qualified names. The parseType() function is provided as a shortcut.

assert(is Type<> stringType = parseType("ceylon.language::String"));

// Adding a Package to the imports is like a wildcard import
assert(is Type<> stringType2 = parseType("String", [`package ceylon.language`]));

// By default the usual abbreviations are supported
assert(is Type<> optionalStringType = parseType("String?", [`package ceylon.language`]);

assert(is ParseError error = parseType("Foo?", [`package ceylon.language`]);

The TypeFormatter does the reverse and formats a Type<> to a String (but not that the string attribute of Type is good if all you need a fully-qualified and unabbreviated expressions).

print(TypeFormatter().format(`String[]`, [`package ceylon.language`]));
// prints "String[]"

Model expressions

The ModelParser supports parsing more general model reference expressions:

assert(is Function<> printFunction = parseModel("ceylon.language::print"));

But currently this doesn't support Imports, nor type abbreviations.

By: Tom Bentley
License: http://www.apache.org/licenses/LICENSE-2.0
Packages
com.github.tombentley.typeparser
Dependencies
ceylon.collection1.2.0
Aliases
Importsshared Imports=> List<Package|ClassOrInterfaceDeclaration|<String->ClassOrInterfaceDeclaration>>
Functions
parseModelshared Model|Type<Anything>|ParseError parseModel(String t)

Parses a “fully-qualified reference expression” returning its Type or Model.

Fully-qualified reference expressions are not defined by the Ceylon language specification, because in Ceylon source code reference expressions always use imported type names, not fully-qualified ones, but the syntax is pretty much as you'd expect.

When given an expression referencing an object, such as ceylon.language::true this function will return the Type in preference to the Value model.

Some examples:

ceylon.language::String
ceylon.language::true     // type Type, not the Value
ceylon.collection::MutableSet<ceylon.json::Object>
ceylon.language::String|ceylon.language::Integer
See also parseType()
parseTypeshared Type<Anything>|ParseError parseType(String t, Imports imports = [], Boolean optionalAbbreviation = true, Boolean entryAbbreviation = true, Boolean sequenceAbbreviation = true, Boolean tupleAbbreviation = true, Boolean callableAbbreviation = true, Boolean iterableAbbreviation = true, Boolean emptyAbbreviation = true)

Parses a “fully-qualified type expression” returning its Type model.

Fully-qualified type expression are not defined by the Ceylon language specification, because in Ceylon source code type expressions always use imported type names, not fully-qualified ones, but the syntax is pretty much as you'd expect.

For example:

ceylon.language::String
ceylon.language::true     // type Type, not the Value
ceylon.collection::MutableSet<ceylon.json::Object>
Parameters:
  • imports = []
  • optionalAbbreviation = true

    Whether to support optional abbreivation syntax X?.

  • entryAbbreviation = true

    Whether to support entry abbreivation syntax X->Y.

  • sequenceAbbreviation = true

    Whether to support sequential abbreivation syntax X[], [Y*] and [Z+].

  • tupleAbbreviation = true

    Whether to support tuple abbreivation syntax [X,Y] and X[3].

  • callableAbbreviation = true

    Whether to support callable abbreivation syntax X(Y).

  • iterableAbbreviation = true

    Whether to support iterable abbreivation syntax {X+} and {Y*}.

  • emptyAbbreviation = true

    Whether to support empty abbreivation syntax [].

See also parseModel()
Classes
TypeFormattershared TypeFormatter

A formatter for Types which can use abbreviations, and produced full-qualified and/or unqualified names according to the given TypeFormatter.imports.

If you don't need abbreviations and do want all types to be fully-qualified using type ceylon.language.meta::type (i.e. type(t).string) will probably be quicker.

TypeParsershared TypeParser

Parses a “type expression” returning its Type model.

Types may be package-qualified or unqualified. To use unqualified types you need to supply a non-empty TypeParser.imports argument, which determines which type names can be used without package-qualification.

To use qualified types the modules containing allow package qualifiers should be passed as the TypeParser.fqResolvableModules argument. This defaults to modules.list which means any type in the runtime can be expressed.

Fully-qualified type expression are not defined by the Ceylon language specification, because in Ceylon source code type expressions always use imported type names, not fully-qualified ones, but the syntax is pretty much as you'd expect.

For example:

ceylon.language::String
ceylon.language::true     // type Type, not the Value
ceylon.collection::MutableSet<ceylon.json::Object>
Exceptions
ParseErrorshared ParseError

Indicates an error during parsing.