Small refactoring

This commit is contained in:
barsdeveloper
2024-09-08 22:28:42 +02:00
parent 2114abef5c
commit 19ef3bd10e
43 changed files with 542 additions and 504 deletions

View File

@@ -29,8 +29,11 @@ export default class ArrayEntity extends IEntity {
P.reg(/\s*(,\s*)?\)/, 1),
).map(([_0, values, trailing]) => {
values = values instanceof Array ? values : []
const result = new this(values)
result.trailing = trailing !== undefined
let Self = this
if (trailing !== undefined != Self.trailing) {
Self = Self.flagTrailing()
}
const result = new Self(values)
return result
}).label(`ArrayEntity of ${this.type?.className() ?? "unknown values"}`)
}

View File

@@ -27,16 +27,15 @@ export default class BooleanEntity extends IEntity {
this.#uppercase = value
}
/** @returns {P<BooleanEntity>} */
static createGrammar() {
return /** @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")
)
return 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")
}
constructor(value = false) {

View File

@@ -15,10 +15,9 @@ export default class ByteEntity extends IntegerEntity {
}
}
/** @returns {P<ByteEntity>} */
createGrammar() {
return /** @type {P<ByteEntity>} */(
// @ts-expect-error
P.numberByte.map(v => new this(v))
)
// @ts-expect-error
return P.numberByte.map(v => new this(v))
}
}

View File

