Skip to content
Snippets Groups Projects
parser.ts 17.4 KiB
Newer Older
import * as M from '..';
import * as P from './core';
import { mergeText } from './util';

// NOTE:
// tsdのテストでファイルを追加しているにも関わらず「@twemoji/parser/dist/lib/regex」の型定義ファイルがないとエラーが出るため、
// このエラーを無視する。
/* eslint @typescript-eslint/ban-ts-comment: 1 */
// @ts-ignore
import twemojiRegex from '@twemoji/parser/dist/lib/regex';
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

type ArgPair = { k: string, v: string | true };
type Args = Record<string, string | true>;

const space = P.regexp(/[\u0020\u3000\t]/);
const alphaAndNum = P.regexp(/[a-z0-9]/i);
const newLine = P.alt([P.crlf, P.cr, P.lf]);

function seqOrText(parsers: P.Parser<any>[]): P.Parser<any[] | string> {
	return new P.Parser<any[] | string>((input, index, state) => {
		const accum: any[] = [];
		let latestIndex = index;
		for (let i = 0 ; i < parsers.length; i++) {
			const result = parsers[i].handler(input, latestIndex, state);
			if (!result.success) {
				if (latestIndex === index) {
					return P.failure();
				} else {
					return P.success(latestIndex, input.slice(index, latestIndex));
				}
			}
			accum.push(result.value);
			latestIndex = result.index;
		}
		return P.success(latestIndex, accum);
	});
}

const notLinkLabel = new P.Parser((_input, index, state) => {
	return (!state.linkLabel)
		? P.success(index, null)
		: P.failure();
});

const nestable = new P.Parser((_input, index, state) => {
	return (state.depth < state.nestLimit)
		? P.success(index, null)
		: P.failure();
});

function nest<T>(parser: P.Parser<T>, fallback?: P.Parser<string>): P.Parser<T | string> {
	// nesting limited? -> No: specified parser, Yes: fallback parser (default = P.char)
	const inner = P.alt([
		P.seq([nestable, parser], 1),
		(fallback != null) ? fallback : P.char,
	]);
	return new P.Parser<T | string>((input, index, state) => {
		state.depth++;
		const result = inner.handler(input, index, state);
		state.depth--;
		return result;
	});
}

