Replace parsing and test libraries

* WIP

* WIP

* wip

* WIP

* Several fixes

* Tests wip port to playwright

* WIP

* Fix more tests

* Serialization tests fixed

* Several fixes for tests

* Input options types

* Type adjustments

* Fix object reference parser

* Tests fixes

* More tests fixes
This commit is contained in:
barsdeveloper
2024-02-14 00:40:42 +01:00
committed by GitHub
parent 90584e16c0
commit 7469d55518
126 changed files with 7443 additions and 6253 deletions

View File

@@ -1,7 +1,7 @@
import Serializer from "./Serializer.js"
/**
* @template {SimpleValueType<SimpleValue>} T
* @template {AttributeConstructor<Attribute>} T
* @extends {Serializer<T>}
*/
export default class CustomSerializer extends Serializer {

View File

@@ -1,13 +1,11 @@
import Configuration from "../Configuration.js"
import IEntity from "../entity/IEntity.js"
import MirroredEntity from "../entity/MirroredEntity.js"
import Parsimmon from "parsimmon"
import Parsernostrum from "parsernostrum"
import Serializable from "./Serializable.js"
import Union from "../entity/Union.js"
import Utility from "../Utility.js"
let P = Parsimmon
export default class Grammar {
static separatedBy = (source, separator, min = 1) =>
@@ -17,14 +15,10 @@ export default class Grammar {
)
static Regex = class {
static ByteInteger = /0*(?:25[0-5]|2[0-4]\d|1?\d?\d)(?!\d|\.)/ // A integer between 0 and 255
static HexDigit = /[0-9a-fA-F]/
static InlineOptWhitespace = /[^\S\n]*/
static InlineWhitespace = /[^\S\n]+/
static InsideString = /(?:[^"\\]|\\.)*/
static InsideSingleQuotedString = /(?:[^'\\]|\\.)*/
static Integer = /[\-\+]?\d+(?!\d|\.)/
static MultilineWhitespace = /\s*\n\s*/
static Number = /[-\+]?(?:\d*\.)?\d+(?!\d|\.)/
static RealUnit = /\+?(?:0(?:\.\d+)?|1(?:\.0+)?)(?![\.\d])/ // A number between 0 and 1 included
static Word = Grammar.separatedBy("[a-zA-Z]", "_")
@@ -37,95 +31,55 @@ export default class Grammar {
/* --- Primitive --- */
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(() =>
this.regexMap(new RegExp(`(${Grammar.Regex.Number.source})|(\\+?inf)|(-inf)`), result => {
if (result[2] !== undefined) {
return Number.POSITIVE_INFINITY
} else if (result[3] !== undefined) {
return Number.NEGATIVE_INFINITY
}
return Number(result[1])
})
)
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
static null = Parsernostrum.reg(/\(\s*\)/).map(() => null)
static true = Parsernostrum.reg(/true/i).map(() => true)
static false = Parsernostrum.reg(/false/i).map(() => false)
static boolean = Parsernostrum.regArray(/(true)|false/i).map(v => v[1] ? true : false)
static number = Parsernostrum.regArray(
new RegExp(`(${Parsernostrum.number.getParser().parser.regexp.source})|(\\+?inf)|(-inf)`)
).map(([_0, n, plusInf, minusInf]) => n ? Number(n) : plusInf ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY)
static bigInt = Parsernostrum.reg(new RegExp(Parsernostrum.number.getParser().parser.regexp.source)).map(BigInt)
.map(result =>
result[2] !== undefined
? Number.POSITIVE_INFINITY
: result[3] !== undefined
? Number.NEGATIVE_INFINITY
: Number(result[1])
)
.map((insideString) => Utility.unescapeString(insideString))
)
static naturalNumber = Parsernostrum.lazy(() => Parsernostrum.reg(/\d+/).map(Number))
static string = Parsernostrum.doubleQuotedString.map(insideString => Utility.unescapeString(insideString))
/* --- Fragment --- */
static colorValue = this.byteNumber
static word = P.regex(Grammar.Regex.Word)
static pathQuotes = Grammar.regexMap(
new RegExp(
`'"(` + Grammar.Regex.InsideString.source + `)"'`
+ `|'(` + Grammar.Regex.InsideSingleQuotedString.source + `)'`
+ `|"(` + Grammar.Regex.InsideString.source + `)"`
),
([_0, a, b, c]) => a ?? b ?? c
)
static path = Grammar.regexMap(
new RegExp(
`'"(` + Grammar.Regex.InsideString.source + `)"'`
+ `|'(` + Grammar.Regex.InsideSingleQuotedString.source + `)'`
+ `|"(` + Grammar.Regex.InsideString.source + `)"`
+ `|(` + Grammar.Regex.Path.source + `)`
),
([_0, a, b, c, d]) => a ?? b ?? c ?? d
)
static symbol = P.regex(Grammar.Regex.Symbol)
static symbolQuoted = Grammar.regexMap(
new RegExp('"(' + Grammar.Regex.Symbol.source + ')"'),
/** @type {(_0: String, v: String) => String} */
([_0, v]) => v
)
static attributeName = P.regex(Grammar.Regex.DotSeparatedSymbols)
static attributeNameQuoted = Grammar.regexMap(
new RegExp('"(' + Grammar.Regex.DotSeparatedSymbols.source + ')"'),
([_0, v]) => v
)
static guid = P.regex(new RegExp(`${Grammar.Regex.HexDigit.source}{32}`))
static commaSeparation = P.regex(/\s*,\s*(?!\))/)
static commaOrSpaceSeparation = P.regex(/\s*,\s*(?!\))|\s+/)
static equalSeparation = P.regex(/\s*=\s*/)
static typeReference = P.alt(P.regex(Grammar.Regex.Path), this.symbol)
static hexColorChannel = P.regex(new RegExp(Grammar.Regex.HexDigit.source + "{2}"))
static colorValue = Parsernostrum.numberByte
static word = Parsernostrum.reg(Grammar.Regex.Word)
static pathQuotes = Parsernostrum.regArray(new RegExp(
`'"(` + Grammar.Regex.InsideString.source + `)"'`
+ `|'(` + Grammar.Regex.InsideSingleQuotedString.source + `)'`
+ `|"(` + Grammar.Regex.InsideString.source + `)"`
)).map(([_0, a, b, c]) => a ?? b ?? c)
static path = Parsernostrum.regArray(new RegExp(
`'"(` + Grammar.Regex.InsideString.source + `)"'`
+ `|'(` + Grammar.Regex.InsideSingleQuotedString.source + `)'`
+ `|"(` + Grammar.Regex.InsideString.source + `)"`
+ `|(` + Grammar.Regex.Path.source + `)`
)).map(([_0, a, b, c, d]) => a ?? b ?? c ?? d)
static symbol = Parsernostrum.reg(Grammar.Regex.Symbol)
static symbolQuoted = Parsernostrum.reg(new RegExp('"(' + Grammar.Regex.Symbol.source + ')"'), 1)
static attributeName = Parsernostrum.reg(Grammar.Regex.DotSeparatedSymbols)
static attributeNameQuoted = Parsernostrum.reg(new RegExp('"(' + Grammar.Regex.DotSeparatedSymbols.source + ')"'), 1)
static guid = Parsernostrum.reg(new RegExp(`${Grammar.Regex.HexDigit.source}{32}`))
static commaSeparation = Parsernostrum.reg(/\s*,\s*(?!\))/)
static commaOrSpaceSeparation = Parsernostrum.reg(/\s*,\s*(?!\))|\s+/)
static equalSeparation = Parsernostrum.reg(/\s*=\s*/)
static typeReference = Parsernostrum.alt(Parsernostrum.reg(Grammar.Regex.Path), this.symbol)
static hexColorChannel = Parsernostrum.reg(new RegExp(Grammar.Regex.HexDigit.source + "{2}"))
/* --- Factory --- */
/**
* @template T
* @param {RegExp} re
* @param {(execResult) => T} mapper
*/
static regexMap(re, mapper) {
const anchored = RegExp("^(?:" + re.source + ")", re.flags)
const expected = "" + re
return P((input, i) => {
const match = anchored.exec(input.slice(i))
if (match) {
return P.makeSuccess(i + match[0].length, mapper(match))
}
return P.makeFailure(i, expected)
})
}
/**
* @template {SimpleValueType<SimpleValue>} T
* @template {AttributeTypeDescription} T
* @param {T} type
* @returns {Parsimmon.Parser<ConstructedType<T>>}
*/
static grammarFor(
attribute,
@@ -139,39 +93,39 @@ export default class Grammar {
if (attribute?.inlined) {
return this.grammarFor(undefined, type[0])
}
result = P.seq(
P.regex(/\(\s*/),
result = Parsernostrum.seq(
Parsernostrum.reg(/\(\s*/),
this.grammarFor(undefined, type[0]).sepBy(this.commaSeparation),
P.regex(/\s*(?:,\s*)?\)/),
Parsernostrum.reg(/\s*(?:,\s*)?\)/),
).map(([_0, values, _3]) => values)
} else if (type instanceof Union) {
result = type.values
.map(v => this.grammarFor(undefined, v))
.reduce((acc, cur) => !cur || cur === this.unknownValue || acc === this.unknownValue
? this.unknownValue
: P.alt(acc, cur)
: Parsernostrum.alt(acc, cur)
)
} else if (type instanceof MirroredEntity) {
return this.grammarFor(type.type.attributes[type.key])
.map(() => new MirroredEntity(type.type, type.key, type.getter))
return this.grammarFor(undefined, type.getTargetType())
.map(v => new MirroredEntity(type.type, () => v))
} else if (attribute?.constructor === Object) {
result = this.grammarFor(undefined, type)
} else {
switch (type) {
case BigInt:
result = this.bigInt
break
case Boolean:
result = this.boolean
break
case Number:
result = this.number
break
case BigInt:
result = this.bigInt
break
case String:
result = this.string
break
default:
if (type?.prototype instanceof Serializable) {
if (/** @type {AttributeConstructor<any>} */(type)?.prototype instanceof Serializable) {
return /** @type {typeof Serializable} */(type).grammar
}
}
@@ -181,18 +135,18 @@ export default class Grammar {
if (result == this.unknownValue) {
result = this.string
} else {
result = P.seq(P.string('"'), result, P.string('"'))
result = Parsernostrum.seq(Parsernostrum.str('"'), result, Parsernostrum.str('"'))
}
}
if (attribute.nullable) {
result = P.alt(result, this.null)
result = Parsernostrum.alt(result, this.null)
}
}
return result
}
/**
* @template {SimpleValueType<SimpleValue>} T
* @template {AttributeConstructor<Attribute>} T
* @param {T} entityType
* @param {String[]} key
* @returns {AttributeInformation}
@@ -227,7 +181,7 @@ export default class Grammar {
valueSeparator = this.equalSeparation,
handleObjectSet = (obj, k, v) => { }
) {
return P.seq(
return Parsernostrum.seq(
attributeName,
valueSeparator,
).chain(([attributeName, _1]) => {
@@ -248,20 +202,19 @@ export default class Grammar {
* @template {IEntity} T
* @param {(new (...args: any) => T) & EntityConstructor} entityType
* @param {Boolean | Number} acceptUnknownKeys Number to specify the limit or true, to let it be a reasonable value
* @returns {Parsimmon.Parser<T>}
*/
static createEntityGrammar = (entityType, acceptUnknownKeys = true, entriesSeparator = this.commaSeparation) =>
P.seq(
this.regexMap(
Parsernostrum.seq(
Parsernostrum.reg(
entityType.lookbehind instanceof Union
? new RegExp(`(${entityType.lookbehind.values.reduce((acc, cur) => acc + "|" + cur)})\\s*\\(\\s*`)
: entityType.lookbehind.constructor == String && entityType.lookbehind.length
? new RegExp(`(${entityType.lookbehind})\\s*\\(\\s*`)
: /()\(\s*/,
result => result[1]
1
),
this.createAttributeGrammar(entityType).sepBy1(entriesSeparator),
P.regex(/\s*(?:,\s*)?\)/), // trailing comma
this.createAttributeGrammar(entityType).sepBy(entriesSeparator),
Parsernostrum.reg(/\s*(?:,\s*)?\)/), // trailing comma
)
.map(([lookbehind, attributes, _2]) => {
let values = {}
@@ -281,13 +234,13 @@ export default class Grammar {
.filter(key => entityType.attributes[key].expected)
.find(key => !totalKeys.includes(key) && (missingKey = key))
) {
return P.fail("Missing key " + missingKey)
return Parsernostrum.failure()
}
const unknownKeys = Object.keys(values).filter(key => !(key in entityType.attributes)).length
if (!acceptUnknownKeys && unknownKeys > 0) {
return P.fail("Too many unknown keys")
return Parsernostrum.failure()
}
return P.succeed(new entityType(values))
return Parsernostrum.success().map(() => new entityType(values))
})
/* --- Entity --- */

View File

@@ -32,11 +32,7 @@ export default class ObjectSerializer extends Serializer {
/** @param {String} value */
doRead(value) {
const parseResult = Grammar.grammarFor(undefined, this.entityType).parse(value)
if (!parseResult.status) {
throw new Error("Error when trying to parse the object.")
}
return parseResult.value
return Grammar.grammarFor(undefined, this.entityType).parse(value)
}
/**
@@ -44,11 +40,7 @@ export default class ObjectSerializer extends Serializer {
* @returns {ObjectEntity[]}
*/
readMultiple(value) {
const parseResult = ObjectEntity.getMultipleObjectsGrammar().parse(value)
if (!parseResult.status) {
throw new Error("Error when trying to parse the object.")
}
return parseResult.value
return ObjectEntity.getMultipleObjectsGrammar().parse(value)
}
/**

View File

@@ -1,6 +1,4 @@
import Parsimmon from "parsimmon"
const P = Parsimmon
import Parsernostrum from "parsernostrum"
export default class Serializable {
@@ -8,8 +6,6 @@ export default class Serializable {
/** @protected */
static createGrammar() {
return /** @type {Parsimmon.Parser<Serializable>} */(P.fail(
"Unimplemented createGrammar() method in " + this.name)
)
return /** @type {Parsernostrum<any>} */(Parsernostrum.failure())
}
}

View File

@@ -3,16 +3,16 @@ import IEntity from "../entity/IEntity.js"
import SerializerFactory from "./SerializerFactory.js"
import Utility from "../Utility.js"
/** @template {SimpleValueType<SimpleValue>} T */
/** @template {AttributeConstructor<Attribute>} T */
export default class Serializer {
/** @type {(v: String) => String} */
static same = v => v
/** @type {(entity: SimpleValue, serialized: String) => String} */
/** @type {(entity: Attribute, serialized: String) => String} */
static notWrapped = (entity, serialized) => serialized
/** @type {(entity: SimpleValue, serialized: String) => String} */
/** @type {(entity: Attribute, serialized: String) => String} */
static bracketsWrapped = (entity, serialized) => `(${serialized})`
/** @param {T} entityType */
@@ -43,7 +43,6 @@ export default class Serializer {
/** @param {ConstructedType<T>} value */
write(value, insideString = false) {
// @ts-expect-error
return this.doWrite(value, insideString)
}
@@ -53,7 +52,7 @@ export default class Serializer {
*/
doRead(value) {
let grammar = Grammar.grammarFor(undefined, this.entityType)
const parseResult = grammar.parse(value)
const parseResult = grammar.run(value)
if (!parseResult.status) {
throw new Error(`Error when trying to parse the entity ${this.entityType.prototype.constructor.name}.`)
}
@@ -61,7 +60,7 @@ export default class Serializer {
}
/**
* @param {ConstructedType<T> & IEntity} entity
* @param {ConstructedType<T>} entity
* @param {Boolean} insideString
* @returns {String}
*/

View File

@@ -3,7 +3,7 @@ export default class SerializerFactory {
static #serializers = new Map()
/**
* @template {SimpleValueType<SimpleValue>} T
* @template {AttributeConstructor<Attribute>} T
* @param {T} type
* @param {Serializer<T>} object
*/
@@ -12,9 +12,9 @@ export default class SerializerFactory {
}
/**
* @template {SimpleValueType<any>} T
* @template {AttributeConstructor<Attribute>} T
* @param {T} type
* @returns {Serializer<ConstructedType<T>>}
* @returns {Serializer<T>}
*/
static getSerializer(type) {
return SerializerFactory.#serializers.get(type)

View File

@@ -2,7 +2,7 @@ import Serializer from "./Serializer.js"
import Utility from "../Utility.js"
/**
* @template {SimpleValueType<SimpleValue>} T
* @template {AttributeConstructor<Attribute>} T
* @extends {Serializer<T>}
*/
export default class ToStringSerializer extends Serializer {

View File

@@ -19,7 +19,7 @@ import MirroredEntity from "../entity/MirroredEntity.js"
import ObjectEntity from "../entity/ObjectEntity.js"
import ObjectReferenceEntity from "../entity/ObjectReferenceEntity.js"
import ObjectSerializer from "./ObjectSerializer.js"
import Parsimmon from "parsimmon"
import Parsernostrum from "parsernostrum"
import PathSymbolEntity from "../entity/PathSymbolEntity.js"
import PinEntity from "../entity/PinEntity.js"
import PinReferenceEntity from "../entity/PinReferenceEntity.js"
@@ -41,7 +41,7 @@ import Vector2DEntity from "../entity/Vector2DEntity.js"
import VectorEntity from "../entity/VectorEntity.js"
Grammar.unknownValue =
Parsimmon.alt(
Parsernostrum.alt(
// Remember to keep the order, otherwise parsing might fail
Grammar.boolean,
GuidEntity.createGrammar(),