EEConfig (empty) → 1.0
raw patch · 5 files changed
+191/−0 lines, 5 filesdep +basedep +containerssetup-changed
Dependencies added: base, containers
Files
- EEConfig.cabal +28/−0
- EEConfig.cabal~ +28/−0
- EEConfig.hs +118/−0
- LICENSE +15/−0
- Setup.hs +2/−0
+ EEConfig.cabal view
@@ -0,0 +1,28 @@+name: EEConfig+version: 1.0+copyright: (c) 2008 Bartosz Wójcik+license: BSD3+license-file: LICENSE+author: Bartosz Wojcik <bartek@sudety.it>+maintainer: Bartosz Wojcik <bartek@sudety.it>+category: Parsing+synopsis: ExtremlyEasyConfig - Extremly Simple parser for config files+description: Implementation of a very simple parser for parameters recognition. + It recognizes given parameters and their values. + As input it becomes list of parameters and all their possible values in format [ParameterInput] + and input String where parameters will be searched for. + As output it deliveres recognized parameters and their values in format [ParameterOutput], + where only these parameters are present which have been found in the input string. + In output list each exisitng parameter has exactly one value - this one that has + been recognized in the input string. + Parameters in the input string have to be always given in following format: + (\<flagname> \<parameter>)*.+ Anything what is not recognized as parameter or value is ignored.+stability: stable+build-type: Simple+cabal-version: >= 1.2.1++library+ exposed-modules: EEConfig+ build-depends: base >= 3.0, containers+
+ EEConfig.cabal~ view
@@ -0,0 +1,28 @@+name: ExtremlyEasyConfig+version: 1.0+copyright: (c) 2008 Bartosz Wójcik+license: BSD3+license-file: LICENSE+author: Bartosz Wojcik <bartek@sudety.it>+maintainer: Bartosz Wojcik <bartek@sudety.it>+category: Parsing+synopsis: Extremly Simple parser for config files+description: Implementation of a very simple parser for parameters recognition. + It recognizes given parameters and their values. + As input it becomes list of parameters and all their possible values in format [ParameterInput] + and input String where parameters will be searched for. + As output it deliveres recognized parameters and their values in format [ParameterOutput], + where only these parameters are present which have been found in the input string. + In output list each exisitng parameter has exactly one value - this one that has + been recognized in the input string. + Parameters in the input string have to be always given in following format: + (\<flagname> \<parameter>)*.+ Anything what is not recognized as parameter or value is ignored.+stability: stable+build-type: Simple+cabal-version: >= 1.2.1++library+ exposed-modules: EEConfig+ build-depends: base >= 3.0, containers+
+ EEConfig.hs view
@@ -0,0 +1,118 @@+-- ================================== +-- Module name: EEConfig +-- Copyright (C) 2008 Bartosz Wójcik +-- Created on: 26.10.2008 +-- Last update: 29.10.2008+-- License: BSD + +-- ========================================================= +-- | +-- This module is a very simple parser for parameters recognition. +-- It recognizes given parameters and their values. +-- As input it becomes list of parameters and their values in format @[ParameterInput]@ +-- and input String where parameters will be searched. +-- As output it deliveres recognized parameters and values in format @[ParameterOutput]@, +-- where only these parameters are present which have been found in the input string. +-- In output list each exisitng parameter has exactly one value - this one that has +-- been recognized in the input string. +-- Parameters in the input string have to be always given in following format: +-- (\<flagname> \<parameter>)*+--+-- Example how to use it.+--+-- @ +-- let paramsList = matchParamsL params inputText+-- let paramsTree = matchParamsT paramsList+-- params :: [ParameterInput] +-- params = [("-pl1",algNum),("-pl2",algNum), +-- ("-br1","0":(map show [5..30])),("-br2","0":(map show [5..30])), +-- ("-dp1","0":(map show [3..8])),("-dp2","0":(map show [3..8])), +-- ("-verbose",["0","1","2"]), +-- ("-evaluator",["only","all"])]+-- @ + +module EEConfig (ParameterInput, + ParameterOutput, + ParameterTree, + matchParamsL, + matchParamsT, + member, + (!) + ) +where + +import qualified Data.Map as Map (Map, + (!), + member, + insert, + empty) + +-- ========================================================= +-- | +-- ParameterInput is a pair of parameter label and all its valid values. +type ParameterInput = (String,[String]) + +-- | +-- Parameter's label and its value formated for output. +type ParameterOutput = (String,String) + +-- | +-- Dictionary is a map of keywords which are parameter labels, +-- and values that are maps of all possible values for given parameter. +-- It is possible to rewrite Dictionary to Trie if anybody whishes. +type Dictionary = Map.Map String (Map.Map String String) + +-- | +-- Read parameters [ParameterOutput] formated can be transformed into +-- binary tree for search purposes. +type ParameterTree = Map.Map String String +-- ========================================================= + +-- ========================================================= +matchParamsL :: [ParameterInput] -- ^ List of parameters' labels and their all possible values + -> String -- ^ Input + -> [ParameterOutput] -- ^ Output - list of matched parameters and values. +matchParamsL params = matchAgainstDict (buildDict params) [] . words +-- ========================================================= + +-- ========================================================= +matchAgainstDict :: Dictionary -> [ParameterOutput] -> [String] -> [ParameterOutput] +-- --------------------------------------------------------- +matchAgainstDict _ os [] = os +matchAgainstDict _ os (w:[]) = os +matchAgainstDict dict os (w:val:ws) + | w `Map.member` dict && val `Map.member` values = matchAgainstDict dict ((w,val):os) ws + | otherwise = matchAgainstDict dict os (val:ws) + where values = dict Map.! w +-- ========================================================= + +-- ========================================================= +buildDict :: [ParameterInput] -> Dictionary +-- --------------------------------------------------------- +buildDict = foldl (\map (key,args) -> Map.insert key (insertValues args) map) Map.empty +-- ========================================================= + +-- ========================================================= +insertValues :: [String] -> Map.Map String String +-- --------------------------------------------------------- +insertValues = foldl (\map key -> Map.insert key [] map) Map.empty +-- ========================================================= + +-- =========================================================+-- | Transfers list of regonized parameters into a tree. +matchParamsT :: [ParameterOutput] -> ParameterTree +-- --------------------------------------------------------- +matchParamsT = foldl (\map (key,arg) -> Map.insert key arg map) Map.empty +-- ========================================================= + +-- ========================================================= +-- | Following functions are supposed to be parameters dictionary access functions. +-- If this modul used another data structure these 2 functions would need to +-- be rewriten to get data from this new data structure. +(!) dictionary key = dictionary Map.! key +-- ========================================================= +-- | Following functions are supposed to be parameters dictionary access functions.+member key dictionary = key `Map.member` dictionary +-- ========================================================= ++
+ LICENSE view
@@ -0,0 +1,15 @@+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution.+ * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived + from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT +SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain