Text.Parsec モジュールの正体
Text.Parsec モジュールのファイル
Text.Parsecモジュールのファイルの内容は次のようになる。インポートするモジュールと、エクスポートするデータ構造や関数を並べただけのものだ。それぞれの関数の実態の定義は、サブモジュールに分割されている。少し大きな自分用のプログラムを開発するときのお手本になる。
エクスポートされている名称は、パーサモナドとパーサコンビネータのものだ、これらの機能は Text.Parsec モジュールをインポートすることで使うことができる。これらの機能を知ることが、Parsec を活用する要となる。
また、Text.Parsec モジュールでインポートされているサブモジュールは次の5つだ。
import Text.Parsec.Pos import Text.Parsec.Error import Text.Parsec.Prim import Text.Parsec.Char import Text.Parsec.Combinator
これらは、機能的に分割されている。Pos モジュールはパーサへの入力の位置情報を扱う。Error モジュールは構文解析の際のエラーを処理する。Prim モジュールは Parsec の処理の心臓部。Prim は primitive の省略形だ。Char モジュールは基本的な文字のパーサを記述している。Combinater モジュールには基本的なパーサを組み合わせて機能を発揮させるパーサコンビネータが記述されている。
これらの機能分担を知っているとコードの解読の際の全体像が見える。
Text.Parsec モジュールのソース
{-# LANGUAGE Safe #-}
{-|
Module : Text.Parsec
Copyright : (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007
License : BSD-style (see the LICENSE file)
Maintainer : aslatter@gmail.com
Stability : provisional
Portability : portable
This module includes everything you need to get started writing a
parser.
By default this module is set up to parse character data. If you'd like
to parse the result of your own tokenizer you should start with the
following imports:
@
import Text.Parsec.Prim
import Text.Parsec.Combinator
@
Then you can implement your own version of 'satisfy' on top of the
'tokenPrim' primitive.
-}
module Text.Parsec
( -- * Parsers
ParsecT
, Parsec
, token
, tokens
, runParserT
, runParser
, parse
, parseTest
, getPosition
, getInput
, getState
, putState
, modifyState
-- * Combinators
, (<|>)
, (<?>)
, label
, labels
, try
, unexpected
, choice
, many
, many1
, skipMany
, skipMany1
, count
, between
, option
, optionMaybe
, optional
, sepBy
, sepBy1
, endBy
, endBy1
, sepEndBy
, sepEndBy1
, chainl
, chainl1
, chainr
, chainr1
, eof
, notFollowedBy
, manyTill
, lookAhead
, anyToken
-- * Character Parsing
, module Text.Parsec.Char
-- * Error messages
, ParseError
, errorPos
-- * Position
, SourcePos
, SourceName, Line, Column
, sourceName, sourceLine, sourceColumn
, incSourceLine, incSourceColumn
, setSourceLine, setSourceColumn, setSourceName
-- * Debugging
--
-- | As a more comprehensive alternative for debugging Parsec parsers,
-- there's also the [parsec-free](http://hackage.haskell.org/package/
parsec-free)
-- package.
--
, parserTrace, parserTraced
-- * Low-level operations
, manyAccum
, tokenPrim
, tokenPrimEx
, runPT
, unknownError
, sysUnExpectError
, mergeErrorReply
, getParserState
, setParserState
, updateParserState
, Stream (..)
, runParsecT
, mkPT
, runP
, Consumed (..)
, Reply (..)
, State (..)
, setPosition
, setInput
-- * Other stuff
, setState
, updateState
, parsecMap
, parserReturn
, parserBind
, parserFail
, parserZero
, parserPlus
) where
import Text.Parsec.Pos
import Text.Parsec.Error
import Text.Parsec.Prim
import Text.Parsec.Char
import Text.Parsec.Combinator