More tests and fixed

This commit is contained in:
barsdeveloper
2024-06-03 12:42:42 +02:00
parent 8fed17b20f
commit 1a8636bb5d
18 changed files with 484 additions and 433 deletions

428
dist/ueblueprint.js vendored
View File

@@ -2524,6 +2524,7 @@ class IEntity {
static wrap = this.defaultWrapped
static attributeSeparator = ","
static keySeparator = "="
/** @type {(k: String) => String} */
static printKey = k => k
@@ -2531,6 +2532,9 @@ class IEntity {
/** @type {P<IEntity>} */
static grammar = /** @type {any} */(Parsernostrum.failure())
/** @type {P<IEntity>} */
static unknownEntityGrammar
/** @type {{ [key: String]: typeof IEntity }} */
static attributes = {}
@@ -2545,6 +2549,14 @@ class IEntity {
static silent = false // Do not serialize if default
static trailing = false // Add attribute separator after the last attribute when serializing
#trailing = this.Self().trailing
get trailing() {
return this.#trailing
}
set trailing(value) {
this.#trailing = value;
}
/** @type {String | String[]} */
static lookbehind = ""
@@ -2611,7 +2623,6 @@ class IEntity {
*/
static asUniqueClass() {
if (this.name.length) {
// @ts-expect-error
return class extends this { }
}
return this
@@ -2688,16 +2699,6 @@ class IEntity {
return result
}
/**
* @template {typeof IEntity} T
* @this {T}
*/
static flagTrailing(value = true) {
const result = this.asUniqueClass();
result.trailing = value;
return result
}
/**
* @template {typeof IEntity} T
* @this {InstanceType<T>}
@@ -2789,9 +2790,9 @@ class IEntity {
if (Self.quoted) {
keyValue = `"${keyValue}"`;
}
result += Self.attributeSeparator.includes("\n") ? indentation : "";
result += (Self.attributeSeparator.includes("\n") ? indentation : "") + keyValue + Self.keySeparator;
}
let serialization = value.toString(insideString, indentation, printKey);
let serialization = value?.toString(insideString, indentation, printKey);
if (Self.serialized) {
serialization = `"${serialization.replaceAll(/(?<=(?:[^\\]|^)(?:\\\\)*?)"/, '\\"')}"`;
}
@@ -2851,6 +2852,41 @@ class Serializable {
static grammar = Parsernostrum.failure()
}
class AlternativesEntity extends IEntity {
/** @type {(typeof IEntity)[]} */
static alternatives = []
static className() {
let result = super.className();
if (this.alternatives.length) {
result += " (accepting: " + this.alternatives.map(v => v.className()).join(", ") + ")";
}
return result
}
static createGrammar() {
const grammars = this.alternatives.map(entity => entity.grammar);
if (grammars.includes(this.unknownEntityGrammar)) {
return this.unknownEntityGrammar
}
return Parsernostrum.alt(...grammars)
}
/**
* @template {(typeof IEntity)[]} Types
* @param {Types} types
*/
static accepting(...types) {
const result = /** @type {typeof AlternativesEntity & { alternatives: Types }} */(
this.asUniqueClass()
);
result.alternatives = types;
result.grammar = result.createGrammar();
return result
}
}
class Grammar {
/** @type {String} */
@@ -2982,36 +3018,38 @@ class Grammar {
return result
}
/**
* @template {AttributeConstructor<Attribute>} T
* @param {T} entityType
* @param {typeof IEntity} entityType
* @param {String[]} key
* @returns {AttributeInfo}
* @returns {typeof IEntity}
*/
static getAttribute(entityType, key) {
let result;
let type;
if (entityType instanceof Union) {
for (let t of entityType.values) {
if (result = this.getAttribute(t, key)) {
return result
static getAttribute(entityType, [key, ...keys]) {
const attribute = entityType?.attributes?.[key];
if (!attribute) {
return
}
if (attribute.prototype instanceof AlternativesEntity) {
for (const alternative of /** @type {typeof AlternativesEntity} */(attribute).alternatives) {
const candidate = this.getAttribute(alternative, keys);
if (candidate) {
return candidate
}
}
}
if (entityType instanceof IEntity.constructor) {
// @ts-expect-error
result = entityType.attributes[key[0]];
type = result?.type;
} else if (entityType instanceof Array) {
result = entityType[key[0]];
type = result;
if (keys.length > 0) {
return this.getAttribute(attribute, keys)
}
if (key.length > 1) {
return this.getAttribute(type, key.slice(1))
}
return result
return attribute
}
/**
* @param {typeof IEntity} entityType
* @param {*} attributeName
* @param {*} valueSeparator
* @param {*} handleObjectSet
* @returns
*/
static createAttributeGrammar(
entityType,
attributeName = this.attributeName,
@@ -3024,14 +3062,12 @@ class Grammar {
).chain(([attributeName, _1]) => {
const attributeKey = attributeName.split(Configuration.keysSeparator);
const attributeValue = this.getAttribute(entityType, attributeKey);
return this
.grammarFor(attributeValue)
.map(attributeValue =>
values => {
handleObjectSet(values, attributeKey, attributeValue);
Utility.objectSet(values, attributeKey, attributeValue);
}
)
return attributeValue.grammar.map(attributeValue =>
values => {
handleObjectSet(values, attributeKey, attributeValue);
Utility.objectSet(values, attributeKey, attributeValue);
}
)
})
}
@@ -3050,10 +3086,10 @@ class Grammar {
.map(([lookbehind, attributes, trailing]) => {
let values = {};
if (lookbehind.length) {
values["#lookbehind"] = lookbehind;
values["lookbehind"] = lookbehind;
}
attributes.forEach(attributeSetter => attributeSetter(values));
values["#trailing"] = trailing !== undefined;
values["trailing"] = trailing !== undefined;
return values
})
// Decide if we accept the entity or not. It is accepted if it doesn't have too many unexpected keys
@@ -3061,9 +3097,6 @@ class Grammar {
if (entityType.lookbehind instanceof Array || entityType.lookbehind !== lookbehind) {
entityType = entityType.withLookbehind(lookbehind);
}
if (entityType.trailing !== values["#trailing"]) {
entityType = entityType.flagTrailing(values["#trailing"]);
}
return Parsernostrum.success().map(() => new entityType(values))
})
}
@@ -3170,9 +3203,9 @@ class LinearColorEntity extends IEntity {
static getWhite() {
return new LinearColorEntity({
R: 1,
G: 1,
B: 1,
R: new ColorChannelEntity(1),
G: new ColorChannelEntity(1),
B: new ColorChannelEntity(1),
})
}
@@ -4487,53 +4520,21 @@ function pinTitle(entity) {
return result
}
class AlternativesEntity extends IEntity {
/** @type {(typeof IEntity)[]} */
static alternatives = []
static className() {
let result = super.className();
if (this.alternatives.length) {
result += " (accepting: " + this.alternatives.map(v => v.className()).join(", ") + ")";
}
return result
}
static createGrammar() {
const grammars = this.alternatives.map(entity => entity.grammar);
if (grammars.includes(Grammar.unknownValue)) {
return Grammar.unknownValue
}
return Parsernostrum.alt(...grammars)
}
/**
* @template {(typeof IEntity)[]} Types
* @param {Types} types
*/
static accepting(...types) {
const result = /** @type {typeof AlternativesEntity & { alternatives: Types }} */(this.asUniqueClass());
result.alternatives = types;
result.grammar = result.createGrammar();
return result
}
}
/** @template {typeof IEntity} T */
/** @template {IEntity} T */
class ArrayEntity extends IEntity {
/** @type {typeof IEntity} */
static type
static grammar = this.createGrammar()
/** @param {ExtractType<T>[]} values */
/** @param {T[]} values */
constructor(values = []) {
super();
this.values = values;
}
static createGrammar(elementGrammar = this.type?.grammar ?? Parsernostrum.lazy(() => Grammar.unknownValue)) {
/** @returns {P<ArrayEntity<IEntity>>} */
static createGrammar(elementGrammar = this.type?.grammar ?? Parsernostrum.lazy(() => this.unknownEntityGrammar)) {
return this.inlined
? elementGrammar
: Parsernostrum.seq(
@@ -4541,22 +4542,18 @@ class ArrayEntity extends IEntity {
elementGrammar.sepBy(Grammar.commaSeparation).opt(),
Parsernostrum.reg(/\s*(,\s*)?\)/, 1),
).map(([_0, values, trailing]) => {
let self = this;
const hasTrailing = trailing !== undefined;
if (hasTrailing !== self.trailing) {
self = self.flagTrailing(hasTrailing);
}
values = values instanceof Array ? values : [];
return new self(values)
const result = new this(values);
result.trailing = trailing !== undefined;
}).label(`ArrayEntity of ${this.type?.className() ?? "unknown values"}`)
}
/**
* @template {typeof IEntity} T
* @param {NonNullable<T>} type
* @param {T} type
*/
static of(type) {
const result = /** @type {typeof ArrayEntity<T> & { type: ExtractType<T>, grammar: P<ArrayEntity<T>> }} */(
const result = /** @type {{type: T, grammar: P<ArrayEntity<ExtractType<T>>> } & typeof ArrayEntity<ExtractType<T>>} */(
this.asUniqueClass()
);
result.type = /** @type {ExtractType<T>} */(type);
@@ -4567,14 +4564,38 @@ class ArrayEntity extends IEntity {
valueOf() {
return this.values
}
toString(
insideString = false,
indentation = "",
printKey = this.Self().printKey,
) {
let result = this.values.map(v => v?.toString()).join(this.Self().attributeSeparator);
if (this.Self().trailing) {
result += this.Self().attributeSeparator;
}
return `(${result})`
}
}
class BooleanEntity extends IEntity {
static grammar = Parsernostrum.regArray(/(true)|false/i)
.map(v => v[1] ? new this(true) : new this(false))
static grammar = Parsernostrum.regArray(/(true)|(True)|(false)|(False)/)
.map(v => {
const result = (v[1] ?? v[2]) ? new this(true) : new this(false);
result.uppercase = (v[2] ?? v[4]) !== undefined;
return result
})
.label("BooleanEntity")
#uppercase = true
get uppercase() {
return this.#uppercase
}
set uppercase(value) {
this.#uppercase = value;
}
constructor(value = false) {
super();
this.value = value;
@@ -4584,14 +4605,18 @@ class BooleanEntity extends IEntity {
return this.value
}
toString(insideString = false) {
toString(
insideString = false,
indentation = "",
printKey = this.Self().printKey,
) {
return this.value
? insideString
? "true"
: "True"
: insideString
? "false"
: "False"
? this.#uppercase
? "True"
: "true"
: this.#uppercase
? "False"
: "false"
}
}
@@ -4639,8 +4664,11 @@ class IntegerEntity extends NumberEntity {
static grammar = Parsernostrum.numberInteger.map(v => new this(v))
get value() {
return super.value
}
set value(value) {
if (value >= -(1 << 31) && value < 1 << 31) {
if (value >= 1 << 31 && value < -(1 << 31)) {
value = Math.floor(value);
super.value = value;
}
@@ -4651,6 +4679,9 @@ class ByteEntity extends IntegerEntity {
static grammar = Parsernostrum.numberByte.map(v => new this(v))
get value() {
return super.value
}
set value(value) {
if (value % 1 == 0 && value >= 0 && value < 1 << 8) {
super.value = value;
@@ -4681,9 +4712,12 @@ class ComputedTypeEntity extends IEntity {
class SymbolEntity extends IEntity {
static attributeConverter = {
fromAttribute: (value, type) => new this(value),
toAttribute: (value, type) => value.toString()
}
static grammar = Grammar.symbol.map(v => new this(v)).label("SymbolEntity")
/** @param {String} value */
constructor(value = "") {
super();
this.value = value;
@@ -4752,67 +4786,6 @@ class InvariantTextEntity extends IPrintableEntity {
}
}
class LocalizedTextEntity extends IPrintableEntity {
static lookbehind = "NSLOCTEXT"
static grammar = Parsernostrum.regArray(new RegExp(
String.raw`${this.lookbehind}\s*\(`
+ String.raw`\s*"(${Grammar.Regex.InsideString.source})"\s*,`
+ String.raw`\s*"(${Grammar.Regex.InsideString.source})"\s*,`
+ String.raw`\s*"(${Grammar.Regex.InsideString.source})"\s*`
+ String.raw`(,\s+)?`
+ String.raw`\)`,
"m"
)).map(matchResult => {
const self = matchResult[4] ? this.flagTrailing() : this;
return new self(
Utility.unescapeString(matchResult[1]),
Utility.unescapeString(matchResult[2]),
Utility.unescapeString(matchResult[3]),
)
}).label("LocalizedTextEntity")
#namespace
get namespace() {
return this.#namespace
}
set namespace(value) {
this.#namespace = value;
}
#key
get key() {
return this.#key
}
set key(value) {
this.#key = value;
}
#value
get value() {
return this.#value
}
set value(value) {
this.#value = value;
}
constructor(namespace = "", key = "", value = "") {
super();
this.namespace = namespace;
this.key = key;
this.value = value;
}
print() {
return Utility.capitalFirstLetter(this.value)
}
toString() {
const trailer = this.Self().trailing ? ", " : "";
return `${this.lookbehind}(${this.namespace}, ${this.key}, ${this.value}${trailer})`
}
}
class StringEntity extends IPrintableEntity {
static grammar = Parsernostrum.doubleQuotedString
@@ -4842,8 +4815,49 @@ class StringEntity extends IPrintableEntity {
}
}
class LocalizedTextEntity extends IPrintableEntity {
static attributeSeparator = ", "
static printKey = k => ""
static lookbehind = "NSLOCTEXT"
static attributes = {
...super.attributes,
namespace: StringEntity.withDefault(),
key: StringEntity.withDefault(),
value: StringEntity.withDefault(),
}
static grammar = Parsernostrum.regArray(new RegExp(
String.raw`${LocalizedTextEntity.lookbehind}\s*\(`
+ String.raw`\s*"(?<namespace>${Grammar.Regex.InsideString.source})"\s*,`
+ String.raw`\s*"(?<key>${Grammar.Regex.InsideString.source})"\s*,`
+ String.raw`\s*"(?<value>${Grammar.Regex.InsideString.source})"\s*`
+ String.raw`(?<trailing>,\s+)?`
+ String.raw`\)`,
"m"
)).map(({ groups: { namespace, key, value, trailing } }) => {
return new this({
namespace: new (this.attributes.namespace)(Utility.unescapeString(namespace)),
key: new (this.attributes.namespace)(Utility.unescapeString(key)),
value: new (this.attributes.namespace)(Utility.unescapeString(value)),
trailing: trailing !== undefined,
})
}).label("LocalizedTextEntity")
constructor(values = {}) {
super(values);
/** @type {InstanceType<typeof LocalizedTextEntity.attributes.namespace>} */ this.namespace;
/** @type {InstanceType<typeof LocalizedTextEntity.attributes.key>} */ this.key;
/** @type {InstanceType<typeof LocalizedTextEntity.attributes.value>} */ this.value;
}
print() {
return Utility.capitalFirstLetter(this.value.valueOf())
}
}
class FormatTextEntity extends IPrintableEntity {
static attributeSeparator = ", "
static lookbehind = ["LOCGEN_FORMAT_NAMED", "LOCGEN_FORMAT_ORDERED"]
/** @type {P<FormatTextEntity>} */
static grammar = Parsernostrum.lazy(() => Parsernostrum.seq(
@@ -4891,6 +4905,18 @@ class FormatTextEntity extends IPrintableEntity {
: "";
return result
}
toString(
insideString = false,
indentation = "",
printKey = this.Self().printKey,
) {
const separator = this.Self().attributeSeparator;
return this.lookbehind + "("
+ this.values.map(v => v.toString(insideString)).join(separator)
+ (this.Self().trailing ? separator : "")
+ ")"
}
}
class Integer64Entity extends IEntity {
@@ -5703,29 +5729,6 @@ function nodeVariadic(entity) {
}
}
class IdentifierEntity extends IEntity {
static attributeConverter = {
fromAttribute: (value, type) => new IdentifierEntity(value),
toAttribute: (value, type) => value.toString()
}
static grammar = Grammar.symbol.map(v => new this(v))
/** @param {String} value */
constructor(value) {
super();
this.value = value;
}
valueOf() {
return this.value
}
toString() {
return this.value.toString()
}
}
class MacroGraphReferenceEntity extends IEntity {
static attributes = {
@@ -5753,14 +5756,13 @@ class NaturalNumberEntity extends IntegerEntity {
static grammar = Parsernostrum.numberNatural.map(v => new this(v))
get value() {
return super.value
}
set value(value) {
value = Math.round(Utility.clamp(this.value, 0));
super.value = value;
}
constructor(value = 0) {
super(value);
}
}
class ScriptVariableEntity extends IEntity {
@@ -5913,9 +5915,9 @@ class ObjectEntity extends IEntity {
bCommentBubblePinned: BooleanEntity,
bCommentBubbleVisible: BooleanEntity,
NodeComment: StringEntity,
AdvancedPinDisplay: IdentifierEntity,
AdvancedPinDisplay: SymbolEntity,
DelegateReference: VariableReferenceEntity,
EnabledState: IdentifierEntity,
EnabledState: SymbolEntity,
NodeGuid: GuidEntity,
ErrorType: IntegerEntity,
ErrorMsg: StringEntity,
@@ -6554,19 +6556,15 @@ class KeyBindingEntity extends IEntity {
bCtrl: BooleanEntity,
bAlt: BooleanEntity,
bCmd: BooleanEntity,
Key: IdentifierEntity,
Key: SymbolEntity,
}
static grammar = Parsernostrum.alt(
IdentifierEntity.grammar.map(identifier => {
const result = new this();
result.Key = identifier;
return result
}),
SymbolEntity.grammar.map(identifier => new this({ Key: identifier })),
Grammar.createEntityGrammar(this)
)
constructor() {
super();
constructor(values) {
super(values);
/** @type {InstanceType<typeof KeyBindingEntity.attributes.ActionName>} */ this.ActionName;
/** @type {InstanceType<typeof KeyBindingEntity.attributes.bShift>} */ this.bShift;
/** @type {InstanceType<typeof KeyBindingEntity.attributes.bCtrl>} */ this.bCtrl;
@@ -9346,7 +9344,7 @@ class NodeElement extends ISelectableDraggableElement {
advancedPinDisplay: {
type: String,
attribute: "data-advanced-display",
converter: IdentifierEntity.attributeConverter,
converter: SymbolEntity.attributeConverter,
reflect: true,
},
enabledState: {
@@ -9541,7 +9539,7 @@ class NodeElement extends ISelectableDraggableElement {
}
setShowAdvancedPinDisplay(value) {
this.entity.AdvancedPinDisplay = new IdentifierEntity(value ? "Shown" : "Hidden");
this.entity.AdvancedPinDisplay = new SymbolEntity(value ? "Shown" : "Hidden");
this.advancedPinDisplay = this.entity.AdvancedPinDisplay;
}
@@ -13216,7 +13214,7 @@ class UnknownKeysEntity extends IEntity {
Parsernostrum.reg(new RegExp(`(${Grammar.Regex.Path.source}|${Grammar.Regex.Symbol.source}\\s*)?\\(\\s*`), 1),
Parsernostrum.seq(Grammar.attributeName, Grammar.equalSeparation).map(([attribute, equal]) => attribute)
.chain(attributeName =>
Grammar.unknownValue.map(attributeValue =>
this.unknownEntityGrammar.map(attributeValue =>
values => values[attributeName] = attributeValue
)
)
@@ -13287,15 +13285,29 @@ class ToStringSerializer extends Serializer {
}
}
class NullEntity extends IEntity {
static grammar = Parsernostrum.reg(new RegExp(String.raw`\(${Parsernostrum.whitespaceInlineOpt.getParser().regexp.source}\)`))
.map(v => new this())
toString(
insideString = false,
indentation = "",
printKey = this.Self().printKey,
) {
return "()"
}
}
function initializeSerializerFactory() {
Grammar.unknownValue =
IEntity.unknownEntityGrammar =
Parsernostrum.alt(
// Remember to keep the order, otherwise parsing might fail
BooleanEntity.grammar,
GuidEntity.grammar,
Parsernostrum.str("None").map(() => ObjectReferenceEntity.createNoneInstance()),
Grammar.null,
NullEntity.grammar,
NumberEntity.grammar,
ObjectReferenceEntity.fullReferenceGrammar,
StringEntity.grammar,

File diff suppressed because one or more lines are too long

View File

@@ -2,10 +2,10 @@ import Configuration from "../Configuration.js"
import Utility from "../Utility.js"
import nodeTemplateClass from "../decoding/nodeTemplate.js"
import nodeTitle from "../decoding/nodeTitle.js"
import IdentifierEntity from "../entity/IdentifierEntity.js"
import ObjectEntity from "../entity/ObjectEntity.js"
import PinEntity from "../entity/PinEntity.js"
import PinReferenceEntity from "../entity/PinReferenceEntity.js"
import SymbolEntity from "../entity/SymbolEntity.js"
import SerializerFactory from "../serialization/SerializerFactory.js"
import NodeTemplate from "../template/node/NodeTemplate.js"
import ISelectableDraggableElement from "./ISelectableDraggableElement.js"
@@ -28,7 +28,7 @@ export default class NodeElement extends ISelectableDraggableElement {
advancedPinDisplay: {
type: String,
attribute: "data-advanced-display",
converter: IdentifierEntity.attributeConverter,
converter: SymbolEntity.attributeConverter,
reflect: true,
},
enabledState: {
@@ -223,7 +223,7 @@ export default class NodeElement extends ISelectableDraggableElement {
}
setShowAdvancedPinDisplay(value) {
this.entity.AdvancedPinDisplay = new IdentifierEntity(value ? "Shown" : "Hidden")
this.entity.AdvancedPinDisplay = new SymbolEntity(value ? "Shown" : "Hidden")
this.advancedPinDisplay = this.entity.AdvancedPinDisplay
}

View File

@@ -1,5 +1,4 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import IEntity from "./IEntity.js"
export default class AlternativesEntity extends IEntity {
@@ -17,8 +16,8 @@ export default class AlternativesEntity extends IEntity {
static createGrammar() {
const grammars = this.alternatives.map(entity => entity.grammar)
if (grammars.includes(Grammar.unknownValue)) {
return Grammar.unknownValue
if (grammars.includes(this.unknownEntityGrammar)) {
return this.unknownEntityGrammar
}
return P.alt(...grammars)
}

View File

@@ -16,7 +16,7 @@ export default class ArrayEntity extends IEntity {
}
/** @returns {P<ArrayEntity<IEntity>>} */
static createGrammar(elementGrammar = this.type?.grammar ?? P.lazy(() => Grammar.unknownValue)) {
static createGrammar(elementGrammar = this.type?.grammar ?? P.lazy(() => this.unknownEntityGrammar)) {
return this.inlined
? elementGrammar
: P.seq(
@@ -24,13 +24,9 @@ export default class ArrayEntity extends IEntity {
elementGrammar.sepBy(Grammar.commaSeparation).opt(),
P.reg(/\s*(,\s*)?\)/, 1),
).map(([_0, values, trailing]) => {
let self = this
const hasTrailing = trailing !== undefined
if (hasTrailing !== self.trailing) {
self = self.flagTrailing(hasTrailing)
}
values = values instanceof Array ? values : []
return new self(values)
const result = new this(values)
result.trailing = trailing !== undefined
}).label(`ArrayEntity of ${this.type?.className() ?? "unknown values"}`)
}

View File

@@ -3,10 +3,22 @@ import IEntity from "./IEntity.js"
export default class BooleanEntity extends IEntity {
static grammar = P.regArray(/(true)|false/i)
.map(v => v[1] ? new this(true) : new this(false))
static grammar = P.regArray(/(true)|(True)|(false)|(False)/)
.map(v => {
const result = (v[1] ?? v[2]) ? new this(true) : new this(false)
result.uppercase = (v[2] ?? v[4]) !== undefined
return result
})
.label("BooleanEntity")
#uppercase = true
get uppercase() {
return this.#uppercase
}
set uppercase(value) {
this.#uppercase = value
}
constructor(value = false) {
super()
this.value = value
@@ -16,13 +28,17 @@ export default class BooleanEntity extends IEntity {
return this.value
}
toString(insideString = false) {
toString(
insideString = false,
indentation = "",
printKey = this.Self().printKey,
) {
return this.value
? insideString
? "true"
: "True"
: insideString
? "false"
: "False"
? this.#uppercase
? "True"
: "true"
: this.#uppercase
? "False"
: "false"
}
}

View File

@@ -24,6 +24,9 @@ export default class IEntity {
/** @type {P<IEntity>} */
static grammar = /** @type {any} */(P.failure())
/** @type {P<IEntity>} */
static unknownEntityGrammar
/** @type {{ [key: String]: typeof IEntity }} */
static attributes = {}
@@ -38,6 +41,14 @@ export default class IEntity {
static silent = false // Do not serialize if default
static trailing = false // Add attribute separator after the last attribute when serializing
#trailing = this.Self().trailing
get trailing() {
return this.#trailing
}
set trailing(value) {
this.#trailing = value
}
/** @type {String | String[]} */
static lookbehind = ""
@@ -180,16 +191,6 @@ export default class IEntity {
return result
}
/**
* @template {typeof IEntity} T
* @this {T}
*/
static flagTrailing(value = true) {
const result = this.asUniqueClass()
result.trailing = value
return result
}
/**
* @template {typeof IEntity} T
* @this {InstanceType<T>}

View File

@@ -1,25 +0,0 @@
import Grammar from "../serialization/Grammar.js"
import IEntity from "./IEntity.js"
export default class IdentifierEntity extends IEntity {
static attributeConverter = {
fromAttribute: (value, type) => new IdentifierEntity(value),
toAttribute: (value, type) => value.toString()
}
static grammar = Grammar.symbol.map(v => new this(v))
/** @param {String} value */
constructor(value) {
super()
this.value = value
}
valueOf() {
return this.value
}
toString() {
return this.value.toString()
}
}

View File

@@ -2,8 +2,8 @@ import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import BooleanEntity from "./BooleanEntity.js"
import IEntity from "./IEntity.js"
import IdentifierEntity from "./IdentifierEntity.js"
import StringEntity from "./StringEntity.js"
import SymbolEntity from "./SymbolEntity.js"
export default class KeyBindingEntity extends IEntity {
@@ -14,19 +14,15 @@ export default class KeyBindingEntity extends IEntity {
bCtrl: BooleanEntity,
bAlt: BooleanEntity,
bCmd: BooleanEntity,
Key: IdentifierEntity,
Key: SymbolEntity,
}
static grammar = P.alt(
IdentifierEntity.grammar.map(identifier => {
const result = new this()
result.Key = identifier
return result
}),
SymbolEntity.grammar.map(identifier => new this({ Key: identifier })),
Grammar.createEntityGrammar(this)
)
constructor() {
super()
constructor(values) {
super(values)
/** @type {InstanceType<typeof KeyBindingEntity.attributes.ActionName>} */ this.ActionName
/** @type {InstanceType<typeof KeyBindingEntity.attributes.bShift>} */ this.bShift
/** @type {InstanceType<typeof KeyBindingEntity.attributes.bCtrl>} */ this.bCtrl

View File

@@ -85,9 +85,9 @@ export default class LinearColorEntity extends IEntity {
static getWhite() {
return new LinearColorEntity({
R: 1,
G: 1,
B: 1,
R: new ColorChannelEntity(1),
G: new ColorChannelEntity(1),
B: new ColorChannelEntity(1),
})
}

View File

@@ -24,11 +24,11 @@ export default class LocalizedTextEntity extends IPrintableEntity {
+ String.raw`\)`,
"m"
)).map(({ groups: { namespace, key, value, trailing } }) => {
const self = trailing ? this.flagTrailing() : this
return new self({
return new this({
namespace: new (this.attributes.namespace)(Utility.unescapeString(namespace)),
key: new (this.attributes.namespace)(Utility.unescapeString(key)),
value: new (this.attributes.namespace)(Utility.unescapeString(value)),
trailing: trailing !== undefined,
})
}).label("LocalizedTextEntity")

View File

@@ -12,7 +12,6 @@ import BooleanEntity from "./BooleanEntity.js"
import FunctionReferenceEntity from "./FunctionReferenceEntity.js"
import GuidEntity from "./GuidEntity.js"
import IEntity from "./IEntity.js"
import IdentifierEntity from "./IdentifierEntity.js"
import IntegerEntity from "./IntegerEntity.js"
import LinearColorEntity from "./LinearColorEntity.js"
import MacroGraphReferenceEntity from "./MacroGraphReferenceEntity.js"
@@ -123,9 +122,9 @@ export default class ObjectEntity extends IEntity {
bCommentBubblePinned: BooleanEntity,
bCommentBubbleVisible: BooleanEntity,
NodeComment: StringEntity,
AdvancedPinDisplay: IdentifierEntity,
AdvancedPinDisplay: SymbolEntity,
DelegateReference: VariableReferenceEntity,
EnabledState: IdentifierEntity,
EnabledState: SymbolEntity,
NodeGuid: GuidEntity,
ErrorType: IntegerEntity,
ErrorMsg: StringEntity,

View File

@@ -3,9 +3,12 @@ import IEntity from "./IEntity.js"
export default class SymbolEntity extends IEntity {
static attributeConverter = {
fromAttribute: (value, type) => new this(value),
toAttribute: (value, type) => value.toString()
}
static grammar = Grammar.symbol.map(v => new this(v)).label("SymbolEntity")
/** @param {String} value */
constructor(value = "") {
super()
this.value = value

View File

@@ -9,7 +9,7 @@ export default class UnknownKeysEntity extends IEntity {
P.reg(new RegExp(`(${Grammar.Regex.Path.source}|${Grammar.Regex.Symbol.source}\\s*)?\\(\\s*`), 1),
P.seq(Grammar.attributeName, Grammar.equalSeparation).map(([attribute, equal]) => attribute)
.chain(attributeName =>
Grammar.unknownValue.map(attributeValue =>
this.unknownEntityGrammar.map(attributeValue =>
values => values[attributeName] = attributeValue
)
)

View File

@@ -1,6 +1,7 @@
import Parsernostrum from "parsernostrum"
import Configuration from "../Configuration.js"
import Utility from "../Utility.js"
import AlternativesEntity from "../entity/AlternativesEntity.js"
import AttributeInfo from "../entity/AttributeInfo.js"
import IEntity from "../entity/IEntity.js"
import MirroredEntity from "../entity/MirroredEntity.js"
@@ -137,36 +138,38 @@ export default class Grammar {
return result
}
/**
* @template {AttributeConstructor<Attribute>} T
* @param {T} entityType
* @param {typeof IEntity} entityType
* @param {String[]} key
* @returns {AttributeInfo}
* @returns {typeof IEntity}
*/
static getAttribute(entityType, key) {
let result
let type
if (entityType instanceof Union) {
for (let t of entityType.values) {
if (result = this.getAttribute(t, key)) {
return result
static getAttribute(entityType, [key, ...keys]) {
const attribute = entityType?.attributes?.[key]
if (!attribute) {
return
}
if (attribute.prototype instanceof AlternativesEntity) {
for (const alternative of /** @type {typeof AlternativesEntity} */(attribute).alternatives) {
const candidate = this.getAttribute(alternative, keys)
if (candidate) {
return candidate
}
}
}
if (entityType instanceof IEntity.constructor) {
// @ts-expect-error
result = entityType.attributes[key[0]]
type = result?.type
} else if (entityType instanceof Array) {
result = entityType[key[0]]
type = result
if (keys.length > 0) {
return this.getAttribute(attribute, keys)
}
if (key.length > 1) {
return this.getAttribute(type, key.slice(1))
}
return result
return attribute
}
/**
* @param {typeof IEntity} entityType
* @param {*} attributeName
* @param {*} valueSeparator
* @param {*} handleObjectSet
* @returns
*/
static createAttributeGrammar(
entityType,
attributeName = this.attributeName,
@@ -179,14 +182,12 @@ export default class Grammar {
).chain(([attributeName, _1]) => {
const attributeKey = attributeName.split(Configuration.keysSeparator)
const attributeValue = this.getAttribute(entityType, attributeKey)
return this
.grammarFor(attributeValue)
.map(attributeValue =>
values => {
handleObjectSet(values, attributeKey, attributeValue)
Utility.objectSet(values, attributeKey, attributeValue)
}
)
return attributeValue.grammar.map(attributeValue =>
values => {
handleObjectSet(values, attributeKey, attributeValue)
Utility.objectSet(values, attributeKey, attributeValue)
}
)
})
}
@@ -205,10 +206,10 @@ export default class Grammar {
.map(([lookbehind, attributes, trailing]) => {
let values = {}
if (lookbehind.length) {
values["#lookbehind"] = lookbehind
values["lookbehind"] = lookbehind
}
attributes.forEach(attributeSetter => attributeSetter(values))
values["#trailing"] = trailing !== undefined
values["trailing"] = trailing !== undefined
return values
})
// Decide if we accept the entity or not. It is accepted if it doesn't have too many unexpected keys
@@ -216,9 +217,6 @@ export default class Grammar {
if (entityType.lookbehind instanceof Array || entityType.lookbehind !== lookbehind) {
entityType = entityType.withLookbehind(lookbehind)
}
if (entityType.trailing !== values["#trailing"]) {
entityType = entityType.flagTrailing(values["#trailing"])
}
return Parsernostrum.success().map(() => new entityType(values))
})
}

View File

@@ -8,7 +8,6 @@ import EnumEntity from "../entity/EnumEntity.js"
import FormatTextEntity from "../entity/FormatTextEntity.js"
import FunctionReferenceEntity from "../entity/FunctionReferenceEntity.js"
import GuidEntity from "../entity/GuidEntity.js"
import IdentifierEntity from "../entity/IdentifierEntity.js"
import Integer64Entity from "../entity/Integer64Entity.js"
import IntegerEntity from "../entity/IntegerEntity.js"
import InvariantTextEntity from "../entity/InvariantTextEntity.js"
@@ -48,10 +47,11 @@ import StringEntity from "../entity/StringEntity.js"
import ArrayEntity from "../entity/ArrayEntity.js"
import AlternativesEntity from "../entity/AlternativesEntity.js"
import NullEntity from "../entity/NullEntity.js"
import IEntity from "../entity/IEntity.js"
export default function initializeSerializerFactory() {
Grammar.unknownValue =
IEntity.unknownEntityGrammar =
Parsernostrum.alt(
// Remember to keep the order, otherwise parsing might fail
BooleanEntity.grammar,

View File

@@ -36,8 +36,8 @@ export default class Entity3 extends IEntity {
lima: StringEntity,
mike: AlternativesEntity.accepting(NumberEntity, StringEntity, ArrayEntity).withDefault(type => new StringEntity("Bar")),
november: AlternativesEntity.accepting(NumberEntity, StringEntity, ArrayEntity).withDefault(type => new NumberEntity(0)),
oscar: Entity1.withDefault(() => new Entity1()),
papa: Entity1.withDefault(() => new Entity1({ a: 12, b: 13 })),
oscar: Entity1.withDefault(type => new type()),
papa: Entity1.withDefault(type => new type({ a: 12, b: 13 })),
quebec: NumberEntity.withDefault(), // will assign undefined because it does not satisfy the predicate,
romeo: Entity1.withDefault().flagInlined(),
sierra: Entity2.withDefault().flagInlined(),

View File

@@ -1,5 +1,6 @@
import { expect, test } from "@playwright/test"
import Utility from "../js/Utility.js"
import AlternativesEntity from "../js/entity/AlternativesEntity.js"
import ArrayEntity from "../js/entity/ArrayEntity.js"
import BooleanEntity from "../js/entity/BooleanEntity.js"
import FormatTextEntity from "../js/entity/FormatTextEntity.js"
@@ -7,6 +8,8 @@ import GuidEntity from "../js/entity/GuidEntity.js"
import IntegerEntity from "../js/entity/IntegerEntity.js"
import KeyBindingEntity from "../js/entity/KeyBindingEntity.js"
import LinearColorEntity from "../js/entity/LinearColorEntity.js"
import NaturalNumberEntity from "../js/entity/NaturalNumberEntity.js"
import NullEntity from "../js/entity/NullEntity.js"
import NumberEntity from "../js/entity/NumberEntity.js"
import ObjectReferenceEntity from "../js/entity/ObjectReferenceEntity.js"
import PinEntity from "../js/entity/PinEntity.js"
@@ -22,9 +25,8 @@ import VectorEntity from "../js/entity/VectorEntity.js"
import Grammar from "../js/serialization/Grammar.js"
import SerializerFactory from "../js/serialization/SerializerFactory.js"
import initializeSerializerFactory from "../js/serialization/initializeSerializerFactory.js"
import NaturalNumberEntity from "../js/entity/NaturalNumberEntity.js"
import NullEntity from "../js/entity/NullEntity.js"
import AlternativesEntity from "../js/entity/AlternativesEntity.js"
import IEntity from "../js/entity/IEntity.js"
import ColorChannelEntity from "../js/entity/ColorChannelEntity.js"
test.beforeAll(() => initializeSerializerFactory())
@@ -35,11 +37,11 @@ test("ArrayEntity", () => {
const grammar = ArrayEntity.grammar
let value = grammar.parse("()")
expect(value).toEqual(new ArrayEntity([]))
value = grammar.parse("( )")
expect(value.toString()).toEqual("()")
value = grammar.parse("( )")
expect(value).toEqual(new ArrayEntity([]))
expect(value.toString()).toEqual("()")
value = grammar.parse("(1, 2, 3, 4, 5, 6)")
expect(value.toString()).toEqual("(1,2,3,4,5,6)")
expect(value).toEqual(new ArrayEntity([
new NumberEntity(1),
new NumberEntity(2),
@@ -48,44 +50,46 @@ test("ArrayEntity", () => {
new NumberEntity(5),
new NumberEntity(6),
]))
expect(value.toString()).toEqual("(1,2,3,4,5,6)")
expect(value.Self().className()).toEqual("ArrayEntity")
value.values.map(v => v.Self().className()).forEach(v => expect(v).toEqual("NumberEntity"))
value = grammar.parse("(1, 2, )")
expect(value.toString()).toEqual("(1,2,)")
expect(value).toEqual(new ArrayEntity([
new NumberEntity(1),
new NumberEntity(2),
]))
expect(value.toString()).toEqual("(1,2,)")
expect(grammar.parse("( - )")).toThrow("Could not parse")
}
{
const grammar = ArrayEntity.of(NumberEntity).grammar
let value = grammar.parse("(2,4,6,8)")
expect(value.toString()).toEqual("(2,4,6,8)")
expect(value).toEqual(new ArrayEntity([
new NumberEntity(2),
new NumberEntity(4),
new NumberEntity(6),
new NumberEntity(8),
]))
expect(value.toString()).toEqual("(2,4,6,8)")
}
{
const grammar = ArrayEntity.of(IntegerEntity).grammar
let value = grammar.parse("(-0, -1, -2)")
expect(value.toString()).toEqual("(0,-1,-2)")
expect(value).toEqual(new ArrayEntity([
new IntegerEntity(0),
new IntegerEntity(-1),
new IntegerEntity(-2),
]))
expect(value.toString()).toEqual("(0,-1,-2)")
value.values.map(v => v.Self().className()).forEach(v => expect(v).toEqual("IntegerEntity"))
value = grammar.parse("(-0, -1, -2,)")
expect(value.toString()).toEqual("(0,-1,-2,)")
expect(value).toEqual(new ArrayEntity([
new IntegerEntity(0),
new IntegerEntity(-1),
new IntegerEntity(-2),
]))
expect(() => grammar.parse("(-1, -2.1, -3)")).toThrowError()
expect(value.toString()).toEqual("(0,-1,-2,)")
expect(() => grammar.parse("(-1, -2.1, -3)")).toThrow("Could not parse")
}
{
const grammar = ArrayEntity.grammar
@@ -96,7 +100,6 @@ test("ArrayEntity", () => {
3BEF2168446CAA32D5B54289FAB2F0BA,
Some(a=1, b="number:\\"2\\"")
)`)
expect(value.toString()).toEqual('("alpha","beta",123,3BEF2168446CAA32D5B54289FAB2F0BA,Some(a=1,b="number:\\"2\\""))')
expect(value).toEqual(new ArrayEntity([
new StringEntity("alpha"),
new StringEntity("beta"),
@@ -107,6 +110,7 @@ test("ArrayEntity", () => {
b: new StringEntity('number:"2"'),
})
]))
expect(value.toString()).toEqual('("alpha","beta",123,3BEF2168446CAA32D5B54289FAB2F0BA,Some(a=1,b="number:\\"2\\""))')
expect(value.values.map(v => v.Self().className())).toEqual([
"StringEntity",
"StringEntity",
@@ -118,7 +122,6 @@ test("ArrayEntity", () => {
A(first = (9,8,7,6,5), second = 00000000000000000000000000000000),
B(key="hello"),
)`)
expect(value.toString()).toEqual('(A(first=(9,8,7,6,5),second=00000000000000000000000000000000),B(key="hello"),)')
expect(value).toEqual(new ArrayEntity([
new UnknownKeysEntity({
lookbehind: new StringEntity("A"),
@@ -136,6 +139,7 @@ test("ArrayEntity", () => {
key: new StringEntity("hello"),
})
]))
expect(value.toString()).toEqual('(A(first=(9,8,7,6,5),second=00000000000000000000000000000000),B(key="hello"),)')
}
{
// Nested
@@ -143,17 +147,16 @@ test("ArrayEntity", () => {
IntegerEntity,
ArrayEntity.of(IntegerEntity)
)).grammar.parse("((1, 2), (3, 4), 5)")
expect(value.toString()).toEqual("((1,2),(3,4),5)")
expect(value).toEqual(new ArrayEntity([
new ArrayEntity([new IntegerEntity(1), new IntegerEntity(2)]),
new ArrayEntity([new IntegerEntity(3), new IntegerEntity(4)]),
new IntegerEntity(5),
]))
expect(value.toString()).toEqual("((1,2),(3,4),5)")
}
{
const grammar = ArrayEntity.grammar
let value = grammar.parse('(((1, "2"), (3, 4)), "5")')
expect(value.toString()).toEqual('(((1,"2"),(3,4)),"5")')
expect(value).toEqual(new ArrayEntity([
new ArrayEntity([
new ArrayEntity([new NumberEntity(1), new StringEntity("2")]),
@@ -161,12 +164,12 @@ test("ArrayEntity", () => {
]),
new StringEntity("5")
]))
expect(value.toString()).toEqual('(((1,"2"),(3,4)),"5")')
}
{
let value = ArrayEntity.grammar.parse(`(
One(a = (1,(2,(3,(4)))), b = ()),
)`)
expect(value.toString()).toEqual("(One(a=(1,(2,(3,(4)))),b=()),)")
expect(value).toEqual(new ArrayEntity([
new UnknownKeysEntity({
lookbehind: "One",
@@ -185,24 +188,29 @@ test("ArrayEntity", () => {
b: new NullEntity(),
}),
]))
expect(value.toString()).toEqual("(One(a=(1,(2,(3,(4)))),b=()),)")
}
})
test("Boolean", () => {
let grammar = BooleanEntity.grammar
let value = grammar.parse("true")
expect(value).toEqual(new BooleanEntity(true))
expect(value).toBeInstanceOf(BooleanEntity)
expect(value.toString()).toEqual("True")
expect(value).toEqual(new BooleanEntity(true))
expect(value.toString()).toEqual("true")
value = grammar.parse("True")
expect(value).toBeInstanceOf(BooleanEntity)
expect(value).toEqual(new BooleanEntity(true))
expect(value.toString()).toEqual("True")
value = grammar.parse("false")
expect(value).toBeInstanceOf(BooleanEntity)
expect(value).toEqual(new BooleanEntity(false))
expect(value.toString()).toEqual("False")
expect(value.toString()).toEqual("false")
value = grammar.parse("False")
expect(value).toBeInstanceOf(BooleanEntity)
expect(value).toEqual(new BooleanEntity(false))
expect(value.toString()).toEqual("False")
expect(() => grammar.parse("truee")).toThrow("Could not parse")
})
test("FormatTextEntity", () => {
@@ -214,19 +222,20 @@ test("FormatTextEntity", () => {
.toEqual('LOCGEN_FORMAT_NAMED(NSLOCTEXT("KismetSchema", "SplitPinFriendlyNameFormat", "{PinDisplayName} {ProtoPinDisplayName}"), "PinDisplayName", "Out Hit", "ProtoPinDisplayName", "Blocking Hit")')
value = grammar.parse(String.raw`LOCGEN_FORMAT_ORDERED(
NSLOCTEXT(
"PCGSettings",
"OverridableParamPinTooltip",
"{0}Attribute type is \"{1}\" and its exact name is \"{2}\""
),
"If InRangeMin = InRangeMax, then that density value is mapped to the average of OutRangeMin and OutRangeMax\n",
"float",
"InRangeMin"
)`)
NSLOCTEXT(
"PCGSettings",
"OverridableParamPinTooltip",
"{0}Attribute type is \"{1}\" and its exact name is \"{2}\""
),
"If InRangeMin = InRangeMax, then that density value is mapped to the average of OutRangeMin and OutRangeMax\n",
"float",
"InRangeMin"
)`)
expect(value.print())
.toEqual(`If InRangeMin = InRangeMax, then that density value is mapped to the average of OutRangeMin and OutRangeMax\nAttribute type is "float" and its exact name is "InRangeMin"`)
expect(value.toString())
.toEqual(String.raw`LOCGEN_FORMAT_ORDERED(NSLOCTEXT("PCGSettings", "OverridableParamPinTooltip", "{0}Attribute type is \"{1}\" and its exact name is \"{2}\""), "If InRangeMin = InRangeMax, then that density value is mapped to the average of OutRangeMin and OutRangeMax\n", "float", "InRangeMin")`)
expect(() => grammar.parse("LOCGEN_FORMAT_NAMED")).toThrow("Could not parse")
})
test("GuidEntity", () => {
@@ -247,124 +256,171 @@ test("GuidEntity", () => {
expect(value).toEqual(new GuidEntity("6edC4a425ca948da8bC78bA52DED6C6C"))
expect(value.toString()).toEqual("6edC4a425ca948da8bC78bA52DED6C6C")
expect(() => grammar.parse("172087193 9B04362973544B3564FDB2C")).toThrow()
expect(() => grammar.parse("E25F14F8F3E9441AB07153E7DA2BA2B")).toThrow()
expect(() => grammar.parse("A78988B0097E48418C8CB87EC5A67ABF7")).toThrow()
expect(() => grammar.parse("172087193 9B04362973544B3564FDB2C")).toThrow("Could not parse")
expect(() => grammar.parse("E25F14F8F3E9441AB07153E7DA2BA2B")).toThrow("Could not parse")
expect(() => grammar.parse("A78988B0097E48418C8CB87EC5A67ABF7")).toThrow("Could not parse")
})
test("IntegerEntity", () => {
let grammar = IntegerEntity.grammar
let integer = grammar.parse("0")
expect(integer).toEqual(new IntegerEntity(0))
expect(integer).not.toEqual(new NaturalNumberEntity(0))
let value = grammar.parse("0")
expect(value).toEqual(new IntegerEntity(0))
expect(value).toBeInstanceOf(IntegerEntity)
integer = grammar.parse("+0")
expect(integer).toEqual(new IntegerEntity(0))
value = grammar.parse("+0")
expect(value).toEqual(new IntegerEntity(0))
expect(value).toBeInstanceOf(IntegerEntity)
integer = grammar.parse("-0")
expect(integer).toEqual(new IntegerEntity(0))
value = grammar.parse("-0")
expect(value).toEqual(new IntegerEntity(0))
expect(value).toBeInstanceOf(IntegerEntity)
integer = grammar.parse("99")
expect(integer).toEqual(new IntegerEntity(99))
value = grammar.parse("99")
expect(value).toEqual(new IntegerEntity(99))
expect(value).toBeInstanceOf(IntegerEntity)
integer = grammar.parse("-8685")
expect(integer).toEqual(new IntegerEntity(-8685))
value = grammar.parse("-8685")
expect(value).toEqual(new IntegerEntity(-8685))
expect(value).toBeInstanceOf(IntegerEntity)
integer = grammar.parse("+555")
expect(integer).toEqual(new IntegerEntity(555))
value = grammar.parse("+555")
expect(value).toEqual(new IntegerEntity(555))
expect(value).toBeInstanceOf(IntegerEntity)
integer = grammar.parse("1000000000")
expect(integer).toEqual(new IntegerEntity(1000000000))
value = grammar.parse("1000000000")
expect(value).toEqual(new IntegerEntity(1000000000))
expect(value).toBeInstanceOf(IntegerEntity)
expect(() => grammar.parse("1.2").value).toThrow()
})
test("KeyBindingEntity", () => {
let serializer = SerializerFactory.getSerializer(KeyBindingEntity)
let grammar = KeyBindingEntity.grammar
let binding = serializer.read("A")
expect(binding).toBeInstanceOf(KeyBindingEntity)
expect(binding).toMatchObject({ Key: { value: "A" } })
let value = grammar.parse("A")
expect(value).toEqual(new KeyBindingEntity({ Key: new SymbolEntity("A") }))
expect(value).toBeInstanceOf(KeyBindingEntity)
expect(value.toString()).toEqual("(Key=A)")
binding = serializer.read("(bCtrl=True,Key=A)")
expect(binding).toBeInstanceOf(KeyBindingEntity)
expect(binding).toMatchObject({ Key: { value: "A" }, bCtrl: true })
value = grammar.parse("(bCtrl=True,Key=A)")
expect(value).toEqual(new KeyBindingEntity({ bCtrl: new BooleanEntity(true), Key: new SymbolEntity("A") }))
expect(value).toBeInstanceOf(KeyBindingEntity)
expect(value.toString()).toEqual("(bCtrl=True,Key=A)")
binding = serializer.read("(bCtrl=false,bShift=false,bCmd=true,bAlt=false,Key=X)")
expect(binding).toBeInstanceOf(KeyBindingEntity)
expect(binding).toMatchObject({ Key: { value: "X" }, bAlt: false, bCtrl: false, bCmd: true })
value = grammar.parse("(bCtrl=false,bShift=false,bCmd=true,bAlt=false,Key=X)")
expect(value).toBeInstanceOf(KeyBindingEntity)
expect(value).toEqual(new KeyBindingEntity({
bCtrl: new BooleanEntity(false),
bShift: new BooleanEntity(false),
bCmd: new BooleanEntity(true),
bAlt: new BooleanEntity(false),
Key: new SymbolEntity("X"),
}))
expect(value.toString()).toEqual("(bCtrl=False,bShift=False,bCmd=True,bAlt=False,Key=X)")
binding = serializer.read("( bCtrl= false \n, Key \n\n\n =Y ,bAlt=true )")
expect(binding).toBeInstanceOf(KeyBindingEntity)
expect(binding).toMatchObject({ Key: { value: "Y" }, bAlt: true, bCtrl: false })
value = grammar.parse("( bCtrl= false \n, Key \n\n\n =Y ,bAlt=true )")
expect(value).toEqual(new KeyBindingEntity({
bCtrl: new BooleanEntity(false),
Key: new SymbolEntity("Y"),
bAlt: new BooleanEntity(true),
}))
expect(value).toBeInstanceOf(KeyBindingEntity)
})
test("LinearColorEntity", () => {
const serializer = SerializerFactory.getSerializer(LinearColorEntity)
const grammar = LinearColorEntity.grammar
let color = LinearColorEntity.getWhite()
expect(color.toRGBA()).toStrictEqual([255, 255, 255, 255])
expect(color.toRGBAString()).toStrictEqual("FFFFFFFF")
expect(color.toNumber()).toStrictEqual(-1)
expect(color.toHSVA()).toStrictEqual([0, 0, 1, 1])
let value = LinearColorEntity.getWhite()
expect(value).toEqual(new LinearColorEntity({
R: new ColorChannelEntity(1),
G: new ColorChannelEntity(1),
B: new ColorChannelEntity(1),
A: new ColorChannelEntity(1),
}))
expect(value.toRGBA()).toStrictEqual([255, 255, 255, 255])
expect(value.toRGBAString()).toStrictEqual("FFFFFFFF")
expect(value.toNumber()).toStrictEqual(-1)
expect(value.toHSVA()).toStrictEqual([0, 0, 1, 1])
expect(value.toString()).toStrictEqual("(R=1.000000,G=1.000000,B=1.000000,A=1.000000)")
color = serializer.read("(R=1,G=0,B=0)")
expect(color.toRGBA()).toStrictEqual([255, 0, 0, 255])
expect(color.toRGBAString()).toStrictEqual("FF0000FF")
expect(color.toNumber()).toStrictEqual(-16776961)
expect(color.toHSVA()).toStrictEqual([0, 1, 1, 1])
value = grammar.parse("(R=1,G=0,B=0)")
expect(value).toEqual(new LinearColorEntity({
R: new ColorChannelEntity(1),
G: new ColorChannelEntity(0),
B: new ColorChannelEntity(0),
A: new ColorChannelEntity(1),
}))
expect(value.toRGBA()).toStrictEqual([255, 0, 0, 255])
expect(value.toRGBAString()).toStrictEqual("FF0000FF")
expect(value.toNumber()).toStrictEqual(-16776961)
expect(value.toHSVA()).toStrictEqual([0, 1, 1, 1])
expect(value.toString()).toStrictEqual("(R=1.000000,G=0.000000,B=0.000000,A=1.000000)")
color = serializer.read("(R=0.000000,G=0.660000,B=1.000000,A=1.000000)")
expect(color.toRGBA()).toStrictEqual([0, 168, 255, 255])
expect(color.toRGBAString()).toStrictEqual("00A8FFFF")
expect(color.toNumber()).toStrictEqual(11075583)
expect(color.toHSVA()).toStrictEqual([0.55666666666666666666, 1, 1, 1])
value = grammar.parse("(R=0.000000,G=0.660000,B=1.000000,A=1.000000)")
expect(value).toEqual(new LinearColorEntity({
R: new ColorChannelEntity(0),
G: new ColorChannelEntity(0.66),
B: new ColorChannelEntity(1),
A: new ColorChannelEntity(1),
}))
expect(value.toRGBA()).toStrictEqual([0, 168, 255, 255])
expect(value.toRGBAString()).toStrictEqual("00A8FFFF")
expect(value.toNumber()).toStrictEqual(11075583)
expect(value.toHSVA()).toStrictEqual([0.55666666666666666666, 1, 1, 1])
expect(value.toString()).toStrictEqual("(R=0.000000,G=0.660000,B=1.000000,A=1.000000)")
color = serializer.read("(B=0.04394509003266556,G=0.026789300067696642,A=0.83663232408635,R=0.6884158028074934,)")
expect(color.toRGBA()).toStrictEqual([176, 7, 11, 213])
expect(color.toRGBAString()).toStrictEqual("B0070BD5")
expect(color.toNumber()).toStrictEqual(-1341715499)
expect(color.toHSVA().map(v => Utility.roundDecimals(v, 3))).toStrictEqual([0.996, 0.961, 0.688, 0.837])
value = grammar.parse("(B=0.04394509003266556,G=0.026789300067696642,A=0.83663232408635,R=0.6884158028074934,)")
expect(value.toRGBA()).toStrictEqual([176, 7, 11, 213])
expect(value.toRGBAString()).toStrictEqual("B0070BD5")
expect(value.toNumber()).toStrictEqual(-1341715499)
expect(value.toHSVA().map(v => Utility.roundDecimals(v, 3))).toStrictEqual([0.996, 0.961, 0.688, 0.837])
color = serializer.read(`(
value = grammar.parse(`(
A = 0.327 ,
R=0.530 , G = 0.685
,B
= 0.9 ,)`)
expect(color.toRGBA()).toStrictEqual([135, 175, 230, 83])
expect(color.toRGBAString()).toStrictEqual("87AFE653")
expect(color.toNumber()).toStrictEqual(-2018515373)
expect(color.toHSVA().map(v => Utility.roundDecimals(v, 3))).toStrictEqual([0.597, 0.411, 0.9, 0.327])
expect(value.toRGBA()).toStrictEqual([135, 175, 230, 83])
expect(value.toRGBAString()).toStrictEqual("87AFE653")
expect(value.toNumber()).toStrictEqual(-2018515373)
expect(value.toHSVA().map(v => Utility.roundDecimals(v, 3))).toStrictEqual([0.597, 0.411, 0.9, 0.327])
expect(() => serializer.read("(R=0.000000,G=0.660000,A=1.000000)")).toThrow()
expect(() => serializer.read("(R=0.000000,G=\"hello\",A=1.000000)")).toThrow()
value = grammar.parse("(R=0.000000,G=0.660000,A=1.000000)")
expect(value).toEqual(new LinearColorEntity({
R: new ColorChannelEntity(0),
G: new ColorChannelEntity(0.66),
B: new ColorChannelEntity(0),
A: new ColorChannelEntity(1),
}))
})
test("Null", () => {
const serializer = SerializerFactory.getSerializer(null)
expect(serializer.read("()")).toBeNull()
expect(() => serializer.read("123")).toThrow()
expect(() => serializer.read("(a)")).toThrow()
expect(() => serializer.read("(")).toThrow()
test("NullEntity", () => {
const grammar = NullEntity.grammar
let value = grammar.parse("()")
expect(value).toBeInstanceOf(NullEntity)
expect(value).toEqual(new NullEntity())
expect(value.toString()).toEqual("()")
expect(() => grammar.parse("123")).toThrow("Could not parse")
expect(() => grammar.parse("(a)")).toThrow("Could not parse")
expect(() => grammar.parse("(")).toThrow("Could not parse")
})
test("Number", () => {
const serializer = SerializerFactory.getSerializer(Number)
test("NumberEntity", () => {
const grammar = NumberEntity.grammar
expect(serializer.read("0")).toBeCloseTo(0, 0.00001)
expect(serializer.read("+0")).toBeCloseTo(0, 0.00001)
expect(serializer.read("-0")).toBeCloseTo(0, 0.00001)
expect(serializer.read("5")).toBeCloseTo(5, 0.00001)
expect(serializer.read("0.05")).toBeCloseTo(0.05, 0.00001)
expect(serializer.read("-999.666")).toBeCloseTo(-999.666, 0.001)
expect(serializer.read("+45.4545")).toBeCloseTo(45.4545, 0.001)
expect(serializer.read("+1000000000")).toBeCloseTo(1E9, 0.1)
expect(serializer.read("inf")).toBe(Number.POSITIVE_INFINITY)
expect(serializer.read("+inf")).toBe(Number.POSITIVE_INFINITY)
expect(serializer.read("-inf")).toBe(Number.NEGATIVE_INFINITY)
expect(() => serializer.read("alpha")).toThrow()
expect(grammar.parse("0")).toBeCloseTo(0, 0.00001)
expect(grammar.parse("+0")).toBeCloseTo(0, 0.00001)
expect(grammar.parse("-0")).toBeCloseTo(0, 0.00001)
expect(grammar.parse("5")).toBeCloseTo(5, 0.00001)
expect(grammar.parse("0.05")).toBeCloseTo(0.05, 0.00001)
expect(grammar.parse("-999.666")).toBeCloseTo(-999.666, 0.001)
expect(grammar.parse("+45.4545")).toBeCloseTo(45.4545, 0.001)
expect(grammar.parse("+1000000000")).toBeCloseTo(1E9, 0.1)
expect(grammar.parse("inf")).toBe(Number.POSITIVE_INFINITY)
expect(grammar.parse("+inf")).toBe(Number.POSITIVE_INFINITY)
expect(grammar.parse("-inf")).toBe(Number.NEGATIVE_INFINITY)
expect(() => grammar.parse("alpha")).toThrow()
})
test("ObjectReferenceEntity", () => {
@@ -575,7 +631,7 @@ test("String", () => {
})
test("UnknownKeysValue", () => {
const parser = Grammar.unknownValue
const parser = IEntity.unknownEntityGrammar
expect(parser.parse('"Hello"').constructor).toStrictEqual(String)
expect(parser.parse("()")).toBeNull()