From b09c3d4691c26fdebff74a7e04525b34a66d14e6 Mon Sep 17 00:00:00 2001 From: marihachi Date: Sun, 18 Apr 2021 13:46:22 +0900 Subject: [PATCH 1/4] Parser: improve performance --- src/parser.pegjs | 71 ++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/src/parser.pegjs b/src/parser.pegjs index 64039362..3ac878dc 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -63,7 +63,7 @@ // fullParser - = nodes:(&. n:(block / inline) { return n; })* { return mergeText(nodes); } + = nodes:(&. n:full { return n; })* { return mergeText(nodes); } plainParser = nodes:(&. n:(emojiCode / unicodeEmoji / plainText) { return n; })* { return mergeText(nodes); } @@ -72,20 +72,56 @@ inlineParser = nodes:(&. n:inline { return n; })* { return mergeText(nodes); } // -// block rules +// syntax list // -block - = quote - / search - / codeBlock - / mathBlock - / center +full + = quote // block + / codeBlock // block + / mathBlock // block + / center // block + / emojiCode + / unicodeEmoji + / big + / bold + / small + / italic + / strike + / inlineCode + / mathInline + / mention + / hashtag + / url + / link + / fn + / search // block + / inlineText + +inline + = emojiCode + / unicodeEmoji + / big + / bold + / small + / italic + / strike + / inlineCode + / mathInline + / mention + / hashtag + / url + / link + / fn + / inlineText + +// +// block rules +// // block: quote quote - = head:quoteMultiLine tails:quoteMultiLine+ + = &(BEGIN ">") head:quoteMultiLine tails:quoteMultiLine+ { const children = applyParser([head, ...tails].join('\n'), 'fullParser'); return QUOTE(children); @@ -161,23 +197,6 @@ center // inline rules // -inline - = emojiCode - / unicodeEmoji - / big - / bold - / small - / italic - / strike - / inlineCode - / mathInline - / mention - / hashtag - / url - / link - / fn - / inlineText - // inline: emoji code emojiCode From c5189d8f5282f6db1dc7e0f91b9cbca6f4dc4863 Mon Sep 17 00:00:00 2001 From: marihachi Date: Sun, 18 Apr 2021 14:13:23 +0900 Subject: [PATCH 2/4] Parser: improve performance --- src/parser.pegjs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/parser.pegjs b/src/parser.pegjs index 3ac878dc..93b53c85 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -121,7 +121,10 @@ inline // block: quote quote - = &(BEGIN ">") head:quoteMultiLine tails:quoteMultiLine+ + = &(BEGIN ">") q:quoteInner { return q; } + +quoteInner + = head:quoteMultiLine tails:quoteMultiLine+ { const children = applyParser([head, ...tails].join('\n'), 'fullParser'); return QUOTE(children); From 627a516cc932e54ef13a23903c9b18f40a017ec4 Mon Sep 17 00:00:00 2001 From: marihachi Date: Sun, 18 Apr 2021 14:38:15 +0900 Subject: [PATCH 3/4] Parser: implement fn syntax (version 2) --- src/parser.pegjs | 15 ++++++++++++--- test/parser.ts | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/parser.pegjs b/src/parser.pegjs index 93b53c85..eccf5f23 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -92,8 +92,9 @@ full / mention / hashtag / url + / fnVer2 / link - / fn + / fnVer1 / search // block / inlineText @@ -110,8 +111,9 @@ inline / mention / hashtag / url + / fnVer2 / link - / fn + / fnVer1 / inlineText // @@ -392,13 +394,20 @@ linkUrl // inline: fn -fn +fnVer1 = "[" name:$([a-z0-9_]i)+ args:fnArgs? _ content:(!"]" i:inline { return i; })+ "]" { args = args || {}; return FN(name, args, mergeText(content)); } +fnVer2 + = "$[" name:$([a-z0-9_]i)+ args:fnArgs? _ content:(!"]" i:inline { return i; })+ "]" +{ + args = args || {}; + return FN(name, args, mergeText(content)); +} + fnArgs = "." head:fnArg tails:("," arg:fnArg { return arg; })* { diff --git a/test/parser.ts b/test/parser.ts index 90881863..b3e7f8fd 100644 --- a/test/parser.ts +++ b/test/parser.ts @@ -621,7 +621,7 @@ describe('FullParser', () => { }); }); - describe('fn', () => { + describe('fn v1', () => { it('basic', () => { const input = '[tada abc]'; const output = [ @@ -641,6 +641,52 @@ describe('FullParser', () => { ]; assert.deepStrictEqual(mfm.parse(input), output); }); + + it('nest', () => { + const input = '[spin.speed=1.1s [shake a]]'; + const output = [ + FN('spin', { speed: '1.1s' }, [ + FN('shake', { }, [ + TEXT('a') + ]) + ]) + ]; + assert.deepStrictEqual(mfm.parse(input), output); + }); + }); + + describe('fn v2', () => { + it('basic', () => { + const input = '$[tada abc]'; + const output = [ + FN('tada', { }, [ + TEXT('abc') + ]) + ]; + assert.deepStrictEqual(mfm.parse(input), output); + }); + + it('with a string argument', () => { + const input = '$[spin.speed=1.1s a]'; + const output = [ + FN('spin', { speed: '1.1s' }, [ + TEXT('a') + ]) + ]; + assert.deepStrictEqual(mfm.parse(input), output); + }); + + it('nest', () => { + const input = '$[spin.speed=1.1s $[shake a]]'; + const output = [ + FN('spin', { speed: '1.1s' }, [ + FN('shake', { }, [ + TEXT('a') + ]) + ]) + ]; + assert.deepStrictEqual(mfm.parse(input), output); + }); }); it('composite', () => { From 86cbc7c701cbd5c09c49082c15772a721a3bc331 Mon Sep 17 00:00:00 2001 From: marihachi Date: Sun, 18 Apr 2021 14:59:57 +0900 Subject: [PATCH 4/4] v0.16.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d7647f78..05859aae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mfm-js", - "version": "0.15.1", + "version": "0.16.0", "description": "An MFM parser implementation with PEG.js", "main": "./built/index.js", "types": "./built/index.d.ts",