export const language = P.createLanguage({
	fullParser: r => {
		return r.full.many(0);
	},

	simpleParser: r => {
		return r.simple.many(0);
	},

	full: r => {
		return P.alt([
			// Regexp
			r.unicodeEmoji,
			// "<center>" block
			r.centerTag,
			// "<small>"
			r.smallTag,
			// "<plain>"
			r.plainTag,
			// "<b>"
			r.boldTag,
			// "<i>"
			r.italicTag,
			// "<s>"
			r.strikeTag,
			// "<http"
			r.urlAlt,
			// "***"
			r.big,
			// "**"
			r.boldAsta,
			// "*"
			r.italicAsta,
			// "__"
			r.boldUnder,
			// "_"
			r.italicUnder,
			// "```" block
			r.codeBlock,
			// "`"
			r.inlineCode,
			// ">" block
			r.quote,
			// "\\[" block
			r.mathBlock,
			// "\\("
			r.mathInline,
			// "~~"
			r.strikeWave,
			// "$[""
			r.fn,
			// "@"
			r.mention,
			// "#"
			r.hashtag,
			// ":"
			r.emojiCode,
			// "?[" or "["
			r.link,
			// http
			r.url,
			// block
			r.search,
			r.text,
		]);
	},

	simple: r => {
		return P.alt([
			r.unicodeEmoji, // Regexp
			r.emojiCode, // ":"
			r.text,
		]);
	},

	inline: r => {
		return P.alt([
			// Regexp
			r.unicodeEmoji,
			// "<small>"
			r.smallTag,
			// "<plain>"
			r.plainTag,
			// "<b>"
			r.boldTag,
			// "<i>"
			r.italicTag,
			// "<s>"
			r.strikeTag,
			// <http
			r.urlAlt,
			// "***"
			r.big,
			// "**"
			r.boldAsta,
			// "*"
			r.italicAsta,
			// "__"
			r.boldUnder,
			// "_"
			r.italicUnder,
			// "`"
			r.inlineCode,
			// "\\("
			r.mathInline,
			// "~~"
			r.strikeWave,
			// "$[""
			r.fn,
			// "@"
			r.mention,
			// "#"
			r.hashtag,
			// ":"
			r.emojiCode,
			// "?[" or "["
			r.link,
			// http
			r.url,
			r.text,
		]);
	},

	quote: r => {
		const lines: P.Parser<string[]> = P.seq([
			P.str('>'),
			space.option(),
			P.seq([P.notMatch(newLine), P.char], 1).many(0).text(),
		], 2).sep(newLine, 1);
		const parser = P.seq([
			newLine.option(),
			newLine.option(),
			P.lineBegin,
			lines,
			newLine.option(),
			newLine.option(),
		], 3);
		return new P.Parser((input, index, state) => {
			let result;
			// parse quote
			result = parser.handler(input, index, state);
			if (!result.success) {
				return result;
			}
			const contents = result.value;
			const quoteIndex = result.index;
			// disallow empty content if single line
			if (contents.length === 1 && contents[0].length === 0) {
				return P.failure();
			}
			// parse inner content
			const contentParser = nest(r.fullParser).many(0);
			result = contentParser.handler(contents.join('\n'), 0, state);
			if (!result.success) {
				return result;
			}
			return P.success(quoteIndex, M.QUOTE(mergeText(result.value)));
		});
	},

	codeBlock: r => {
		const mark = P.str('```');
		return P.seq([
			newLine.option(),
			P.lineBegin,
			mark,
			P.seq([P.notMatch(newLine), P.char], 1).many(0),
			newLine,
			P.seq([P.notMatch(P.seq([newLine, mark, P.lineEnd])), P.char], 1).many(1),
			newLine,
			mark,
			P.lineEnd,
			newLine.option(),
		]).map(result => {
			const lang = (result[3] as string[]).join('').trim();
			const code = (result[5] as string[]).join('');
			return M.CODE_BLOCK(code, (lang.length > 0 ? lang : null));
		});
	},

	mathBlock: r => {
		const open = P.str('\\[');
		const close = P.str('\\]');
		return P.seq([
			newLine.option(),
			P.lineBegin,
			open,
			newLine.option(),
			P.seq([P.notMatch(P.seq([newLine.option(), close])), P.char], 1).many(1),
			newLine.option(),
			close,
			P.lineEnd,
			newLine.option(),
		]).map(result => {
			const formula = (result[4] as string[]).join('');
			return M.MATH_BLOCK(formula);
		});
	},

	centerTag: r => {
		const open = P.str('<center>');
		const close = P.str('</center>');
		return P.seq([
			newLine.option(),
			P.lineBegin,
			open,
			newLine.option(),
			P.seq([P.notMatch(P.seq([newLine.option(), close])), nest(r.inline)], 1).many(1),
			newLine.option(),
			close,
			P.lineEnd,
			newLine.option(),
		]).map(result => {
			return M.CENTER(mergeText(result[4]));
		});
	},

	big: r => {
		const mark = P.str('***');
		return seqOrText([
			mark,
			P.seq([P.notMatch(mark), nest(r.inline)], 1).many(1),
			mark,
		]).map(result => {
			if (typeof result === 'string') return result;
			return M.FN('tada', {}, mergeText(result[1]));
		});
	},

	boldAsta: r => {
		const mark = P.str('**');
		return seqOrText([
			mark,
			P.seq([P.notMatch(mark), nest(r.inline)], 1).many(1),
			mark,
		]).map(result => {
			if (typeof result === 'string') return result;
			return M.BOLD(mergeText(result[1] as (M.MfmInline | string)[]));
		});
	},

	boldTag: r => {
		const open = P.str('<b>');
		const close = P.str('</b>');
		return seqOrText([
			open,
			P.seq([P.notMatch(close), nest(r.inline)], 1).many(1),
			close,
		]).map(result => {
			if (typeof result === 'string') return result;
			return M.BOLD(mergeText(result[1] as (M.MfmInline | string)[]));
		});
	},

	boldUnder: r => {
		const mark = P.str('__');
		return P.seq([
			mark,
			P.alt([alphaAndNum, space]).many(1),
			mark,
		]).map(result => M.BOLD(mergeText(result[1] as string[])));
	},

	smallTag: r => {
		const open = P.str('<small>');
		const close = P.str('</small>');
		return seqOrText([
			open,
			P.seq([P.notMatch(close), nest(r.inline)], 1).many(1),
			close,
		]).map(result => {
			if (typeof result === 'string') return result;
			return M.SMALL(mergeText(result[1] as (M.MfmInline | string)[]));
		});
	},

	italicTag: r => {
		const open = P.str('<i>');
		const close = P.str('</i>');
		return seqOrText([
			open,
			P.seq([P.notMatch(close), nest(r.inline)], 1).many(1),
			close,
		]).map(result => {
			if (typeof result === 'string') return result;
			return M.ITALIC(mergeText(result[1] as (M.MfmInline | string)[]));
		});
	},

	italicAsta: r => {
		const mark = P.str('*');
		const parser = P.seq([
			mark,
			P.alt([alphaAndNum, space]).many(1),
			mark,
		]);
		return new P.Parser((input, index, state) => {
			const result = parser.handler(input, index, state);
			if (!result.success) {
				return P.failure();
			}
			// check before
			const beforeStr = input.slice(0, index);
			if (/[a-z0-9]$/i.test(beforeStr)) {
				return P.failure();
			}
			return P.success(result.index, M.ITALIC(mergeText(result.value[1] as string[])));
		});
	},

	italicUnder: r => {
		const mark = P.str('_');
		const parser = P.seq([
			mark,
			P.alt([alphaAndNum, space]).many(1),
			mark,
		]);
		return new P.Parser((input, index, state) => {
			const result = parser.handler(input, index, state);
			if (!result.success) {
				return P.failure();
			}
			// check before
			const beforeStr = input.slice(0, index);
			if (/[a-z0-9]$/i.test(beforeStr)) {
				return P.failure();
			}
			return P.success(result.index, M.ITALIC(mergeText(result.value[1] as string[])));
		});
	},

	strikeTag: r => {
		const open = P.str('<s>');
		const close = P.str('</s>');
		return seqOrText([
			open,
			P.seq([P.notMatch(close), nest(r.inline)], 1).many(1),
			close,
		]).map(result => {
			if (typeof result === 'string') return result;
			return M.STRIKE(mergeText(result[1] as (M.MfmInline | string)[]));
		});
	},

	strikeWave: r => {
		const mark = P.str('~~');
		return seqOrText([
			mark,
			P.seq([P.notMatch(P.alt([mark, newLine])), nest(r.inline)], 1).many(1),
			mark,
		]).map(result => {
			if (typeof result === 'string') return result;
			return M.STRIKE(mergeText(result[1] as (M.MfmInline | string)[]));
		});
	},

	unicodeEmoji: r => {
		const emoji = RegExp(twemojiRegex.source);
		return P.regexp(emoji).map(content => M.UNI_EMOJI(content));
	},

	plainTag: r => {
		const open = P.str('<plain>');
		const close = P.str('</plain>');
		return P.seq([
			open,
			newLine.option(),
			P.seq([
				P.notMatch(P.seq([newLine.option(), close])),
				P.char,
			], 1).many(1).text(),
			newLine.option(),
			close,
		], 2).map(result => M.PLAIN(result));
	},

	fn: r => {
		const fnName = new P.Parser((input, index, state) => {
			const result = P.regexp(/[a-z0-9_]+/i).handler(input, index, state);
			if (!result.success) {
				return result;
			}
			return P.success(result.index, result.value);
		});
		const arg: P.Parser<ArgPair> = P.seq([
			P.regexp(/[a-z0-9_]+/i),
			P.seq([
				P.str('='),
syuilo's avatar
syuilo committed
				P.regexp(/[a-z0-9_.-]+/i),
			], 1).option(),
		]).map(result => {
			return {
				k: result[0],
				v: (result[1] != null) ? result[1] : true,
			};
		});
		const args = P.seq([
			P.str('.'),
			arg.sep(P.str(','), 1),
		], 1).map(pairs => {
			const result: Args = { };
			for (const pair of pairs) {
				result[pair.k] = pair.v;
			}
			return result;
		});
		const fnClose = P.str(']');
		return seqOrText([
			P.str('$['),
			fnName,
			args.option(),
			P.str(' '),
			P.seq([P.notMatch(fnClose), nest(r.inline)], 1).many(1),
			fnClose,
		]).map(result => {
			if (typeof result === 'string') return result;
			const name = result[1];
			const args = result[2] || {};
			const content = result[4];
			return M.FN(name, args, mergeText(content));
		});
	},

	inlineCode: r => {
		const mark = P.str('`');
		return P.seq([
			mark,
			P.seq([
				P.notMatch(P.alt([mark, P.str('´'), newLine])),
				P.char,
			], 1).many(1),
			mark,
		]).map(result => M.INLINE_CODE(result[1].join('')));
	},

	mathInline: r => {
		const open = P.str('\\(');
		const close = P.str('\\)');
		return P.seq([
			open,
			P.seq([
				P.notMatch(P.alt([close, newLine])),
				P.char,
			], 1).many(1),
			close,
		]).map(result => M.MATH_INLINE(result[1].join('')));
	},

	mention: r => {
		const parser = P.seq([
			notLinkLabel,
			P.str('@'),
			P.seq([
				P.str('@'),
				P.regexp(/[a-z0-9_.-]+/i),
			], 1).option(),
		]);
		return new P.Parser<M.MfmMention | string>((input, index, state) => {
			let result;
			result = parser.handler(input, index, state);
			if (!result.success) {
				return P.failure();
			}
			// check before (not mention)
			const beforeStr = input.slice(0, index);
			if (/[a-z0-9]$/i.test(beforeStr)) {
				return P.failure();
			}
			let invalidMention = false;
			const resultIndex = result.index;
			const username: string = result.value[2];
			const hostname: string | null = result.value[3];
			// remove [.-] of tail of hostname
			let modifiedHost = hostname;
			if (hostname != null) {
				result = /[.-]+$/.exec(hostname);
				if (result != null) {
					modifiedHost = hostname.slice(0, (-1 * result[0].length));
					if (modifiedHost.length === 0) {
						// disallow invalid char only hostname
						invalidMention = true;
						modifiedHost = null;
					}
				}
			}
			// remove "-" of tail of username
			let modifiedName = username;
			result = /-+$/.exec(username);
			if (result != null) {
				if (modifiedHost == null) {
					modifiedName = username.slice(0, (-1 * result[0].length));
				} else {
					// cannnot to remove tail of username if exist hostname
					invalidMention = true;
				}
			}
			// disallow "-" of head of username
			if (modifiedName.length === 0 || modifiedName[0] === '-') {
				invalidMention = true;
			}
			// disallow [.-] of head of hostname
			if (modifiedHost != null && /^[.-]/.test(modifiedHost)) {
				invalidMention = true;
			}
			// generate a text if mention is invalid
			if (invalidMention) {
				return P.success(resultIndex, input.slice(index, resultIndex));
			}
			const acct = modifiedHost != null ? `@${modifiedName}@${modifiedHost}` : `@${modifiedName}`;
			return P.success(index + acct.length, M.MENTION(modifiedName, modifiedHost, acct));
		});
	},

	hashtag: r => {
		const mark = P.str('#');
		const hashTagChar = P.seq([
			P.notMatch(P.alt([P.regexp(/[ \u3000\t.,!?'"#:/[\]【】()「」()<>]/), space, newLine])),
			P.char,
		], 1);
		const innerItem: P.Parser<any> = P.lazy(() => P.alt([
			P.seq([
				P.str('('), nest(innerItem, hashTagChar).many(0), P.str(')'),
			]),
			P.seq([
				P.str('['), nest(innerItem, hashTagChar).many(0), P.str(']'),
			]),
			P.seq([
				P.str(''), nest(innerItem, hashTagChar).many(0), P.str(''),
			]),
			P.seq([
				P.str(''), nest(innerItem, hashTagChar).many(0), P.str(''),
			]),
			hashTagChar,
		]));
		const parser = P.seq([
			notLinkLabel,
			mark,
			innerItem.many(1).text(),
		], 2);
		return new P.Parser((input, index, state) => {
			const result = parser.handler(input, index, state);
			if (!result.success) {
				return P.failure();
			}
			// check before
			const beforeStr = input.slice(0, index);
			if (/[a-z0-9]$/i.test(beforeStr)) {
				return P.failure();
			}
			const resultIndex = result.index;
			const resultValue = result.value;
			// disallow number only
			if (/^[0-9]+$/.test(resultValue)) {
				return P.failure();
			}
			return P.success(resultIndex, M.HASHTAG(resultValue));
		});
	},

	emojiCode: r => {
syuilo's avatar
syuilo committed
		const side = P.notMatch(P.regexp(/[a-z0-9]/i));
		const mark = P.str(':');
		return P.seq([
syuilo's avatar
syuilo committed
			P.alt([P.lineBegin, side]),
			mark,
			P.regexp(/[a-z0-9_+-]+/i),
			mark,
syuilo's avatar
syuilo committed
			P.alt([P.lineEnd, side]),
		], 2).map(name => M.EMOJI_CODE(name as string));
	},

	link: r => {
		const labelInline = new P.Parser((input, index, state) => {
			state.linkLabel = true;
			const result = r.inline.handler(input, index, state);
			state.linkLabel = false;
			return result;
		});
		const closeLabel = P.str(']');
		return P.seq([
			notLinkLabel,
			P.alt([P.str('?['), P.str('[')]),
			P.seq([
				P.notMatch(P.alt([closeLabel, newLine])),
				nest(labelInline),
			], 1).many(1),
			closeLabel,
			P.str('('),
			P.alt([r.urlAlt, r.url]),
			P.str(')'),
		]).map(result => {
			const silent = (result[1] === '?[');
			const label = result[2];
			const url: M.MfmUrl = result[5];
			return M.LINK(silent, url.props.url, mergeText(label));
		});
	},

	url: r => {
		const urlChar = P.regexp(/[.,a-z0-9_/:%#@$&?!~=+-]/i);
		const innerItem: P.Parser<any> = P.lazy(() => P.alt([
			P.seq([
				P.str('('), nest(innerItem, urlChar).many(0), P.str(')'),
			]),
			P.seq([
				P.str('['), nest(innerItem, urlChar).many(0), P.str(']'),
			]),
			urlChar,
		]));
		const parser = P.seq([
			notLinkLabel,
			P.regexp(/https?:\/\//),
			innerItem.many(1).text(),
		]);
		return new P.Parser<M.MfmUrl | string>((input, index, state) => {
			let result;
			result = parser.handler(input, index, state);
			if (!result.success) {
				return P.failure();
			}
			const resultIndex = result.index;
			let modifiedIndex = resultIndex;
			const schema: string = result.value[1];
			let content: string = result.value[2];
			// remove the ".," at the right end
			result = /[.,]+$/.exec(content);
			if (result != null) {
				modifiedIndex -= result[0].length;
				content = content.slice(0, (-1 * result[0].length));
				if (content.length === 0) {
					return P.success(resultIndex, input.slice(index, resultIndex));
				}
			}
			return P.success(modifiedIndex, M.N_URL(schema + content, false));
		});
	},

	urlAlt: r => {
		const open = P.str('<');
		const close = P.str('>');
		const parser = P.seq([
			notLinkLabel,
			open,
			P.regexp(/https?:\/\//),
			P.seq([P.notMatch(P.alt([close, space])), P.char], 1).many(1),
			close,
		]).text();
		return new P.Parser((input, index, state) => {
			const result = parser.handler(input, index, state);
			if (!result.success) {
				return P.failure();
			}
			const text = result.value.slice(1, (result.value.length - 1));
			return P.success(result.index, M.N_URL(text, true));
		});
	},

	search: r => {
		const button = P.alt([
			P.regexp(/\[(検索|search)\]/i),
		]);
		return P.seq([
			newLine.option(),
			P.lineBegin,
			P.seq([
				P.notMatch(P.alt([
					newLine,
					P.seq([space, button, P.lineEnd]),
				])),
				P.char,
			], 1).many(1),
			space,
			button,
			P.lineEnd,
			newLine.option(),
		]).map(result => {
			const query = result[2].join('');
			return M.SEARCH(query, `${query}${result[3]}${result[4]}`);
		});
	},

	text: r => P.char,
});