ifc-expressions
Version:
Parsing and evaluation of IFC expressions
102 lines (101 loc) • 3.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Types = exports.Type = void 0;
const SimpleType_js_1 = require("./SimpleType.js");
const TypeDisjunction_js_1 = require("./TypeDisjunction.js");
const ArrayType_js_1 = require("./ArrayType.js");
const TupleType_js_1 = require("./TupleType.js");
class Type {
}
exports.Type = Type;
Type.ANY = new SimpleType_js_1.SimpleType("any");
Type.NUMERIC = new SimpleType_js_1.SimpleType("numeric");
Type.STRING = new SimpleType_js_1.SimpleType("string");
Type.LOGICAL = new SimpleType_js_1.SimpleType("logical");
Type.BOOLEAN = new SimpleType_js_1.SimpleType("boolean", Type.LOGICAL);
Type.IFC_OBJECT_REF = new SimpleType_js_1.SimpleType("ifcObjectRef");
Type.IFC_ELEMENT_REF = new SimpleType_js_1.SimpleType("ifcElementRef", Type.IFC_OBJECT_REF);
Type.IFC_PROPERTY_REF = new SimpleType_js_1.SimpleType("ifcPropertyRef", Type.IFC_OBJECT_REF);
Type.IFC_PROPERTY_SET_REF = new SimpleType_js_1.SimpleType("ifcPropertySetRef", Type.IFC_OBJECT_REF);
Type.IFC_TYPE_OBJECT_REF = new SimpleType_js_1.SimpleType("ifcTypeObjectRef", Type.IFC_OBJECT_REF);
Type.ARRAY = new SimpleType_js_1.SimpleType("array");
class Types {
static or(...types) {
const disjunction = new TypeDisjunction_js_1.TypeDisjunction(...types);
if (disjunction.getTypes().length == 1) {
return disjunction.getTypes()[0];
}
return disjunction;
}
static array(elementType) {
return new ArrayType_js_1.ArrayType(elementType);
}
static tuple(...types) {
return new TupleType_js_1.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_js_1.TypeDisjunction) {
if (expectedType.overlapsWith(actualType)) {
return;
}
}
if (actualType instanceof TupleType_js_1.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();
}
}
}
exports.Types = Types;
//# sourceMappingURL=Types.js.map