少し前にHaskellを勉強中にマークアップ言語を作ったので紹介します。
本当はすぐにアウトプットとして記事にしたかったのですが、
私にとってHaskellは難解で、モナドと格闘しているうちに季節が2回変わりました。
決して、先延ばし癖のせいではありません( ˘ω˘ )
クオリティはコンパイラ(笑)ですがご容赦ください。
使い方
# クローン
git clone https://github.com/ryotarofr/zin_lang.git
# ビルド
cabal build
# インストール
cabal install
# 使用例
zin-compiler mock/example_simple.zin output.html
サンプルコード
拡張子は「zin」です。
h1 bold[0:15] : Getting Started with Zin
p : Welcome to the zin markup language documentation.
: This guide will help you understand all the features.
h2 : Text Styling Examples
p bold[0:4] italic[5:9] strike[10:16] : Bold text sample here
p link[0:9] url[https://zin-lang.org] : Visit our website for more information.
ul : Simple and intuitive syntax
: Powerful styling capabilities
: Multiple output formats supported
cb lang[python]:
: def fibonacci(n)
: if n <= 1:
: return n
: return fibonacci(n- fibonacci(n-2))
t : | Framework | Performance | Learning Curve |
r : | React | High | Medium |
r : | Vue | High | Low |
r : | Svelte | Very High | Low |
q italic[0:8] : Remember: simplicity is the ultimate sophistication.
cssは適当に作ってください。
↓↓ サンプルソースをスタイルなしで生成した場合は次のような見た目になります。
まあ、動けばいいかくらいの感覚でやってます。
アーキテクチャ
Zin コンパイラのモジュール構成:
- Lexer - 字句解析(テキスト → トークン)
- Parser - 構文解析(トークン → AST)
- Semantic - 意味解析と妥当性検証
- CodeGen - HTML 生成(直接変換)
- VirtualDOM - 仮想 DOM 経由での変換
- Renderer - レンダリング機能
zin → Lexer → Parser → Semantic → CodeGen/VirtualDOM → HTML
基本構文
タグ
対応してるhtmlタグ
h1,
h2,
h3,
h4,
p,
ul,
ol,
li,
table,
tr,
td,
th,
strong,
em,
del,
a,
blockquote,
pre,
code,
エラー通知
構文エラーがある場合はコンパイル時に通知してくれます。
例えば、サンプルコードの1行目をh100に変えます。
h100 bold[0:15] : Getting Started with Zin
実行
$ zin-compiler mock/example_simple.zin output.htm
Compiling mock/example_simple.zin -> output.htm
mock/example_simple.zin:1:1: error: Unexpected token: Text "h100 bold[0:15] : Getting Started with Zin"
コンパイラについて
コンパイルの種類は4つ用意しています。
- FullCompilation -- 全体を毎回コンパイル
- IncrementalOnly -- 変更部分のみコンパイル
- SmartIncremental -- 依存関係も追跡してコンパイル
- FastIncremental -- ハッシュベースでコンパイル(最速)
未完成ですが、今回は増分ビルド(差分のみをビルドするような仕組みを)検討しています。
余談
Haskellは難しいし、GHCは遅いし、実行速度もそんなに早くないが、
美しく、謎の中毒性がある言語です。挫折さえもで楽しむことができるのです。