mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-04 08:50:33 +08:00
Refactoring entities (#23)
* Still WIP * WIP * ArrayEntity parsing fixed * Fix format text entity * Tests for various entity classes and update entity class implementations * More tests and fixed * More entities fixed * Simple entities serialization fixed * Entities tests fixed * Remove serialization bits * Fix Function reference * CustomProperties creating fixed * WIP * Better typing for grammars * Decoding code fixes * Fixing still * Several fixes * rename toString to serialize * Several fixes * More fixes * Moving more stuff out of Utility * Several fixes * Fixing Linear color entity print * Serialization fixes * Fix serialization * Method to compute grammar * Renaming fix * Fix array grammar and equality check * Fix inlined keys * Fix type * Several serialization fixes * Fix undefined dereference * Several fixes * More fixes and cleanup * Fix keys quoting mechanism * Fix natural number assignment * Fix Int64 toString() * Fix quoted keys for inlined arrays * Fix PG pins * Fix several test cases * Types fixes * New pin default value empty * Fix non existing DefaultValue for variadic nodes * Smaller fixes for crashes * Fix link color when attached to knot * Linking test and more reliability operations for adding pins * Improve issue 18 test * More tests and fixes * Fix enum pin entity * Remove failing test
This commit is contained in:
@@ -1,56 +1,62 @@
|
||||
import Parsernostrum from "parsernostrum"
|
||||
import Grammar from "../serialization/Grammar.js"
|
||||
import AttributeInfo from "./AttributeInfo.js"
|
||||
import IEntity from "./IEntity.js"
|
||||
import P from "parsernostrum"
|
||||
import InvariantTextEntity from "./InvariantTextEntity.js"
|
||||
import LocalizedTextEntity from "./LocalizedTextEntity.js"
|
||||
import Union from "./Union.js"
|
||||
import StringEntity from "./StringEntity.js"
|
||||
import IEntity from "./IEntity.js"
|
||||
|
||||
export default class FormatTextEntity extends IEntity {
|
||||
|
||||
static attributes = {
|
||||
...super.attributes,
|
||||
value: new AttributeInfo({
|
||||
type: [new Union(String, LocalizedTextEntity, InvariantTextEntity, FormatTextEntity)],
|
||||
default: [],
|
||||
}),
|
||||
lookbehind: /** @type {AttributeInfo<Union<String[]>>} */(new AttributeInfo({
|
||||
...super.attributes.lookbehind,
|
||||
default: new Union("LOCGEN_FORMAT_NAMED", "LOCGEN_FORMAT_ORDERED"),
|
||||
})),
|
||||
}
|
||||
static attributeSeparator = ", "
|
||||
static lookbehind = ["LOCGEN_FORMAT_NAMED", "LOCGEN_FORMAT_ORDERED"]
|
||||
static grammar = this.createGrammar()
|
||||
|
||||
static createGrammar() {
|
||||
return Parsernostrum.seq(
|
||||
Parsernostrum.reg(
|
||||
// Resulting regex: /(LOCGEN_FORMAT_NAMED|LOCGEN_FORMAT_ORDERED)\s*/
|
||||
new RegExp(`(${this.attributes.lookbehind.default.values.reduce((acc, cur) => acc + "|" + cur)})\\s*`),
|
||||
1
|
||||
),
|
||||
Grammar.grammarFor(this.attributes.value)
|
||||
)
|
||||
.map(([lookbehind, values]) => {
|
||||
const result = new this({
|
||||
value: values,
|
||||
lookbehind,
|
||||
})
|
||||
return result
|
||||
})
|
||||
/** @param {(StringEntity | LocalizedTextEntity | InvariantTextEntity | FormatTextEntity)[]} values */
|
||||
constructor(values) {
|
||||
super()
|
||||
this.values = values
|
||||
}
|
||||
|
||||
constructor(values) {
|
||||
super(values)
|
||||
/** @type {(String | LocalizedTextEntity | InvariantTextEntity | FormatTextEntity)[]} */ this.value
|
||||
/** @returns {P<FormatTextEntity>} */
|
||||
static createGrammar() {
|
||||
return 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")
|
||||
}
|
||||
|
||||
doSerialize(
|
||||
insideString = false,
|
||||
indentation = "",
|
||||
Self = /** @type {typeof FormatTextEntity} */(this.constructor),
|
||||
printKey = Self.printKey,
|
||||
keySeparator = Self.keySeparator,
|
||||
attributeSeparator = Self.attributeSeparator,
|
||||
wrap = Self.wrap,
|
||||
) {
|
||||
const separator = Self.attributeSeparator
|
||||
return this.lookbehind + "("
|
||||
+ this.values.map(v => v.serialize(insideString)).join(separator)
|
||||
+ (Self.trailing ? separator : "")
|
||||
+ ")"
|
||||
}
|
||||
|
||||
toString() {
|
||||
const pattern = this.value?.[0]?.toString() // The pattern is always the first element of the array
|
||||
const pattern = this.values?.[0]?.toString() // The pattern is always the first element of the array
|
||||
if (!pattern) {
|
||||
return ""
|
||||
}
|
||||
const values = this.value.slice(1).map(v => v.toString())
|
||||
return this.lookbehind == "LOCGEN_FORMAT_NAMED"
|
||||
const values = this.values.slice(1).map(v => v?.valueOf())
|
||||
let result = this.lookbehind == "LOCGEN_FORMAT_NAMED"
|
||||
? pattern.replaceAll(/\{([a-zA-Z]\w*)\}/g, (substring, arg) => {
|
||||
const argLocation = values.indexOf(arg) + 1
|
||||
return argLocation > 0 && argLocation < values.length
|
||||
@@ -65,5 +71,6 @@ export default class FormatTextEntity extends IEntity {
|
||||
: substring
|
||||
})
|
||||
: ""
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user