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.3.0
ceylon.collection1.3.0
Aliases
BodyIshshared BodyIsh=> Body|{Declaration|Statement*}

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

CaseTypeIshshared CaseTypeIsh=> PrimaryTypeIsh|IdentifierIsh
IdentifierIshshared IdentifierIsh=> Identifier|String

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

PositionalArgumentIshshared PositionalArgumentIsh=> Expression|SpreadArgument|Comprehension
PrimaryTypeIshshared PrimaryTypeIsh=> PrimaryType|IdentifierIsh
TypeParameterIshshared TypeParameterIsh=> TypeParameter|IdentifierIsh
Functions
annotationsshared 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 = {}
baseExpressionshared 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).

This function is not suitable in all cases; if the following restrictions affect you, you should instead use the regular constructors:

  • the type arguments will all be invariant, use-site variance is unavailable
  • empty typeArguments mean the absence of a type argument list; an empty but present type argument list is unavailable

Usage examples:

baseExpression("null") // null
baseExpression("String") // String; not to be confused with baseType("String")!
baseExpression("emptyOrSingleton", "Integer") // emptyOrSingleton<Integer>
baseTypeshared 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).

This function is not suitable in all cases; if the following restrictions affect you, you should instead use the regular constructors:

  • the type arguments will all be invariant, use-site variance is unavailable
  • empty typeArguments mean the absence of a type argument list; an empty but present type argument list is unavailable

Usage examples:

baseType("String") // String
baseType("Sequence", "Integer") // Sequence<Integer>
blockshared Block block(BodyIsh content)
Throws
  • AssertionError

    If the content is a different kind of content.

callableConstructorDefinitionshared CallableConstructorDefinition callableConstructorDefinition(IdentifierIsh name, Parameters|{Parameter*} parameters = ..., {Declaration|Statement*} block = [], ExtendedType? extendedType = null, Annotations annotations = ...)

Creates a callable constructor definition.

Usage example:

callableConstructorDefinition {
    name = "rgb";
    parameters = {
        ValueParameter {
            type = baseType("Integer");
            name = lidentifier("rgb");
        }
    };
    annotations = annotations {
        "Constructs a color, taking the red, green, and blue values
         from the argument integer.";
        "shared"
    };

    ValueSpecification {
        qualifier = This();
        name = lidentifier("rgb");
        specifier = Specifier(baseExpression("rgb"));
    }
}
Parameters:
  • parameters = Parameters()
  • block = []
  • extendedType = null
  • annotations = Annotations()
caseTypesshared 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")
classBodyshared ClassBody classBody(BodyIsh content)

Converts a stream of declarations and statements to a ClassBody.

Throws
  • AssertionError

    If the content is a different kind of content.

