Fix empty array grammar

This commit is contained in:
barsdeveloper
2024-03-25 23:57:11 +01:00
parent cc9e3d833a
commit 77a583e17f
4 changed files with 9 additions and 6 deletions

View File

@@ -69,6 +69,7 @@ export default class Grammar {
/**
* @template T
* @param {AttributeInfo<T>} attribute
* @returns {Parsernostrum<T>}
*/
static grammarFor(attribute, type = attribute?.type, defaultGrammar = this.unknownValue) {
let result = defaultGrammar
@@ -78,9 +79,9 @@ export default class Grammar {
}
result = Parsernostrum.seq(
Parsernostrum.reg(/\(\s*/),
this.grammarFor(undefined, type[0]).sepBy(this.commaSeparation),
this.grammarFor(undefined, type[0]).sepBy(this.commaSeparation).opt(),
Parsernostrum.reg(/\s*(?:,\s*)?\)/),
).map(([_0, values, _3]) => values)
).map(([_0, values, _3]) => values instanceof Array ? values : [])
} else if (type instanceof Union) {
result = type.values
.map(v => this.grammarFor(undefined, v))
@@ -89,6 +90,7 @@ export default class Grammar {
: Parsernostrum.alt(acc, cur)
)
} else if (type instanceof MirroredEntity) {
// @ts-expect-error
return this.grammarFor(undefined, type.getTargetType())
.map(v => new MirroredEntity(type.type, () => v))
} else if (attribute?.constructor === Object) {

View File

@@ -21,6 +21,7 @@ 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"
import RBSerializationVector2DEntity from "../entity/RBSerializationVector2DEntity.js"
import RotatorEntity from "../entity/RotatorEntity.js"
import SimpleSerializationRotatorEntity from "../entity/SimpleSerializationRotatorEntity.js"
@@ -39,7 +40,6 @@ import ObjectSerializer from "./ObjectSerializer.js"
import Serializer from "./Serializer.js"
import SerializerFactory from "./SerializerFactory.js"
import ToStringSerializer from "./ToStringSerializer.js"
import PinTypeEntity from "../entity/PinTypeEntity.js"
Grammar.unknownValue =
Parsernostrum.alt(