mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-04 08:41:34 +08:00
Fixing various bugs in the grammar
This commit is contained in:
@@ -28,8 +28,7 @@ export default class GeneralSerializer extends ISerializer {
|
||||
* @returns {T}
|
||||
*/
|
||||
read(value) {
|
||||
// @ts-expect-error
|
||||
let grammar = Grammar.getGrammarForType(ISerializer.grammar, this.entityType)
|
||||
let grammar = Grammar.grammarFor(undefined, this.entityType)
|
||||
const parseResult = grammar.parse(value)
|
||||
if (!parseResult.status) {
|
||||
throw new Error(`Error when trying to parse the entity ${this.entityType.prototype.constructor.name}.`)
|
||||
|
||||
@@ -72,28 +72,30 @@ export default class Grammar {
|
||||
|
||||
/* --- Primitive --- */
|
||||
|
||||
static null = P.regex(/\(\s*\)/).map(() => null)
|
||||
static true = P.regex(/true/i).map(() => true)
|
||||
static false = P.regex(/false/i).map(() => false)
|
||||
static boolean = Grammar.regexMap(/(true)|false/i, v => v[1] ? true : false)
|
||||
static number = P.regex(Grammar.Regex.Number).map(Number)
|
||||
static integer = P.regex(Grammar.Regex.Integer).map(Number)
|
||||
static bigInt = P.regex(Grammar.Regex.Integer).map(BigInt)
|
||||
static realUnit = P.regex(Grammar.Regex.RealUnit).map(Number)
|
||||
static naturalNumber = P.regex(/\d+/).map(Number)
|
||||
static byteNumber = P.regex(Grammar.Regex.ByteInteger).map(Number)
|
||||
static null = P.lazy(() => P.regex(/\(\s*\)/).map(() => null))
|
||||
static true = P.lazy(() => P.regex(/true/i).map(() => true))
|
||||
static false = P.lazy(() => P.regex(/false/i).map(() => false))
|
||||
static boolean = P.lazy(() => Grammar.regexMap(/(true)|false/i, v => v[1] ? true : false))
|
||||
static number = P.lazy(() => P.regex(Grammar.Regex.Number).map(Number))
|
||||
static integer = P.lazy(() => P.regex(Grammar.Regex.Integer).map(Number))
|
||||
static bigInt = P.lazy(() => P.regex(Grammar.Regex.Integer).map(BigInt))
|
||||
static realUnit = P.lazy(() => P.regex(Grammar.Regex.RealUnit).map(Number))
|
||||
static naturalNumber = P.lazy(() => P.regex(/\d+/).map(Number))
|
||||
static byteNumber = P.lazy(() => P.regex(Grammar.Regex.ByteInteger).map(Number))
|
||||
static string = P.lazy(() =>
|
||||
Grammar.regexMap(
|
||||
new RegExp(`"(${Grammar.Regex.InsideString.source})"`),
|
||||
([_0, value]) => value
|
||||
)
|
||||
.map((insideString) => Utility.unescapeString(insideString))
|
||||
)
|
||||
|
||||
/* --- Fragment --- */
|
||||
|
||||
static colorValue = this.byteNumber
|
||||
static word = P.regex(Grammar.Regex.Word)
|
||||
static string = Grammar.regexMap(
|
||||
new RegExp(`"(${Grammar.Regex.InsideString})"`),
|
||||
([_0, value]) => value
|
||||
)
|
||||
.map(([_1, insideString, _2]) => Utility.unescapeString(insideString))
|
||||
static path = Grammar.regexMap(
|
||||
new RegExp(`(${Grammar.Regex.Path})|"(${Grammar.Regex.PathOptSpace})"|'"(${Grammar.Regex.PathOptSpace})"'`),
|
||||
new RegExp(`(${Grammar.Regex.Path.source})|"(${Grammar.Regex.PathOptSpace.source})"|'"(${Grammar.Regex.PathOptSpace.source})"'`),
|
||||
([_0, a, b, c]) => a ?? b ?? c
|
||||
)
|
||||
static symbol = P.regex(Grammar.Regex.Symbol)
|
||||
@@ -284,16 +286,8 @@ export default class Grammar {
|
||||
entityType.lookbehind.length
|
||||
? P.regex(new RegExp(`${entityType.lookbehind}\\s*\\(\\s*`))
|
||||
: P.regex(/\(\s*/),
|
||||
P.seq(
|
||||
this.createAttributeGrammar(entityType),
|
||||
P.seq(
|
||||
this.commaSeparation,
|
||||
this.createAttributeGrammar(entityType),
|
||||
)
|
||||
.map(([_0, value]) => value)
|
||||
.many()
|
||||
).map(([first, remaining]) => [first, ...remaining]),
|
||||
this.optTrailingComma,
|
||||
this.createAttributeGrammar(entityType).sepBy1(this.commaSeparation),
|
||||
P.regex(/\s*(?:,\s*)?\)/),
|
||||
)
|
||||
.map(([_0, attributes, _2]) => {
|
||||
let values = {}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import Grammar from "./Grammar.js"
|
||||
import SerializerFactory from "./SerializerFactory.js"
|
||||
import Utility from "../Utility.js"
|
||||
import IEntity from "../entity/IEntity.js"
|
||||
import Parsimmon from "parsimmon"
|
||||
|
||||
/**
|
||||
* @typedef {import("../entity/IEntity").EntityConstructor} EntityConstructor
|
||||
@@ -13,8 +11,6 @@ import Parsimmon from "parsimmon"
|
||||
/** @template {AnyValue} T */
|
||||
export default class ISerializer {
|
||||
|
||||
static grammar = Parsimmon.createLanguage(new Grammar())
|
||||
|
||||
/** @param {AnyValueConstructor} entityType */
|
||||
constructor(
|
||||
entityType,
|
||||
@@ -131,7 +127,7 @@ export default class ISerializer {
|
||||
|
||||
showProperty(entity, object, attributeKey, attributeValue) {
|
||||
const attributes = /** @type {EntityConstructor} */(this.entityType).attributes
|
||||
const attribute = Utility.objectGet(attributes, attributeKey)
|
||||
const attribute = attributes[attributeKey]
|
||||
if (attribute?.constructor === Object) {
|
||||
if (attribute.ignored) {
|
||||
return false
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import Grammar from "./Grammar.js"
|
||||
import ISerializer from "./ISerializer.js"
|
||||
import ObjectEntity from "../entity/ObjectEntity.js"
|
||||
import PinEntity from "../entity/PinEntity.js"
|
||||
@@ -22,7 +23,7 @@ export default class ObjectSerializer extends ISerializer {
|
||||
|
||||
/** @param {String} value */
|
||||
read(value) {
|
||||
const parseResult = ISerializer.grammar.objectEntity.parse(value)
|
||||
const parseResult = Grammar.objectEntity.parse(value)
|
||||
if (!parseResult.status) {
|
||||
throw new Error("Error when trying to parse the object.")
|
||||
}
|
||||
@@ -34,7 +35,7 @@ export default class ObjectSerializer extends ISerializer {
|
||||
* @returns {ObjectEntity[]}
|
||||
*/
|
||||
readMultiple(value) {
|
||||
const parseResult = ISerializer.grammar.multipleObject.parse(value)
|
||||
const parseResult = Grammar.multipleObject.parse(value)
|
||||
if (!parseResult.status) {
|
||||
throw new Error("Error when trying to parse the object.")
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import Utility from "../Utility.js"
|
||||
import GeneralSerializer from "./GeneralSerializer.js"
|
||||
|
||||
/**
|
||||
* @typedef {import("../entity/IEntity").AnyValue} AnyValue
|
||||
/**
|
||||
* @typedef {import("../entity/IEntity").AnyValue} AnyValue
|
||||
* @typedef {import("../entity/IEntity").AnyValueConstructor<*>} AnyValueConstructor
|
||||
*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user