From 196a64d3e450300dab2939a84197c58eec37c164 Mon Sep 17 00:00:00 2001 From: marihachi <marihachi0620@gmail.com> Date: Thu, 30 Sep 2021 20:26:19 +0900 Subject: [PATCH] refactor parser (#78) --- src/internal/parser.pegjs | 56 +++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/src/internal/parser.pegjs b/src/internal/parser.pegjs index 7cb949e..7abc8de 100644 --- a/src/internal/parser.pegjs +++ b/src/internal/parser.pegjs @@ -63,13 +63,13 @@ // fullParser - = nodes:(&. n:full { return n; })* { return mergeText(nodes); } + = nodes:(&. @full)* { return mergeText(nodes); } plainParser - = nodes:(&. n:plain { return n; })* { return mergeText(nodes); } + = nodes:(&. @plain)* { return mergeText(nodes); } inlineParser - = nodes:(&. n:inline { return n; })* { return mergeText(nodes); } + = nodes:(&. @inline)* { return mergeText(nodes); } // // syntax list @@ -131,7 +131,7 @@ quote = &(BEGIN ">") q:quoteInner LF? { return q; } quoteInner - = head:quoteMultiLine tails:quoteMultiLine+ + = head:(quoteLine / quoteEmptyLine) tails:(quoteLine / quoteEmptyLine)+ { const children = applyParser([head, ...tails].join('\n'), 'fullParser'); return QUOTE(children); @@ -142,11 +142,8 @@ quoteInner return QUOTE(children); } -quoteMultiLine - = quoteLine / quoteEmptyLine - quoteLine - = BEGIN ">" _? text:$(CHAR+) END { return text; } + = BEGIN ">" _? text:$CHAR+ END { return text; } quoteEmptyLine = BEGIN ">" _? END { return ''; } @@ -198,7 +195,7 @@ mathBlockLine // block: center center - = BEGIN "<center>" LF? content:(!(LF? "</center>" END) i:inline { return i; })+ LF? "</center>" END + = BEGIN "<center>" LF? content:(!(LF? "</center>" END) @inline)+ LF? "</center>" END { return CENTER(mergeText(content)); } @@ -210,14 +207,11 @@ center // inline: emoji code emojiCode - = ":" name:emojiCodeName ":" + = ":" name:$[a-z0-9_+-]i+ ":" { return EMOJI_CODE(name); } -emojiCodeName - = [a-z0-9_+-]i+ { return text(); } - // inline: unicode emoji // NOTE: if the text matches one of the emojis, it will count the length of the emoji sequence and consume it. @@ -230,7 +224,7 @@ unicodeEmoji // inline: big big - = "***" content:(!"***" i:inline { return i; })+ "***" + = "***" content:(!"***" @inline)+ "***" { return FN('tada', { }, mergeText(content)); } @@ -238,15 +232,15 @@ big // inline: bold bold - = "**" content:(!"**" i:inline { return i; })+ "**" + = "**" content:(!"**" @inline)+ "**" { return BOLD(mergeText(content)); } - / "<b>" content:(!"</b>" i:inline { return i; })+ "</b>" + / "<b>" content:(!"</b>" @inline)+ "</b>" { return BOLD(mergeText(content)); } - / "__" content:$(!"__" c:([a-z0-9]i / _) { return c; })+ "__" + / "__" content:$(!"__" @([a-z0-9]i / _))+ "__" { const parsedContent = applyParser(content, 'inlineParser'); return BOLD(parsedContent); @@ -255,7 +249,7 @@ bold // inline: small small - = "<small>" content:(!"</small>" i:inline { return i; })+ "</small>" + = "<small>" content:(!"</small>" @inline)+ "</small>" { return SMALL(mergeText(content)); } @@ -267,7 +261,7 @@ italic / italicAlt italicTag - = "<i>" content:(!"</i>" i:inline { return i; })+ "</i>" + = "<i>" content:(!"</i>" @inline)+ "</i>" { return ITALIC(mergeText(content)); } @@ -287,11 +281,11 @@ italicAlt // inline: strike strike - = "~~" content:(!("~" / LF) i:inline { return i; })+ "~~" + = "~~" content:(!("~" / LF) @inline)+ "~~" { return STRIKE(mergeText(content)); } - / "<s>" content:(!("</s>" / LF) i:inline { return i; })+ "</s>" + / "<s>" content:(!("</s>" / LF) @inline)+ "</s>" { return STRIKE(mergeText(content)); } @@ -299,7 +293,7 @@ strike // inline: inlineCode inlineCode - = "`" content:$(![`´] c:CHAR { return c; })+ "`" + = "`" content:$(![`´] CHAR)+ "`" { return INLINE_CODE(content); } @@ -307,7 +301,7 @@ inlineCode // inline: mathInline mathInline - = "\\(" content:$(!"\\)" c:CHAR { return c; })+ "\\)" + = "\\(" content:$(!"\\)" CHAR)+ "\\)" { return MATH_INLINE(content); } @@ -315,7 +309,7 @@ mathInline // inline: mention mention - = "@" name:mentionName host:("@" host:mentionHost { return host; })? + = "@" name:mentionName host:("@" @mentionHost)? { return MENTION(name, host, text()); } @@ -355,7 +349,8 @@ invalidHashtagContent = [0-9]+ hashtagContentPart - = hashtagBracketPair / hashtagChar + = hashtagBracketPair + / hashtagChar hashtagBracketPair = "(" hashtagContent* ")" @@ -407,16 +402,13 @@ link } linkLabel - = parts:linkLabelPart+ -{ - return parts; -} + = linkLabelPart+ linkLabelPart = url { return text(); /* text node */ } / link { return text(); /* text node */ } / mention { return text(); /* text node */ } - / !"]" n:inline { return n; } + / !"]" @inline linkUrl = url { return text(); } @@ -438,7 +430,7 @@ fnVer2 } fnArgs - = "." head:fnArg tails:("," arg:fnArg { return arg; })* + = "." head:fnArg tails:("," @fnArg)* { const args = { }; for (const pair of [head, ...tails]) { @@ -458,7 +450,7 @@ fnArg } fnContentPart - = !("]") i:inline { return i; } + = !("]") @inline // inline: text -- GitLab