Split grammar (#15)

* Move grammar parsers to entity classes

* Fix includes

* Fix Entity5 test

* Small detail

* Fix unknown keys entities

* Persistent grammar objects

* Fix grammar

* Grammar from variable
This commit is contained in:
barsdeveloper
2023-09-18 21:13:28 +02:00
committed by GitHub
parent 872bdb7128
commit 78c62ee59a
48 changed files with 1807 additions and 1845 deletions

View File

@@ -1,5 +1,6 @@
import ComputedType from "./ComputedType.js"
import MirroredEntity from "./MirroredEntity.js"
import Serializable from "../serialization/Serializable.js"
import SerializerFactory from "../serialization/SerializerFactory.js"
import Union from "./Union.js"
import Utility from "../Utility.js"
@@ -42,7 +43,7 @@ import Utility from "../Utility.js"
* }} TypeGetter
*/
export default class IEntity {
export default class IEntity extends Serializable {
/** @type {String | Union<String[]>} */
static lookbehind = ""
@@ -62,6 +63,7 @@ export default class IEntity {
}
constructor(values = {}, suppressWarns = false) {
super()
/** @type {String} */ this.lookbehind
const Self = /** @type {EntityConstructor} */(this.constructor)
let attributes = Self.attributes
@@ -86,6 +88,9 @@ export default class IEntity {
const valuesNames = Object.keys(values)
const attributesNames = Object.keys(attributes)
const allAttributesNames = Utility.mergeArrays(valuesNames, attributesNames)
if (valuesNames.includes("lookbehind")) {
this.lookbehind = undefined // To keep it first
}
for (const attributeName of allAttributesNames) {
if (attributeName == "attributes") {
continue
@@ -219,6 +224,11 @@ export default class IEntity {
}
}
/**
* @template {new (...args: any) => any} C
* @param {C} type
* @returns {value is InstanceType<C>}
*/
static isValueOfType(value, type) {
return value != null && (value instanceof type || value.constructor === type)
}