mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-03-15 04:50:53 +08:00
Still WIP
This commit is contained in:
29
js/entity/AlternativesEntity.js
Normal file
29
js/entity/AlternativesEntity.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import P from "parsernostrum"
|
||||
import Grammar from "../serialization/Grammar.js"
|
||||
import IEntity from "./IEntity.js"
|
||||
|
||||
export default class AlternativesEntity extends IEntity {
|
||||
|
||||
/** @type {(typeof IEntity)[]} */
|
||||
static alternatives = []
|
||||
|
||||
static createGrammar() {
|
||||
return this.alternatives
|
||||
.map(entity => entity.grammar)
|
||||
.reduce((acc, cur) => !cur || cur === Grammar.unknownValue || acc === Grammar.unknownValue
|
||||
? Grammar.unknownValue
|
||||
: P.alt(acc, cur)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {(typeof IEntity)[]} Types
|
||||
* @param {Types} types
|
||||
*/
|
||||
static accepting(...types) {
|
||||
const result = /** @type {typeof AlternativesEntity & { alternatives: Types }} */(this.asUniqueClass())
|
||||
result.alternatives = types
|
||||
result.grammar = result.createGrammar()
|
||||
return result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user