@zsnout/ithkuil
Version:
A set of tools which can generate and parse romanized Ithkuil text and which can generate Ithkuil script from text and JSON data.
78 lines (77 loc) • 2.5 kB
JavaScript
import { ALL_REFERENTS, REFERENT_TO_ITHKUIL_MAP, } from "../../generate/referential/referent/referent.js";
const ALL_REFERENTS_REVERSED = /* @__PURE__ */ ALL_REFERENTS.slice().reverse();
const PERSPECTIVES = [
["ļ", "G"],
["tļ", "G"],
["ç", "N"],
["x", "N"],
["w", "A"],
["y", "A"],
];
/**
* Parses a referent list (e.g. `"sml"`).
*
* @param list The referent list to be parsed.
* @returns The parsed `ReferentList`.
*/
export function parseReferentList(list) {
const output = [];
outer: while (list.length) {
for (const referent of ALL_REFERENTS_REVERSED) {
const value = REFERENT_TO_ITHKUIL_MAP.false[referent];
if (list.startsWith(value)) {
output.push(referent);
list = list.slice(value.length);
continue outer;
}
}
throw new Error("Invalid referent: " + list + ".");
}
if (output.length == 0) {
throw new Error("Invalid referent: " + list + ".");
}
return output;
}
/**
* Parses a referent list along with an optional perspective (e.g. `"smlx"`).
*
* @param list The referent list and perspective to be parsed.
* @param isReferentialAffix Whether the referent list is from a referential
* affix.
* @returns An array containing two items: an array of parsed referents, and an
* optional perspective.
*/
export function parseReferentListAndPerspective(list, isReferentialAffix) {
let perspective;
for (const [value, persp] of PERSPECTIVES) {
if (isReferentialAffix && persp == "A") {
continue;
}
if (list.startsWith(value)) {
list = list.slice(value.length);
perspective = persp;
break;
}
if (list.endsWith(value)) {
list = list.slice(0, -value.length);
perspective = persp;
break;
}
}
const output = [];
outer: while (list.length) {
for (const referent of ALL_REFERENTS_REVERSED) {
const value = REFERENT_TO_ITHKUIL_MAP[`${isReferentialAffix}`][referent];
if (list.startsWith(value)) {
output.push(referent);
list = list.slice(value.length);
continue outer;
}
}
throw new Error("Invalid referent: " + list + ".");
}
if (output.length == 0) {
throw new Error("Invalid referent: " + list + ".");
}
return [output, perspective];
}