Fredrik Karlsson | 5 Jul 18:42 2013
Picon

Basic Parsec float & integer parsing question

Dear list,

Sorry for asking a simple parsec question, but both Parsec and Haskell is new to me, so please be gentle :-)

I have this code:

------------------------------------------------------------------------------------------------
import Text.ParserCombinators.Parsec
import Text.Parsec.Token 
import Text.ParserCombinators.Parsec.Char 


data VariableLine = VariableLine String String deriving Show
data TierType = IntervalTier | PointTier deriving Show 

data Tier = Tier TierType String Float Float Integer
data Label = Interval Float Float String 
data LabelFile = LabelFile Float Float

symbol :: Parser Char
symbol = oneOf "!#$%&|*+-/:<=>? <at> ^_~"

testString = "intervals [1]:\n    xmin = 0 \n    xmax = 0.028199999999999992 \n    text = \"\""
headTS1 = "File type = \"ooTextFile\"\nObject class = \"TextGrid\"\n\nxmin ="

header :: Parser LabelFile
header = do
headTS1
start <- float
string "\nxmax = "
end <- float
string "\ntiers? <exists>\nsize = "
integer
char '\n'
return $ LabelFile start end

------------------------------------------------------------------------------------------------

Loading it into ghci I get :

Prelude> :l parsectest.hs
[1 of 1] Compiling Main             ( parsectest.hs, interpreted )

parsectest.hs:21:9:
    Couldn't match type `[]'
                  with `Text.Parsec.Prim.ParsecT
                          String () Data.Functor.Identity.Identity'
    Expected type: Text.Parsec.Prim.ParsecT
                     String () Data.Functor.Identity.Identity Char
      Actual type: [Char]
    In a stmt of a 'do' block: headTS1
    In the expression:
      do { headTS1;
           start <- float;
           string
             "\
             \xmax = ";
           end <- float;
           .... }
    In an equation for `header':
        header
          = do { headTS1;
                 start <- float;
                 string
                   "\
                   \xmax = ";
                 .... }

parsectest.hs:22:18:
    Couldn't match expected type `Text.Parsec.Prim.ParsecT
                                    String () Data.Functor.Identity.Identity Float'
                with actual type `GenTokenParser s0 u0 m0
                                  -> Text.Parsec.Prim.ParsecT s0 u0 m0 Double'
    In a stmt of a 'do' block: start <- float
    In the expression:
      do { headTS1;
           start <- float;
           string
             "\
             \xmax = ";
           end <- float;
           .... }
    In an equation for `header':
        header
          = do { headTS1;
                 start <- float;
                 string
                   "\
                   \xmax = ";
                 .... }

parsectest.hs:24:16:
    Couldn't match expected type `Text.Parsec.Prim.ParsecT
                                    String () Data.Functor.Identity.Identity Float'
                with actual type `GenTokenParser s1 u1 m1
                                  -> Text.Parsec.Prim.ParsecT s1 u1 m1 Double'
    In a stmt of a 'do' block: end <- float
    In the expression:
      do { headTS1;
           start <- float;
           string
             "\
             \xmax = ";
           end <- float;
           .... }
    In an equation for `header':
        header
          = do { headTS1;
                 start <- float;
                 string
                   "\
                   \xmax = ";
                 .... }

parsectest.hs:26:9:
    Couldn't match expected type `Text.Parsec.Prim.ParsecT
                                    String () Data.Functor.Identity.Identity a0'
                with actual type `GenTokenParser s2 u2 m2
                                  -> Text.Parsec.Prim.ParsecT s2 u2 m2 Integer'
    In a stmt of a 'do' block: integer
    In the expression:
      do { headTS1;
           start <- float;
           string
             "\
             \xmax = ";
           end <- float;
           .... }
    In an equation for `header':
        header
          = do { headTS1;
                 start <- float;
                 string
                   "\
                   \xmax = ";
                 .... }
Failed, modules loaded: none.

I'm sure I'm doing something really stupid here, but I need help to get through this problem. I've used the predefined "letter" parser at other places in the code, so I can't understand why "float" and "integer" does not work.

/Fredrik 

--
"Life is like a trumpet - if you don't put anything into it, you don't get anything out of it."
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
David McBride | 5 Jul 19:32 2013
Picon

Re: Basic Parsec float & integer parsing question

Token parsers are specific to different languages.  Afterall, haskell
parses floats differently than C, which is different from java
(probably).  In order to use it in your code you have to tell it that,
like so:

haskelldef = makeTokenParser haskellDef

header :: Parser LabelFile
header = do
  string headTS1
  start <- fmap double2Float $ float haskelldef
  string "\nxmax = "
  end <- fmap double2Float $ float haskelldef
  string "\ntiers? <exists>\nsize = "
  integer haskelldef
  char '\n'
  return $ LabelFile start end

