Better typing for grammars

This commit is contained in:
barsdeveloper
2024-06-06 23:16:21 +02:00
parent ad4ba2c46d
commit 98ebdd78b2
40 changed files with 301 additions and 233 deletions

View File

@@ -7,7 +7,9 @@ export default class ArrayEntity extends IEntity {
/** @type {typeof IEntity} */
static type
static grammar = this.createGrammar()
static grammar = /** @type {P<ArrayEntity<IEntity>>} */(
this.createGrammar()
)
get length() {
return this.values.length
@@ -43,8 +45,8 @@ export default class ArrayEntity extends IEntity {
const result = /** @type {typeof ArrayEntity<ExtractType<T>> & {type: T, grammar: P<ArrayEntity<ExtractType<T>>> }} */(
this.asUniqueClass()
)
result.type = /** @type {ExtractType<T>} */(type)
result.grammar = result.createGrammar()
result.type = type
result.grammar = /** @type {P<ArrayEntity>} */(result.createGrammar())
return result
}

View File

@@ -3,13 +3,15 @@ import IEntity from "./IEntity.js"
export default class BooleanEntity extends IEntity {
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")
static grammar = /** @type {P<BooleanEntity>} */(
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() {

View File

@@ -3,7 +3,9 @@ import IntegerEntity from "./IntegerEntity.js"
export default class ByteEntity extends IntegerEntity {
static grammar = P.numberByte.map(v => new this(v))
static grammar = /** @type {P<ByteEntity>} */(
P.numberByte.map(v => new this(v))
)
get value() {
return super.value

View File

@@ -3,7 +3,9 @@ import IEntity from "./IEntity.js"
export default class ColorChannelEntity extends IEntity {
static grammar = P.number.map(v => new this(v))
static grammar = /** @type {P<ColorChannelEntity>} */(
P.number.map(v => new this(v))
)
constructor(value = 0) {
super()

View File

@@ -4,5 +4,7 @@ import EnumEntity from "./EnumEntity.js"
export default class EnumDisplayValueEntity extends EnumEntity {
static grammar = P.reg(Grammar.Regex.InsideString).map(v => new this(v))
static grammar = /** @type {P<EnumDisplayValueEntity>} */(
P.reg(Grammar.Regex.InsideString).map(v => new this(v))
)
}

View File

@@ -1,7 +1,10 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import SymbolEntity from "./SymbolEntity.js"
export default class EnumEntity extends SymbolEntity {
static grammar = Grammar.symbol.map(v => new this(v))
static grammar = /** @type {P<EnumEntity>} */(
Grammar.symbol.map(v => new this(v))
)
}

View File

@@ -8,21 +8,22 @@ export default class FormatTextEntity extends IPrintableEntity {
static attributeSeparator = ", "
static lookbehind = ["LOCGEN_FORMAT_NAMED", "LOCGEN_FORMAT_ORDERED"]
/** @type {P<FormatTextEntity>} */
static grammar = P.lazy(() => P.seq(
// Resulting regex: /(LOCGEN_FORMAT_NAMED|LOCGEN_FORMAT_ORDERED)\s*/
P.reg(new RegExp(String.raw`(${this.lookbehind.join("|")})\s*\(\s*`), 1),
P.alt(
...[StringEntity, LocalizedTextEntity, InvariantTextEntity, FormatTextEntity].map(type => type.grammar)
).sepBy(P.reg(/\s*\,\s*/)),
P.reg(/\s*\)/)
static grammar = /** @type {P<FormatTextEntity>} */(
P.lazy(() => P.seq(
// Resulting regex: /(LOCGEN_FORMAT_NAMED|LOCGEN_FORMAT_ORDERED)\s*/
P.reg(new RegExp(String.raw`(${this.lookbehind.join("|")})\s*\(\s*`), 1),
P.alt(
...[StringEntity, LocalizedTextEntity, InvariantTextEntity, FormatTextEntity].map(type => type.grammar)
).sepBy(P.reg(/\s*\,\s*/)),
P.reg(/\s*\)/)
)
.map(([lookbehind, values]) => {
const result = new this(values)
result.lookbehind = lookbehind
return result
}))
.label("FormatTextEntity")
)
.map(([lookbehind, values]) => {
const result = new this(values)
result.lookbehind = lookbehind
return result
}))
.label("FormatTextEntity")
/** @param {(StringEntity | LocalizedTextEntity | InvariantTextEntity | FormatTextEntity)[]} values */
constructor(values) {

View File

@@ -13,8 +13,9 @@ export default class FunctionReferenceEntity extends IEntity {
MemberName: StringEntity,
MemberGuid: GuidEntity,
}
/** @type {P<FunctionReferenceEntity>} */
static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, false, 0)
static grammar = /** @type {P<FunctionReferenceEntity>} */(
Grammar.createEntityGrammar(this, Grammar.commaSeparation, false, 0)
)
constructor(values) {
super(values)

View File

@@ -10,7 +10,9 @@ if (typeof window === "undefined") {
export default class GuidEntity extends IEntity {
static grammar = P.reg(/[0-9A-F]{32}/i).map(v => new this(v)).label("GuidEntity")
static grammar = /** @type {P<GuidEntity>} */(
P.reg(/[0-9A-F]{32}/i).map(v => new this(v)).label("GuidEntity")
)
static generateGuid() {
let values = new Uint32Array(4)

View File

@@ -3,7 +3,9 @@ import IEntity from "./IEntity.js"
export default class Integer64Entity extends IEntity {
static grammar = P.numberBigInteger.map(v => new this(v))
static grammar = /** @type {P<Integer64Entity>} */(
P.numberBigInteger.map(v => new this(v))
)
/** @type {bigint} */
#value

View File

@@ -3,7 +3,9 @@ import NumberEntity from "./NumberEntity.js"
export default class IntegerEntity extends NumberEntity {
static grammar = P.numberInteger.map(v => new this(v))
static grammar = /** @type {P<IntegerEntity>} */(
P.numberInteger.map(v => new this(v))
)
get value() {
return super.value

View File

@@ -1,20 +1,22 @@
import Parsernostrum from "parsernostrum"
import P from "parsernostrum"
import IPrintableEntity from "./IPrintableEntity.js"
export default class InvariantTextEntity extends IPrintableEntity {
static lookbehind = "INVTEXT"
static grammar = Parsernostrum.alt(
Parsernostrum.seq(
Parsernostrum.reg(new RegExp(`${this.lookbehind}\\s*\\(`)),
Parsernostrum.doubleQuotedString,
Parsernostrum.reg(/\s*\)/)
).map(([_0, value, _2]) => Number(value)),
Parsernostrum.reg(new RegExp(this.lookbehind)).map(() => 0) // InvariantTextEntity can not have arguments
static grammar = /** @type {P<InvariantTextEntity>} */(
P.alt(
P.seq(
P.reg(new RegExp(`${this.lookbehind}\\s*\\(`)),
P.doubleQuotedString,
P.reg(/\s*\)/)
).map(([_0, value, _2]) => Number(value)),
P.reg(new RegExp(this.lookbehind)).map(() => 0) // InvariantTextEntity can not have arguments
)
.map(value => new this(value))
.label("InvariantTextEntity")
)
.map(value => new this(value))
.label("InvariantTextEntity")
constructor(value = "") {
super()
@@ -22,13 +24,13 @@ export default class InvariantTextEntity extends IPrintableEntity {
}
print() {
let xxxx = Parsernostrum.alt(
Parsernostrum.seq(
Parsernostrum.reg(new RegExp(`${this.lookbehind}\\s*\\(`)),
Parsernostrum.doubleQuotedString,
Parsernostrum.reg(/\s*\)/)
let xxxx = P.alt(
P.seq(
P.reg(new RegExp(`${this.lookbehind}\\s*\\(`)),
P.doubleQuotedString,
P.reg(/\s*\)/)
).map(([_0, value, _2]) => Number(value)),
Parsernostrum.reg(new RegExp(this.lookbehind)).map(() => 0) // InvariantTextEntity can not have arguments
P.reg(new RegExp(this.lookbehind)).map(() => 0) // InvariantTextEntity can not have arguments
)
return this.value
}

View File

@@ -16,10 +16,11 @@ export default class KeyBindingEntity extends IEntity {
bCmd: BooleanEntity,
Key: SymbolEntity,
}
/** @type {P<KeyBindingEntity>} */
static grammar = P.alt(
SymbolEntity.grammar.map(identifier => new this({ Key: identifier })),
Grammar.createEntityGrammar(this)
static grammar = /** @type {P<KeyBindingEntity>} */(
P.alt(
SymbolEntity.grammar.map(identifier => new this({ Key: identifier })),
Grammar.createEntityGrammar(this)
)
)
constructor(values) {

View File

@@ -14,7 +14,9 @@ export default class LinearColorEntity extends IEntity {
B: ColorChannelEntity.withDefault(),
A: ColorChannelEntity.withDefault(type => new type(1)),
}
static grammar = Grammar.createEntityGrammar(this).label("LinearColorEntity")
static grammar = /** @type {P<LinearColorEntity>} */(
Grammar.createEntityGrammar(this).label("LinearColorEntity")
)
#H = new ColorChannelEntity()
get H() {

View File

@@ -15,22 +15,24 @@ export default class LocalizedTextEntity extends IPrintableEntity {
key: StringEntity.withDefault(),
value: StringEntity.withDefault(),
}
static grammar = P.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")
static grammar = /** @type {P<LocalizedTextEntity>} */(
P.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)

View File

@@ -1,3 +1,4 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import GuidEntity from "./GuidEntity.js"
import IEntity from "./IEntity.js"
@@ -11,7 +12,9 @@ export default class MacroGraphReferenceEntity extends IEntity {
GraphBlueprint: ObjectReferenceEntity,
GraphGuid: GuidEntity,
}
static grammar = Grammar.createEntityGrammar(this)
static grammar = /** @type {P<MacroGraphReferenceEntity>} */(
Grammar.createEntityGrammar(this)
)
constructor(values) {
super(values)

View File

@@ -4,7 +4,9 @@ import IntegerEntity from "./IntegerEntity.js"
export default class NaturalNumberEntity extends IntegerEntity {
static grammar = P.numberNatural.map(v => new this(v))
static grammar = /** @type {P<NaturalNumberEntity>} */(
P.numberNatural.map(v => new this(v))
)
get value() {
return super.value

View File

@@ -3,9 +3,11 @@ import IEntity from "./IEntity.js"
export default class NullEntity extends IEntity {
// @ts-expect-error
static grammar = P.reg(new RegExp(String.raw`\(${P.whitespaceInlineOpt.getParser().regexp.source}\)`))
.map(v => new this())
static grammar = /** @type {P<NullEntity>} */(
// @ts-expect-error
P.reg(new RegExp(String.raw`\(${P.whitespaceInlineOpt.getParser().regexp.source}\)`))
.map(v => new this())
)
toString(
insideString = false,

View File

@@ -5,13 +5,15 @@ import IEntity from "./IEntity.js"
export default class NumberEntity extends IEntity {
static numberRegexSource = String.raw`${Grammar.numberRegexSource}(?<=(?:\.(\d*0+))?)`
static grammar = P.regArray(
new RegExp(`(?<n>${this.numberRegexSource})|(?<posInf>\\+?inf)|(?<negInf>-inf)`)
).map(({ 2: precision, groups: { n, posInf, negInf } }) => new this(
n ? Number(n) : posInf ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY,
precision?.length
static grammar = /** @type {P<NumberEntity>} */(
P.regArray(
new RegExp(`(?<n>${this.numberRegexSource})|(?<posInf>\\+?inf)|(?<negInf>-inf)`)
).map(({ 2: precision, groups: { n, posInf, negInf } }) => new this(
n ? Number(n) : posInf ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY,
precision?.length
)
).label("NumberEntity")
)
).label("NumberEntity")
#precision = 0
get precision() {

View File

@@ -169,31 +169,32 @@ export default class ObjectEntity extends IEntity {
}
)
)
/** @type {P<ObjectEntity>} */
static grammar = P.seq(
P.reg(/Begin +Object/),
static grammar = /** @type {P<ObjectEntity>} */(
P.seq(
P.whitespace,
P.alt(
this.createSubObjectGrammar(),
this.customPropertyGrammar,
Grammar.createAttributeGrammar(this, P.reg(Grammar.Regex.MultipleWordsSymbols)),
Grammar.createAttributeGrammar(this, Grammar.attributeNameQuoted, undefined, (obj, k, v) =>
Utility.objectSet(obj, ["attributes", ...k, "quoted"], true)
),
this.inlinedArrayEntryGrammar,
P.reg(/Begin +Object/),
P.seq(
P.whitespace,
P.alt(
this.createSubObjectGrammar(),
this.customPropertyGrammar,
Grammar.createAttributeGrammar(this, P.reg(Grammar.Regex.MultipleWordsSymbols)),
Grammar.createAttributeGrammar(this, Grammar.attributeNameQuoted, undefined, (obj, k, v) =>
Utility.objectSet(obj, ["attributes", ...k, "quoted"], true)
),
this.inlinedArrayEntryGrammar,
)
)
.map(([_0, entry]) => entry)
.many(),
P.reg(/\s+End +Object/),
)
.map(([_0, entry]) => entry)
.many(),
P.reg(/\s+End +Object/),
.map(([_0, attributes, _2]) => {
const values = {}
attributes.forEach(attributeSetter => attributeSetter(values))
return new this(values)
})
.label("ObjectEntity")
)
.map(([_0, attributes, _2]) => {
const values = {}
attributes.forEach(attributeSetter => attributeSetter(values))
return new this(values)
})
.label("ObjectEntity")
static grammarMultipleObjects = P.seq(
P.whitespaceOpt,
this.grammar,
@@ -302,7 +303,7 @@ export default class ObjectEntity extends IEntity {
const pinObject = this[Configuration.subObjectAttributeNameFromReference(objectReference, true)]
if (pinObject) {
const pinEntity = PinEntity.fromLegacyObject(pinObject)
pinEntity.LinkedTo = []
pinEntity.LinkedTo = new (PinEntity.attributes.LinkedTo)()
this.getCustomproperties(true).push(pinEntity)
Utility.objectSet(this, ["attributes", "CustomProperties", "ignored"], true)
}
@@ -321,21 +322,14 @@ export default class ObjectEntity extends IEntity {
if (this.getType() === Configuration.paths.materialExpressionComponentMask) {
// The following attributes are too generic therefore not assigned a MirroredEntity
const rgbaPins = Configuration.rgba.map(pinName =>
this.getPinEntities().find(pin => pin.PinName === pinName && (pin.recomputesNodeTitleOnChange = true))
this.getPinEntities().find(pin => pin.PinName.toString() === pinName && (pin.recomputesNodeTitleOnChange = true))
)
const attribute = {}
obj.R = new (
MirroredEntity.of(BooleanEntity).withDefault().flagSilent()
)(() => rgbaPins[0].DefaultValue)
obj.G = new (
MirroredEntity.of(BooleanEntity).withDefault().flagSilent()
)(() => rgbaPins[1].DefaultValue)
obj.B = new (
MirroredEntity.of(BooleanEntity).withDefault().flagSilent()
)(() => rgbaPins[2].DefaultValue)
obj.A = new (
MirroredEntity.of(BooleanEntity).withDefault().flagSilent()
)(() => rgbaPins[3].DefaultValue)
const silentBool = MirroredEntity.of(BooleanEntity).withDefault().flagSilent()
obj["R"] = new silentBool(() => rgbaPins[0].DefaultValue)
obj["G"] = new silentBool(() => rgbaPins[1].DefaultValue)
obj["B"] = new silentBool(() => rgbaPins[2].DefaultValue)
obj["A"] = new silentBool(() => rgbaPins[3].DefaultValue)
obj.keys = [...Configuration.rgba, ...super.keys.filter(k => !Configuration.rgba.includes(k))]
}
}
@@ -591,7 +585,7 @@ export default class ObjectEntity extends IEntity {
}
getDelegatePin() {
return this.getCustomproperties().find(pin => pin.PinType.PinCategory === "delegate")
return this.getCustomproperties().find(pin => pin.PinType.PinCategory.toString() === "delegate")
}
nodeColor() {

View File

@@ -29,12 +29,13 @@ export default class ObjectReferenceEntity extends IEntity {
)
).map(([full, type, path]) => new this(type, path, full))
static typeReferenceGrammar = this.typeReference.map(v => new this(v, "", v))
/** @type {P<ObjectReferenceEntity>} */
static grammar = P.alt(
this.fullReferenceSerializedGrammar,
this.fullReferenceGrammar,
this.typeReferenceGrammar,
).label("ObjectReferenceEntity")
static grammar = /** @type {P<ObjectReferenceEntity>} */(
P.alt(
this.fullReferenceSerializedGrammar,
this.fullReferenceGrammar,
this.typeReferenceGrammar,
).label("ObjectReferenceEntity")
)
#type
get type() {

View File

@@ -92,8 +92,9 @@ export default class PinEntity extends IEntity {
bAdvancedView: BooleanEntity.withDefault(),
bOrphanedPin: BooleanEntity.withDefault(),
}
/** @type {P<PinEntity>} */
static grammar = Grammar.createEntityGrammar(this)
static grammar = /** @type {P<PinEntity>} */(
Grammar.createEntityGrammar(this)
)
#recomputesNodeTitleOnChange = false
set recomputesNodeTitleOnChange(value) {

View File

@@ -5,13 +5,15 @@ import SymbolEntity from "./SymbolEntity.js"
export default class PinReferenceEntity extends IEntity {
static grammar = P.seq(
SymbolEntity.grammar,
P.whitespace,
GuidEntity.grammar
static grammar = /** @type {P<PinReferenceEntity>} */(
P.seq(
SymbolEntity.grammar,
P.whitespace,
GuidEntity.grammar
)
.map(([objectName, _1, pinGuid]) => new this(objectName, pinGuid))
.label("PinReferenceEntity")
)
.map(([objectName, _1, pinGuid]) => new this(objectName, pinGuid))
.label("PinReferenceEntity")
/**
* @param {SymbolEntity} objectName

View File

@@ -1,3 +1,4 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import BooleanEntity from "./BooleanEntity.js"
import FunctionReferenceEntity from "./FunctionReferenceEntity.js"
@@ -21,7 +22,9 @@ export default class PinTypeEntity extends IEntity {
bIsUObjectWrapper: BooleanEntity.withDefault(),
bSerializeAsSinglePrecisionFloat: BooleanEntity.withDefault(),
}
static grammar = Grammar.createEntityGrammar(this).label("PinTypeEntity")
static grammar = /** @type {P<PinTypeEntity>} */(
Grammar.createEntityGrammar(this).label("PinTypeEntity")
)
constructor(values = {}) {
super(values)

View File

@@ -4,7 +4,7 @@ import Vector2DEntity from "./Vector2DEntity.js"
export default class RBSerializationVector2DEntity extends Vector2DEntity {
static grammar = P.alt(
static grammar = /** @type {P<RBSerializationVector2DEntity>} */(P.alt(
P.regArray(new RegExp(
/X\s*=\s*/.source + "(?<x>" + Grammar.numberRegexSource + ")"
+ "\\s+"
@@ -14,5 +14,5 @@ export default class RBSerializationVector2DEntity extends Vector2DEntity {
Y: Number(y),
})),
Vector2DEntity.grammar
).label("RBSerializationVector2DEntity")
).label("RBSerializationVector2DEntity"))
}

View File

@@ -1,3 +1,4 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import IEntity from "./IEntity.js"
import NumberEntity from "./NumberEntity.js"
@@ -10,7 +11,9 @@ export default class RotatorEntity extends IEntity {
P: NumberEntity.withDefault(),
Y: NumberEntity.withDefault(),
}
static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("RotatorEntity")
static grammar = /** @type {P<RotatorEntity>} */(
Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("RotatorEntity")
)
constructor(values) {
super(values)

View File

@@ -1,3 +1,4 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import GuidEntity from "./GuidEntity.js"
import IEntity from "./IEntity.js"
@@ -10,7 +11,9 @@ export default class ScriptVariableEntity extends IEntity {
ScriptVariable: ObjectReferenceEntity,
OriginalChangeId: GuidEntity,
}
static grammar = Grammar.createEntityGrammar(this).label("ScriptVariableEntity")
static grammar = /** @type {P<ScriptVariableEntity>} */(
Grammar.createEntityGrammar(this).label("ScriptVariableEntity")
)
constructor(values = {}) {
super(values)

View File

@@ -6,25 +6,26 @@ import NumberEntity from "./NumberEntity.js"
export default class SimpleSerializationRotatorEntity extends RotatorEntity {
static attributeSeparator = ", "
/** @type {P<SimpleSerializationRotatorEntity>} */
static grammar = P.alt(
P.regArray(new RegExp(
`(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
)).map(([_, p, pPrecision, y, yPrecision, r, rPrecision]) => new this({
R: new NumberEntity(r, rPrecision?.length),
P: new NumberEntity(p, pPrecision?.length),
Y: new NumberEntity(y, yPrecision?.length),
})),
RotatorEntity.grammar.map(v => new this({
R: v.R,
P: v.P,
Y: v.Y,
}))
).label("SimpleSerializationRotatorEntity")
static grammar = /** @type {P<SimpleSerializationRotatorEntity>} */(
P.alt(
P.regArray(new RegExp(
`(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
)).map(([_, p, pPrecision, y, yPrecision, r, rPrecision]) => new this({
R: new NumberEntity(r, rPrecision?.length),
P: new NumberEntity(p, pPrecision?.length),
Y: new NumberEntity(y, yPrecision?.length),
})),
RotatorEntity.grammar.map(v => new this({
R: v.R,
P: v.P,
Y: v.Y,
}))
).label("SimpleSerializationRotatorEntity")
)
toString(
insideString = false,

View File

@@ -6,21 +6,22 @@ import NumberEntity from "./NumberEntity.js"
export default class SimpleSerializationVector2DEntity extends Vector2DEntity {
static attributeSeparator = ", "
/** @type {P<SimpleSerializationVector2DEntity>} */
static grammar = P.alt(
P.regArray(new RegExp(
`(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
)).map(([_, x, xPrecision, y, yPrecision]) => new this({
X: new NumberEntity(x, xPrecision?.length),
Y: new NumberEntity(y, yPrecision?.length),
})),
Vector2DEntity.grammar.map(v => new this({
X: v.X,
Y: v.Y,
}))
).label("SimpleSerializationVector2DEntity")
static grammar = /** @type {P<SimpleSerializationVector2DEntity>} */(
P.alt(
P.regArray(new RegExp(
`(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
)).map(([_, x, xPrecision, y, yPrecision]) => new this({
X: new NumberEntity(x, xPrecision?.length),
Y: new NumberEntity(y, yPrecision?.length),
})),
Vector2DEntity.grammar.map(v => new this({
X: v.X,
Y: v.Y,
}))
).label("SimpleSerializationVector2DEntity")
)
toString(
insideString = false,

View File

@@ -4,7 +4,7 @@ import Vector4DEntity from "./Vector4DEntity.js"
export default class SimpleSerializationVector4DEntity extends Vector4DEntity {
static grammar = this.createGrammar()
static grammar = /** @type {P<SimpleSerializationVector4DEntity> } */(this.createGrammar())
static createGrammar() {
const number = Grammar.numberRegexSource

View File

@@ -6,25 +6,26 @@ import NumberEntity from "./NumberEntity.js"
export default class SimpleSerializationVectorEntity extends VectorEntity {
static attributeSeparator = ", "
/** @type {P<SimpleSerializationVectorEntity>} */
static grammar = P.alt(
P.regArray(new RegExp(
`(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
))
.map(([_, x, xPrecision, y, yPrecision, z, zPrecision]) => new this({
X: new NumberEntity(x, xPrecision?.length),
Y: new NumberEntity(y, yPrecision?.length),
Z: new NumberEntity(z, zPrecision?.length),
})),
VectorEntity.grammar.map(v => new this({
X: v.X,
Y: v.Y,
Z: v.Z,
}))
static grammar = /** @type {P<SimpleSerializationVectorEntity>} */(
P.alt(
P.regArray(new RegExp(
`(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
))
.map(([_, x, xPrecision, y, yPrecision, z, zPrecision]) => new this({
X: new NumberEntity(x, xPrecision?.length),
Y: new NumberEntity(y, yPrecision?.length),
Z: new NumberEntity(z, zPrecision?.length),
})),
VectorEntity.grammar.map(v => new this({
X: v.X,
Y: v.Y,
Z: v.Z,
}))
)
)
toString(

View File

@@ -4,9 +4,11 @@ import IPrintableEntity from "./IPrintableEntity.js"
export default class StringEntity extends IPrintableEntity {
static grammar = P.doubleQuotedString
.map(insideString => new this(Utility.unescapeString(insideString)))
.label("StringEntity")
static grammar = /** @type {P<StringEntity>} */(
P.doubleQuotedString
.map(insideString => new this(Utility.unescapeString(insideString)))
.label("StringEntity")
)
/** @param {String} value */
constructor(value = "") {

View File

@@ -1,3 +1,4 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import IEntity from "./IEntity.js"
@@ -7,7 +8,9 @@ export default class SymbolEntity extends IEntity {
fromAttribute: (value, type) => new this(value),
toAttribute: (value, type) => value.toString()
}
static grammar = Grammar.symbol.map(v => new this(v)).label("SymbolEntity")
static grammar = /** @type {P<SymbolEntity>} */(
Grammar.symbol.map(v => new this(v)).label("SymbolEntity")
)
constructor(value = "") {
super()

View File

@@ -1,3 +1,5 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import BooleanEntity from "./BooleanEntity.js"
import IEntity from "./IEntity.js"
import StringEntity from "./StringEntity.js"
@@ -12,6 +14,9 @@ export default class TerminalTypeEntity extends IEntity {
bTerminalIsWeakPointer: BooleanEntity,
bTerminalIsUObjectWrapper: BooleanEntity,
}
static grammar = /** @type {P<TerminalTypeEntity>} */(
Grammar.createEntityGrammar(this)
)
constructor(values) {
super(values)

View File

@@ -4,27 +4,28 @@ import IEntity from "./IEntity.js"
export default class UnknownKeysEntity extends IEntity {
/** @type {P<UnknownKeysEntity>} */
static grammar = P.seq(
// Lookbehind
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 =>
this.unknownEntityGrammar.map(attributeValue =>
values => values[attributeName] = attributeValue
static grammar = /** @type {P<UnknownKeysEntity>} */(
P.seq(
// Lookbehind
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 =>
this.unknownEntityGrammar.map(attributeValue =>
values => values[attributeName] = attributeValue
)
)
)
.sepBy(Grammar.commaSeparation),
P.reg(/\s*(?:,\s*)?\)/),
).map(([lookbehind, attributes, _2]) => {
lookbehind ??= ""
let values = {}
if (lookbehind.length) {
values.lookbehind = lookbehind
}
attributes.forEach(attributeSetter => attributeSetter(values))
return new this(values)
}).label("UnknownKeysEntity")
.sepBy(Grammar.commaSeparation),
P.reg(/\s*(?:,\s*)?\)/),
).map(([lookbehind, attributes, _2]) => {
lookbehind ??= ""
let values = {}
if (lookbehind.length) {
values.lookbehind = lookbehind
}
attributes.forEach(attributeSetter => attributeSetter(values))
return new this(values)
}).label("UnknownKeysEntity")
)
static {
IEntity.unknownEntity = this

View File

@@ -4,17 +4,19 @@ import PinEntity from "./PinEntity.js"
export default class UnknownPinEntity extends PinEntity {
static grammar = P.seq(
P.reg(new RegExp(`(${Grammar.Regex.Symbol.source})\\s*\\(\\s*`), 1),
Grammar.createAttributeGrammar(this).sepBy(Grammar.commaSeparation),
P.reg(/\s*(?:,\s*)?\)/)
).map(([lookbehind, attributes, _2]) => {
lookbehind ??= ""
let values = {}
if (lookbehind.length) {
values.lookbehind = lookbehind
}
attributes.forEach(attributeSetter => attributeSetter(values))
return new this(values)
}).label("UnknownPinEntity")
static grammar = /** @type {P<UnknownPinEntity>} */(
P.seq(
P.reg(new RegExp(`(${Grammar.Regex.Symbol.source})\\s*\\(\\s*`), 1),
Grammar.createAttributeGrammar(this).sepBy(Grammar.commaSeparation),
P.reg(/\s*(?:,\s*)?\)/)
).map(([lookbehind, attributes, _2]) => {
lookbehind ??= ""
let values = {}
if (lookbehind.length) {
values.lookbehind = lookbehind
}
attributes.forEach(attributeSetter => attributeSetter(values))
return new this(values)
}).label("UnknownPinEntity")
)
}

View File

@@ -1,3 +1,4 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import BooleanEntity from "./BooleanEntity.js"
import GuidEntity from "./GuidEntity.js"
@@ -13,7 +14,9 @@ export default class VariableReferenceEntity extends IEntity {
MemberGuid: GuidEntity,
bSelfContext: BooleanEntity,
}
static grammar = Grammar.createEntityGrammar(this).label("VariableReferenceEntity")
static grammar = /** @type {P<VariableReferenceEntity>} */(
Grammar.createEntityGrammar(this).label("VariableReferenceEntity")
)
constructor(values) {
super(values)

View File

@@ -10,8 +10,9 @@ export default class Vector2DEntity extends IEntity {
X: NumberEntity.withDefault(),
Y: NumberEntity.withDefault(),
}
/** @type {P<Vector2DEntity>} */
static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("Vector2DEntity")
static grammar = /** @type {P<Vector2DEntity>} */(
Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("Vector2DEntity")
)
constructor(values) {
super(values)

View File

@@ -1,3 +1,4 @@
import P from "parsernostrum"
import Grammar from "../serialization/Grammar.js"
import IEntity from "./IEntity.js"
import NumberEntity from "./NumberEntity.js"
@@ -11,7 +12,9 @@ export default class Vector4DEntity extends IEntity {
Z: NumberEntity.withDefault(),
W: NumberEntity.withDefault(),
}
static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("Vector4DEntity")
static grammar = /** @type {P<Vector4DEntity>} */(
Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("Vector4DEntity")
)
constructor(values) {
super(values)

View File

@@ -11,8 +11,9 @@ export default class VectorEntity extends IEntity {
Y: NumberEntity.withDefault(),
Z: NumberEntity.withDefault(),
}
/** @type {P<VectorEntity>} */
static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("VectorEntity")
static grammar = /** @type {P<VectorEntity>} */(
Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("VectorEntity")
)
constructor(values) {
super(values)