Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6899557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:26:11+00:00 2026-05-27T07:26:11+00:00

As a learning exercise I’m using parsec to look for values in a test

  • 0

As a learning exercise I’m using parsec to look for values in a test file. I’d normally use regexp for this particular case, but want to see if parsec makes sense as well. Unfortunately, I’m running into some problems.

The data file consists of repeating sections that look similar to the following. The ‘SHEF’ is one of six values and changes from page to page, and I want to use it in constructing a data type.

Part A SHEF Nov/14/2011 (10:52)
       -------------------
       Portfolio Valuation
       -------------------

       FOREIGN COMMON STOCK                            6,087,152.65
       FOREIGN COMMON STOCK - USA                      7,803,858.84
       RIGHTS                                                  0.00

I’m constructing a data type of the amounts in each asset class:

type Sector = String
type Amount = Double
type FundCode = String

data SectorAmount = SectorAmount (Sector,Amount) deriving (Show, Eq)

data FundSectors = FundSectors {
      fund             :: FundCode
    , sectorAmounts    :: [SectorAmount]
      } deriving (Show, Eq)

My code, which compiles successfully, is as shown below. It parses the file and correctly retrieves the values in each asset class, but I’m never able to set the state correctly in the fundValue parser. I’ve tested the fundValue parser with an input string and it does successfully parse it, but for some reason the line function isn’t working the way I thought it would. I want it to look for lines in the file which start with “Part A”, find the code and store it in state for later use when the tag parser successfully parses a line.

Is the use of fail causing the problem?

allocationParser :: String -> Either ParseError [FundSectors]
allocationParser input = do
                 runParser allocationFile "" "" input


allocationFile :: GenParser Char FundCode [FundSectors]
allocationFile = do
        secAmt <- many line
        return secAmt


line :: GenParser Char FundCode FundSectors
line = try (do fund <- try fundValue
               eol
               fail "")
       <|> do result <- try tag
              eol
              f <- getState
              return $ FundSectors {fund=f, sectorAmounts = [result]}


fundValue :: GenParser Char FundCode FundCode
fundValue = do manyTill anyChar . try $ lookAhead (string "Part A ")
               string "Part A "
               fCode <- try fundCode
               setState fCode
               v <- many (noneOf "\n\r")
               eol
               return fCode


fundCode :: GenParser Char FundCode String
fundCode = try (string "SHSF")
         <|> try (string "SHIF")
         <|> try (string "SHFF")
         <|> try (string "SHEF")
         <|> try (string "SHGE")
         <|> try (string "SHSE")
         <|> fail "Couldn't match fundCode"


tag :: GenParser Char FundCode SectorAmount
tag = do manyTill anyChar . try $ lookAhead tagName 
         name <- tagName 
         v <- many (noneOf "\n\r")
         let value = read ([x | x <- v, x /= ',']) :: Double -- remove commas from currency
         return $ SectorAmount (name,value)

eol :: GenParser Char FundCode String
eol = try (string "\n\r")
    <|> try (string "\r\n")
    <|> string "\n"
    <|> string "\r"
    <|> fail "Couldn't find EOL"

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:26:11+00:00Added an answer on May 27, 2026 at 7:26 am

    Yes, the fail in the “try fundValue” block undoes the setState.
    You will need to slightly redesign the parser, but you seem close.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This app is a learning exercise for me using fragments, among other things. It
I'm creating a blog engine as a learning exercise and one particular problem has
I'm currently playing with ARM assembly on Linux as a learning exercise. I'm using
This is a learning exercise in expression trees. I have this working code: class
I'm currently writing my own PHP framework as a learning exercise using the HMVC
So as a learning exercise, I am trying to make a simple file browser
As a learning exercise I'm building a basic scientific computation environment based on .NET.
I am writing a huffman implementation in Python as a learning exercise. I have
I'm designing an application that supports plugin development. Its more of a learning exercise.
I'm brand new to MVC and, as a learning exercise, I'm trying to re-write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.