classDefinitionshared ClassDefinition classDefinition(IdentifierIsh name, Parameters|{Parameter*}? parameters = ..., {Declaration|Statement*} body = ..., {CaseTypeIsh*}|CaseTypes? caseTypes = ..., ExtendedType? extendedType = ..., {PrimaryTypeIsh*}|SatisfiedTypes? satisfiedTypes = ..., {TypeParameterIsh*}|TypeParameters? typeParameters = ..., {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 = = nu
  • extendedType = = nu
  • satisfiedTypes = = nu
  • typeParameters = = nu
  • typeConstraints = =
  • annotations = = Annotations
classSpecifiershared ClassSpecifier classSpecifier(IdentifierIsh|TypeNameWithTypeArguments name, PackageQualifier|Super? qualifier = null, {PositionalArgumentIsh*} arguments = ...)

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

Note: this only supports extension. If you want to delegate to a constructor, do it manually.

Usage examples:

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

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

Note: this only supports extension. If you want to delegate to a constructor, do it manually.

Usage examples:

extendedType("Sup")
extendedType {
    name = "Point";
    baseExpression("x"),
    baseExpression("y")
}
Parameters:
  • qualifier = null
  • arguments = {}
extensionshared Extension extension(IdentifierIsh|TypeNameWithTypeArguments name, PackageQualifier|Super? qualifier = null, {PositionalArgumentIsh*} arguments = ...)

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

Note: this function does not support null arguments. If you want to define a class with constructors, do it manually.

Usage examples:

extension("Object")
extension {
    name = "Point";
    baseExpression("y"),
    baseExpression("x")
}
Parameters:
  • qualifier = null
  • arguments = {}
functionDefinitionshared FunctionDefinition functionDefinition(IdentifierIsh name, Type|VoidModifier|FunctionModifier|DynamicModifier type, {Parameters+}|Parameters|{Parameter*} parameters = ..., {Declaration|Statement*} block = ..., {TypeParameterIsh*}|TypeParameters? typeParameters = ..., {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 = = nu
  • typeConstraints = =
  • annotations = = Annotations
identifiershared 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``")
interfaceBodyshared InterfaceBody interfaceBody(BodyIsh content)

Converts a stream of declarations to an InterfaceBody.

Throws
  • AssertionError

    If the content is a different kind of content.

interfaceDefinitionshared InterfaceDefinition interfaceDefinition(IdentifierIsh name, {Declaration|Specification*} body = ..., {CaseTypeIsh*}|CaseTypes? caseTypes = ..., {PrimaryTypeIsh*}|SatisfiedTypes? satisfiedTypes = ..., {TypeParameterIsh*}|TypeParameters? typeParameters = ..., {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 = = nu
  • satisfiedTypes = = nu
  • typeParameters = = nu
  • typeConstraints = =
  • annotations = = Annotations
invocationshared Invocation invocation(Primary|IdentifierIsh invoked, {PositionalArgumentIsh*} arguments)

Creates an Invocation using positional arguments.

Usage example:

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

Creates an InvocationStatement using positional arguments.

Usage example:

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

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

Examples:

lidentifier("null")
lidentifier(attributeName)
Throws
  • AssertionError

    If name is a UIdentifier.

nameWithTypeArgumentsshared 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
positionalArgumentsshared 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(This(), SpreadArgument(baseExpression("others")))
primaryTypeshared 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().

qualifiedExpressionshared 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).

This function is not suitable in all cases; if the following restrictions affect you, you should instead use the regular constructors:

  • the type arguments will all be invariant, use-site variance is unavailable
  • empty typeArguments mean the absence of a type argument list; an empty but present type argument list is unavailable

Usage examples:

qualifiedExpression("x", "Y") // x.Y
qualifiedExpression(qualifiedExpression(baseExpression("process"), "arguments"), "first") // process.arguments.first
qualifiedTypeshared 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).

This function is not suitable in all cases; if the following restrictions affect you, you should instead use the regular constructors:

  • the type arguments will all be invariant, use-site variance is unavailable
  • empty typeArguments mean the absence of a type argument list; an empty but present type argument list is unavailable

Usage examples:

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

Converts a stream of primary types to SatisfiedTypes.

Usage examples:

satisfiedTypes("Identifiable")
satisfiedTypes(baseType("MutableList", "Element"))
stringTemplateshared StringTemplate stringTemplate([StringLiteral|Expression+] 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.

typeParametersshared 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
uidentifiershared UIdentifier uidentifier(IdentifierIsh name)

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

Examples:

uidentifier("Anything")
uidentifier(className)
Throws
  • AssertionError

    If name is an LIdentifier.

valueConstructorDefinitionshared ValueConstructorDefinition valueConstructorDefinition(IdentifierIsh name, {Declaration|Statement*} block = [], ExtendedType? extendedType = null, Annotations annotations = ...)

Creates a value constructor definition.

Usage example:

valueConstructorDefinition {
    name = "red";
    annotations = annotations {
        "The color red.";
        "shared"
    };

    ValueSpecification {
        qualifier = This();
        name = lidentifier("rgb");
        specifier = Specifier(IntegerLiteral("#FF0000"));
    }
}
Parameters:
  • block = []
  • extendedType = null
  • annotations = Annotations()