parsec

Parsing data with Parsec and omitting comments

一个人想着一个人 提交于 2019-12-14 01:28:27
问题 I am trying to write a Haksell Parsec Parser that parses input data from a file into the LogLine datatype as follows: --Final parser that holds the indvidual parsers. final :: Parser [LogLine] final = do{ logLines <- sepBy1 logLine eol ; return logLines } --The logline token declaration logLine :: Parser LogLine logLine = do name <- plainValue -- parse the name (identifier) many1 space -- parse and throw away a space args1 <- bracketedValue -- parse the first arguments many1 space -- throw

Parsec ignore everything except one fragment

依然范特西╮ 提交于 2019-12-13 00:54:40
问题 I need to parse a single select tag in a poorly formed HTML document (so XML-based parsers don't work). I think I know how to use parsec to parse the select tag once I get there, but how do I skip all the stuff before and after that tag? Example: <html> random content with lots of tags... <select id=something title="whatever"><option value=1 selected>1. First<option value=2>2. Second</select> more random content... </html> That's actually what the HTML looks like in the select tag. How would

Is this idiomatic use of Text.Parsec?

╄→尐↘猪︶ㄣ 提交于 2019-12-12 12:25:16
问题 My use of Text.Parsec is a little rusty. If I just want to return the matched string is this idiomatic? category :: Stream s m Char => ParsecT s u m [Char] category = concat <$> (many1 $ (:) <$> char '/' <*> (many1 $ noneOf "/\n")) I feel like there might be an existing operator for liftM concat . many1 or (:) <$> p1 <*> p2 that I'm ignoring, but I'm not sure. 回答1: That's fine, I think. A little judicious naming would make it prettier: category = concat <$> many1 segment where segment = (:) <

Parse recursive data with parsec

偶尔善良 提交于 2019-12-12 12:20:44
问题 import Data.Attoparsec.Text.Lazy import Data.Text.Lazy.Internal (Text) import Data.Text.Lazy (pack) data List a = Nil | Cons a (List a) list :: Text list = pack $ unlines [ "0" , "1" , "2" , "5" ] How can List Int parser coud be implemented to parse Cons 0 (Cons 1 (Cons 2 (Cons 5 Nil))) from list ? ps : pure parser without parsing a [Int] and converting it to List Int is preferable. 回答1: Like this: import Control.Applicative -- rest of imports as in question data List a = Nil | Cons a (List a

How can I use buildExpressionParser from Text.Parsec.Expr to parse this language?

ぐ巨炮叔叔 提交于 2019-12-12 12:18:14
问题 I've been trying to use buildExpressionParser to parse a language, and I almost have it. Thanks to Parsec.Expr repeated Prefix/Postfix operator not supported for solving one of my big problems. This code snippet illustrates (what I hope to be) my last difficulty: import Text.Parsec.Expr import Text.Parsec data Expr = Lit Char | A1 Expr | A2 Expr | B Expr Expr deriving (Show) expr :: Parsec String () Expr expr = buildExpressionParser table (fmap Lit digit) prefix p = Prefix . chainl1 p $

Parsing the arrow type with FParsec

孤人 提交于 2019-12-11 16:58:01
问题 I'm trying to parse the arrow type with FParsec. That is, this: Int -> Int -> Int -> Float -> Char For example. I tried with this code, but it only works for one type of arrow ( Int -> Int ) and no more. I also want to avoid parentheses, because I already have a tuple type that uses them, and I don't want it to be too heavy in terms of syntax either. let ws = pspaces >>. many pspaces |>> (fun _ -> ()) let str_ws s = pstring s .>> ws type Type = ArrowType of Type * Type let arrowtype' = pipe2

Parsec: Parsing a list of lists, both with the same delimiter

大憨熊 提交于 2019-12-11 12:22:09
问题 Consider a simple language that's a list of space-delimited commands. Each command takes a single letter as the command name, and a series of space-delimited numbers as its arguments; e.g. a 1 2 3 b 4 5 6 d 7 e f would represent the following commands: a 1 2 3 b 4 5 6 d 7 e f The problem is that it keeps grabbing items through sepBy , and reaches for another number but fails when it gets to "b". However, this yields the following error when passed through the code below: Left "error" (line 1,

I don't understand how to use the lexeme function

主宰稳场 提交于 2019-12-11 04:34:04
问题 From Text.Parsec.Token : lexeme p = do { x <- p; whiteSpace; return x } It appears that lexeme takes a parser p and delivers a parser that has the same behavior as p, except that it also skips all the trailing whitespace. Correct? Then how come the following does not work: constant :: Parser Int constant = do digits <- many1 digit return (read digits) lexConst :: Parser Int lexConst = lexeme constant The last line results in the following error message: Couldn't match expected type `ParsecT

Is there any trick about translating BNF to Parsec program?

戏子无情 提交于 2019-12-10 20:01:58
问题 The BNF that match function call chain (like x(y)(z)... ): expr = term T T = (expr) T | EMPTY term = (expr) | VAR Translate it to Parsec program that looks so tricky. term :: Parser Term term = parens expr <|> var expr :: Parser Term expr = do whiteSpace e <- term maybeAddSuffix e where addSuffix e0 = do e1 <- parens expr maybeAddSuffix $ TermApp e0 e1 maybeAddSuffix e = addSuffix e <|> return e Could you list all the design patterns about translating BNF to Parsec program? 回答1: The simplest

Parsec-Parser works alright, but could it be done better?

匆匆过客 提交于 2019-12-10 17:26:57
问题 I try to do this: Parse a Text in the form: Some Text #{0,0,0} some Text #{0,0,0}#{0,0,0} more Text #{0,0,0} into a list of some data structure: [Inside "Some Text ",Outside (0,0,0),Inside " some Text ",Outside (0,0,0),Outside (0,0,0),Inside " more Text ",Outside (0,0,0)] So these #{a,b,c}-bits should turn into different things as the rest of the text. I have this code: module ParsecTest where import Text.ParserCombinators.Parsec import Monad type Reference = (Int, Int, Int) data Transc =