Writing INI parser
In this recipe, we will further build on the concepts of Functor, Applicative, and monad, and build a parser for the INI
file from scratch. We will write a simple parser, and define its Functor
, Applicative
, and monad
instances. Then, we will slowly build upon the concept to finally build an INI parser.
The INI
file is usually used as a configuration file. A typical INI
file contains the number of sections, each section representing a set of name-value assignments. A sample INI
file may look like this:
[Section] name1 = value1 name2 = value2 [Section2] name1 = value1 name3 = value3
How to do it...
- Create a new project
ini-parser
using thesimple
Stack template:
stack new ini-parser simple
- Open the file
src/Main.hs
for editing. - After the initial module header, add the following imports:
import Data.Functor import Control.Applicative import Control.Monad import Data.Map hiding (empty) import Data.Char
- Define...