Skip to content
Snippets Groups Projects
Commit 150cbd26 authored by marihachi's avatar marihachi
Browse files

refactor(parser): use node generation macros

parent cbb00a5a
No related branches found
No related tags found
No related merge requests found
{ {
const { const {
createNode, // block
QUOTE,
SEARCH,
CODE_BLOCK,
MATH_BLOCK,
CENTER,
// inline
UNI_EMOJI,
EMOJI_CODE,
BOLD,
SMALL,
ITALIC,
STRIKE,
INLINE_CODE,
MATH_INLINE,
MENTION,
HASHTAG,
N_URL,
LINK,
FN,
TEXT
} = require('./node');
const {
mergeText, mergeText,
setConsumeCount, setConsumeCount,
consumeDynamically consumeDynamically
...@@ -64,7 +88,7 @@ quote ...@@ -64,7 +88,7 @@ quote
= lines:quoteLine+ = lines:quoteLine+
{ {
const children = applyParser(lines.join('\n'), 'fullParser'); const children = applyParser(lines.join('\n'), 'fullParser');
return createNode('quote', null, children); return QUOTE(children);
} }
quoteLine quoteLine
...@@ -75,10 +99,7 @@ quoteLine ...@@ -75,10 +99,7 @@ quoteLine
search search
= BEGIN q:searchQuery sp:_ key:searchKey END = BEGIN q:searchQuery sp:_ key:searchKey END
{ {
return createNode('search', { return SEARCH(q, `${ q }${ sp }${ key }`);
query: q,
content: `${ q }${ sp }${ key }`
});
} }
searchQuery searchQuery
...@@ -95,10 +116,7 @@ codeBlock ...@@ -95,10 +116,7 @@ codeBlock
= BEGIN "```" lang:$(CHAR*) LF code:codeBlockContent LF "```" END = BEGIN "```" lang:$(CHAR*) LF code:codeBlockContent LF "```" END
{ {
lang = lang.trim(); lang = lang.trim();
return createNode('blockCode', { return CODE_BLOCK(code, lang.length > 0 ? lang : null);
code: code,
lang: lang.length > 0 ? lang : null,
});
} }
codeBlockContent codeBlockContent
...@@ -110,9 +128,7 @@ codeBlockContent ...@@ -110,9 +128,7 @@ codeBlockContent
mathBlock mathBlock
= BEGIN "\\[" LF? formula:mathBlockLines LF? "\\]" END = BEGIN "\\[" LF? formula:mathBlockLines LF? "\\]" END
{ {
return createNode('mathBlock', { return MATH_BLOCK(formula.trim());
formula: formula.trim()
});
} }
mathBlockLines mathBlockLines
...@@ -127,7 +143,7 @@ mathBlockLine ...@@ -127,7 +143,7 @@ mathBlockLine
center center
= BEGIN "<center>" LF? content:(!(LF? "</center>" END) i:inline { return i; })+ LF? "</center>" END = BEGIN "<center>" LF? content:(!(LF? "</center>" END) i:inline { return i; })+ LF? "</center>" END
{ {
return createNode('center', null, mergeText(content)); return CENTER(mergeText(content));
} }
// //
...@@ -156,7 +172,7 @@ inline ...@@ -156,7 +172,7 @@ inline
emojiCode emojiCode
= ":" name:emojiCodeName ":" = ":" name:emojiCodeName ":"
{ {
return createNode('emojiCode', { name: name }); return EMOJI_CODE(name);
} }
emojiCodeName emojiCodeName
...@@ -168,7 +184,7 @@ emojiCodeName ...@@ -168,7 +184,7 @@ emojiCodeName
unicodeEmoji unicodeEmoji
= &{ return matchUnicodeEmoji(); } (&{ return consumeDynamically(); } .)+ = &{ return matchUnicodeEmoji(); } (&{ return consumeDynamically(); } .)+
{ {
return createNode('unicodeEmoji', { emoji: text() }); return UNI_EMOJI(text());
} }
// inline: big // inline: big
...@@ -176,10 +192,7 @@ unicodeEmoji ...@@ -176,10 +192,7 @@ unicodeEmoji
big big
= "***" content:(!"***" i:inline { return i; })+ "***" = "***" content:(!"***" i:inline { return i; })+ "***"
{ {
return createNode('fn', { return FN('tada', { }, mergeText(content));
name: 'tada',
args: { }
}, mergeText(content));
} }
// inline: bold // inline: bold
...@@ -187,12 +200,12 @@ big ...@@ -187,12 +200,12 @@ big
bold bold
= "**" content:(!"**" i:inline { return i; })+ "**" = "**" content:(!"**" i:inline { return i; })+ "**"
{ {
return createNode('bold', null, mergeText(content)); return BOLD(mergeText(content));
} }
/ "__" content:$(!"__" c:([a-z0-9]i / _) { return c; })+ "__" / "__" content:$(!"__" c:([a-z0-9]i / _) { return c; })+ "__"
{ {
const parsedContent = applyParser(content, 'inlineParser'); const parsedContent = applyParser(content, 'inlineParser');
return createNode('bold', null, parsedContent); return BOLD(parsedContent);
} }
// inline: small // inline: small
...@@ -200,7 +213,7 @@ bold ...@@ -200,7 +213,7 @@ bold
small small
= "<small>" content:(!"</small>" i:inline { return i; })+ "</small>" = "<small>" content:(!"</small>" i:inline { return i; })+ "</small>"
{ {
return createNode('small', null, mergeText(content)); return SMALL(mergeText(content));
} }
// inline: italic // inline: italic
...@@ -208,17 +221,17 @@ small ...@@ -208,17 +221,17 @@ small
italic italic
= "<i>" content:(!"</i>" i:inline { return i; })+ "</i>" = "<i>" content:(!"</i>" i:inline { return i; })+ "</i>"
{ {
return createNode('italic', null, mergeText(content)); return ITALIC(mergeText(content));
} }
/ "*" content:$(!"*" ([a-z0-9]i / _))+ "*" / "*" content:$(!"*" ([a-z0-9]i / _))+ "*"
{ {
const parsedContent = applyParser(content, 'inlineParser'); const parsedContent = applyParser(content, 'inlineParser');
return createNode('italic', null, parsedContent); return ITALIC(parsedContent);
} }
/ "_" content:$(!"_" ([a-z0-9]i / _))+ "_" / "_" content:$(!"_" ([a-z0-9]i / _))+ "_"
{ {
const parsedContent = applyParser(content, 'inlineParser'); const parsedContent = applyParser(content, 'inlineParser');
return createNode('italic', null, parsedContent); return ITALIC(parsedContent);
} }
// inline: strike // inline: strike
...@@ -226,7 +239,7 @@ italic ...@@ -226,7 +239,7 @@ italic
strike strike
= "~~" content:(!("~" / LF) i:inline { return i; })+ "~~" = "~~" content:(!("~" / LF) i:inline { return i; })+ "~~"
{ {
return createNode('strike', null, mergeText(content)); return STRIKE(mergeText(content));
} }
// inline: inlineCode // inline: inlineCode
...@@ -234,9 +247,7 @@ strike ...@@ -234,9 +247,7 @@ strike
inlineCode inlineCode
= "`" content:$(!"`" c:CHAR { return c; })+ "`" = "`" content:$(!"`" c:CHAR { return c; })+ "`"
{ {
return createNode('inlineCode', { return INLINE_CODE(content);
code: content
});
} }
// inline: mathInline // inline: mathInline
...@@ -244,9 +255,7 @@ inlineCode ...@@ -244,9 +255,7 @@ inlineCode
mathInline mathInline
= "\\(" content:$(!"\\)" c:CHAR { return c; })+ "\\)" = "\\(" content:$(!"\\)" c:CHAR { return c; })+ "\\)"
{ {
return createNode('mathInline', { return MATH_INLINE(content);
formula: content
});
} }
// inline: mention // inline: mention
...@@ -254,11 +263,7 @@ mathInline ...@@ -254,11 +263,7 @@ mathInline
mention mention
= "@" name:mentionName host:("@" host:mentionHost { return host; })? = "@" name:mentionName host:("@" host:mentionHost { return host; })?
{ {
return createNode('mention', { return MENTION(name, host, text());
username: name,
host: host,
acct: text()
});
} }
mentionName mentionName
...@@ -286,7 +291,7 @@ mentionHostPart ...@@ -286,7 +291,7 @@ mentionHostPart
hashtag hashtag
= "#" content:hashtagContent = "#" content:hashtagContent
{ {
return createNode('hashtag', { hashtag: content }); return HASHTAG(content);
} }
hashtagContent hashtagContent
...@@ -305,11 +310,11 @@ hashtagChar ...@@ -305,11 +310,11 @@ hashtagChar
url url
= "<" url:urlFormat ">" = "<" url:urlFormat ">"
{ {
return createNode('url', { url: url }); return N_URL(url);
} }
/ url:urlFormat / url:urlFormat
{ {
return createNode('url', { url: url }); return N_URL(url);
} }
urlFormat urlFormat
...@@ -335,10 +340,7 @@ urlBracketPair ...@@ -335,10 +340,7 @@ urlBracketPair
link link
= silent:"?"? "[" label:linkLabelPart+ "](" url:linkUrl ")" = silent:"?"? "[" label:linkLabelPart+ "](" url:linkUrl ")"
{ {
return createNode('link', { return LINK((silent != null), url, mergeText(label));
silent: (silent != null),
url: url
}, mergeText(label));
} }
linkLabelPart linkLabelPart
...@@ -355,10 +357,7 @@ fn ...@@ -355,10 +357,7 @@ fn
= "[" name:$([a-z0-9_]i)+ args:fnArgs? _ content:(!"]" i:inline { return i; })+ "]" = "[" name:$([a-z0-9_]i)+ args:fnArgs? _ content:(!"]" i:inline { return i; })+ "]"
{ {
args = args || {}; args = args || {};
return createNode('fn', { return FN(name, args, mergeText(content));
name: name,
args: args
}, mergeText(content));
} }
fnArgs fnArgs
......
import { isMfmBlock, MfmNode } from './node'; import { isMfmBlock, MfmNode, TEXT } from './node';
export function createNode(type: string, props?: Record<string, any>, children?: MfmNode[]): MfmNode { export function createNode(type: string, props?: Record<string, any>, children?: MfmNode[]): MfmNode {
const node: any = { type }; const node: any = { type };
...@@ -20,8 +20,7 @@ export function mergeText(nodes: (MfmNode | string)[]): MfmNode[] { ...@@ -20,8 +20,7 @@ export function mergeText(nodes: (MfmNode | string)[]): MfmNode[] {
*/ */
function generateText() { function generateText() {
if (storedChars.length > 0) { if (storedChars.length > 0) {
const textNode = createNode('text', { text: storedChars.join('') }); dest.push(TEXT(storedChars.join('')));
dest.push(textNode);
storedChars.length = 0; storedChars.length = 0;
} }
} }
...@@ -150,7 +149,7 @@ export function stringifyTree(nodes: MfmNode[]): string { ...@@ -150,7 +149,7 @@ export function stringifyTree(nodes: MfmNode[]): string {
state = stringifyState.inline; state = stringifyState.inline;
} }
if (pushLf) { if (pushLf) {
dest.push(createNode('text', { text: '\n' })); dest.push(TEXT('\n'));
} }
dest.push(node); dest.push(node);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment