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