ifc-expressions
Version:
Parsing and evaluation of IFC expressions
132 lines (131 loc) • 4.35 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_DATE = new SimpleType("ifcDate", Type.STRING);
Type.IFC_DATE_TIME = new SimpleType("ifcDateTime", Type.STRING);
Type.IFC_TIME = new SimpleType("ifcTime", Type.STRING);
Type.IFC_DURATION = new SimpleType("ifcDuration", Type.STRING);
Type.IFC_TIME_STAMP = new SimpleType("ifcTimeStamp", Type.NUMERIC);
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 isIfcDate(actualType) {
return this.isType(actualType, Type.IFC_DATE);
}
static isIfcDateTime(actualType) {
return this.isType(actualType, Type.IFC_DATE_TIME);
}
static isIfcTime(actualType) {
return this.isType(actualType, Type.IFC_TIME);
}
static isIfcDuration(actualType) {
return this.isType(actualType, Type.IFC_DURATION);
}
static isIfcTimeStamp(actualType) {
return this.isType(actualType, Type.IFC_TIME_STAMP);
}
static isType(actualType, type) {
return actualType === type;
}
static boolean() {
return Type.BOOLEAN;
}
static logical() {
return Type.LOGICAL;
}
static string() {
return Type.STRING;
}
static ifcDate() {
return Type.IFC_DATE;
}
static ifcDateTime() {
return Type.IFC_DATE_TIME;
}
static ifcTime() {
return Type.IFC_TIME;
}
static ifcDuration() {
return Type.IFC_DURATION;
}
static ifcTimeStamp() {
return Type.IFC_TIME_STAMP;
}
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