mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-11 14:24:42 +08:00
38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
import P from "parsernostrum"
|
|
import IEntity from "./IEntity.js"
|
|
|
|
export default class AlternativesEntity extends IEntity {
|
|
|
|
/** @type {(typeof IEntity)[]} */
|
|
static alternatives = []
|
|
|
|
static className() {
|
|
let result = super.className()
|
|
if (this.alternatives.length) {
|
|
result += " (accepting: " + this.alternatives.map(v => v.className()).join(", ") + ")"
|
|
}
|
|
return result
|
|
}
|
|
|
|
static createGrammar() {
|
|
const grammars = this.alternatives.map(entity => entity.grammar)
|
|
if (grammars.includes(this.unknownEntityGrammar)) {
|
|
return this.unknownEntityGrammar
|
|
}
|
|
return P.alt(...grammars)
|
|
}
|
|
|
|
/**
|
|
* @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
|
|
}
|
|
}
|