Source Code

This module contains helper functions to simplify creating ceylon.ast.core nodes.

The most useful functions are baseExpression() and baseType(), which allow you to abbreviate

BaseExpression(MemberNameWithTypeArguments(LIdentifier("null")))
BaseType(TypeNameWithTypeArguments(UIdentifier("String")))

as

baseExpression("null")
baseType("String")

Many of the functions in this package are also designed to be used with named arguments, where the “listed” argument (comma-separated) is assigned to the first unassigned iterable parameter. For example, we intend you to write

annotations {
    documentation = "Documentation.";
    "shared", "actual", "default"
}

and not

annotations {
    documentation = "Documentation.";
    annotations = { "shared", "actual", "default" };
}

This is especially important for the functionDefinition(), classDefinition() and interfaceDefinition() functions.

Packages
ceylon.ast.create
Dependencies
ceylon.ast.core1.1.0
ceylon.collection1.1.0
Aliases
BodyIshSource Codeshared BodyIsh=> Body|{<Declaration|Statement>*}

Something that can readily be converted to a Body using the block(), classBody() or interfaceBody() utility functions.

CaseTypeIshSource Codeshared CaseTypeIsh=> PrimaryTypeIsh|IdentifierIsh
IdentifierIshSource Codeshared IdentifierIsh=> Identifier|String

Something that can readily be converted to an Identifier using the identifier() utility function.

PositionalArgumentIshSource Codeshared PositionalArgumentIsh=> Expression|SpreadArgument|Comprehension
PrimaryTypeIshSource Codeshared PrimaryTypeIsh=> PrimaryType|IdentifierIsh
TypeParameterIshSource Codeshared TypeParameterIsh=> TypeParameter|IdentifierIsh
Functions
annotationsSource Codeshared Annotations annotations(StringLiteral|String? doc = null, {<Annotation|IdentifierIsh>*} annotations = ...)

Converts a string (or literal) and a stream of annotation names (or annotations) to Annotations.

Usage examples:

annotations { "shared", "actual" }
annotations {
    doc = "Determines if the stream is empty, that is to say, if 
           the iterator returns no elements.";
    "shared", "default"
}
Parameters:
  • doc = null
  • annotations = {}
baseExpressionSource Codeshared BaseExpression baseExpression(IdentifierIsh name, <IdentifierIsh|Type>[] typeArguments)

A utility function to create a BaseExpression directly from a string, without having to use NameWithTypeArguments (a rather verbose name).

(The type arguments will all be invariant; if you want to use use-site variance, you have to construct the TypeArgument objects yourself.)

Usage examples:

baseExpression("null") // null
baseExpression("String") // String; not to be confused with baseType("String")!
baseExpression("emptyOrSingleton", "Integer") // emptyOrSingleton<Integer>
baseTypeSource Codeshared BaseType baseType(IdentifierIsh name, <IdentifierIsh|Type>[] typeArguments)

A utility function to create a BaseType directly from a string, without having to use TypeNameWithTypeArguments (a rather verbose name).

(The type arguments will all be invariant; if you want to use use-site variance, you have to construct the TypeArgument objects yourself.)

Usage examples:

baseType("String") // String
baseType("Sequence", "Integer") // Sequence<Integer>
blockSource Codeshared Block block(BodyIsh content)
Throws
caseTypesSource Codeshared CaseTypes caseTypes([CaseTypeIsh+] caseTypes)

Converts a stream of primary types and anonymous class names to CaseTypes.

Usage examples:

caseTypes("Other") // self type constraint
caseTypes("true", "false")
classBodySource Codeshared ClassBody classBody(BodyIsh content)
Throws
classDefinitionSource Codeshared ClassDefinition classDefinition(IdentifierIsh name, Parameters|{Parameter*} parameters = ..., {<Declaration|Statement>*} body = [], {CaseTypeIsh*}|CaseTypes? caseTypes = null, ExtendedType? extendedType = null, {PrimaryTypeIsh*}|SatisfiedTypes? satisfiedTypes = null, {TypeParameterIsh*}|TypeParameters? typeParameters = null, {TypeConstraint*} typeConstraints = [], Annotations annotations = ...)

Creates a class definition.

Usage example:

classDefinition {
    name = "MyClass";
    parameters = {
        ValueParameter {
            annotations = annotations { "shared", "actual" };
            type = baseType("String");
            name = lidentifier("string");
        }
    };
    caseTypes = { "CT1", "CT2" };
    extendedType = extendedType {
        "Super";
        IntegerLiteral("1"), baseExpression("string")
    };
    satisfiedTypes = { "Identifiable" };
    typeParameters = { "Element", "Absent" };
    annotations = annotations {
        "My class does something.

         It’s the most amazing class ever.";
        "shared"
    };

    functionDefinition {
        // ...
    },
    functionDefinition {
        // ...
    }
}
Parameters:
  • parameters = Parameters()
  • body = []
  • caseTypes = null
  • extendedType = null
  • satisfiedTypes = null
  • typeParameters = null
  • typeConstraints = []
  • annotations = Annotations()
classInstantiationSource Codeshared ClassInstantiation classInstantiation(IdentifierIsh|TypeNameWithTypeArguments name, Super? qualifier = null, {PositionalArgumentIsh*} arguments = ...)

Creates a ClassInstantiation from its name, arguments, and optionally qualifier.

Keep in mind that ClassInstantiation appears only as child node of ExtendedType and ClassSpecifier, for which you should just use extendedType() / classSpecifier() instead. A normal class instantiation is just a regular Invocation.

Usage examples:

classInstantiation("Sup")
classInstantiation {
    name = "Point";
    baseExpression("x"),
    baseExpression("y")
}
Parameters:
  • qualifier = null
  • arguments = {}
classSpecifierSource Codeshared ClassSpecifier classSpecifier(IdentifierIsh|TypeNameWithTypeArguments name, Super? qualifier = null, {PositionalArgumentIsh*} arguments = ...)

Creates a ClassSpecifier from its name, arguments, and optionally qualifier.

Usage examples:

classSpecifier("Object")
classSpecifier {
    name = "Point";
    baseExpression("y"),
    baseExpression("x")
}
Parameters:
  • qualifier = null
  • arguments = {}
extendedTypeSource Codeshared ExtendedType extendedType(IdentifierIsh|TypeNameWithTypeArguments name, Super? qualifier = null, {PositionalArgumentIsh*} arguments = ...)

Creates an ExtendedType from its name, arguments, and optionally qualifier.

Usage examples:

extendedType("Sup")
extendedType {
    name = "Point";
    baseExpression("x"),
    baseExpression("y")
}
Parameters:
  • qualifier = null
  • arguments = {}
functionDefinitionSource Codeshared FunctionDefinition functionDefinition(IdentifierIsh name, Type|VoidModifier|FunctionModifier|DynamicModifier type, {Parameters+}|Parameters|{Parameter*} parameters = ..., {<Declaration|Statement>*} block = [], {TypeParameterIsh*}|TypeParameters? typeParameters = null, {TypeConstraint*} typeConstraints = [], Annotations annotations = ...)

Creates a function definition.

Usage example:

functionDefinition {
    name = "myFunction";
    type = VoidModifier();
    parameters = {
        ValueParameter {
            type = baseType("String");
            name = lidentifier("name");
        }
    };
    typeParameters = { "Element" };
    annotations = annotations {
        "My function does something.

         It’s the most amazing function ever.";
        "shared", "actual", "default"
    };

    invocationStatement { invoked = "print"; StringLiteral("Hello, World!") }
}
Parameters:
  • parameters = [Parameters()]
  • block = []
  • typeParameters = null
  • typeConstraints = []
  • annotations = Annotations()
identifierSource Codeshared Identifier identifier(IdentifierIsh text)

Parses an identifier from its text. The text may contain the prefix, but no escape sequences.

Examples:

identifier("name")
identifier("S_``state``")
interfaceBodySource Codeshared InterfaceBody interfaceBody(BodyIsh content)
Throws
interfaceDefinitionSource Codeshared InterfaceDefinition interfaceDefinition(IdentifierIsh name, {Declaration*} body = [], {CaseTypeIsh*}|CaseTypes? caseTypes = null, {PrimaryTypeIsh*}|SatisfiedTypes? satisfiedTypes = null, {TypeParameterIsh*}|TypeParameters? typeParameters = null, {TypeConstraint*} typeConstraints = [], Annotations annotations = ...)

Creates an interface definition.

Usage example:

interfaceDefinition {
    name = "MyInterface";
    caseTypes = { "CT1", "CT2" };
    satisfiedTypes = { "Identifiable" };
    typeParameters = { "Element", "Absent" };
    annotations = annotations {
        "My interface does something.

         It’s the most amazing interface ever.";
        "shared", "sealed"
    };

    functionDefinition {
        // ...
    },
    functionDefinition {
        // ...
    }
}
Parameters:
  • body = []
  • caseTypes = null
  • satisfiedTypes = null
  • typeParameters = null
  • typeConstraints = []
  • annotations = Annotations()
invocationSource Codeshared Invocation invocation(Primary|IdentifierIsh invoked, {PositionalArgumentIsh*} arguments)

Creates an Invocation using positional arguments.

Usage example:

invocation {
    "printAll";
    StringLiteral("Hello, World!"), StringLiteral("Goodbye, World!")
}
invocationStatementSource Codeshared InvocationStatement invocationStatement(Primary|IdentifierIsh invoked, {PositionalArgumentIsh*} arguments)

Creates an InvocationStatement using positional arguments.

Usage example:

invocationStatement {
    "printAll";
    StringLiteral("Hello, World!"), StringLiteral("Goodbye, World!")
}
lidentifierSource Codeshared LIdentifier lidentifier(IdentifierIsh name)

Utility function to create an LIdentifier, with prefix if and only if necessary.

Examples:

lidentifier("null")
lidentifier(attributeName)
nameWithTypeArgumentsSource Codeshared NameWithTypeArguments nameWithTypeArguments(IdentifierIsh name, TypeArguments? typeArguments = null)

Convenience function to create either a MemberNameWithTypeArguments or a TypeNameWithTypeArguments, depending on the kind of name.

Parameters:
  • typeArguments = null
positionalArgumentsSource Codeshared PositionalArguments positionalArguments(PositionalArgumentIsh[] arguments)

A utility function to create PositionalArguments directly from a list of expressions, spread arguments and comprehensions, without having to use ArgumentList and having to wrap the listed arguments in a sequence.

Usage examples:

positionalArguments()
positionalArguments(thisInstance, SpreadArgument(baseExpression("others")))
primaryTypeSource Codeshared PrimaryType primaryType(PrimaryTypeIsh name)

Converts a type name to a PrimaryType.

This function is not intended for external use. Use baseType() or qualifiedType() instead. It exists only for use in other functions like caseTypes() and satisfiedTypes().

qualifiedExpressionSource Codeshared QualifiedExpression qualifiedExpression(IdentifierIsh|Primary receiverExpression, IdentifierIsh name, <IdentifierIsh|Type>[] typeArguments)

A utility function to create a QualifiedExpression directly from some strings, without having to use TypeNameWithTypeArguments (a rather verbose name).

(The type arguments will all be invariant; if you want to use use-site variance, you have to construct the TypeArgument objects yourself.)

Usage examples:

qualifiedExpression("x", "Y") // x.Y
qualifiedExpression(qualifiedExpression(baseExpression("process"), "arguments"), "first") // process.arguments.first
qualifiedTypeSource Codeshared QualifiedType qualifiedType(IdentifierIsh|SimpleType|GroupedType qualifyingType, IdentifierIsh name, <IdentifierIsh|Type>[] typeArguments)

A utility function to create a QualifiedType directly from a string, without having to use TypeNameWithTypeArguments (a rather verbose name).

(The type arguments will all be invariant; if you want to use use-site variance, you have to construct the TypeArgument objects yourself.)

Usage examples:

qualifiedType("X", "Y") // X.Y
qualifiedType(qualifiedType(baseType("X", "A"), "Y", "B"), "Z", "C") //X<A>.Y<B>.Z<C>
satisfiedTypesSource Codeshared SatisfiedTypes satisfiedTypes([PrimaryTypeIsh+] satisfiedTypes)

Converts a stream of primary types to SatisfiedTypes.

Usage examples:

satisfiedTypes("Identifiable")
satisfiedTypes(baseType("MutableList", "Element"))
stringTemplateSource Codeshared StringTemplate stringTemplate([<StringLiteral|ValueExpression>+] parts)

Utility function to construct a StringTemplate from an alternating list of string literals and expressions.

Using this function, you can list the string literals and expressions in exactly the order in which they appear in the string template (rather than separated into two sequences, as in StringLiteral), at the cost of some type safety.

typeParametersSource Codeshared TypeParameters typeParameters([TypeParameterIsh+] typeParameters)

A utility function to create TypeParameters from a stream of objects that can readily be converted to a TypeParameter.

Usage examples:

typeParameters("Other") // self type
typeParameters("Element") // invariant, e. g. MutableList
typeParameters(
    TypeParameter(UIdentifier("Element"), OutModifier()),
    TypeParameter(UIdentifier("Absent"), OutModifier(), baseType("Null"))
) // covariant, e. g. Iterable
uidentifierSource Codeshared UIdentifier uidentifier(IdentifierIsh name)

Utility function to create an UIdentifier, with prefix if and only if necessary.

Examples:

uidentifier("Anything")
uidentifier(className)