You'll need to import GHC.Float (double2Float) to get those parsed
values as floats.

On Fri, Jul 5, 2013 at 12:42 PM, Fredrik Karlsson <dargosch <at> gmail.com> wrote:
> Dear list,
>
> Sorry for asking a simple parsec question, but both Parsec and Haskell is
> new to me, so please be gentle :-)
>
> I have this code:
>
> ------------------------------------------------------------------------------------------------
> import Text.ParserCombinators.Parsec
> import Text.Parsec.Token
> import Text.ParserCombinators.Parsec.Char
>
>
> data VariableLine = VariableLine String String deriving Show
> data TierType = IntervalTier | PointTier deriving Show
>
> data Tier = Tier TierType String Float Float Integer
> data Label = Interval Float Float String
> data LabelFile = LabelFile Float Float
>
> symbol :: Parser Char
> symbol = oneOf "!#$%&|*+-/:<=>? <at> ^_~"
>
> testString = "intervals [1]:\n    xmin = 0 \n    xmax = 0.028199999999999992
> \n    text = \"\""
> headTS1 = "File type = \"ooTextFile\"\nObject class = \"TextGrid\"\n\nxmin
> ="
>
> header :: Parser LabelFile
> header = do
> headTS1
> start <- float
> string "\nxmax = "
> end <- float
> string "\ntiers? <exists>\nsize = "
> integer
> char '\n'
> return $ LabelFile start end
>
> ------------------------------------------------------------------------------------------------
>
> Loading it into ghci I get :
>
> Prelude> :l parsectest.hs
> [1 of 1] Compiling Main             ( parsectest.hs, interpreted )
>
> parsectest.hs:21:9:
>     Couldn't match type `[]'
>                   with `Text.Parsec.Prim.ParsecT
>                           String () Data.Functor.Identity.Identity'
>     Expected type: Text.Parsec.Prim.ParsecT
>                      String () Data.Functor.Identity.Identity Char
>       Actual type: [Char]
>     In a stmt of a 'do' block: headTS1
>     In the expression:
>       do { headTS1;
>            start <- float;
>            string
>              "\
>              \xmax = ";
>            end <- float;
>            .... }
>     In an equation for `header':
>         header
>           = do { headTS1;
>                  start <- float;
>                  string
>                    "\
>                    \xmax = ";
>                  .... }
>
> parsectest.hs:22:18:
>     Couldn't match expected type `Text.Parsec.Prim.ParsecT
>                                     String () Data.Functor.Identity.Identity
> Float'
>                 with actual type `GenTokenParser s0 u0 m0
>                                   -> Text.Parsec.Prim.ParsecT s0 u0 m0
> Double'
>     In a stmt of a 'do' block: start <- float
>     In the expression:
>       do { headTS1;
>            start <- float;
>            string
>              "\
>              \xmax = ";
>            end <- float;
>            .... }
>     In an equation for `header':
>         header
>           = do { headTS1;
>                  start <- float;
>                  string
>                    "\
>                    \xmax = ";
>                  .... }
>
> parsectest.hs:24:16:
>     Couldn't match expected type `Text.Parsec.Prim.ParsecT
>                                     String () Data.Functor.Identity.Identity
> Float'
>                 with actual type `GenTokenParser s1 u1 m1
>                                   -> Text.Parsec.Prim.ParsecT s1 u1 m1
> Double'
>     In a stmt of a 'do' block: end <- float
>     In the expression:
>       do { headTS1;
>            start <- float;
>            string
>              "\
>              \xmax = ";
>            end <- float;
>            .... }
>     In an equation for `header':
>         header
>           = do { headTS1;
>                  start <- float;
>                  string
>                    "\
>                    \xmax = ";
>                  .... }
>
> parsectest.hs:26:9:
>     Couldn't match expected type `Text.Parsec.Prim.ParsecT
>                                     String () Data.Functor.Identity.Identity
> a0'
>                 with actual type `GenTokenParser s2 u2 m2
>                                   -> Text.Parsec.Prim.ParsecT s2 u2 m2
> Integer'
>     In a stmt of a 'do' block: integer
>     In the expression:
>       do { headTS1;
>            start <- float;
>            string
>              "\
>              \xmax = ";
>            end <- float;
>            .... }
>     In an equation for `header':
>         header
>           = do { headTS1;
>                  start <- float;
>                  string
>                    "\
>                    \xmax = ";
>                  .... }
> Failed, modules loaded: none.
>
> I'm sure I'm doing something really stupid here, but I need help to get
> through this problem. I've used the predefined "letter" parser at other
> places in the code, so I can't understand why "float" and "integer" does not
> work.
>
> /Fredrik
>
> --
> "Life is like a trumpet - if you don't put anything into it, you don't get
> anything out of it."
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
Antoine Latter | 5 Jul 19:51 2013
Picon

Fwd: Basic Parsec float & integer parsing question

Forwarding to the list.

---------- Forwarded message ----------
From: Fredrik Karlsson <dargosch <at> gmail.com>
Date: Fri, Jul 5, 2013 at 11:42 AM
Subject: [Haskell-cafe] Basic Parsec float & integer parsing question
To: haskell-cafe <at> haskell.org

Dear list,

Sorry for asking a simple parsec question, but both Parsec and Haskell
is new to me, so please be gentle :-)

I have this code:

------------------------------------------------------------------------------------------------
import Text.ParserCombinators.Parsec
import Text.Parsec.Token
import Text.ParserCombinators.Parsec.Char

data VariableLine = VariableLine String String deriving Show
data TierType = IntervalTier | PointTier deriving Show

data Tier = Tier TierType String Float Float Integer
data Label = Interval Float Float String
data LabelFile = LabelFile Float Float

symbol :: Parser Char
symbol = oneOf "!#$%&|*+-/:<=>? <at> ^_~"

testString = "intervals [1]:\n    xmin = 0 \n    xmax =
0.028199999999999992 \n    text = \"\""
headTS1 = "File type = \"ooTextFile\"\nObject class = \"TextGrid\"\n\nxmin ="

header :: Parser LabelFile
header = do
headTS1
start <- float
string "\nxmax = "
end <- float
string "\ntiers? <exists>\nsize = "
integer
char '\n'
return $ LabelFile start end

------------------------------------------------------------------------------------------------

Loading it into ghci I get :

Prelude> :l parsectest.hs
[1 of 1] Compiling Main             ( parsectest.hs, interpreted )

parsectest.hs:21:9:
    Couldn't match type `[]'
                  with `Text.Parsec.Prim.ParsecT
                          String () Data.Functor.Identity.Identity'
    Expected type: Text.Parsec.Prim.ParsecT
                     String () Data.Functor.Identity.Identity Char
      Actual type: [Char]
    In a stmt of a 'do' block: headTS1
    In the expression:
      do { headTS1;
           start <- float;
           string
             "\
             \xmax = ";
           end <- float;
           .... }
    In an equation for `header':
        header
          = do { headTS1;
                 start <- float;
                 string
                   "\
                   \xmax = ";
                 .... }

parsectest.hs:22:18:
    Couldn't match expected type `Text.Parsec.Prim.ParsecT
                                    String ()
Data.Functor.Identity.Identity Float'
                with actual type `GenTokenParser s0 u0 m0
                                  -> Text.Parsec.Prim.ParsecT s0 u0 m0 Double'
    In a stmt of a 'do' block: start <- float
    In the expression:
      do { headTS1;
           start <- float;
           string
             "\
             \xmax = ";
           end <- float;
           .... }
    In an equation for `header':
        header
          = do { headTS1;
                 start <- float;
                 string
                   "\
                   \xmax = ";
                 .... }

parsectest.hs:24:16:
    Couldn't match expected type `Text.Parsec.Prim.ParsecT
                                    String ()
Data.Functor.Identity.Identity Float'
                with actual type `GenTokenParser s1 u1 m1
                                  -> Text.Parsec.Prim.ParsecT s1 u1 m1 Double'
    In a stmt of a 'do' block: end <- float
    In the expression:
      do { headTS1;
           start <- float;
           string
             "\
             \xmax = ";
           end <- float;
           .... }
    In an equation for `header':
        header
          = do { headTS1;
                 start <- float;
                 string
                   "\
                   \xmax = ";
                 .... }

parsectest.hs:26:9:
    Couldn't match expected type `Text.Parsec.Prim.ParsecT
                                    String () Data.Functor.Identity.Identity a0'
                with actual type `GenTokenParser s2 u2 m2
                                  -> Text.Parsec.Prim.ParsecT s2 u2 m2 Integer'
    In a stmt of a 'do' block: integer
    In the expression:
      do { headTS1;
           start <- float;
           string
             "\
             \xmax = ";
           end <- float;
           .... }
    In an equation for `header':
        header
          = do { headTS1;
                 start <- float;
                 string
                   "\
                   \xmax = ";
                 .... }
Failed, modules loaded: none.

I'm sure I'm doing something really stupid here, but I need help to
get through this problem. I've used the predefined "letter" parser at
other places in the code, so I can't understand why "float" and
"integer" does not work.

/Fredrik

--
"Life is like a trumpet - if you don't put anything into it, you don't
get anything out of it."

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane