Skip to content
Back to skills

Megaparsec

ASecurity

Parse complex text such as command output, delimited or escaped fields, and numbers with units by writing megaparsec parsers instead of hand-written splitting with breakOn, span, or manual recursion. Use when writing or reviewing Haskell code that parses or extracts values from structured text.

  • 3 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 3, 2026
ai-agents

Security analysis

A100/100

Scanned December 3, 2026

npx -y skills add ncaq/konoka --skill megaparsec --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Megaparsec?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Megaparsec
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ncaq-megaparsec/badge)](https://www.skillsdirectory.com/skills/ncaq-megaparsec)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: megaparsec
description: Parse complex text such as command output, delimited or escaped fields, and numbers with units by writing megaparsec parsers instead of hand-written splitting with breakOn, span, or manual recursion. Use when writing or reviewing Haskell code that parses or extracts values from structured text.
user-invocable: false
---

# 複雑な文字列のパースはmegaparsecで書く

複雑な文字列をパースするときは、
[megaparsec](https://hackage.haskell.org/package/megaparsec)でパーサを書いてください。
`Data.Text`の`breakOn`、`breakOnEnd`、`splitOn`、`span`などを組み合わせた手書きの切り出しや、
エスケープを追いかける手書きの再帰で済ませません。

手書きの切り出しは、書いたときに想定した形の入力しか考えていないことが多いです。
後ろに行が続く、区切りの文字が値の中にも現れる、といった少し違う形の入力に対して、
失敗せずに誤った値を返しがちです。
パーサなら入力の形を文法として書き下せるので、想定と違う入力は失敗として扱えます。
失敗したときは、どこで何を期待していたのかがエラーに残ります。

megaparsecがまだ依存関係に無いことは、手書きで済ませる理由にしません。
cabalファイルの`build-depends`に追加してください。

## 対象

以下のような文字列はmegaparsecのパーサにします。

- 外部のコマンドが人に読ませるために出力する行。
- 区切りやエスケープのある欄。
- 単位付きの数値のように、複数の要素が組み合わさった値。

以下は対象外です。

- 完全一致の`case`や、`readMaybe`のような既存の関数だけで読める単純な値。
- JSONのように、専用のライブラリがある形式。
  外部のコマンドが`--json`のような機械向けの出力形式を持っている場合は、
  人向けの出力をパースせずにそちらを使います。

## 書き方

以下のようにimportして、パーサの型の別名を定義します。

```haskell
import Text.Megaparsec hiding (many, some)
import Text.Megaparsec.Char
import Text.Megaparsec.Char.Lexer qualified as L

type Parser = Parsec Void Text
```

`Text.Megaparsec`が再exportする`many`と`some`は、
`Control.Applicative`の`many`と`some`と名前が衝突します。
[haskell-tasuke:himari](../himari/SKILL.md)のように`Control.Applicative`を再exportするPreludeを使っている場合は、
Preludeの側を絞らずに、`Text.Megaparsec`の側で`hiding`します。

`Text.Megaparsec.Char.Lexer`は`space`などが`Text.Megaparsec.Char`と衝突するので、
qualified importするか、使う関数だけを列挙してimportします。

入力の全体を読むパーサは、最後に`eof`を置きます。
読み残しを黙って捨てずに、想定外の入力として失敗させるためです。

`runParser`が返す`Left`の`ParseErrorBundle`は握り潰さずに、
例外の型に包んで呼び出し側へ伝播させます。
人に見せるときは、`errorBundlePretty`で位置と期待したものを含む説明に整形できます。
例外の型の付け方は[haskell-tasuke:exception](../exception/SKILL.md)に従います。

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.