@@ -10,10 +10,9 @@ export default class ColorChannelEntity extends IEntity {
this.value = value
}
/** @returns {P<ColorChannelEntity>} */
static createGrammar() {
return /** @type {P<ColorChannelEntity>} */(
P.number.map(v => new this(v))
)
return P.number.map(v => new this(v))
}
serialize(

View File

@@ -6,9 +6,8 @@ export default class EnumDisplayValueEntity extends EnumEntity {
static grammar = this.createGrammar()
/** @returns {P<EnumDisplayValueEntity>} */
static createGrammar() {
return /** @type {P<EnumDisplayValueEntity>} */(
P.reg(Grammar.Regex.InsideString).map(v => new this(v))
)
return P.reg(Grammar.Regex.InsideString).map(v => new this(v))
}
}

View File

@@ -6,9 +6,8 @@ export default class EnumEntity extends SymbolEntity {
static grammar = this.createGrammar()
/** @returns {P<EnumEntity>} */
static createGrammar() {
return /** @type {P<EnumEntity>} */(
Grammar.symbol.map(v => new this(v))
)
return Grammar.symbol.map(v => new this(v))
}
}

View File

@@ -24,6 +24,6 @@ export default class FunctionReferenceEntity extends IEntity {
/** @returns {P<FunctionReferenceEntity>} */
static createGrammar() {
return Grammar.createEntityGrammar(this, Grammar.commaSeparation, false, 0)
return Grammar.createEntityGrammar(this, Grammar.commaSeparation, 0, 0)
}
}

View File

@@ -28,10 +28,9 @@ export default class GuidEntity extends IEntity {
this.value = value
}
/** @returns {P<GuidEntity>} */
static createGrammar() {
return /** @type {P<GuidEntity>} */(
P.reg(/[0-9A-F]{32}/i).map(v => new this(v)).label("GuidEntity")
)
return P.reg(/[0-9A-F]{32}/i).map(v => new this(v)).label("GuidEntity")
}
serialize(

View File

@@ -52,7 +52,7 @@ export default class IEntity {
this.#lookbehind = value
}
#ignored = this.constructor.ignored
#ignored = /** @type {typeof IEntity} */(this.constructor).ignored
get ignored() {
return this.#ignored
}
@@ -62,15 +62,16 @@ export default class IEntity {
#quoted
get quoted() {
return /** @type {typeof IEntity} */(this.constructor).quoted ?? this.#quoted ?? false
return this.#quoted ?? /** @type {typeof IEntity} */(this.constructor).quoted ?? false
}
set quoted(value) {
this.#quoted = value
}
#trailing = this.constructor.trailing
/** @type {Boolean} */
#trailing
get trailing() {
return this.#trailing
return this.#trailing ?? /** @type {typeof IEntity} */(this.constructor).trailing ?? false
}
set trailing(value) {
this.#trailing = value
@@ -230,6 +231,16 @@ 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
}
/**
* @protected
* @param {String} string
@@ -299,6 +310,7 @@ export default class IEntity {
attributeSeparator = Self.attributeSeparator,
wrap = Self.wrap,
) {
const isSelfOverriden = Self !== this.constructor
let result = ""
let first = true
const keys = this instanceof IEntity ? this.keys : Object.keys(this)
@@ -315,7 +327,7 @@ export default class IEntity {
result += attributeSeparator
}
let keyValue = this instanceof Array ? `(${key})` : key
if (keyValue.length && (Self.attributes[key]?.quoted === true || value.quoted === true)) {
if (keyValue.length && (Self.attributes[key]?.quoted || value.quoted)) {
keyValue = `"${keyValue}"`
}
if (valueType.inlined) {
@@ -340,7 +352,7 @@ export default class IEntity {
let serialization = value?.serialize(insideString, indentation)
result += serialization
}
if (this instanceof IEntity && this.trailing && result.length) {
if (this instanceof IEntity && (isSelfOverriden && Self.trailing || this.trailing) && result.length) {
result += attributeSeparator
}
return wrap(/** @type {IEntity} */(this), result)
@@ -356,6 +368,7 @@ export default class IEntity {
attributeSeparator = Self.attributeSeparator,
wrap = Self.wrap,
) {
const isSelfOverriden = Self !== this.constructor
let result = this instanceof Array
? IEntity.prototype.doSerialize.bind(this)(insideString, indentation, Self, printKey, keySeparator, attributeSeparator, wrap)
: this.doSerialize(insideString, indentation, Self, printKey, keySeparator, attributeSeparator, wrap)

View File

@@ -25,10 +25,9 @@ export default class Integer64Entity extends IEntity {
this.value = BigInt(value)
}
/** @returns {P<Integer64Entity>} */
static createGrammar() {
return /** @type {P<Integer64Entity>} */(
P.numberBigInteger.map(v => new this(v))
)
return P.numberBigInteger.map(v => new this(v))
}
serialize(

View File

@@ -16,9 +16,8 @@ export default class IntegerEntity extends NumberEntity {
}
}
/** @returns {P<IntegerEntity>} */
static createGrammar() {
return /** @type {P<IntegerEntity>} */(
P.numberInteger.map(v => new this(v))
)
return P.numberInteger.map(v => new this(v))
}
}

View File

@@ -12,19 +12,18 @@ export default class InvariantTextEntity extends IEntity {
this.value = value
}
/** @returns {P<InvariantTextEntity>} */
static createGrammar() {
return /** @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")
return 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")
}
doSerialize() {

View File

@@ -28,12 +28,11 @@ export default class KeyBindingEntity extends IEntity {
/** @type {InstanceType<typeof KeyBindingEntity.attributes.Key>} */ this.Key
}
/** @returs {P<KeyBindingEntity>} */
static createGrammar() {
return /** @type {P<KeyBindingEntity>} */(
P.alt(
SymbolEntity.grammar.map(identifier => new this({ Key: identifier })),
Grammar.createEntityGrammar(this)
)
return P.alt(
SymbolEntity.grammar.map(identifier => new this({ Key: identifier })),
Grammar.createEntityGrammar(this),
)
}
}

View File

@@ -57,10 +57,9 @@ export default class LinearColorEntity extends IEntity {
this.#updateHSV()
}
/** @returns {P<LinearColorEntity>} */
static createGrammar() {
return /** @type {P<LinearColorEntity>} */(
Grammar.createEntityGrammar(this).label("LinearColorEntity")
)
return Grammar.createEntityGrammar(this, Grammar.commaSeparation, 0.5).label("LinearColorEntity")
}
/** @param {LinearColorEntity} value */

View File

@@ -24,25 +24,24 @@ export default class LocalizedTextEntity extends IEntity {
/** @type {InstanceType<typeof LocalizedTextEntity.attributes.value>} */ this.value
}
/** @returns {P<LocalizedTextEntity>} */
static createGrammar() {
return /** @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")
)
return 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")
}
toString() {

View File

@@ -21,10 +21,9 @@ export default class MacroGraphReferenceEntity extends IEntity {
/** @type {InstanceType<typeof MacroGraphReferenceEntity.attributes.GraphGuid>} */ this.GraphGuid
}
/** @returns {P<MacroGraphReferenceEntity>} */
static createGrammar() {
return /** @type {P<MacroGraphReferenceEntity>} */(
Grammar.createEntityGrammar(this)
)
return Grammar.createEntityGrammar(this)
}
getMacroName() {

View File

@@ -14,9 +14,8 @@ export default class NaturalNumberEntity extends IntegerEntity {
super.value = value
}
/** @returns {P<NaturalNumberEntity>} */
static createGrammar() {
return /** @type {P<NaturalNumberEntity>} */(
P.numberNatural.map(v => new this(v))
)
return P.numberNatural.map(v => new this(v))
}
}

View File

@@ -5,12 +5,11 @@ export default class NullEntity extends IEntity {
static grammar = this.createGrammar()
/** @returns {P<NullEntity>} */
static createGrammar() {
return /** @type {P<NullEntity>} */(
// @ts-expect-error
P.reg(new RegExp(String.raw`\(${P.whitespaceInlineOpt.getParser().regexp.source}\)`))
.map(v => new this())
)
// @ts-expect-error
return P.reg(new RegExp(String.raw`\(${P.whitespaceInlineOpt.getParser().regexp.source}\)`))
.map(v => new this())
}
serialize(

View File

@@ -41,16 +41,15 @@ export default class NumberEntity extends IEntity {
}
}
/** @returns {P<NumberEntity>} */
static createGrammar() {
return /** @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")
return 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")
}
/**

View File

@@ -666,7 +666,10 @@ export default class ObjectEntity extends IEntity {
wrap = Self.wrap,
) {
const deeperIndentation = indentation + Configuration.indentation
const initial_trailing = this.trailing
this.trailing = false
const content = super.doSerialize(insideString, deeperIndentation, Self, printKey, keySeparator, attributeSeparator, wrap)
this.trailing = initial_trailing
let result = indentation + "Begin Object"
+ ((this.Class?.type || this.Class?.path)
// && Self.attributes.Class.ignored !== true
@@ -694,11 +697,14 @@ export default class ObjectEntity extends IEntity {
)
+ (content ? attributeSeparator + content : "")
+ (Self.attributes.CustomProperties.ignored !== true && this.CustomProperties.ignored !== true
? this.getCustomproperties().map(pin =>
deeperIndentation
+ printKey("CustomProperties ")
+ pin.serialize(insideString)
).join(Self.attributeSeparator)
? this.getCustomproperties()
.map(pin =>
attributeSeparator
+ deeperIndentation
+ printKey("CustomProperties ")
+ pin.serialize(insideString)
)
.join("")
: ""
)
+ attributeSeparator

View File

@@ -160,10 +160,9 @@ export default class PinEntity extends IEntity {
/** @type {ObjectEntity} */ this.objectEntity
}
/** @returns {P<PinEntity>} */
static createGrammar() {
return /** @type {P<PinEntity>} */(
Grammar.createEntityGrammar(this)
)
return Grammar.createEntityGrammar(this)
}
/** @param {ObjectEntity} objectEntity */

View File

@@ -17,16 +17,15 @@ export default class PinReferenceEntity extends IEntity {
this.pinGuid = pinGuid
}
/** @returns {P<PinReferenceEntity>} */
static createGrammar() {
return /** @type {P<PinReferenceEntity>} */(
P.seq(
SymbolEntity.grammar,
P.whitespace,
GuidEntity.grammar
)
.map(([objectName, _1, pinGuid]) => new this(objectName, pinGuid))
.label("PinReferenceEntity")
return P.seq(
SymbolEntity.grammar,
P.whitespace,
GuidEntity.grammar
)
.map(([objectName, _1, pinGuid]) => new this(objectName, pinGuid))
.label("PinReferenceEntity")
}
doSerialize() {

View File

@@ -39,10 +39,9 @@ export default class PinTypeEntity extends IEntity {
/** @type {InstanceType<typeof PinTypeEntity.attributes.bSerializeAsSinglePrecisionFloat>} */ this.bSerializeAsSinglePrecisionFloat
}
/** @returns {P<PinTypeEntity>} */
static createGrammar() {
return /** @type {P<PinTypeEntity>} */(
Grammar.createEntityGrammar(this).label("PinTypeEntity")
)
return Grammar.createEntityGrammar(this).label("PinTypeEntity")
}
/** @param {PinTypeEntity} other */

View File

@@ -6,8 +6,9 @@ export default class RBSerializationVector2DEntity extends Vector2DEntity {
static grammar = this.createGrammar()
/** @returns {P<RBSerializationVector2DEntity>} */
static createGrammar() {
return /** @type {P<RBSerializationVector2DEntity>} */(P.alt(
return P.alt(
P.regArray(new RegExp(
/X\s*=\s*/.source + "(?<x>" + Grammar.numberRegexSource + ")"
+ "\\s+"
@@ -20,6 +21,6 @@ export default class RBSerializationVector2DEntity extends Vector2DEntity {
X: v.X,
Y: v.Y,
}))
).label("RBSerializationVector2DEntity"))
).label("RBSerializationVector2DEntity")
}
}

View File

@@ -20,10 +20,9 @@ export default class RotatorEntity extends IEntity {
/** @type {InstanceType<typeof RotatorEntity.attributes.Y>} */ this.Y
}
/** @returns {P<RotatorEntity>} */
static createGrammar() {
return /** @type {P<RotatorEntity>} */(
Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("RotatorEntity")
)
return Grammar.createEntityGrammar(this, Grammar.commaSeparation, 1).label("RotatorEntity")
}
getRoll() {

View File

@@ -19,9 +19,8 @@ export default class ScriptVariableEntity extends IEntity {
/** @type {InstanceType<typeof ScriptVariableEntity.attributes.OriginalChangeId>} */ this.OriginalChangeId
}
/** @returns {P<ScriptVariableEntity>} */
static createGrammar() {
return /** @type {P<ScriptVariableEntity>} */(
Grammar.createEntityGrammar(this).label("ScriptVariableEntity")
)
return Grammar.createEntityGrammar(this).label("ScriptVariableEntity")
}
}

View File

@@ -7,27 +7,26 @@ export default class SimpleSerializationRotatorEntity extends RotatorEntity {
static attributeSeparator = ", "
static grammar = this.createGrammar()
/** @returns {P<SimpleSerializationRotatorEntity>} */
static createGrammar() {
return /** @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 (RotatorEntity.attributes.R)(r, rPrecision?.length),
P: new (RotatorEntity.attributes.P)(p, pPrecision?.length),
Y: new (RotatorEntity.attributes.Y)(y, yPrecision?.length),
})),
RotatorEntity.grammar.map(v => new this({
R: v.R,
P: v.P,
Y: v.Y,
}))
).label("SimpleSerializationRotatorEntity")
)
return 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 (RotatorEntity.attributes.R)(r, rPrecision?.length),
P: new (RotatorEntity.attributes.P)(p, pPrecision?.length),
Y: new (RotatorEntity.attributes.Y)(y, yPrecision?.length),
})),
RotatorEntity.grammar.map(v => new this({
R: v.R,
P: v.P,
Y: v.Y,
}))
).label("SimpleSerializationRotatorEntity")
}
doSerialize() {

View File

@@ -7,23 +7,22 @@ export default class SimpleSerializationVector2DEntity extends Vector2DEntity {
static attributeSeparator = ", "
static grammar = this.createGrammar()
/** @returns {P<SimpleSerializationVector2DEntity>} */
static createGrammar() {
return /** @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 (Vector2DEntity.attributes.X)(x, xPrecision?.length),
Y: new (Vector2DEntity.attributes.Y)(y, yPrecision?.length),
})),
Vector2DEntity.grammar.map(v => new this({
X: v.X,
Y: v.Y,
}))
).label("SimpleSerializationVector2DEntity")
)
return P.alt(
P.regArray(new RegExp(
`(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
)).map(([_, x, xPrecision, y, yPrecision]) => new this({
X: new (Vector2DEntity.attributes.X)(x, xPrecision?.length),
Y: new (Vector2DEntity.attributes.Y)(y, yPrecision?.length),
})),
Vector2DEntity.grammar.map(v => new this({
X: v.X,
Y: v.Y,
}))
).label("SimpleSerializationVector2DEntity")
}
doSerialize() {

View File

@@ -8,31 +8,30 @@ export default class SimpleSerializationVectorEntity extends VectorEntity {
static attributeSeparator = ", "
static grammar = this.createGrammar()
/** @returns {P<SimpleSerializationVectorEntity>} */
static createGrammar() {
return /** @type {P<SimpleSerializationVectorEntity>} */(
P.alt(
P.regArray(new RegExp(
`(${NumberEntity.numberRegexSource})`
// If allow simple serialization then it can parse only a single number ...
+ (this.allowShortSerialization ? `(?:` : "")
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
// ... that will be assigned to X and the rest is optional and set to 0
+ (this.allowShortSerialization ? `)?` : "")
))
.map(([_, x, xPrecision, y, yPrecision, z, zPrecision]) => new this({
X: new (VectorEntity.attributes.X)(x, xPrecision?.length),
Y: new (VectorEntity.attributes.Y)(y, yPrecision?.length),
Z: new (VectorEntity.attributes.Z)(z, zPrecision?.length),
})),
VectorEntity.grammar.map(v => new this({
X: v.X,
Y: v.Y,
Z: v.Z,
}))
)
return P.alt(
P.regArray(new RegExp(
`(${NumberEntity.numberRegexSource})`
// If allow simple serialization then it can parse only a single number ...
+ (this.allowShortSerialization ? `(?:` : "")
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
+ String.raw`\s*,\s*`
+ `(${NumberEntity.numberRegexSource})`
// ... that will be assigned to X and the rest is optional and set to 0
+ (this.allowShortSerialization ? `)?` : "")
))
.map(([_, x, xPrecision, y, yPrecision, z, zPrecision]) => new this({
X: new (VectorEntity.attributes.X)(x, xPrecision?.length),
Y: new (VectorEntity.attributes.Y)(y, yPrecision?.length),
Z: new (VectorEntity.attributes.Z)(z, zPrecision?.length),
})),
VectorEntity.grammar.map(v => new this({
X: v.X,
Y: v.Y,
Z: v.Z,
}))
)
}

View File

@@ -1,28 +1,48 @@
import P from "parsernostrum"
import Utility from "../Utility.js"
import IEntity from "./IEntity.js"
export default class StringEntity extends IEntity {
static grammar = this.createGrammar()
static escapedCharacters = /['"\\]/g
static unescapedBackslash = /(?<=(?:[^\\]|^)(?:\\\\)*)\\(?!\\)/
constructor(value = "") {
super()
this.value = value
}
/** @returns {P<StringEntity>} */
static createGrammar() {
return /** @type {P<StringEntity>} */(
P.doubleQuotedString
.map(insideString => new this(Utility.unescapeString(insideString)))
.label("StringEntity")
)
return P.doubleQuotedString
.map(insideString => new this(StringEntity.unescape(insideString)))
.label("StringEntity")
}
/** @param {String} value */
static escape(value, inline = true) {
let result = value.replaceAll(new RegExp(`(${StringEntity.escapedCharacters.source})`, "g"), '\\$1')
if (inline) {
result = result
.replaceAll("\n", "\\n") // Replace newline with \n
.replaceAll("\t", "\\t") // Replace tab with \t
}
return result
}
/** @param {String} value */
static unescape(value) {
return value
.replaceAll(new RegExp(StringEntity.unescapedBackslash.source + "t", "g"), "\t") // Replace tab with \t
.replaceAll(new RegExp(StringEntity.unescapedBackslash.source + "n", "g"), "\n") // Replace newline with \n
.replaceAll(new RegExp(`\\\\(${StringEntity.escapedCharacters.source})`, "g"), "$1")
}
doSerialize(insideString = false) {
let result = `"${Utility.escapeString(this.value)}"`
let result = `"${StringEntity.escape(this.value)}"`
if (insideString) {
result = Utility.escapeString(result, false)
result = StringEntity.escape(result, false)
}
return result
}

View File

@@ -10,10 +10,9 @@ export default class SymbolEntity extends IEntity {
}
static grammar = this.createGrammar()
/** @returns {P<SymbolEntity>} */
static createGrammar() {
return /** @type {P<SymbolEntity>} */(
Grammar.symbol.map(v => new this(v)).label("SymbolEntity")
)
return Grammar.symbol.map(v => new this(v)).label("SymbolEntity")
}
constructor(value = "") {

View File

@@ -25,9 +25,8 @@ export default class TerminalTypeEntity extends IEntity {
/** @type {Boolean} */ this.bTerminalIsUObjectWrapper
}
/** @returns {P<TerminalTypeEntity>} */
static createGrammar() {
return /** @type {P<TerminalTypeEntity>} */(
Grammar.createEntityGrammar(this)
)
return Grammar.createEntityGrammar(this)
}
}

View File

@@ -10,28 +10,27 @@ export default class UnknownKeysEntity extends IEntity {
IEntity.unknownEntity = this
}
/** @returns {P<UnknownKeysEntity>} */
static createGrammar() {
return /** @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
)
return 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")
}
}

View File

@@ -6,21 +6,20 @@ export default class UnknownPinEntity extends PinEntity {
static grammar = this.createGrammar()
/** @returns {P<UnknownPinEntity>} */
static createGrammar() {
return /** @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")
)
return 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

@@ -24,9 +24,8 @@ export default class VariableReferenceEntity extends IEntity {
/** @type {InstanceType<typeof VariableReferenceEntity.attributes.bSelfContext>} */ this.bSelfContext
}
/** @returns {P<VariableReferenceEntity>} */
static createGrammar() {
return /** @type {P<VariableReferenceEntity>} */(
Grammar.createEntityGrammar(this).label("VariableReferenceEntity")
)
return Grammar.createEntityGrammar(this).label("VariableReferenceEntity")
}
}

View File

@@ -20,7 +20,7 @@ export default class Vector2DEntity extends IEntity {
/** @returns {P<Vector2DEntity>} */
static createGrammar() {
return Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("Vector2DEntity")
return Grammar.createEntityGrammar(this, Grammar.commaSeparation, 1).label("Vector2DEntity")
}
/** @returns {[Number, Number]} */

View File

@@ -22,10 +22,9 @@ export default class Vector4DEntity extends IEntity {
/** @type {InstanceType<typeof Vector4DEntity.attributes.W>} */ this.W
}
/** @returns {P<Vector4DEntity>} */
static createGrammar() {
return /** @type {P<Vector4DEntity>} */(
Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("Vector4DEntity")
)
return Grammar.createEntityGrammar(this, Grammar.commaSeparation, 1).label("Vector4DEntity")
}
/** @returns {[Number, Number, Number, Number]} */

View File

@@ -19,11 +19,10 @@ export default class VectorEntity extends IEntity {
/** @type {InstanceType<typeof VectorEntity.attributes.Y>} */ this.Y
/** @type {InstanceType<typeof VectorEntity.attributes.X>} */ this.Z
}
c
/** @returns {P<VectorEntity>} */
static createGrammar() {
return /** @type {P<VectorEntity>} */(
Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("VectorEntity")
)
return Grammar.createEntityGrammar(this, Grammar.commaSeparation, 1).label("VectorEntity")
}
/** @returns {[Number, Number, Number]} */