This commit is contained in:
barsdeveloper
2024-05-31 15:09:48 +02:00
parent 1c2778fbf8
commit ecc71b76d1
45 changed files with 1191 additions and 1078 deletions

View File

@@ -8,6 +8,10 @@ import Serializable from "./Serializable.js"
export default class Grammar {
/** @type {String} */
// @ts-expect-error
static numberRegexSource = Parsernostrum.number.getParser().parser.regexp.source
static separatedBy = (source, separator, min = 1) =>
new RegExp(
source + "(?:" + separator + source + ")"
@@ -188,46 +192,32 @@ export default class Grammar {
/**
* @template {IEntity} T
* @param {(new (...args: any) => T) & EntityConstructor} entityType
* @param {Boolean | Number} acceptUnknownKeys Number to specify the limit or true, to let it be a reasonable value
* @param {new (...args: any) => T} entityType
* @return {Parsernostrum<T>}
*/
static createEntityGrammar(entityType, acceptUnknownKeys = true, entriesSeparator = this.commaSeparation) {
const lookbehind = entityType.attributes.lookbehind.default
static createEntityGrammar(entityType, entriesSeparator = this.commaSeparation) {
const lookbehind = entityType.lookbehind instanceof Array ? entityType.lookbehind.join("|") : entityType.lookbehind
return Parsernostrum.seq(
Parsernostrum.reg(
lookbehind instanceof Union
? new RegExp(`(${lookbehind.values.reduce((acc, cur) => acc + "|" + cur)})\\s*\\(\\s*`)
: lookbehind.constructor == String && lookbehind.length > 0
? new RegExp(`(${lookbehind})\\s*\\(\\s*`)
: /()\(\s*/,
1
),
Parsernostrum.reg(new RegExp(String.raw`(${lookbehind})\s*\(\s*`), 1),
this.createAttributeGrammar(entityType).sepBy(entriesSeparator),
Parsernostrum.reg(/\s*(?:,\s*)?\)/), // trailing comma
Parsernostrum.reg(/\s*(,\s*)?\)/, 1), // optional trailing comma
)
.map(([lookbehind, attributes, _2]) => {
.map(([lookbehind, attributes, trailing]) => {
let values = {}
attributes.forEach(attributeSetter => attributeSetter(values))
if (lookbehind.length) {
values.lookbehind = lookbehind
values["#lookbehind"] = lookbehind
}
attributes.forEach(attributeSetter => attributeSetter(values))
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
.chain(values => {
let totalKeys = Object.keys(values)
let missingKey
// Check missing values
if (
Object.keys(/** @type {AttributeDeclarations} */(entityType.attributes))
.filter(key => entityType.attributes[key].expected)
.find(key => !totalKeys.includes(key) && (missingKey = key))
) {
return Parsernostrum.failure()
if (entityType.lookbehind instanceof Array || entityType.lookbehind !== lookbehind) {
entityType = entityType.withLookbehind(lookbehind)
}
const unknownKeys = Object.keys(values).filter(key => !(key in entityType.attributes)).length
if (!acceptUnknownKeys && unknownKeys > 0) {
return Parsernostrum.failure()
if (entityType.trailing !== values["#trailing"]) {
entityType = entityType.flagTrailing(values["#trailing"])
}
return Parsernostrum.success().map(() => new entityType(values))
})

View File

@@ -19,7 +19,6 @@ import MacroGraphReferenceEntity from "../entity/MacroGraphReferenceEntity.js"
import MirroredEntity from "../entity/MirroredEntity.js"
import ObjectEntity from "../entity/ObjectEntity.js"
import ObjectReferenceEntity from "../entity/ObjectReferenceEntity.js"
import PathSymbolEntity from "../entity/PathSymbolEntity.js"
import PinEntity from "../entity/PinEntity.js"
import PinReferenceEntity from "../entity/PinReferenceEntity.js"
import PinTypeEntity from "../entity/PinTypeEntity.js"
@@ -49,34 +48,34 @@ import StringEntity from "../entity/StringEntity.js"
import ArrayEntity from "../entity/ArrayEntity.js"
import AlternativesEntity from "../entity/AlternativesEntity.js"
Grammar.unknownValue =
Parsernostrum.alt(
// Remember to keep the order, otherwise parsing might fail
BooleanEntity.grammar,
GuidEntity.grammar,
Parsernostrum.str("None").map(() => ObjectReferenceEntity.createNoneInstance()),
Grammar.null,
NumberEntity.grammar,
ObjectReferenceEntity.fullReferenceGrammar,
StringEntity.grammar,
LocalizedTextEntity.grammar,
InvariantTextEntity.grammar,
FormatTextEntity.grammar,
PinReferenceEntity.grammar,
Vector4DEntity.grammar,
VectorEntity.grammar,
RotatorEntity.grammar,
LinearColorEntity.grammar,
Vector2DEntity.grammar,
UnknownKeysEntity.grammar,
SymbolEntity.grammar,
ArrayEntity.of(PinReferenceEntity).grammar,
ArrayEntity.of(AlternativesEntity.accepting(NumberEntity, StringEntity, SymbolEntity)).grammar,
Parsernostrum.lazy(() => ArrayEntity.createGrammar(Grammar.unknownValue)),
)
export default function initializeSerializerFactory() {
Grammar.unknownValue =
Parsernostrum.alt(
// Remember to keep the order, otherwise parsing might fail
BooleanEntity.grammar,
GuidEntity.grammar,
Parsernostrum.str("None").map(() => ObjectReferenceEntity.createNoneInstance()),
Grammar.null,
NumberEntity.grammar,
ObjectReferenceEntity.fullReferenceGrammar,
StringEntity.grammar,
LocalizedTextEntity.grammar,
InvariantTextEntity.grammar,
FormatTextEntity.grammar,
PinReferenceEntity.grammar,
Vector4DEntity.grammar,
VectorEntity.grammar,
RotatorEntity.grammar,
LinearColorEntity.grammar,
Vector2DEntity.grammar,
UnknownKeysEntity.grammar,
SymbolEntity.grammar,
ArrayEntity.of(PinReferenceEntity).grammar,
ArrayEntity.of(AlternativesEntity.accepting(NumberEntity, StringEntity, SymbolEntity)).grammar,
Parsernostrum.lazy(() => ArrayEntity.createGrammar(Grammar.unknownValue)),
)
SerializerFactory.registerSerializer(
null,
new CustomSerializer(
@@ -122,26 +121,6 @@ export default function initializeSerializerFactory() {
)
)
SerializerFactory.registerSerializer(
ByteEntity,
new ToStringSerializer(ByteEntity)
)
SerializerFactory.registerSerializer(
ColorChannelEntity,
new ToStringSerializer(ColorChannelEntity)
)
SerializerFactory.registerSerializer(
EnumDisplayValueEntity,
new ToStringSerializer(EnumDisplayValueEntity)
)
SerializerFactory.registerSerializer(
EnumEntity,
new ToStringSerializer(EnumEntity)
)
SerializerFactory.registerSerializer(
FormatTextEntity,
new CustomSerializer(
@@ -156,94 +135,11 @@ export default function initializeSerializerFactory() {
FormatTextEntity)
)
SerializerFactory.registerSerializer(
FunctionReferenceEntity,
new Serializer(FunctionReferenceEntity, Serializer.bracketsWrapped)
)
SerializerFactory.registerSerializer(
GuidEntity,
new ToStringSerializer(GuidEntity)
)
SerializerFactory.registerSerializer(
IdentifierEntity,
new ToStringSerializer(IdentifierEntity)
)
SerializerFactory.registerSerializer(
Integer64Entity,
new ToStringSerializer(Integer64Entity)
)
SerializerFactory.registerSerializer(
IntegerEntity,
new ToStringSerializer(IntegerEntity)
)
SerializerFactory.registerSerializer(
InvariantTextEntity,
new Serializer(InvariantTextEntity, (entity, v) => `${entity.getLookbehind()}(${v})`, ", ", false, "", () => "")
)
SerializerFactory.registerSerializer(
KeyBindingEntity,
new Serializer(KeyBindingEntity, Serializer.bracketsWrapped)
)
SerializerFactory.registerSerializer(
LinearColorEntity,
new Serializer(LinearColorEntity, Serializer.bracketsWrapped)
)
SerializerFactory.registerSerializer(
LocalizedTextEntity,
new Serializer(LocalizedTextEntity, (entity, v) => `${entity.getLookbehind()}(${v})`, ", ", false, "", () => "")
)
SerializerFactory.registerSerializer(
MacroGraphReferenceEntity,
new Serializer(MacroGraphReferenceEntity, Serializer.bracketsWrapped)
)
SerializerFactory.registeOrSerializer(
MirroredEntity,
new CustomSerializer(
(v, insideString) => SerializerFactory.getSerializer(v.getTargetType()).write(v.get(), insideString),
MirroredEntity
)
)
SerializerFactory.registerSerializer(
Number,
new ToStringSerializer(Number)
)
SerializerFactory.registerSerializer(
ObjectEntity,
new ObjectSerializer()
)
SerializerFactory.registerSerializer(
ObjectReferenceEntity,
new ToStringSerializer(ObjectReferenceEntity, false)
)
SerializerFactory.registerSerializer(
PathSymbolEntity,
new ToStringSerializer(PathSymbolEntity)
)
SerializerFactory.registerSerializer(
PinEntity,
new Serializer(PinEntity, (entity, v) => `${entity.getLookbehind()} (${v})`, ",", true)
)
SerializerFactory.registerSerializer(
PinReferenceEntity,
new Serializer(PinReferenceEntity, undefined, " ", false, "", () => "")
)
SerializerFactory.registerSerializer(
PinTypeEntity,
new Serializer(PinTypeEntity)