简体   繁体   中英

Haskell command system parser

I've written this simple parser that take from the command line [ps auxww | ./myparser] and parses the output of the ps command in order to insert it into the process data structure I created. I succeed to parse one line of the result String, but now I'm stuck trying to parse the whole string and return a [Process] and not a single Process. The problem is how to implement parsePS. It has to call many times myParser in order to parse every single line and return a list of Process and print it into the terminal. Can someone help me?

I'm not sure what's failing for you, but I am guessing the spacing is killing you. If so, I have two ideas that might help.

Modify myParser to consume spaces at the end and the many combinator should work.

myParser = do
  ...
  spaces
  command <- pCommand
  spaces -- CONSUME END OF LINE
  return Entry{ ... }

Then many myParser should work.

Alternately, you could split the input into lines separately first and call parse on each.

  argLines <- fmap lines getContents

(I take it you mean to burn the first line via getLine before the hGetContents ?)

It sounds to me like you're looking for a way to parse each line in sequence and return a list of parsed results. How about mapM from the Prelude?

If myParser :: String -> Parser Process , then mapM myParser :: [String] -> Parser [Process] , which seems to be what you're looking for (using generic names for Parsec 's Parser types). So if you have a list of lines (call it lns ) that you want to parse in sequence, you can use parse (mapM myParser) lns to get what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM