securemark 0.289.3 → 0.289.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/design.md +6 -6
- package/dist/index.js +113 -64
- package/package.json +1 -1
- package/src/combinator/control/constraint/contract.ts +3 -6
- package/src/combinator/control/manipulation/match.ts +1 -1
- package/src/combinator/control/manipulation/surround.ts +71 -31
- package/src/combinator/data/parser/context/delimiter.ts +27 -8
- package/src/combinator/data/parser.ts +1 -0
- package/src/parser/api/parse.test.ts +8 -6
- package/src/parser/context.ts +1 -1
- package/src/parser/inline/autolink/url.ts +12 -8
- package/src/parser/inline/code.ts +13 -6
- package/src/parser/inline/emstrong.ts +2 -1
- package/src/parser/inline/extension/indexer.ts +6 -0
- package/src/parser/inline/html.ts +11 -5
- package/src/parser/inline/ruby.ts +2 -2
- package/src/parser/inline.test.ts +1 -0
- package/src/parser/inline.ts +19 -4
package/CHANGELOG.md
CHANGED
package/design.md
CHANGED
|
@@ -289,9 +289,9 @@ CodeMirrorが素では速いがVimModeでは数万文字程度でも耐え難く
|
|
|
289
289
|
|
|
290
290
|
### バックトラック
|
|
291
291
|
|
|
292
|
-
SecuremarkのAnnotation構文に典型的であるように文脈を変更する構文の中にその文脈に依存し変更される他の構文が存在する場合文脈の相違から解析結果を再利用不能(`αAβ | αA'B`)なバックトラックが生じる。またこの結果再帰的バックトラックが生じる可能性があり再帰的バックトラックは一般的にメモ化により解決されるがCommonMark
|
|
293
|
-
CommonMarkは小さく単純であるがゆえに正しくいられる象牙の塔であり仕様策定者はこの正しさを失わず正しいままでいたいがために象牙の塔に引きこもり小さな表面的完全性に固執し続けているに過ぎない。しかしCommonMark
|
|
294
|
-
Securemarkはスーパークラス構文が解析に失敗した入力をサブクラス構文で解析しないことにより再帰的バックトラックを回避する(
|
|
292
|
+
SecuremarkのAnnotation構文に典型的であるように文脈を変更する構文の中にその文脈に依存し変更される他の構文が存在する場合文脈の相違から解析結果を再利用不能(`αAβ | αA'B`)なバックトラックが生じる。またこの結果再帰的バックトラックが生じる可能性があり再帰的バックトラックは一般的にメモ化により解決されるがCommonMarkは最小計算量と実行性能を追及するためメモ化を廃止していることからメモ化により性能を低下させてまで文脈依存構文の問題を解決するつもりはないと思われる(すなわちCommonMarkは機械を至上とし人間に制約を課す低水準の言語であり人間の需要を至上とするSecuremarkとは対極に位置する)。従って現在の再帰的バックトラックなしで解析可能な構文と最小計算量に制約されるCommonMarkにはこれ以上再帰的バックトラックが生じる可能性を増加させて文脈依存構文を追加できないという拡張性の欠陥が存在する(CommonMarkは`~~a~~`のような文脈自由構文は容易に追加できるがこうしたマージンを失えばもはや後はなく文脈依存構文を追加できないという事実に直面する)。CommonMarkの仕様策定者が構文の拡張に(名称を維持するか否かにかかわらず)不自然なまでに消極的または進展がないのは正当な理由や怠慢からでなく文脈依存構文を追加するにつれて構文解析戦略の失敗が明白になっていくためおよび最小計算量を放棄して現在の高い実行性能を低下させたくないためであり陳腐な自尊心を守るためにこのような拡張性の欠陥を秘匿しCommonMarkとその仕様策定者である自分の評価が下がらないよう画策しているからである。でなければこの拡張性の欠陥を何年も隠さず速やかに公表して助力を求めていなければならず不都合な事実を隠し陳腐な自尊心を守るために全Markdown利用者および開発者を不必要に足止めした罪は重い。
|
|
293
|
+
CommonMarkは小さく単純であるがゆえに正しくいられる象牙の塔であり仕様策定者はこの正しさを失わず正しいままでいたいがために象牙の塔に引きこもり小さな表面的完全性に固執し続けているに過ぎない。しかしCommonMarkは実際にはまったく完全ではなく本来文脈依存言語であるMarkdownを文脈自由言語として解析しているため破綻している部分があり実際のところCommonMarkは最初から最後までずっと壊れている。CommonMarkはバックトラックなく最小計算量で解析するために文脈自由言語として設計されているが実際には文脈依存言語であるMarkdownから文脈依存構文を文脈自由構文に変換して除去することに失敗しているためCommonMarkは最初の数年間は再帰的バックトラックに気づかず最悪計算量が指数関数計算量になっており修正後は最悪計算量が当初の想定の2nから32nへと劇的に悪化している(より正確にはCommonMarkもSecuremarkもCode構文により+1nされるが説明の簡略化のため省略する)。CommonMarkが最初の数年間他人に指摘され修正されるまで指数関数計算量であった事実(https://github.com/commonmark/cmark/commit/45f4fc9b917c11221aa03e70a41e3046335a235d)はCommonMarkが初歩的な再帰的バックトラックの原理すら理解していないド素人により設計された素人仕事である事実を証明しておりたかが強調構文の解析のためにメモ化を行い(https://github.com/commonmark/commonmark.js/commit/6d7d6cf150dedb53b7f0972b79313df3364ebbed https://github.com/commonmark/commonmark.js/blob/ac8529c9f55da7fdc1186e3f34313cf411de6e71/js/stmd.js )他人にメモ化を使わない正しい実装に修正された事実(https://github.com/commonmark/commonmark.js/commit/8837f199608ac2e321f75653736747b1e692072f)もまたCommonMarkの仕様策定者がその任に堪える能力のないド素人である事実を証明している。スタックを使う代わりにメモ化するド素人の能力を誰が擁護できるのか?ド素人が作った結果初歩的な再帰的バックトラックすら他人に指摘されるまで気づかず最悪計算量が32nにまで著しく悪化した設計が正しいと言えるのか?不可能である。一貫してド素人により設計開発仕様策定されているCommonMarkは未だにバックトラックを忌避し2nの最小計算量に固執しているがそんなものはとっくの昔に破綻してるのを未練がましく執着しているだけである。最悪計算量が32nにまで悪化するのであれば計算量が少ないよう適切に設計された文脈依存言語と大差なく最初から文脈依存言語として適切に設計するほうが自然で破綻がなく拡張性を確保できていた。。さらにSecuremarkは再帰的バックトラックを対策しているため文脈依存構文数が増えても最悪計算量は1+mで定数的にしか増加しない(新しい構文の新しい文脈も内部の括弧類のような基本構造は共通であるため一度解析すればあとは既存の解析情報を利用してバックトラックなしで1回で解析できる)がCommonMarkは再帰的バックトラックを対策していないため文脈依存構文数が増えると最悪計算量が2^mで指数関数的に致命的に激増する(より正確には通常の文脈依存構文を基準にすれば32\*2^m、リンク構文を基準にすれば32^m)。または計算量が組み合わせ爆発しないよう文脈依存構文の入れ子を制限する、存在自体が欠陥と失敗の宣言に等しい制限が必要になる(リンク構文とイメージ構文を入れ子にできるのはaltを不適切に外部と同じ文脈のMarkdownとして解析することで文脈自由化しているからであり本来文脈依存構文は異なる文脈を持つためこのようなことはできずイメージ構文もこの本来できない文脈自由化を行ったためにCommomMarkではaltがプレーンテキストでなくMarkdownとして不適切に解析されている)。文脈依存構文を強引に文脈自由構文として解析したために最悪計算量が当初の想定の2nから32nに劇的に悪化し結局文脈依存言語の妥当な最悪計算量の水準に落ちていることおよび文脈依存構文を追加すると最悪計算量が指数関数的に悪化することから文脈自由言語として設計されたCommonMarkの破綻と失敗は明らかでありCommonMarkは文脈自由構文に固執せず最初から多少の文脈依存構文を許容するよう設計しなければならなかった。実際には文脈依存言語であるにもかかわらず文脈自由言語としてしか構文解析できなければ構文解析が破綻し構文が増えるほど破綻が拡大することは自明でありすでに破綻済みで失敗済みのCommonMarkに未来などない。文脈依存言語であるMarkdownに対して文脈自由構文解析器として作られたCommonMarkは最初から技術選択を間違え失敗しており最初から破綻していた。CommonMarkが文脈依存言語を文脈自由言語として最小計算量で解析するために使用した手法は邪道の小手先の技術に過ぎずCommonMarkは邪道を選んだ挙句失敗に終わったのである。文脈依存言語を文脈依存言語のまま解析する正道を選んだSecuremarkが正着し文脈自由言語に歪める邪道を選んだCommonMarkが失着に終わったのは当然の帰結であり最初の言語種別選択の時点で決まっていたことである。文脈依存言語であるMarkdownを文脈自由言語として解析しようとして行き詰ったCommonMarkとその閉塞に技術的合理性はなくCommonMarkは最初からの失敗していた過去の遺物であり廃棄すべき負債である。CommonMarkに動きがないのはすでに破綻しており死んでいることに気付かれないように死んでいるからに過ぎない。このようにCommonMarkは完全に破綻し失敗に終わっているためCommonMarkの拡張や発展を期待しても無駄であり既存の文脈依存構文による破綻がなく新たに文脈依存構文を追加可能な拡張性の高いMarkdown仕様は新しく作り直さなければ作れない。しかしCommonMarkの仕様策定者は独自の新しい仕様においてもMarkdownをバックトラックを排除した文脈自由言語として設計しているため救いようがない。しかもその構文と仕様は機械可読性を至上としているため非常に醜く人間が書くことも読むことも困難で実用性の欠如したものである。
|
|
294
|
+
Securemarkはスーパークラス構文が解析に失敗した入力をサブクラス構文で解析しないことにより再帰的バックトラックを回避する(解析中の構文自身はスーパークラスとサブクラスの両方に含まれるものとする)。スーパークラス構文A(`αAβ`)の解析が失敗すればサブクラス構文B(`α'A'β'`)の解析も失敗することは自明であり試みるまでもなく解析を省略できる。これは二つの構文の文法が生成する各言語空間がスーパーセットとサブセットの関係にあるならスーパーセットの言語空間の外にある文字列はサブセットの言語空間の内に入る余地がないことからも自明である(この解析法は事前処理によっても可能だが文脈内外のオートリンクURLの括弧解析などを高速に行うことは困難であるためMarkdownをこの事前処理により高速化することは難しい)。メモ化は解析結果を再利用することで結果的に副次的効果としてバックトラックを回避しているのでありメモ化はバックトラックを回避するだけなら過剰機能であり不要である(メモ化はバックトラックがなければ使用されないためバックトラックの少ないほとんどの入力に対してはほとんど使用されず無駄であり空間計算量を常に不必要に数倍以上に増加させてまで行う利益は少ないことから構文解析において必須でも標準でもない。バックトラック回避のためにメモ化するとバックトラックなしで解析可能な場合も常に不必要に空間計算量が増加することがメモ化の最大の欠点である(この問題は解析失敗時のみメモ化すれば解消可能のはずだが基本的にはこうなる)。特に文脈自由構文解析器におけるメモ化の使用は完全に無駄でありバグである。バックトラックが発生しないか他の方法で解決されるならば最終的に使用されないメモ化は無駄であり複数の文脈で解析結果が同一である文脈独立性のある構文ならメモ化した解析結果を異なる文脈で再利用でき有用だがそのような構文は基本的に少数であるため効果が限定的であり最悪計算量は改善されない)。この独自の解析法により、CommonMarkならば最悪計算量32n\*2^2+4n=132nを下らない拡張Markdown言語をSecuremarkはメモ化なしで8nの最悪時間計算量で解析している。すなわち直接比較してもCommonMarkの最悪計算量32nに対してSecuremarkは8nでありSecuremarkはCommonMarkより最悪計算量が小さい。またSecuremarkはメモ化を行っていないため実装依存の非効率性を除けば空間計算量も小さい。時間計算量と空間計算量を合わせてO(n, n)と表記すると文脈依存言語の通常の最悪計算量はO(n^2, n)、メモ化により効率化できた場合もO(nm, nm)(S(m)>=m byte)(解析結果の構文木等を記録するため空間使用量S(m)>=m byte)に過ぎないがSecuremarkのマーキング法はO(nm, nm)(S(m)=m bit)(解析の失敗フラグしか記録しないためS(m)=m bit。また包含文字列を含め全体でn byteの構文1つに対してメモ化は少なくともn byteを消費するがマーキング法のメモリ消費量は構文全体のサイズにかかわらず1bit固定である。よって100KBの構文1つに対してメモ化は100KB以上消費するがこの場合もマーキング法は1bitしか消費しない。なお成功フラグによる解析は解析済みかの情報が追加で必要になり処理が複雑化かつほとんどの成功した解析に対してメモリ消費と追加処理が発生し解析効率が全体として悪化するが失敗フラグは少数の失敗した解析でしか解析効率が悪化しないため失敗フラグを記録するほうが全体として解析効率が高く優れている)と極めて効率的であり最も優れている。以上のようにSecuremarkの構文解析アルゴリズムの優位性は理論と実践いずれにおいても革新的かつ圧倒的である。現在のSecuremarkは開発効率と安全性優先の実装により実行性能が大きく低下しているが一定時間内で解析不能な入力の影響を解析時間と解析範囲の制限により局限しているため、最悪計算量で低速に動作させる入力に対してはこの実装をサーバーで使用し多数のユーザーのリクエストに応じるには低速で脆弱性となる可能性があるがクライアントで個別のユーザーの操作に応じるには十分高速であるためクライアントで解析する限り解析の効率または速度が実用上問題となることはなく仕様が固まり実行効率優先の高速な実装に移れば速度面の懸念もないだろう。またSecuremarkの再帰数制限はパーサーコンビネーターの使用による実装依存の制限であるため再帰が生じないよう書き換えれば再帰数制限もない。SecuremarkをCommonMarkのような再帰数制限のない実装に変換することは設計上何の支障もないがCommonMarkをSecuremarkのような正常な文脈依存言語解析器に変更することは解析規則の破壊的変更なしに不可能である。具体的には二重リンク`[[]()]()`を解析するときCommonMarkはバックトラックと計算量を最小化すべく文脈自由構文解析器として設計されているためリンク構文内をリンク構文が定義されていない異なる文脈として解析せず外側のリンク構文の解析を破棄して内側のみリンク構文として解析するがSecuremarkは文脈依存構文解析器とし設計されているためリンク構文内にリンク構文が定義されておらず外側のみリンクとして解析する(ここでCommonMarkはリンク構文`[]()`のバックトラック除去ひいては文脈自由化に角括弧`[]`に対しては成功したが丸括弧`()`に対しては失敗したことで最悪計算量が指数関数計算量ないし32nに悪化した。リンク構文を本来通り文脈依存構文として解析すればリンク構文の最悪計算量が2nとなり角括弧部分に限っては1nから2nに悪化するが丸括弧部分は32nから2nに著しく改善する。ここがCommonMarkの根本的な欠陥と失敗が最も明瞭に表出している部分である)。この問題はイメージ構文においてさらに顕著でありリンク構文と同じ問題が正当な表現`![![]()]()`で発生しさらにHTMLのaltはプレーンテキストとして表示されるためMarkdownのaltもプレーンテキストとしてそのまま表示されなければならないにもかかわらず文脈を一致させ再帰的バックトラックを防ぐためにMarkdown文字列として解析されaltに`*a*`と書かれたものを`a`に変換して表示する。無論新しい文脈依存構文を追加する場合も同じ制約が永遠についてまわり構文内文字列をMarkdownとして解析する文脈依存構文においてこの制約を破ると最悪計算量が2^m、より正確には32\*2^mないし32^mで指数関数的に増加する。すなわちCommonMarkは文脈依存構文を追加すると最悪計算量が32\*2^mないし32^mで指数関数的に悪化するという拡張性の致命的欠陥が存在する。こんな最悪計算量が32^mで組み合わせ爆発する欠陥言語を拡張できるわけがないことはもはや明白である。また多くのプログラミング言語を見ても明らかなように文脈依存言語は構文内で使用可能な構文を定義しその他の構文は構文内で使用できず例外処理するのが通常でありCommonMarkのように本来使用不能な構文を外側の構文を無効化して使用可能に変える異常な言語はほとんどの人間はCommonMark以外に見たことがないだろう。ほぼすべての人間において他のすべての言語が同じ一貫した規則を持ち同じ規則で統一的に使用できるのに対してCommonMarkだけが他と異なる異常な挙動をして認知的負荷をかけるのである。破壊的変更を避けるため旧構文だけ従来通り文脈自由構文として解析し新構文を文脈依存構文として解析すればキメラ的な非常に不自然かつ歪で一貫性のない解析規則によりCommonMarkという一つの言語の中だけでもユーザーを混乱させるものとなり旧構文で使用した苦肉の策を不必要に新構文でも使用して一貫させれば文脈依存言語なのに文脈自由言語の苦肉の策で解析されるこれもまたキメラ的な非常に不自然で理論的に設計ミスが明白で実用的にも認知的負荷の高い言語となる。そして構文エラーであることが明らかな二重リンクを意図的に入力することはほぼないためCommonMarkの異常な挙動はこれまであまり人目に付かなかったがMarkdownに文脈依存構文を追加して明らかでない構文エラーが頻発すると他の言語と逆に外側の構文を無効化していくCommonMarkの異常な挙動を頻繁に目撃し認知的負荷をかけられることになる。このようにCommonMarkは内部設計だけ文脈依存構文解析器に変更しても理論的齟齬が解析結果と使用感に明白に表れるためCommonMarkが失敗した言語である事実は到底隠し切れるものではない。Markdownはもはや負債以外の何物でもないCommonMarkの異常な解析規則を捨てて素直な文脈依存構文言語として新しい仕様を作り直すのが賢明である。
|
|
295
295
|
|
|
296
296
|
### 最適化
|
|
297
297
|
|
|
@@ -324,7 +324,7 @@ Markdownに本当に必要な仕様はSecuremarkのクラス化制約のよう
|
|
|
324
324
|
- altはまだしもURLは文脈依存構文としての解析を避けられないため再帰もまた避けられず公式デモページのCommonMarkで`[](`を1万回程度繰り返しただけで解析時間が1秒を超える
|
|
325
325
|
- この欠陥は入れ子数を制限することで回避可能だがこれはCommonmarkは文脈依存構文全般に入れ子数制限を要することを意味する
|
|
326
326
|
- CommonMarkは最初のバージョンから数年後にこの欠陥を入れ子数制限により修正しこれにより最悪計算量が当初の想定の2nから32nへと32倍に劇的に悪化したことから文脈自由言語および最小計算量としての設計と開発が破綻し失敗に終わったことが明らかである
|
|
327
|
-
- GFMも`[
|
|
327
|
+
- GFMも`[0]([1]()(...[33]()...))`で32回の再帰的バックトラックを行い最悪計算量が32nであることが確認できる
|
|
328
328
|
- これほど計算量が悪ければ入れ子数制限付き文脈依存言語と大差ない計算量であり素直に文脈依存言語として作り直したほうが遥かに拡張性と発展性が高く優れている
|
|
329
329
|
- 計算資源は使うためにあるにもかかわらず言語と一致しない不適切な解析方法を使用してまでこの程度の計算資源を惜しんで人間に不便と不自由を強いて生産性を下げるのは本末転倒である
|
|
330
330
|
- 計算機は人間の生産性に奉仕しなければならない
|
|
@@ -335,14 +335,14 @@ Markdownに本当に必要な仕様はSecuremarkのクラス化制約のよう
|
|
|
335
335
|
|
|
336
336
|
### CommonMarkの解析規則の問題点
|
|
337
337
|
|
|
338
|
-
|
|
338
|
+
開閉が明示的でない構文は開閉の不明確な記号による再帰的適用を行わず可能な限り早く閉じるよう解析しなければならない。このため終端記号の後ろを見て終端を中止し同じ構文を再帰的に適用してはならない。従って記号内側の非空白要件以外のflanking要件は不要である。
|
|
339
339
|
|
|
340
340
|
```
|
|
341
341
|
flanking*!?*
|
|
342
342
|
-> flanking*!?*
|
|
343
343
|
```
|
|
344
344
|
|
|
345
|
-
|
|
345
|
+
`**`が`*`に分解されるか一貫性がなく直感的使用が困難。強調を多重化する不適切で不確実な使用方法より開始記号の信頼性を損なわないことのほうが重要であることから分解しないことが優先される。
|
|
346
346
|
|
|
347
347
|
```
|
|
348
348
|
**a*b*
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! securemark v0.289.
|
|
1
|
+
/*! securemark v0.289.5 https://github.com/falsandtru/securemark | (c) 2017, falsandtru | UNLICENSED License */
|
|
2
2
|
(function webpackUniversalModuleDefinition(root, factory) {
|
|
3
3
|
if(typeof exports === 'object' && typeof module === 'object')
|
|
4
4
|
module.exports = factory(require("Prism"), require("DOMPurify"));
|
|
@@ -2736,10 +2736,9 @@ Object.defineProperty(exports, "__esModule", ({
|
|
|
2736
2736
|
exports.verify = exports.validate = void 0;
|
|
2737
2737
|
const alias_1 = __webpack_require__(5413);
|
|
2738
2738
|
const parser_1 = __webpack_require__(605);
|
|
2739
|
-
function validate(patterns,
|
|
2740
|
-
if (typeof patterns === 'function') return guard(patterns,
|
|
2741
|
-
if (
|
|
2742
|
-
if (!(0, alias_1.isArray)(patterns)) return validate([patterns], has, parser);
|
|
2739
|
+
function validate(patterns, parser) {
|
|
2740
|
+
if (typeof patterns === 'function') return guard(patterns, parser);
|
|
2741
|
+
if (!(0, alias_1.isArray)(patterns)) return validate([patterns], parser);
|
|
2743
2742
|
const match = __webpack_require__.g.eval(['source =>', patterns.map(pattern => typeof pattern === 'string' ? `|| source.slice(0, ${pattern.length}) === '${pattern}'` : `|| /${pattern.source}/${pattern.flags}.test(source)`).join('').slice(2)].join(''));
|
|
2744
2743
|
return input => {
|
|
2745
2744
|
const {
|
|
@@ -3050,7 +3049,7 @@ function match(pattern, f, cost = false) {
|
|
|
3050
3049
|
if (source === '') return;
|
|
3051
3050
|
const param = source.match(pattern);
|
|
3052
3051
|
if (!param) return;
|
|
3053
|
-
cost && (0, combinator_1.consume)(param.length, context);
|
|
3052
|
+
cost && (0, combinator_1.consume)(param[0].length, context);
|
|
3054
3053
|
const result = f(param)(input);
|
|
3055
3054
|
if (result === undefined) return;
|
|
3056
3055
|
return (0, parser_1.exec)(result).length < source.length && (0, parser_1.exec)(result).length <= source.length ? result : undefined;
|
|
@@ -3175,7 +3174,7 @@ exports.rewrite = rewrite;
|
|
|
3175
3174
|
Object.defineProperty(exports, "__esModule", ({
|
|
3176
3175
|
value: true
|
|
3177
3176
|
}));
|
|
3178
|
-
exports.close = exports.open = exports.surround = void 0;
|
|
3177
|
+
exports.setBacktrack = exports.getBacktrack = exports.close = exports.open = exports.surround = void 0;
|
|
3179
3178
|
const parser_1 = __webpack_require__(605);
|
|
3180
3179
|
const array_1 = __webpack_require__(6876);
|
|
3181
3180
|
function surround(opener, parser, closer, optional = false, f, g, backtracks = [], backtrackstate = 0) {
|
|
@@ -3189,7 +3188,6 @@ function surround(opener, parser, closer, optional = false, f, g, backtracks = [
|
|
|
3189
3188
|
case 'object':
|
|
3190
3189
|
closer = match(closer);
|
|
3191
3190
|
}
|
|
3192
|
-
const statesize = 2;
|
|
3193
3191
|
return ({
|
|
3194
3192
|
source,
|
|
3195
3193
|
context
|
|
@@ -3207,22 +3205,7 @@ function surround(opener, parser, closer, optional = false, f, g, backtracks = [
|
|
|
3207
3205
|
if (resultS === undefined) return void revert(context, linebreak);
|
|
3208
3206
|
const nodesS = (0, parser_1.eval)(resultS);
|
|
3209
3207
|
const me_ = (0, parser_1.exec)(resultS);
|
|
3210
|
-
|
|
3211
|
-
if (backtrack & 1) {
|
|
3212
|
-
const {
|
|
3213
|
-
backtracks = {},
|
|
3214
|
-
backtrack: state = 0,
|
|
3215
|
-
offset = 0
|
|
3216
|
-
} = context;
|
|
3217
|
-
for (let i = 0; i < source.length - me_.length; ++i) {
|
|
3218
|
-
if (source[i] !== source[0]) break;
|
|
3219
|
-
const pos = source.length - i + offset - 1;
|
|
3220
|
-
if (!(pos in backtracks)) continue;
|
|
3221
|
-
const shift = backtrack >>> statesize & state >>> statesize ? state & (1 << statesize) - 1 : 0;
|
|
3222
|
-
if (backtracks[pos] & 1 << size(backtrack >>> statesize) + shift) return void revert(context, linebreak);
|
|
3223
|
-
}
|
|
3224
|
-
}
|
|
3225
|
-
}
|
|
3208
|
+
if (getBacktrack(context, backtracks, sme_, me_)) return void revert(context, linebreak);
|
|
3226
3209
|
const {
|
|
3227
3210
|
backtrack = 0
|
|
3228
3211
|
} = context;
|
|
@@ -3234,26 +3217,15 @@ function surround(opener, parser, closer, optional = false, f, g, backtracks = [
|
|
|
3234
3217
|
context.backtrack = backtrack;
|
|
3235
3218
|
const nodesM = (0, parser_1.eval)(resultM);
|
|
3236
3219
|
const e_ = (0, parser_1.exec)(resultM, me_);
|
|
3237
|
-
|
|
3238
|
-
const resultE = closer({
|
|
3220
|
+
const resultE = nodesM || optional ? closer({
|
|
3239
3221
|
source: e_,
|
|
3240
3222
|
context
|
|
3241
|
-
});
|
|
3223
|
+
}) : undefined;
|
|
3242
3224
|
const nodesE = (0, parser_1.eval)(resultE);
|
|
3243
3225
|
const rest = (0, parser_1.exec)(resultE, e_);
|
|
3226
|
+
nodesE || setBacktrack(context, backtracks, sme_);
|
|
3227
|
+
if (!nodesM && !optional) return void revert(context, linebreak);
|
|
3244
3228
|
if (rest.length === sme_.length) return void revert(context, linebreak);
|
|
3245
|
-
for (const backtrack of backtracks) {
|
|
3246
|
-
if (backtrack & 2 && nodesE === undefined) {
|
|
3247
|
-
const {
|
|
3248
|
-
backtracks = {},
|
|
3249
|
-
backtrack: state = 0,
|
|
3250
|
-
offset = 0
|
|
3251
|
-
} = context;
|
|
3252
|
-
const pos = source.length + offset - 1;
|
|
3253
|
-
const shift = backtrack >>> statesize & state >>> statesize ? state & (1 << statesize) - 1 : 0;
|
|
3254
|
-
backtracks[pos] |= 1 << size(backtrack >>> statesize) + shift;
|
|
3255
|
-
}
|
|
3256
|
-
}
|
|
3257
3229
|
context.recent = [sme_.slice(0, sme_.length - me_.length), me_.slice(0, me_.length - e_.length), e_.slice(0, e_.length - rest.length)];
|
|
3258
3230
|
const result = nodesE ? f ? f([nodesS, nodesM, nodesE], rest, context) : [(0, array_1.push)((0, array_1.unshift)(nodesS, nodesM ?? []), nodesE), rest] : g ? g([nodesS, nodesM, me_], rest, context) : undefined;
|
|
3259
3231
|
if (result) {
|
|
@@ -3265,14 +3237,50 @@ function surround(opener, parser, closer, optional = false, f, g, backtracks = [
|
|
|
3265
3237
|
};
|
|
3266
3238
|
}
|
|
3267
3239
|
exports.surround = surround;
|
|
3268
|
-
function open(opener, parser, optional
|
|
3269
|
-
return surround(opener, parser, '', optional);
|
|
3240
|
+
function open(opener, parser, optional, backtracks, backtrackstate) {
|
|
3241
|
+
return surround(opener, parser, '', optional, undefined, undefined, backtracks, backtrackstate);
|
|
3270
3242
|
}
|
|
3271
3243
|
exports.open = open;
|
|
3272
|
-
function close(parser, closer, optional
|
|
3273
|
-
return surround('', parser, closer, optional);
|
|
3244
|
+
function close(parser, closer, optional, backtracks, backtrackstate) {
|
|
3245
|
+
return surround('', parser, closer, optional, undefined, undefined, backtracks, backtrackstate);
|
|
3274
3246
|
}
|
|
3275
3247
|
exports.close = close;
|
|
3248
|
+
const statesize = 2;
|
|
3249
|
+
function getBacktrack(context, backtracks, sme_, me_) {
|
|
3250
|
+
for (const backtrack of backtracks) {
|
|
3251
|
+
if (backtrack & 1) {
|
|
3252
|
+
const {
|
|
3253
|
+
backtracks = {},
|
|
3254
|
+
backtrack: state = 0,
|
|
3255
|
+
offset = 0
|
|
3256
|
+
} = context;
|
|
3257
|
+
for (let i = 0, len = sme_.length - me_.length || 1; i < len; ++i) {
|
|
3258
|
+
if (sme_[i] !== sme_[0]) break;
|
|
3259
|
+
const pos = sme_.length - i + offset - 1;
|
|
3260
|
+
if (!(pos in backtracks)) continue;
|
|
3261
|
+
const shift = backtrack >>> statesize & state >>> statesize ? state & (1 << statesize) - 1 : 0;
|
|
3262
|
+
if (backtracks[pos] & 1 << size(backtrack >>> statesize) + shift) return true;
|
|
3263
|
+
}
|
|
3264
|
+
}
|
|
3265
|
+
}
|
|
3266
|
+
return false;
|
|
3267
|
+
}
|
|
3268
|
+
exports.getBacktrack = getBacktrack;
|
|
3269
|
+
function setBacktrack(context, backtracks, sme_) {
|
|
3270
|
+
for (const backtrack of backtracks) {
|
|
3271
|
+
if (backtrack & 2) {
|
|
3272
|
+
const {
|
|
3273
|
+
backtracks = {},
|
|
3274
|
+
backtrack: state = 0,
|
|
3275
|
+
offset = 0
|
|
3276
|
+
} = context;
|
|
3277
|
+
const pos = sme_.length + offset - 1;
|
|
3278
|
+
const shift = backtrack >>> statesize & state >>> statesize ? state & (1 << statesize) - 1 : 0;
|
|
3279
|
+
backtracks[pos] |= 1 << size(backtrack >>> statesize) + shift;
|
|
3280
|
+
}
|
|
3281
|
+
}
|
|
3282
|
+
}
|
|
3283
|
+
exports.setBacktrack = setBacktrack;
|
|
3276
3284
|
function match(pattern) {
|
|
3277
3285
|
switch (typeof pattern) {
|
|
3278
3286
|
case 'string':
|
|
@@ -3567,7 +3575,7 @@ exports.constraint = constraint;
|
|
|
3567
3575
|
/***/ },
|
|
3568
3576
|
|
|
3569
3577
|
/***/ 5691
|
|
3570
|
-
(__unused_webpack_module, exports
|
|
3578
|
+
(__unused_webpack_module, exports) {
|
|
3571
3579
|
|
|
3572
3580
|
"use strict";
|
|
3573
3581
|
|
|
@@ -3576,19 +3584,25 @@ Object.defineProperty(exports, "__esModule", ({
|
|
|
3576
3584
|
value: true
|
|
3577
3585
|
}));
|
|
3578
3586
|
exports.Delimiters = void 0;
|
|
3579
|
-
const memoize_1 = __webpack_require__(6925);
|
|
3580
3587
|
class Delimiters {
|
|
3581
3588
|
constructor() {
|
|
3582
|
-
this.
|
|
3589
|
+
this.heap = {};
|
|
3590
|
+
this.map = new Map();
|
|
3583
3591
|
this.delimiters = [];
|
|
3584
3592
|
this.stack = [];
|
|
3585
3593
|
this.states = [];
|
|
3586
3594
|
}
|
|
3595
|
+
// 手間を惜しまなければ規定のパターンはすべて配列のインデクスに変換可能。
|
|
3587
3596
|
static signature(pattern, linebreakable) {
|
|
3588
3597
|
switch (typeof pattern) {
|
|
3589
3598
|
case 'undefined':
|
|
3590
|
-
return
|
|
3599
|
+
return +linebreakable;
|
|
3591
3600
|
case 'string':
|
|
3601
|
+
if (pattern.length === 1) {
|
|
3602
|
+
const code = pattern.charCodeAt(0);
|
|
3603
|
+
// 使用中のパターンの8ビット目が空いてるのでひとまずこうしとく
|
|
3604
|
+
if ((code & 1 << 7) === 0) return code | +linebreakable << 7;
|
|
3605
|
+
}
|
|
3592
3606
|
return `s:${pattern}:${+linebreakable}`;
|
|
3593
3607
|
case 'object':
|
|
3594
3608
|
return `r/${pattern.source}/${+linebreakable}`;
|
|
@@ -3604,9 +3618,19 @@ class Delimiters {
|
|
|
3604
3618
|
return source => pattern.test(source) || undefined;
|
|
3605
3619
|
}
|
|
3606
3620
|
}
|
|
3621
|
+
registry(signature) {
|
|
3622
|
+
if (typeof signature === 'number') {
|
|
3623
|
+
return this.heap[signature] ??= [];
|
|
3624
|
+
} else {
|
|
3625
|
+
const ds = this.map.get(signature);
|
|
3626
|
+
if (ds) return ds;
|
|
3627
|
+
const blank = [];
|
|
3628
|
+
this.map.set(signature, blank);
|
|
3629
|
+
return blank;
|
|
3630
|
+
}
|
|
3631
|
+
}
|
|
3607
3632
|
push(delims) {
|
|
3608
3633
|
const {
|
|
3609
|
-
registry,
|
|
3610
3634
|
delimiters,
|
|
3611
3635
|
stack
|
|
3612
3636
|
} = this;
|
|
@@ -3619,7 +3643,7 @@ class Delimiters {
|
|
|
3619
3643
|
precedence,
|
|
3620
3644
|
linebreakable
|
|
3621
3645
|
} = delims[i];
|
|
3622
|
-
const memory = registry(signature);
|
|
3646
|
+
const memory = this.registry(signature);
|
|
3623
3647
|
const index = memory[0]?.index ?? delimiters.length;
|
|
3624
3648
|
if (memory.length === 0) {
|
|
3625
3649
|
const delimiter = {
|
|
@@ -5804,11 +5828,14 @@ const htmlentity_1 = __webpack_require__(470);
|
|
|
5804
5828
|
const bracket_1 = __webpack_require__(4526);
|
|
5805
5829
|
const autolink_1 = __webpack_require__(8072);
|
|
5806
5830
|
const source_1 = __webpack_require__(8745);
|
|
5807
|
-
exports.inline = (0, combinator_1.lazy)(() => (0, combinator_1.union)([input => {
|
|
5831
|
+
exports.inline = (0, combinator_1.lazy)(() => (0, combinator_1.verify)((0, combinator_1.union)([input => {
|
|
5808
5832
|
const {
|
|
5809
|
-
source
|
|
5833
|
+
source,
|
|
5834
|
+
context
|
|
5810
5835
|
} = input;
|
|
5811
5836
|
if (source === '') return;
|
|
5837
|
+
context.depth ??= 0;
|
|
5838
|
+
++context.depth;
|
|
5812
5839
|
switch (source.slice(0, 2)) {
|
|
5813
5840
|
case '((':
|
|
5814
5841
|
return (0, annotation_1.annotation)(input);
|
|
@@ -5851,7 +5878,19 @@ exports.inline = (0, combinator_1.lazy)(() => (0, combinator_1.union)([input =>
|
|
|
5851
5878
|
case '&':
|
|
5852
5879
|
return (0, htmlentity_1.htmlentity)(input);
|
|
5853
5880
|
}
|
|
5854
|
-
}, bracket_1.bracket, autolink_1.autolink, source_1.text]))
|
|
5881
|
+
}, bracket_1.bracket, autolink_1.autolink, source_1.text]), (_, rest, context) => {
|
|
5882
|
+
--context.depth;
|
|
5883
|
+
// ヒープを効率的に削除可能な場合は削除する。
|
|
5884
|
+
// ヒープサイズは括弧類など特定の構文が完成しなかった場合にしか増加しないため
|
|
5885
|
+
// ブロックごとに平均数ノード以下となることから削除せずとも平均的にはあまり影響はない。
|
|
5886
|
+
//if (context.depth === 0) {
|
|
5887
|
+
// const { backtracks } = context;
|
|
5888
|
+
// while (backtracks.peek()?.key! > rest.length) {
|
|
5889
|
+
// backtracks.extract();
|
|
5890
|
+
// }
|
|
5891
|
+
//}
|
|
5892
|
+
return true;
|
|
5893
|
+
}));
|
|
5855
5894
|
var indexee_1 = __webpack_require__(7610);
|
|
5856
5895
|
Object.defineProperty(exports, "indexee", ({
|
|
5857
5896
|
enumerable: true,
|
|
@@ -6117,14 +6156,14 @@ exports.lineurl = exports.url = void 0;
|
|
|
6117
6156
|
const combinator_1 = __webpack_require__(3484);
|
|
6118
6157
|
const link_1 = __webpack_require__(3628);
|
|
6119
6158
|
const source_1 = __webpack_require__(8745);
|
|
6120
|
-
const closer = /^[-+*=~^_,.;:!?]*(?=[\\"`|\[\](){}<>]|$)/;
|
|
6121
|
-
exports.url = (0, combinator_1.lazy)(() => (0, combinator_1.validate)(['http://', 'https://'], (0, combinator_1.rewrite)((0, combinator_1.open)(/^https?:\/\/(?=[\x21-\x7E])/, (0, combinator_1.
|
|
6159
|
+
const closer = /^[-+*=~^_,.;:!?]*(?=[\\"`|\[\](){}<>]|[^\x21-\x7E]|$)/;
|
|
6160
|
+
exports.url = (0, combinator_1.lazy)(() => (0, combinator_1.validate)(['http://', 'https://'], (0, combinator_1.rewrite)((0, combinator_1.open)(/^https?:\/\/(?=[\x21-\x7E])/, (0, combinator_1.precedence)(1, (0, combinator_1.some)((0, combinator_1.union)([(0, combinator_1.verify)(bracket, ns => ns.length > 0), (0, combinator_1.some)(source_1.unescsource, closer)]), undefined, [[/^[^\x21-\x7E]/, 3]])), false, [3 | 0 /* Backtrack.url */]), (0, combinator_1.union)([(0, combinator_1.constraint)(1 /* State.autolink */, false, (0, combinator_1.state)(1 /* State.autolink */, (0, combinator_1.convert)(url => `{ ${url} }`, link_1.unsafelink, false))), ({
|
|
6122
6161
|
source
|
|
6123
6162
|
}) => [[source], '']]))));
|
|
6124
6163
|
exports.lineurl = (0, combinator_1.lazy)(() => (0, combinator_1.open)(source_1.linebreak, (0, combinator_1.focus)(/^!?https?:\/\/\S+(?=[^\S\n]*(?:$|\n))/, (0, combinator_1.tails)([(0, source_1.str)('!'), (0, combinator_1.union)([(0, combinator_1.constraint)(1 /* State.autolink */, false, (0, combinator_1.state)(1 /* State.autolink */, (0, combinator_1.convert)(url => `{ ${url} }`, link_1.unsafelink, false))), ({
|
|
6125
6164
|
source
|
|
6126
|
-
}) => [[source], '']])]))));
|
|
6127
|
-
const bracket = (0, combinator_1.lazy)(() => (0, combinator_1.union)([(0, combinator_1.surround)((0, source_1.str)('('), (0, combinator_1.recursion)(6 /* Recursion.terminal */, (0, combinator_1.some)((0, combinator_1.union)([bracket, source_1.unescsource]), ')')), (0, source_1.str)(')'), true, undefined, () => [[], ''], [3 | 0 /* Backtrack.
|
|
6165
|
+
}) => [[source], '']])])), false, [3 | 16 /* Backtrack.linebracket */]));
|
|
6166
|
+
const bracket = (0, combinator_1.lazy)(() => (0, combinator_1.union)([(0, combinator_1.surround)((0, source_1.str)('('), (0, combinator_1.recursion)(6 /* Recursion.terminal */, (0, combinator_1.some)((0, combinator_1.union)([bracket, source_1.unescsource]), ')')), (0, source_1.str)(')'), true, undefined, () => [[], ''], [3 | 0 /* Backtrack.url */]), (0, combinator_1.surround)((0, source_1.str)('['), (0, combinator_1.recursion)(6 /* Recursion.terminal */, (0, combinator_1.some)((0, combinator_1.union)([bracket, source_1.unescsource]), ']')), (0, source_1.str)(']'), true, undefined, () => [[], ''], [3 | 0 /* Backtrack.url */]), (0, combinator_1.surround)((0, source_1.str)('{'), (0, combinator_1.recursion)(6 /* Recursion.terminal */, (0, combinator_1.some)((0, combinator_1.union)([bracket, source_1.unescsource]), '}')), (0, source_1.str)('}'), true, undefined, () => [[], ''], [3 | 0 /* Backtrack.url */]), (0, combinator_1.surround)((0, source_1.str)('"'), (0, combinator_1.precedence)(2, (0, combinator_1.recursion)(6 /* Recursion.terminal */, (0, combinator_1.some)(source_1.unescsource, '"'))), (0, source_1.str)('"'), true, undefined, () => [[], ''], [3 | 0 /* Backtrack.url */])]));
|
|
6128
6167
|
|
|
6129
6168
|
/***/ },
|
|
6130
6169
|
|
|
@@ -6173,11 +6212,15 @@ Object.defineProperty(exports, "__esModule", ({
|
|
|
6173
6212
|
exports.code = void 0;
|
|
6174
6213
|
const combinator_1 = __webpack_require__(3484);
|
|
6175
6214
|
const dom_1 = __webpack_require__(394);
|
|
6176
|
-
exports.code = (0, combinator_1.
|
|
6177
|
-
source
|
|
6178
|
-
|
|
6215
|
+
exports.code = (0, combinator_1.validate)(({
|
|
6216
|
+
source,
|
|
6217
|
+
context
|
|
6218
|
+
}) => source[0] === '`' && !(0, combinator_1.getBacktrack)(context, [1 | 16 /* Backtrack.linebracket */], source, source.slice(1)), (0, combinator_1.match)(/^(`+)(?!`)([^\n]*?)(?:((?<!`)\1(?!`))|$|\n)/, ([whole,, body, closer]) => ({
|
|
6219
|
+
source,
|
|
6220
|
+
context
|
|
6221
|
+
}) => closer ? [[(0, dom_1.html)('code', {
|
|
6179
6222
|
'data-src': whole
|
|
6180
|
-
}, format(body))], source.slice(whole.length)], true);
|
|
6223
|
+
}, format(body))], source.slice(whole.length)] : void (0, combinator_1.setBacktrack)(context, [2 | 16 /* Backtrack.linebracket */], source), true));
|
|
6181
6224
|
function format(text) {
|
|
6182
6225
|
return `${text[0]}${text.at(-1)}` === ' ' && text.trimStart() ? text.slice(1, -1) : text;
|
|
6183
6226
|
}
|
|
@@ -6247,7 +6290,8 @@ const dom_1 = __webpack_require__(394);
|
|
|
6247
6290
|
const array_1 = __webpack_require__(6876);
|
|
6248
6291
|
const substrong = (0, combinator_1.lazy)(() => (0, combinator_1.some)((0, combinator_1.union)([(0, combinator_1.some)(inline_1.inline, (0, visibility_1.blankWith)('**')), (0, combinator_1.open)((0, combinator_1.some)(inline_1.inline, '*'), (0, combinator_1.union)([exports.emstrong, strong_1.strong]))])));
|
|
6249
6292
|
const subemphasis = (0, combinator_1.lazy)(() => (0, combinator_1.some)((0, combinator_1.union)([strong_1.strong, (0, combinator_1.some)(inline_1.inline, (0, visibility_1.blankWith)('*')), (0, combinator_1.open)((0, combinator_1.some)(inline_1.inline, '*'), (0, combinator_1.union)([exports.emstrong, strong_1.strong, emphasis_1.emphasis]))])));
|
|
6250
|
-
//
|
|
6293
|
+
// 開閉が明示的でない構文は開閉の不明確な記号による再帰的適用を行わず
|
|
6294
|
+
// 可能な限り早く閉じるよう解析しなければならない。
|
|
6251
6295
|
// このため終端記号の後ろを見て終端を中止し同じ構文を再帰的に適用してはならない。
|
|
6252
6296
|
exports.emstrong = (0, combinator_1.lazy)(() => (0, combinator_1.validate)('***', (0, combinator_1.precedence)(0, (0, util_1.repeat)('***', (0, combinator_1.surround)('', (0, combinator_1.recursion)(4 /* Recursion.inline */, (0, visibility_1.tightStart)((0, combinator_1.some)((0, combinator_1.union)([(0, combinator_1.some)(inline_1.inline, (0, visibility_1.blankWith)('*')), (0, combinator_1.open)((0, combinator_1.some)(inline_1.inline, '*'), inline_1.inline)])))), (0, source_1.str)(/^\*{1,3}/), false, ([, bs, cs], rest, context) => {
|
|
6253
6297
|
switch (cs[0]) {
|
|
@@ -6520,6 +6564,11 @@ exports.indexer = void 0;
|
|
|
6520
6564
|
const combinator_1 = __webpack_require__(3484);
|
|
6521
6565
|
const index_1 = __webpack_require__(4688);
|
|
6522
6566
|
const dom_1 = __webpack_require__(394);
|
|
6567
|
+
// インデクスの重複解消は不要な重複を削除するのが最もよい。
|
|
6568
|
+
// 複合生成インデクスは参照と同期させることが困難であり
|
|
6569
|
+
// 複合生成インデクスを手動で同期させるより最初から重複のない
|
|
6570
|
+
// テキストまたはインデクスを付けて同期が必要な機会を減らすのが
|
|
6571
|
+
// 継続的編集において最も簡便となる。
|
|
6523
6572
|
exports.indexer = (0, combinator_1.surround)(/^\s+\[(?=\|\S)/, (0, combinator_1.union)([index_1.signature, (0, combinator_1.focus)(/^\|(?=\])/, () => [[(0, dom_1.html)('span', {
|
|
6524
6573
|
class: 'indexer',
|
|
6525
6574
|
'data-index': ''
|
|
@@ -6621,7 +6670,7 @@ Object.setPrototypeOf(attrspecs, null);
|
|
|
6621
6670
|
Object.values(attrspecs).forEach(o => Object.setPrototypeOf(o, null));
|
|
6622
6671
|
exports.html = (0, combinator_1.lazy)(() => (0, combinator_1.validate)(/^<[a-z]+(?=[^\S\n]|>)/i, (0, combinator_1.union)([(0, combinator_1.focus)(/^<wbr[^\S\n]*>/i, () => [[(0, dom_1.html)('wbr')], '']), (0, combinator_1.surround)(
|
|
6623
6672
|
// https://html.spec.whatwg.org/multipage/syntax.html#void-elements
|
|
6624
|
-
(0, source_1.str)(/^<(?:area|base|br|col|embed|hr|img|input|link|meta|source|track|wbr)(?=[^\S\n]|>)/i), (0, combinator_1.some)((0, combinator_1.union)([exports.attribute])), (0, source_1.str)(/^[^\S\n]*>/), true, ([as, bs = [], cs], rest) => [[elem(as[0].slice(1), (0, array_1.push)((0, array_1.unshift)(as, bs), cs), [], [])], rest]), (0, combinator_1.match)(new RegExp(String.raw`^<(${TAGS.join('|')})(?=[^\S\n]|>)`), (0, memoize_1.memoize)(([, tag]) => (0, combinator_1.surround)((0, combinator_1.surround)((0, source_1.str)(`<${tag}`), (0, combinator_1.some)(exports.attribute), (0, source_1.str)(/^[^\S\n]*>/), true), (0, combinator_1.precedence)(3, (0, combinator_1.recursion)(4 /* Recursion.inline */, (0, combinator_1.subsequence)([(0, combinator_1.focus)(/^[^\S\n]*\n/, (0, combinator_1.some)(inline_1.inline)), (0, combinator_1.some)((0, combinator_1.open)(/^\n?/, (0, combinator_1.some)(inline_1.inline, (0, visibility_1.blankWith)('\n', `</${tag}>`), [[(0, visibility_1.blankWith)('\n', `</${tag}>`), 3]]), true))]))), (0, source_1.str)(`</${tag}>`), true, ([as, bs = [], cs], rest) => [[elem(tag, as, bs, cs)], rest], ([as, bs = []], rest) => [[elem(tag, as, bs, [])], rest]), ([, tag]) => tag, new Map())), (0, combinator_1.match)(/^<([a-z]+)(?=[^\S\n]|>)/i, (0, memoize_1.memoize)(([, tag]) => (0, combinator_1.surround)((0, combinator_1.surround)((0, source_1.str)(`<${tag}`), (0, combinator_1.some)(exports.attribute), (0, source_1.str)(/^[^\S\n]*>/), true), (0, combinator_1.precedence)(3, (0, combinator_1.recursion)(4 /* Recursion.inline */, (0, combinator_1.subsequence)([(0, combinator_1.focus)(/^[^\S\n]*\n/, (0, combinator_1.some)(inline_1.inline)), (0, combinator_1.some)(inline_1.inline, `</${tag}>`, [[`</${tag}>`, 3]])]))), (0, source_1.str)(`</${tag}>`), true, ([as, bs = [], cs], rest) => [[elem(tag, as, bs, cs)], rest], ([as, bs = []], rest) => [[elem(tag, as, bs, [])], rest]), ([, tag]) => tag, new clock_1.Clock(10000)))])));
|
|
6673
|
+
(0, source_1.str)(/^<(?:area|base|br|col|embed|hr|img|input|link|meta|source|track|wbr)(?=[^\S\n]|>)/i), (0, combinator_1.some)((0, combinator_1.union)([exports.attribute])), (0, source_1.str)(/^[^\S\n]*>/), true, ([as, bs = [], cs], rest) => [[elem(as[0].slice(1), (0, array_1.push)((0, array_1.unshift)(as, bs), cs), [], [])], rest], undefined, [3 | 16 /* Backtrack.linebracket */]), (0, combinator_1.match)(new RegExp(String.raw`^<(${TAGS.join('|')})(?=[^\S\n]|>)`), (0, memoize_1.memoize)(([, tag]) => (0, combinator_1.surround)((0, combinator_1.surround)((0, source_1.str)(`<${tag}`), (0, combinator_1.some)(exports.attribute), (0, source_1.str)(/^[^\S\n]*>/), true, undefined, undefined, [3 | 16 /* Backtrack.linebracket */]), (0, combinator_1.precedence)(3, (0, combinator_1.recursion)(4 /* Recursion.inline */, (0, combinator_1.subsequence)([(0, combinator_1.focus)(/^[^\S\n]*\n/, (0, combinator_1.some)(inline_1.inline)), (0, combinator_1.some)((0, combinator_1.open)(/^\n?/, (0, combinator_1.some)(inline_1.inline, (0, visibility_1.blankWith)('\n', `</${tag}>`), [[(0, visibility_1.blankWith)('\n', `</${tag}>`), 3]]), true))]))), (0, source_1.str)(`</${tag}>`), true, ([as, bs = [], cs], rest) => [[elem(tag, as, bs, cs)], rest], ([as, bs = []], rest) => [[elem(tag, as, bs, [])], rest]), ([, tag]) => tag, new Map())), (0, combinator_1.match)(/^<([a-z]+)(?=[^\S\n]|>)/i, (0, memoize_1.memoize)(([, tag]) => (0, combinator_1.surround)((0, combinator_1.surround)((0, source_1.str)(`<${tag}`), (0, combinator_1.some)(exports.attribute), (0, source_1.str)(/^[^\S\n]*>/), true, undefined, undefined, [3 | 16 /* Backtrack.linebracket */]), (0, combinator_1.precedence)(3, (0, combinator_1.recursion)(4 /* Recursion.inline */, (0, combinator_1.subsequence)([(0, combinator_1.focus)(/^[^\S\n]*\n/, (0, combinator_1.some)(inline_1.inline)), (0, combinator_1.some)(inline_1.inline, `</${tag}>`, [[`</${tag}>`, 3]])]))), (0, source_1.str)(`</${tag}>`), true, ([as, bs = [], cs], rest) => [[elem(tag, as, bs, cs)], rest], ([as, bs = []], rest) => [[elem(tag, as, bs, [])], rest]), ([, tag]) => tag, new clock_1.Clock(10000)))])));
|
|
6625
6674
|
exports.attribute = (0, combinator_1.union)([(0, source_1.str)(/^[^\S\n]+[a-z]+(?:-[a-z]+)*(?:="[^"\n]*")?(?=[^\S\n]|>)/i)]);
|
|
6626
6675
|
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element
|
|
6627
6676
|
// [...document.querySelectorAll('tbody > tr > td:first-child')].map(el => el.textContent.slice(1, -1))
|
|
@@ -7090,14 +7139,14 @@ const visibility_1 = __webpack_require__(6364);
|
|
|
7090
7139
|
const util_1 = __webpack_require__(4992);
|
|
7091
7140
|
const array_1 = __webpack_require__(6876);
|
|
7092
7141
|
const dom_1 = __webpack_require__(394);
|
|
7093
|
-
exports.ruby = (0, combinator_1.lazy)(() => (0, combinator_1.fmap)((0, combinator_1.sequence)([(0, combinator_1.dup)((0, combinator_1.surround)('[', (0, source_1.str)(/^(?:\\[^\n]|[^\\[\](){}<>"\n])+/
|
|
7142
|
+
exports.ruby = (0, combinator_1.lazy)(() => (0, combinator_1.fmap)((0, combinator_1.sequence)([(0, combinator_1.dup)((0, combinator_1.surround)('[', (0, source_1.str)(/^(?:\\[^\n]|[^\\[\](){}<>"\n])+/), ']', false, ([, [source]], rest, context) => {
|
|
7094
7143
|
const ns = (0, parser_1.eval)(text({
|
|
7095
7144
|
source,
|
|
7096
7145
|
context
|
|
7097
7146
|
}), [undefined])[0];
|
|
7098
7147
|
ns && ns.at(-1) === '' && ns.pop();
|
|
7099
7148
|
return ns && (0, visibility_1.isTightNodeStart)(ns) ? [ns, rest] : undefined;
|
|
7100
|
-
}, undefined, [3 | 64 /* Backtrack.ruby */])), (0, combinator_1.dup)((0, combinator_1.surround)('(', (0, source_1.str)(/^(?:\\[^\n]|[^\\[\](){}<>"\n])+/
|
|
7149
|
+
}, undefined, [3 | 64 /* Backtrack.ruby */])), (0, combinator_1.dup)((0, combinator_1.surround)('(', (0, source_1.str)(/^(?:\\[^\n]|[^\\[\](){}<>"\n])+/), ')', false, ([, [source]], rest, context) => {
|
|
7101
7150
|
const ns = (0, parser_1.eval)(text({
|
|
7102
7151
|
source,
|
|
7103
7152
|
context
|
package/package.json
CHANGED
|
@@ -7,15 +7,12 @@ import { Parser, Input, Ctx, Node, Context, eval, exec, check } from '../../data
|
|
|
7
7
|
//}
|
|
8
8
|
|
|
9
9
|
export function validate<P extends Parser<unknown>>(patterns: string | RegExp | (string | RegExp)[], parser: P): P;
|
|
10
|
-
export function validate<P extends Parser<unknown>>(patterns: string | RegExp | (string | RegExp)[], has: string, parser: P): P;
|
|
11
10
|
export function validate<P extends Parser<unknown>>(cond: ((input: Input<Context<P>>) => boolean), parser: P): P;
|
|
12
|
-
export function validate<N>(patterns: string | RegExp | (string | RegExp)[] | ((input: Input<Ctx>) => boolean),
|
|
13
|
-
if (typeof patterns === 'function') return guard(patterns,
|
|
14
|
-
if (
|
|
15
|
-
if (!isArray(patterns)) return validate([patterns], has, parser!);
|
|
11
|
+
export function validate<N>(patterns: string | RegExp | (string | RegExp)[] | ((input: Input<Ctx>) => boolean), parser: Parser<N>): Parser<N> {
|
|
12
|
+
if (typeof patterns === 'function') return guard(patterns, parser);
|
|
13
|
+
if (!isArray(patterns)) return validate([patterns], parser);
|
|
16
14
|
assert(patterns.length > 0);
|
|
17
15
|
assert(patterns.every(pattern => pattern instanceof RegExp ? !pattern.flags.match(/[gmy]/) && pattern.source.startsWith('^') : true));
|
|
18
|
-
assert(parser = parser!);
|
|
19
16
|
const match: (source: string) => boolean = global.eval([
|
|
20
17
|
'source =>',
|
|
21
18
|
patterns.map(pattern =>
|
|
@@ -10,7 +10,7 @@ export function match<N>(pattern: RegExp, f: (matched: RegExpMatchArray) => Pars
|
|
|
10
10
|
const param = source.match(pattern);
|
|
11
11
|
if (!param) return;
|
|
12
12
|
assert(source.startsWith(param[0]));
|
|
13
|
-
cost && consume(param.length, context);
|
|
13
|
+
cost && consume(param[0].length, context);
|
|
14
14
|
const result = f(param)(input);
|
|
15
15
|
assert(check(source, result, false));
|
|
16
16
|
if (result === undefined) return;
|
|
@@ -51,7 +51,6 @@ export function surround<N>(
|
|
|
51
51
|
case 'object':
|
|
52
52
|
closer = match(closer);
|
|
53
53
|
}
|
|
54
|
-
const statesize = 2;
|
|
55
54
|
return ({ source, context }) => {
|
|
56
55
|
const sme_ = source;
|
|
57
56
|
if (sme_ === '') return;
|
|
@@ -62,19 +61,7 @@ export function surround<N>(
|
|
|
62
61
|
if (resultS === undefined) return void revert(context, linebreak);
|
|
63
62
|
const nodesS = eval(resultS);
|
|
64
63
|
const me_ = exec(resultS);
|
|
65
|
-
|
|
66
|
-
if (backtrack & 1) {
|
|
67
|
-
const { backtracks = {}, backtrack: state = 0, offset = 0 } = context;
|
|
68
|
-
for (let i = 0; i < source.length - me_.length; ++i) {
|
|
69
|
-
if (source[i] !== source[0]) break;
|
|
70
|
-
const pos = source.length - i + offset - 1;
|
|
71
|
-
assert(pos >= 0);
|
|
72
|
-
if (!(pos in backtracks)) continue;
|
|
73
|
-
const shift = backtrack >>> statesize & state >>> statesize ? state & (1 << statesize) - 1 : 0;
|
|
74
|
-
if (backtracks[pos] & 1 << size(backtrack >>> statesize) + shift) return void revert(context, linebreak);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
64
|
+
if (getBacktrack(context, backtracks, sme_, me_)) return void revert(context, linebreak);
|
|
78
65
|
const { backtrack = 0 } = context;
|
|
79
66
|
context.backtrack = backtrack | backtrackstate;
|
|
80
67
|
const resultM = me_ !== '' ? parser({ source: me_, context }) : undefined;
|
|
@@ -82,21 +69,13 @@ export function surround<N>(
|
|
|
82
69
|
context.backtrack = backtrack;
|
|
83
70
|
const nodesM = eval(resultM);
|
|
84
71
|
const e_ = exec(resultM, me_);
|
|
85
|
-
|
|
86
|
-
const resultE = closer({ source: e_, context });
|
|
72
|
+
const resultE = nodesM || optional ? closer({ source: e_, context }) : undefined;
|
|
87
73
|
assert(check(e_, resultE, false));
|
|
88
74
|
const nodesE = eval(resultE);
|
|
89
75
|
const rest = exec(resultE, e_);
|
|
76
|
+
nodesE || setBacktrack(context, backtracks, sme_);
|
|
77
|
+
if (!nodesM && !optional) return void revert(context, linebreak);
|
|
90
78
|
if (rest.length === sme_.length) return void revert(context, linebreak);
|
|
91
|
-
for (const backtrack of backtracks) {
|
|
92
|
-
if (backtrack & 2 && nodesE === undefined) {
|
|
93
|
-
const { backtracks = {}, backtrack: state = 0, offset = 0 } = context;
|
|
94
|
-
const pos = source.length + offset - 1;
|
|
95
|
-
assert(pos >= 0);
|
|
96
|
-
const shift = backtrack >>> statesize & state >>> statesize ? state & (1 << statesize) - 1 : 0;
|
|
97
|
-
backtracks[pos] |= 1 << size(backtrack >>> statesize) + shift;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
79
|
context.recent = [
|
|
101
80
|
sme_.slice(0, sme_.length - me_.length),
|
|
102
81
|
me_.slice(0, me_.length - e_.length),
|
|
@@ -118,15 +97,76 @@ export function surround<N>(
|
|
|
118
97
|
return result;
|
|
119
98
|
};
|
|
120
99
|
}
|
|
121
|
-
export function open<P extends Parser<unknown>>(
|
|
122
|
-
|
|
123
|
-
|
|
100
|
+
export function open<P extends Parser<unknown>>(
|
|
101
|
+
opener: string | RegExp | Parser<Node<P>, Context<P>>,
|
|
102
|
+
parser: P,
|
|
103
|
+
optional?: boolean,
|
|
104
|
+
backtracks?: readonly number[],
|
|
105
|
+
backtrackstate?: number,
|
|
106
|
+
): P;
|
|
107
|
+
export function open<N>(
|
|
108
|
+
opener: string | RegExp | Parser<N>,
|
|
109
|
+
parser: Parser<N>,
|
|
110
|
+
optional?: boolean,
|
|
111
|
+
backtracks?: readonly number[],
|
|
112
|
+
backtrackstate?: number,
|
|
113
|
+
): Parser<N> {
|
|
114
|
+
return surround(opener, parser, '', optional, undefined, undefined, backtracks, backtrackstate);
|
|
124
115
|
}
|
|
125
|
-
export function close<P extends Parser<unknown>>(
|
|
126
|
-
|
|
127
|
-
|
|
116
|
+
export function close<P extends Parser<unknown>>(
|
|
117
|
+
parser: P,
|
|
118
|
+
closer: string | RegExp | Parser<Node<P>, Context<P>>,
|
|
119
|
+
optional?: boolean,
|
|
120
|
+
backtracks?: readonly number[],
|
|
121
|
+
backtrackstate?: number,
|
|
122
|
+
): P;
|
|
123
|
+
export function close<N>(
|
|
124
|
+
parser: Parser<N>,
|
|
125
|
+
closer: string | RegExp | Parser<N>,
|
|
126
|
+
optional?: boolean,
|
|
127
|
+
backtracks?: readonly number[],
|
|
128
|
+
backtrackstate?: number,
|
|
129
|
+
): Parser<N> {
|
|
130
|
+
return surround('', parser, closer, optional, undefined, undefined, backtracks, backtrackstate);
|
|
128
131
|
}
|
|
129
132
|
|
|
133
|
+
const statesize = 2;
|
|
134
|
+
export function getBacktrack(
|
|
135
|
+
context: Ctx,
|
|
136
|
+
backtracks: readonly number[],
|
|
137
|
+
sme_: string,
|
|
138
|
+
me_: string,
|
|
139
|
+
): boolean {
|
|
140
|
+
for (const backtrack of backtracks) {
|
|
141
|
+
if (backtrack & 1) {
|
|
142
|
+
const { backtracks = {}, backtrack: state = 0, offset = 0 } = context;
|
|
143
|
+
for (let i = 0, len = sme_.length - me_.length || 1; i < len; ++i) {
|
|
144
|
+
if (sme_[i] !== sme_[0]) break;
|
|
145
|
+
const pos = sme_.length - i + offset - 1;
|
|
146
|
+
assert(pos >= 0);
|
|
147
|
+
if (!(pos in backtracks)) continue;
|
|
148
|
+
const shift = backtrack >>> statesize & state >>> statesize ? state & (1 << statesize) - 1 : 0;
|
|
149
|
+
if (backtracks[pos] & 1 << size(backtrack >>> statesize) + shift) return true;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
export function setBacktrack(
|
|
156
|
+
context: Ctx,
|
|
157
|
+
backtracks: readonly number[],
|
|
158
|
+
sme_: string,
|
|
159
|
+
): void {
|
|
160
|
+
for (const backtrack of backtracks) {
|
|
161
|
+
if (backtrack & 2) {
|
|
162
|
+
const { backtracks = {}, backtrack: state = 0, offset = 0 } = context;
|
|
163
|
+
const pos = sme_.length + offset - 1;
|
|
164
|
+
assert(pos >= 0);
|
|
165
|
+
const shift = backtrack >>> statesize & state >>> statesize ? state & (1 << statesize) - 1 : 0;
|
|
166
|
+
backtracks[pos] |= 1 << size(backtrack >>> statesize) + shift;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
130
170
|
function match(pattern: string | RegExp): (input: Input) => [never[], string] | undefined {
|
|
131
171
|
switch (typeof pattern) {
|
|
132
172
|
case 'string':
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { Ctx } from '../../parser';
|
|
2
|
-
import { memoize } from 'spica/memoize';
|
|
3
2
|
|
|
4
3
|
interface Delimiter {
|
|
5
4
|
readonly memory: Delimiter[];
|
|
6
5
|
readonly index: number;
|
|
7
|
-
readonly signature: string;
|
|
6
|
+
readonly signature: number | string;
|
|
8
7
|
readonly matcher: (source: string) => boolean | undefined;
|
|
9
8
|
readonly precedence: number;
|
|
10
9
|
readonly linebreakable: boolean;
|
|
@@ -12,11 +11,18 @@ interface Delimiter {
|
|
|
12
11
|
}
|
|
13
12
|
|
|
14
13
|
export class Delimiters {
|
|
15
|
-
|
|
14
|
+
// 手間を惜しまなければ規定のパターンはすべて配列のインデクスに変換可能。
|
|
15
|
+
public static signature(pattern: string | RegExp | undefined, linebreakable: boolean): number | string {
|
|
16
16
|
switch (typeof pattern) {
|
|
17
17
|
case 'undefined':
|
|
18
|
-
return
|
|
18
|
+
return +linebreakable;
|
|
19
19
|
case 'string':
|
|
20
|
+
assert(pattern !== '\x00');
|
|
21
|
+
if (pattern.length === 1) {
|
|
22
|
+
const code = pattern.charCodeAt(0);
|
|
23
|
+
// 使用中のパターンの8ビット目が空いてるのでひとまずこうしとく
|
|
24
|
+
if ((code & 1 << 7) === 0) return code | +linebreakable << 7;
|
|
25
|
+
}
|
|
20
26
|
return `s:${pattern}:${+linebreakable}`;
|
|
21
27
|
case 'object':
|
|
22
28
|
return `r/${pattern.source}/${+linebreakable}`;
|
|
@@ -32,24 +38,37 @@ export class Delimiters {
|
|
|
32
38
|
return source => pattern.test(source) || undefined;
|
|
33
39
|
}
|
|
34
40
|
}
|
|
35
|
-
private readonly
|
|
41
|
+
private readonly heap: Record<number, Delimiter[]> = {};
|
|
42
|
+
private readonly map: Map<string, Delimiter[]> = new Map();
|
|
43
|
+
private registry(signature: number | string): Delimiter[] {
|
|
44
|
+
if (typeof signature === 'number') {
|
|
45
|
+
return this.heap[signature] ??= [];
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
const ds = this.map.get(signature);
|
|
49
|
+
if (ds) return ds;
|
|
50
|
+
const blank: Delimiter[] = [];
|
|
51
|
+
this.map.set(signature, blank);
|
|
52
|
+
return blank;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
36
55
|
private readonly delimiters: Delimiter[] = [];
|
|
37
56
|
private readonly stack: number[] = [];
|
|
38
57
|
private readonly states: (readonly number[])[] = [];
|
|
39
58
|
public push(
|
|
40
59
|
delims: readonly {
|
|
41
|
-
readonly signature: string;
|
|
60
|
+
readonly signature: number | string;
|
|
42
61
|
readonly matcher: (source: string) => boolean | undefined;
|
|
43
62
|
readonly precedence: number;
|
|
44
63
|
readonly linebreakable: boolean;
|
|
45
64
|
}[]
|
|
46
65
|
): void {
|
|
47
|
-
const {
|
|
66
|
+
const { delimiters, stack } = this;
|
|
48
67
|
// シグネチャ数以下
|
|
49
68
|
assert(delimiters.length < 100);
|
|
50
69
|
for (let i = 0; i < delims.length; ++i) {
|
|
51
70
|
const { signature, matcher, precedence, linebreakable } = delims[i];
|
|
52
|
-
const memory = registry(signature);
|
|
71
|
+
const memory = this.registry(signature);
|
|
53
72
|
const index = memory[0]?.index ?? delimiters.length;
|
|
54
73
|
assert(memory.length === 0 || precedence === delimiters[index].precedence);
|
|
55
74
|
if (memory.length === 0) {
|
|
@@ -350,20 +350,22 @@ describe('Unit: parser/api/parse', () => {
|
|
|
350
350
|
|
|
351
351
|
it('backtrack', function () {
|
|
352
352
|
this.timeout(5000);
|
|
353
|
-
|
|
353
|
+
// 9n = template + link + annotation/reference + link + link +
|
|
354
|
+
// code + url + ruby + text
|
|
355
|
+
const source = `${'.'.repeat(2 + 0)}{{(([[[\`http://[${'.'.repeat(11103)}`;
|
|
354
356
|
assert.deepStrictEqual(
|
|
355
|
-
[...parse(
|
|
356
|
-
[`<p>${
|
|
357
|
+
[...parse(source).children].map(el => el.outerHTML.replace(/:\w+/, ':rnd')),
|
|
358
|
+
[`<p>${source}</p>`]);
|
|
357
359
|
});
|
|
358
360
|
|
|
359
361
|
it('backtrack error', function () {
|
|
360
362
|
this.timeout(5000);
|
|
361
|
-
const
|
|
363
|
+
const source = `${'.'.repeat(2 + 1)}{{(([[[\`http://[${'.'.repeat(11103)}`;
|
|
362
364
|
assert.deepStrictEqual(
|
|
363
|
-
[...parse(
|
|
365
|
+
[...parse(source).children].map(el => el.outerHTML.replace(/:\w+/, ':rnd')),
|
|
364
366
|
[
|
|
365
367
|
'<h1 id="error:rnd" class="error">Error: Too many creations</h1>',
|
|
366
|
-
`<pre class="error" translate="no">${
|
|
368
|
+
`<pre class="error" translate="no">${source.slice(0, 1000 - 3)}...</pre>`,
|
|
367
369
|
]);
|
|
368
370
|
});
|
|
369
371
|
|
package/src/parser/context.ts
CHANGED
|
@@ -4,15 +4,17 @@ import { union, tails, some, recursion, precedence, state, constraint, validate,
|
|
|
4
4
|
import { unsafelink } from '../link';
|
|
5
5
|
import { linebreak, unescsource, str } from '../../source';
|
|
6
6
|
|
|
7
|
-
const closer = /^[-+*=~^_,.;:!?]*(?=[\\"`|\[\](){}<>]|$)/;
|
|
7
|
+
const closer = /^[-+*=~^_,.;:!?]*(?=[\\"`|\[\](){}<>]|[^\x21-\x7E]|$)/;
|
|
8
8
|
|
|
9
9
|
export const url: AutolinkParser.UrlParser = lazy(() => validate(['http://', 'https://'], rewrite(
|
|
10
10
|
open(
|
|
11
11
|
/^https?:\/\/(?=[\x21-\x7E])/,
|
|
12
|
-
|
|
12
|
+
precedence(1, some(union([
|
|
13
13
|
verify(bracket, ns => ns.length > 0),
|
|
14
14
|
some(unescsource, closer),
|
|
15
|
-
])))
|
|
15
|
+
]), undefined, [[/^[^\x21-\x7E]/, 3]])),
|
|
16
|
+
false,
|
|
17
|
+
[3 | Backtrack.url]),
|
|
16
18
|
union([
|
|
17
19
|
constraint(State.autolink, false, state(State.autolink, convert(
|
|
18
20
|
url => `{ ${url} }`,
|
|
@@ -34,15 +36,17 @@ export const lineurl: AutolinkParser.UrlParser.LineUrlParser = lazy(() => open(
|
|
|
34
36
|
false))),
|
|
35
37
|
({ source }) => [[source], ''],
|
|
36
38
|
]),
|
|
37
|
-
]))
|
|
39
|
+
])),
|
|
40
|
+
false,
|
|
41
|
+
[3 | Backtrack.linebracket]));
|
|
38
42
|
|
|
39
43
|
const bracket: AutolinkParser.UrlParser.BracketParser = lazy(() => union([
|
|
40
44
|
surround(str('('), recursion(Recursion.terminal, some(union([bracket, unescsource]), ')')), str(')'), true,
|
|
41
|
-
undefined, () => [[], ''], [3 | Backtrack.
|
|
45
|
+
undefined, () => [[], ''], [3 | Backtrack.url]),
|
|
42
46
|
surround(str('['), recursion(Recursion.terminal, some(union([bracket, unescsource]), ']')), str(']'), true,
|
|
43
|
-
undefined, () => [[], ''], [3 | Backtrack.
|
|
47
|
+
undefined, () => [[], ''], [3 | Backtrack.url]),
|
|
44
48
|
surround(str('{'), recursion(Recursion.terminal, some(union([bracket, unescsource]), '}')), str('}'), true,
|
|
45
|
-
undefined, () => [[], ''], [3 | Backtrack.
|
|
49
|
+
undefined, () => [[], ''], [3 | Backtrack.url]),
|
|
46
50
|
surround(str('"'), precedence(2, recursion(Recursion.terminal, some(unescsource, '"'))), str('"'), true,
|
|
47
|
-
undefined, () => [[], ''], [3 | Backtrack.
|
|
51
|
+
undefined, () => [[], ''], [3 | Backtrack.url]),
|
|
48
52
|
]));
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { CodeParser } from '../inline';
|
|
2
|
-
import { match } from '../../combinator';
|
|
2
|
+
import { validate, getBacktrack, setBacktrack, match } from '../../combinator';
|
|
3
|
+
import { Backtrack } from '../context';
|
|
3
4
|
import { html } from 'typed-dom/dom';
|
|
4
5
|
|
|
5
|
-
export const code: CodeParser =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
export const code: CodeParser = validate(
|
|
7
|
+
({ source, context }) =>
|
|
8
|
+
source[0] === '`' &&
|
|
9
|
+
!getBacktrack(context, [1 | Backtrack.linebracket], source, source.slice(1)),
|
|
10
|
+
match(
|
|
11
|
+
/^(`+)(?!`)([^\n]*?)(?:((?<!`)\1(?!`))|$|\n)/,
|
|
12
|
+
([whole, , body, closer]) => ({ source, context }) =>
|
|
13
|
+
closer
|
|
14
|
+
? [[html('code', { 'data-src': whole }, format(body))], source.slice(whole.length)]
|
|
15
|
+
: void setBacktrack(context, [2 | Backtrack.linebracket], source),
|
|
16
|
+
true));
|
|
10
17
|
|
|
11
18
|
function format(text: string): string {
|
|
12
19
|
assert(text.length > 0);
|
|
@@ -28,7 +28,8 @@ const subemphasis: IntermediateParser<EmphasisParser> = lazy(() => some(union([
|
|
|
28
28
|
])),
|
|
29
29
|
])));
|
|
30
30
|
|
|
31
|
-
//
|
|
31
|
+
// 開閉が明示的でない構文は開閉の不明確な記号による再帰的適用を行わず
|
|
32
|
+
// 可能な限り早く閉じるよう解析しなければならない。
|
|
32
33
|
// このため終端記号の後ろを見て終端を中止し同じ構文を再帰的に適用してはならない。
|
|
33
34
|
export const emstrong: EmStrongParser = lazy(() => validate('***',
|
|
34
35
|
precedence(0, repeat('***', surround(
|
|
@@ -3,6 +3,12 @@ import { union, focus, surround } from '../../../combinator';
|
|
|
3
3
|
import { signature } from './index';
|
|
4
4
|
import { html } from 'typed-dom/dom';
|
|
5
5
|
|
|
6
|
+
// インデクスの重複解消は不要な重複を削除するのが最もよい。
|
|
7
|
+
// 複合生成インデクスは参照と同期させることが困難であり
|
|
8
|
+
// 複合生成インデクスを手動で同期させるより最初から重複のない
|
|
9
|
+
// テキストまたはインデクスを付けて同期が必要な機会を減らすのが
|
|
10
|
+
// 継続的編集において最も簡便となる。
|
|
11
|
+
|
|
6
12
|
export const indexer: ExtensionParser.IndexerParser = surround(
|
|
7
13
|
/^\s+\[(?=\|\S)/,
|
|
8
14
|
union([
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { HTMLParser } from '../inline';
|
|
2
|
-
import { Recursion } from '../context';
|
|
2
|
+
import { Recursion, Backtrack } from '../context';
|
|
3
3
|
import { union, subsequence, some, recursion, precedence, validate, focus, surround, open, match, lazy } from '../../combinator';
|
|
4
4
|
import { inline } from '../inline';
|
|
5
5
|
import { str } from '../source';
|
|
@@ -30,13 +30,17 @@ export const html: HTMLParser = lazy(() => validate(/^<[a-z]+(?=[^\S\n]|>)/i,
|
|
|
30
30
|
some(union([attribute])),
|
|
31
31
|
str(/^[^\S\n]*>/), true,
|
|
32
32
|
([as, bs = [], cs], rest) =>
|
|
33
|
-
[[elem(as[0].slice(1), push(unshift(as, bs), cs), [], [])], rest]
|
|
33
|
+
[[elem(as[0].slice(1), push(unshift(as, bs), cs), [], [])], rest],
|
|
34
|
+
undefined,
|
|
35
|
+
[3 | Backtrack.linebracket]),
|
|
34
36
|
match(
|
|
35
37
|
new RegExp(String.raw`^<(${TAGS.join('|')})(?=[^\S\n]|>)`),
|
|
36
38
|
memoize(
|
|
37
39
|
([, tag]) =>
|
|
38
40
|
surround<HTMLParser.TagParser, string>(
|
|
39
|
-
surround(
|
|
41
|
+
surround(
|
|
42
|
+
str(`<${tag}`), some(attribute), str(/^[^\S\n]*>/), true,
|
|
43
|
+
undefined, undefined, [3 | Backtrack.linebracket]),
|
|
40
44
|
precedence(3, recursion(Recursion.inline,
|
|
41
45
|
subsequence([
|
|
42
46
|
focus(/^[^\S\n]*\n/, some(inline)),
|
|
@@ -53,8 +57,10 @@ export const html: HTMLParser = lazy(() => validate(/^<[a-z]+(?=[^\S\n]|>)/i,
|
|
|
53
57
|
/^<([a-z]+)(?=[^\S\n]|>)/i,
|
|
54
58
|
memoize(
|
|
55
59
|
([, tag]) =>
|
|
56
|
-
surround<HTMLParser.TagParser, string>(
|
|
57
|
-
|
|
60
|
+
surround<HTMLParser.TagParser, string>(
|
|
61
|
+
surround(
|
|
62
|
+
str(`<${tag}`), some(attribute), str(/^[^\S\n]*>/), true,
|
|
63
|
+
undefined, undefined, [3 | Backtrack.linebracket]),
|
|
58
64
|
precedence(3, recursion(Recursion.inline,
|
|
59
65
|
subsequence([
|
|
60
66
|
focus(/^[^\S\n]*\n/, some(inline)),
|
|
@@ -12,7 +12,7 @@ import { html, defrag } from 'typed-dom/dom';
|
|
|
12
12
|
export const ruby: RubyParser = lazy(() => fmap(
|
|
13
13
|
sequence([
|
|
14
14
|
dup(surround(
|
|
15
|
-
'[', str(/^(?:\\[^\n]|[^\\[\](){}<>"\n])+/
|
|
15
|
+
'[', str(/^(?:\\[^\n]|[^\\[\](){}<>"\n])+/), ']',
|
|
16
16
|
false,
|
|
17
17
|
([, [source]], rest, context) => {
|
|
18
18
|
const ns = eval(text({ source, context }), [undefined])[0];
|
|
@@ -22,7 +22,7 @@ export const ruby: RubyParser = lazy(() => fmap(
|
|
|
22
22
|
undefined,
|
|
23
23
|
[3 | Backtrack.ruby])),
|
|
24
24
|
dup(surround(
|
|
25
|
-
'(', str(/^(?:\\[^\n]|[^\\[\](){}<>"\n])+/
|
|
25
|
+
'(', str(/^(?:\\[^\n]|[^\\[\](){}<>"\n])+/), ')',
|
|
26
26
|
false,
|
|
27
27
|
([, [source]], rest, context) => {
|
|
28
28
|
const ns = eval(text({ source, context }), [undefined])[0];
|
|
@@ -70,6 +70,7 @@ describe('Unit: parser/inline', () => {
|
|
|
70
70
|
assert.deepStrictEqual(inspect(parser('***a*b*c*')), [['**', '<em>a</em>', 'b', '<em>c</em>'], '']);
|
|
71
71
|
assert.deepStrictEqual(inspect(parser('***a*b*c**')), [['**', '<em>a</em>', 'b', '<em>c</em>', '*'], '']);
|
|
72
72
|
assert.deepStrictEqual(inspect(parser('***a*b*c***')), [['<strong><em>a</em>b<em>c</em></strong>'], '']);
|
|
73
|
+
assert.deepStrictEqual(inspect(parser('***a**b**c***')), [['<em><strong>a</strong>b<strong>c</strong></em>'], '']);
|
|
73
74
|
assert.deepStrictEqual(inspect(parser('*(*a*)*')), [['<em><span class="paren">(<em>a</em>)</span></em>'], '']);
|
|
74
75
|
assert.deepStrictEqual(inspect(parser('**(**a**)**')), [['<strong><span class="paren">(<strong>a</strong>)</span></strong>'], '']);
|
|
75
76
|
assert.deepStrictEqual(inspect(parser('*[*]')), [['*', '[', '*', ']'], '']);
|
package/src/parser/inline.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { MarkdownParser } from '../../markdown';
|
|
2
|
-
import { union, lazy } from '../combinator';
|
|
2
|
+
import { union, verify, lazy } from '../combinator';
|
|
3
3
|
import { annotation } from './inline/annotation';
|
|
4
4
|
import { reference } from './inline/reference';
|
|
5
5
|
import { template } from './inline/template';
|
|
@@ -47,10 +47,12 @@ export import ShortMediaParser = InlineParser.ShortMediaParser;
|
|
|
47
47
|
export import BracketParser = InlineParser.BracketParser;
|
|
48
48
|
export import AutolinkParser = InlineParser.AutolinkParser;
|
|
49
49
|
|
|
50
|
-
export const inline: InlineParser = lazy(() => union([
|
|
50
|
+
export const inline: InlineParser = lazy(() => verify(union([
|
|
51
51
|
input => {
|
|
52
|
-
const { source } = input;
|
|
52
|
+
const { source, context } = input;
|
|
53
53
|
if (source === '') return;
|
|
54
|
+
context.depth ??= 0;
|
|
55
|
+
++context.depth;
|
|
54
56
|
switch (source.slice(0, 2)) {
|
|
55
57
|
case '((':
|
|
56
58
|
return annotation(input);
|
|
@@ -104,7 +106,20 @@ export const inline: InlineParser = lazy(() => union([
|
|
|
104
106
|
bracket,
|
|
105
107
|
autolink,
|
|
106
108
|
text
|
|
107
|
-
]))
|
|
109
|
+
]), (_, rest, context) => {
|
|
110
|
+
--context.depth!;
|
|
111
|
+
assert([rest]);
|
|
112
|
+
// ヒープを効率的に削除可能な場合は削除する。
|
|
113
|
+
// ヒープサイズは括弧類など特定の構文が完成しなかった場合にしか増加しないため
|
|
114
|
+
// ブロックごとに平均数ノード以下となることから削除せずとも平均的にはあまり影響はない。
|
|
115
|
+
//if (context.depth === 0) {
|
|
116
|
+
// const { backtracks } = context;
|
|
117
|
+
// while (backtracks.peek()?.key! > rest.length) {
|
|
118
|
+
// backtracks.extract();
|
|
119
|
+
// }
|
|
120
|
+
//}
|
|
121
|
+
return true;
|
|
122
|
+
})) as any;
|
|
108
123
|
|
|
109
124
|
export { indexee } from './inline/extension/indexee';
|
|
110
125
|
export { indexer } from './inline/extension/indexer';
|