Something went wrong. Try again.
(WIP) Modern Minecraft server-side HUD framework.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433import { TokenType, type Token, type ParserOptions, type ParseResult, type RootNode, type TagNode, type TextNode, type ASTNode, type ParseError} from './types';import { TagRegistry } from './tag-registry';
const DEFAULT_OPTIONS: ParserOptions = { mode: 'lenient', maxNestingDepth: 50};
export class MiniMessageParser { private registry: TagRegistry; private options: ParserOptions;
constructor(options: Partial<ParserOptions> = {}) { this.registry = new TagRegistry(); this.options = { ...DEFAULT_OPTIONS, ...options }; }
parse(input: string): ParseResult { const errors: ParseError[] = [];
try { const tokens = this.tokenize(input); const ast = this.buildAST(tokens, errors);
return { ast, errors, success: errors.every((e) => e.severity !== 'error') }; } catch (error) { errors.push({ message: error instanceof Error ? error.message : 'Unknown parsing error', position: 0, severity: 'error' });
return { ast: { type: 'root', children: [], start: 0, end: input.length }, errors, success: false }; } }
private tokenize(input: string): Token[] { const tokens: Token[] = []; let pos = 0; let textBuffer = ''; let textStart = 0;
const flushText = () => { if (textBuffer.length > 0) { tokens.push({ type: TokenType.TEXT, value: textBuffer, position: textStart }); textBuffer = ''; } };
while (pos < input.length) { const char = input[pos];
if (char === '\\' && pos + 1 < input.length) { const nextChar = input[pos + 1]; if (nextChar === '<' || nextChar === '>' || nextChar === '\\') { if (textBuffer.length === 0) textStart = pos; textBuffer += nextChar; pos += 2; continue; } }
if (char === '<') { flushText();
const tagResult = this.parseTag(input, pos); if (tagResult) { tokens.push(tagResult.token); pos = tagResult.newPos; continue; } else { if (textBuffer.length === 0) textStart = pos; textBuffer += char; pos++; } } else { if (textBuffer.length === 0) textStart = pos; textBuffer += char; pos++; } }
flushText();
tokens.push({ type: TokenType.EOF, value: '', position: pos });
return tokens; }
private parseTag( input: string, startPos: number ): { token: Token; newPos: number } | null { let pos = startPos + 1;
if (pos < input.length && input[pos] === '/') { pos++; const closeResult = this.parseTagName(input, pos); if (!closeResult) return null;
pos = closeResult.newPos; while (pos < input.length && (input[pos] === ' ' || input[pos] === '\t')) pos++;
if (pos >= input.length || input[pos] !== '>') return null;
return { token: { type: TokenType.TAG_CLOSE, value: closeResult.name, position: startPos }, newPos: pos + 1 }; }
const isNegation = pos < input.length && input[pos] === '!'; if (isNegation) pos++;
const nameResult = this.parseTagName(input, pos); if (!nameResult) return null;
let tagName = nameResult.name; pos = nameResult.newPos;
while (pos < input.length && (input[pos] === ' ' || input[pos] === '\t')) pos++;
if (pos + 1 < input.length && input[pos] === '/' && input[pos + 1] === '>') { return { token: { type: TokenType.TAG_SELF_CLOSE, value: tagName, position: startPos, args: [] }, newPos: pos + 2 }; }
let args: string[] = []; if (pos < input.length && input[pos] === ':') { pos++; const argsResult = this.parseArguments(input, pos); if (argsResult) { args = argsResult.args; pos = argsResult.newPos; } }
while (pos < input.length && (input[pos] === ' ' || input[pos] === '\t')) pos++; if (pos >= input.length || input[pos] !== '>') return null;
if (isNegation && args.length === 0) { args = ['false']; }
return { token: { type: TokenType.TAG_OPEN, value: tagName, position: startPos, args }, newPos: pos + 1 }; }
private parseTagName(input: string, startPos: number): { name: string; newPos: number } | null { let pos = startPos; let name = '';
while ( pos < input.length && /[a-zA-Z0-9_#]/.test(input[pos]) ) { name += input[pos]; pos++; }
if (name.length === 0) return null;
return { name, newPos: pos }; }
private parseArguments( input: string, startPos: number ): { args: string[]; newPos: number } | null { const args: string[] = []; let pos = startPos; let currentArg = ''; let inQuotes = false; let quoteChar = '';
while (pos < input.length) { const char = input[pos];
if (!inQuotes && char === '>') { if (currentArg.length > 0) { args.push(currentArg); } break; }
if ((char === '"' || char === "'") && (pos === startPos || input[pos - 1] !== '\\')) { if (!inQuotes) { inQuotes = true; quoteChar = char; pos++; continue; } else if (char === quoteChar) { inQuotes = false; quoteChar = ''; pos++; continue; } }
if (!inQuotes && char === ':') { args.push(currentArg); currentArg = ''; pos++; continue; }
if (inQuotes && char === '\\' && pos + 1 < input.length) { const nextChar = input[pos + 1]; if (nextChar === quoteChar || nextChar === '\\') { currentArg += nextChar; pos += 2; continue; } }
currentArg += char; pos++; }
return { args, newPos: pos }; }
private buildAST(tokens: Token[], errors: ParseError[]): RootNode { const root: RootNode = { type: 'root', children: [], start: 0, end: tokens[tokens.length - 1]?.position || 0 };
const stack: TagNode[] = []; let currentParent: RootNode | TagNode = root; let depth = 0;
for (let i = 0; i < tokens.length; i++) { const token = tokens[i];
if (token.type === TokenType.TEXT) { const textNode: TextNode = { type: 'text', content: token.value, start: token.position, end: token.position + token.value.length }; currentParent.children.push(textNode); } else if (token.type === TokenType.TAG_OPEN) { depth++; if (depth > this.options.maxNestingDepth!) { errors.push({ message: `Maximum nesting depth exceeded (${this.options.maxNestingDepth})`, position: token.position, severity: 'error' }); continue; }
const tagDef = this.registry.get(token.value); if (!tagDef) { if (this.options.mode === 'strict') { errors.push({ message: `Unknown tag: ${token.value}`, position: token.position, severity: 'error' }); continue; } else { const textNode: TextNode = { type: 'text', content: `<${token.value}${token.args?.length ? ':' + token.args.join(':') : ''}>`, start: token.position, end: token.position }; currentParent.children.push(textNode); depth--; continue; } }
const argsString = token.args?.join(':') || ''; const tagArgs = tagDef.parseArgs(argsString);
const tagNode: TagNode = { type: 'tag', tagName: tagDef.name, args: tagArgs, children: [], selfClosing: false, start: token.position, end: token.position };
currentParent.children.push(tagNode);
if (tagDef.canHaveChildren) { stack.push(tagNode); currentParent = tagNode; } else { depth--; } } else if (token.type === TokenType.TAG_CLOSE) { depth--;
const matchIndex = this.findMatchingTag(stack, token.value);
if (matchIndex === -1) { if (this.options.mode === 'strict') { errors.push({ message: `Unmatched closing tag: </${token.value}>`, position: token.position, severity: 'error' }); } continue; }
if (matchIndex < stack.length - 1 && this.options.mode === 'strict') { errors.push({ message: `Tag closed out of order: </${token.value}>`, position: token.position, severity: 'error' }); }
const closedTags = stack.splice(matchIndex); for (const tag of closedTags) { tag.end = token.position; }
currentParent = stack.length > 0 ? stack[stack.length - 1] : root; } else if (token.type === TokenType.TAG_SELF_CLOSE) { const tagDef = this.registry.get(token.value); if (!tagDef) { if (this.options.mode === 'strict') { errors.push({ message: `Unknown tag: ${token.value}`, position: token.position, severity: 'error' }); } continue; }
const argsString = token.args?.join(':') || ''; const tagArgs = tagDef.parseArgs(argsString);
const tagNode: TagNode = { type: 'tag', tagName: tagDef.name, args: tagArgs, children: [], selfClosing: true, start: token.position, end: token.position };
currentParent.children.push(tagNode); } }
if (stack.length > 0) { if (this.options.mode === 'strict') { for (const tag of stack) { errors.push({ message: `Unclosed tag: <${tag.tagName}>`, position: tag.start, severity: 'error' }); } } else { const lastPos = tokens[tokens.length - 1].position; for (const tag of stack) { tag.end = lastPos; } } }
return root; }
private findMatchingTag(stack: TagNode[], tagName: string): number { for (let i = stack.length - 1; i >= 0; i--) { if (stack[i].tagName.toLowerCase() === tagName.toLowerCase()) { return i; } } return -1; }}