ifc-expressions
Version:
Parsing and evaluation of IFC expressions
97 lines (96 loc) • 3.21 kB
JavaScript
import { SimpleType } from "./SimpleType.js";
import { TypeDisjunction } from "./TypeDisjunction.js";
import { ArrayType } from "./ArrayType.js";
import { TupleType } from "./TupleType.js";
export class Type {
}
Type.ANY = new SimpleType("any");
Type.NUMERIC = new SimpleType("numeric");
Type.STRING = new SimpleType("string");
Type.LOGICAL = new SimpleType("logical");
Type.BOOLEAN = new SimpleType("boolean", Type.LOGICAL);
Type.IFC_OBJECT_REF = new SimpleType("ifcObjectRef");
Type.IFC_ELEMENT_REF = new SimpleType("ifcElementRef", Type.IFC_OBJECT_REF);
Type.IFC_PROPERTY_REF = new SimpleType("ifcPropertyRef", Type.IFC_OBJECT_REF);
Type.IFC_PROPERTY_SET_REF = new SimpleType("ifcPropertySetRef", Type.IFC_OBJECT_REF);
Type.IFC_TYPE_OBJECT_REF = new SimpleType("ifcTypeObjectRef", Type.IFC_OBJECT_REF);
Type.ARRAY = new SimpleType("array");
export class Types {
static or(...types) {
const disjunction = new TypeDisjunction(...types);
if (disjunction.getTypes().length == 1) {
return disjunction.getTypes()[0];
}
return disjunction;
}
static array(elementType) {
return new ArrayType(elementType);
}
static tuple(...types) {
return new TupleType(...types);
}
static isNumeric(actualType) {
return this.isType(actualType, Type.NUMERIC);
}
static isBoolean(actualType) {
return this.isType(actualType, Type.BOOLEAN);
}
static isLogical(actualType) {
return this.isType(actualType, Type.LOGICAL);
}
static isString(actualType) {
return this.isType(actualType, Type.STRING);
}
static isType(actualType, type) {
return actualType === type;
}
static boolean() {
return Type.BOOLEAN;
}
static logical() {
return Type.LOGICAL;
}
static string() {
return Type.STRING;
}
static numeric() {
return Type.NUMERIC;
}
static ifcObjectRef() {
return Type.IFC_OBJECT_REF;
}
static requireIsAssignableFrom(expectedType, actualType, exceptionProducer) {
if (!expectedType.isAssignableFrom(actualType)) {
throw exceptionProducer();
}
}
/**
* Requires overlap if actual is a disjunction, assignable from if it is a type
* @param expectedType
* @param actualType
* @param exceptionProducer
*/
static requireWeakIsAssignableFrom(expectedType, actualType, exceptionProducer) {
if (expectedType.isAssignableFrom(actualType)) {
return;
}
if (actualType instanceof TypeDisjunction) {
if (expectedType.overlapsWith(actualType)) {
return;
}
}
if (actualType instanceof TupleType) {
const weakerType = Types.array(Types.or(...actualType.getTypes()));
if (expectedType.isAssignableFrom(weakerType)) {
return;
}
}
throw exceptionProducer();
}
static requireTypesOverlap(actualType, actualType2, exceptionProducer) {
if (!actualType.overlapsWith(actualType2)) {
throw exceptionProducer();
}
}
}
//# sourceMappingURL=Types.js.map