Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Parsers.hs @ 9d049fb4

History | View | Annotate | Download (1.7 kB)

1 f5bbddb5 Spyros Trigazis
{-# LANGUAGE OverloadedStrings #-}
2 f5bbddb5 Spyros Trigazis
{-| Utility functions for several parsers
3 f5bbddb5 Spyros Trigazis
4 f5bbddb5 Spyros Trigazis
This module holds the definition for some utility functions for two
5 f5bbddb5 Spyros Trigazis
parsers.  The parser for the @/proc/stat@ file and the parser for the
6 f5bbddb5 Spyros Trigazis
@/proc/diskstats@ file.
7 f5bbddb5 Spyros Trigazis
8 f5bbddb5 Spyros Trigazis
-}
9 f5bbddb5 Spyros Trigazis
{-
10 f5bbddb5 Spyros Trigazis
11 f5bbddb5 Spyros Trigazis
Copyright (C) 2013 Google Inc.
12 f5bbddb5 Spyros Trigazis
13 f5bbddb5 Spyros Trigazis
This program is free software; you can redistribute it and/or modify
14 f5bbddb5 Spyros Trigazis
it under the terms of the GNU General Public License as published by
15 f5bbddb5 Spyros Trigazis
the Free Software Foundation; either version 2 of the License, or
16 f5bbddb5 Spyros Trigazis
(at your option) any later version.
17 f5bbddb5 Spyros Trigazis
18 f5bbddb5 Spyros Trigazis
This program is distributed in the hope that it will be useful, but
19 f5bbddb5 Spyros Trigazis
WITHOUT ANY WARRANTY; without even the implied warranty of
20 f5bbddb5 Spyros Trigazis
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 f5bbddb5 Spyros Trigazis
General Public License for more details.
22 f5bbddb5 Spyros Trigazis
23 f5bbddb5 Spyros Trigazis
You should have received a copy of the GNU General Public License
24 f5bbddb5 Spyros Trigazis
along with this program; if not, write to the Free Software
25 f5bbddb5 Spyros Trigazis
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 f5bbddb5 Spyros Trigazis
02110-1301, USA.
27 f5bbddb5 Spyros Trigazis
28 f5bbddb5 Spyros Trigazis
-}
29 f5bbddb5 Spyros Trigazis
module Ganeti.Parsers where
30 f5bbddb5 Spyros Trigazis
31 f5bbddb5 Spyros Trigazis
import Control.Applicative ((*>))
32 f5bbddb5 Spyros Trigazis
import qualified Data.Attoparsec.Text as A
33 f5bbddb5 Spyros Trigazis
import Data.Attoparsec.Text (Parser)
34 f5bbddb5 Spyros Trigazis
import Data.Text (unpack)
35 f5bbddb5 Spyros Trigazis
36 f5bbddb5 Spyros Trigazis
-- * Utility functions
37 f5bbddb5 Spyros Trigazis
38 f5bbddb5 Spyros Trigazis
-- | Our own space-skipping function, because A.skipSpace also skips
39 f5bbddb5 Spyros Trigazis
-- newline characters. It skips ZERO or more spaces, so it does not
40 f5bbddb5 Spyros Trigazis
-- fail if there are no spaces.
41 f5bbddb5 Spyros Trigazis
skipSpaces :: Parser ()
42 f5bbddb5 Spyros Trigazis
skipSpaces = A.skipWhile A.isHorizontalSpace
43 f5bbddb5 Spyros Trigazis
44 f5bbddb5 Spyros Trigazis
-- | A parser recognizing a number preceeded by spaces.
45 f5bbddb5 Spyros Trigazis
numberP :: Parser Int
46 f5bbddb5 Spyros Trigazis
numberP = skipSpaces *> A.decimal
47 f5bbddb5 Spyros Trigazis
48 f5bbddb5 Spyros Trigazis
-- | A parser recognizing a word preceded by spaces, and closed by a space.
49 f5bbddb5 Spyros Trigazis
stringP :: Parser String
50 f5bbddb5 Spyros Trigazis
stringP = skipSpaces *> fmap unpack (A.takeWhile $ not . A.isHorizontalSpace)