~ubuntu-branches/ubuntu/maverick/haskell-configfile/maverick

« back to all changes in this revision

Viewing changes to src/Data/ConfigFile/Types.hs

  • Committer: Bazaar Package Importer
  • Author(s): John Goerzen
  • Date: 2007-03-08 14:22:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070308142203-j9niks7p5q8e9ycs
Tags: 1.0.1
Rebuild against newer MissingH.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{- arch-tag: ConfigParser types
 
2
Copyright (C) 2004-2005 John Goerzen <jgoerzen@complete.org>
 
3
 
 
4
This program is free software; you can redistribute it and/or modify
 
5
it under the terms of the GNU Lesser General Public License as published by
 
6
the Free Software Foundation; either version 2.1 of the License, or
 
7
(at your option) any later version.
 
8
 
 
9
This program is distributed in the hope that it will be useful,
 
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
GNU Lesser General Public License for more details.
 
13
 
 
14
You should have received a copy of the GNU Lesser General Public License
 
15
along with this program; if not, write to the Free Software
 
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
-}
 
18
 
 
19
{- |
 
20
   Module     : Data.ConfigFile.Types
 
21
   Copyright  : Copyright (C) 2004-2005 John Goerzen
 
22
   License    : GNU LGPL, version 2.1 or above
 
23
 
 
24
   Maintainer : John Goerzen <jgoerzen@complete.org> 
 
25
   Stability  : provisional
 
26
   Portability: portable
 
27
 
 
28
Internal types for "Data.ConfigFile".  This module is not intended to be
 
29
used directly by your programs.
 
30
 
 
31
Copyright (c) 2004 John Goerzen, jgoerzen\@complete.org
 
32
-}
 
33
 
 
34
module Data.ConfigFile.Types (
 
35
                                    CPOptions, CPData, 
 
36
                                    CPErrorData(..), CPError, {-CPResult,-}
 
37
                                    ConfigParser(..),
 
38
                                    SectionSpec,
 
39
                                    OptionSpec,
 
40
                                    ParseOutput
 
41
                                   ) where
 
42
import qualified Data.Map as Map
 
43
import Data.Char
 
44
import Control.Monad.Error
 
45
 
 
46
{- | Internal output from parser -}
 
47
type ParseOutput = [(String, [(String, String)])]
 
48
 
 
49
{- | Names of sections -}
 
50
type SectionSpec = String
 
51
 
 
52
{- | Names of options -}
 
53
type OptionSpec = String
 
54
 
 
55
{- | Storage of options. -}
 
56
type CPOptions = Map.Map OptionSpec String
 
57
 
 
58
{- | The main data storage type (storage of sections).
 
59
 
 
60
PLEASE NOTE: This type is exported only for use by other modules under
 
61
Data.ConfigFile.  You should NEVER access the FiniteMap in a ConfigParser
 
62
directly.  This type may change in future releases of MissingH, which could
 
63
break your programs.  Please retrict yourself to the interface in
 
64
'Data.ConfigFile'.
 
65
 -}
 
66
type CPData = Map.Map SectionSpec CPOptions
 
67
 
 
68
{- | Possible ConfigParser errors. -}
 
69
data CPErrorData = ParseError String        -- ^ Parse error
 
70
                 | SectionAlreadyExists SectionSpec -- ^ Attempt to create an already-existing ection
 
71
                 | NoSection SectionSpec    -- ^ The section does not exist
 
72
                 | NoOption OptionSpec      -- ^ The option does not exist
 
73
                 | OtherProblem String      -- ^ Miscellaneous error
 
74
                 | InterpolationError String -- ^ Raised by 'Data.ConfigFile.interpolatingAccess' if a request was made for a non-existant option
 
75
                   deriving (Eq, Ord, Show)
 
76
 
 
77
{- | Indicates an error occurred.  The String is an explanation of the location
 
78
of the error. -}
 
79
type CPError = (CPErrorData, String)
 
80
 
 
81
instance Error CPError where
 
82
    noMsg = (OtherProblem "", "")
 
83
    strMsg x = (OtherProblem x, "")
 
84
 
 
85
{- Removed due to Hugs incompatibility.
 
86
 
 
87
| Basic ConfigParser error handling.  The Left value indicates
 
88
an error, while a Right value indicates success.
 
89
type CPResult a = MonadError CPError m => m a
 
90
-}
 
91
 
 
92
{- | This is the main record that is used by 'Data.ConfigFile'.
 
93
-}
 
94
data ConfigParser = ConfigParser 
 
95
    { -- | The data itself
 
96
      content :: CPData,
 
97
      -- | How to transform an option into a standard representation
 
98
      optionxform :: (OptionSpec -> OptionSpec),
 
99
      -- | Function to look up an option, considering a default value
 
100
      -- if 'usedefault' is True; or ignoring a default value otherwise.
 
101
      -- The option specification is assumed to be already transformed.
 
102
      defaulthandler :: ConfigParser -> SectionSpec -> OptionSpec -> Either CPError String,
 
103
      -- | Whether or not to seek out a default action when no match
 
104
      -- is found.
 
105
      usedefault :: Bool,
 
106
      -- | Function that is used to perform lookups, do optional
 
107
      -- interpolation, etc.  It is assumed that accessfunc
 
108
      -- will internally call defaulthandler to do the underlying lookup.
 
109
      -- The option value is not assumed to be transformed.
 
110
      accessfunc :: (ConfigParser -> SectionSpec -> OptionSpec -> Either CPError String)
 
111
    }
 
112
 
 
113