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

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,