Interface TypeBuilder
Interface for constructing riskscape Type
objects from a riskscape type-expression, e.g.
# returns a simple text type
text
# returns an integer that can be null
nullable(integer)
# returns a one-based enum containing foo -1, bar -2, baz -3
enum('foo', 'bar', 'baz')
# returns a WithinRange type of Integers between 0 and 100
range(integer, 0, 100)
# return a struct
struct(animal: enum('cat', 'dog', 'pig'), weight: range(floating, 0, 1000000))
-
Method Summary
Modifier and TypeMethodDescriptionConstruct a RiskscapeType
from a type-expression.Construct a type from a complex type expression - e.g.buildSimpleType
(AST.Symbol symbol) Construct a type from a symbol.default <T extends AST>
Tdefault Object
expectConstant
(AST ast, Function<AST, RuntimeException> orElse) Helper method forTypeConstructor
s that expects anAST
to be anAST.Constant
and return its java value.expectConstantsOfType
(AST.ComplexType complexType, Type subType, List<AST> args, int argOffset) Helper method forTypeConstructor
s that expects the tail of an argument list to all be constants that coerce to a particular riskscape type.default Type
expectType
(AST ast, Function<AST, RuntimeException> orElse) Helper method forTypeConstructor
s for expecting anAST
to be either aAST.Symbol
or aAST.ComplexType
.A type set to use for referring to user-definedLinkedType
s
-
Method Details
-
build
Construct a Riskscape
Type
from a type-expression.- Throws:
TypeBuildingException
- if there was a problem building the type
-
buildSimpleType
Construct a type from a symbol. These are normally going to be
SimpleType
s but this is not enforced -
buildComplexType
Construct a type from a complex type expression - e.g. the type is a composition of other types
-
getTypeSet
TypeSet getTypeSet()A type set to use for referring to user-defined
LinkedType
s -
expectType
Helper method for
TypeConstructor
s for expecting anAST
to be either aAST.Symbol
or aAST.ComplexType
. If ast is a type-ish node, then aType
is built and returned- Parameters:
orElse
- exception supplier if ast is not of the correct type
-
expectConstant
Helper method for
TypeConstructor
s that expects anAST
to be anAST.Constant
and return its java value.- Parameters:
orElse
- exception supplier if ast is not anAST.Constant
-
expectConstantsOfType
default List<Object> expectConstantsOfType(AST.ComplexType complexType, Type subType, List<AST> args, int argOffset) Helper method for
TypeConstructor
s that expects the tail of an argument list to all be constants that coerce to a particular riskscape type. -
expectAST
default <T extends AST> T expectAST(Class<T> expectedASTClass, AST ast, Function<AST, RuntimeException> orElse) Helper method for
TypeConstructor
s for return a castAST
where it is a particular type ofAST
node.- Parameters:
expectedASTClass
- the desired classast
- the node to checkorElse
- an exception supplier if the ast is not of the desired type.- Returns:
- the ast, cast according to expectedASTClass
-