parsec

How to parse string into GADT

浪子不回头ぞ 提交于 2019-12-07 08:46:17
问题 I am trying trying to implement Combinatory Logic in Haskell, and I would like to write to parser for the language. I am having trouble getting a parser to work via Parsec. The basic problem is that I need a way to ensure that the objects returned by the parser are well typed. Does anyone have any creative ideas on how to do this? {-# Language GeneralizedNewtypeDeriving #-} import qualified Data.Map as Map import qualified Text.ParserCombinators.Parsec as P import Text.Parsec.Token (parens)

Custom whiteSpace using Haskell Parsec

徘徊边缘 提交于 2019-12-07 03:47:47
问题 I would like to use Parsec's makeTokenParser to build my parser, but I want to use my own definition of whiteSpace . Doing the following replaces whiteSpace with my definition, but all the lexeme parsers still use the old definition (e.g. P.identifier lexer will use the old whiteSpace). ... lexer :: P.TokenParser () lexer = l { P.whiteSpace = myWhiteSpace } where l = P.makeTokenParser myLanguageDef ... Looking at the code for makeTokenParser I think I understand why it works this way. I want

The type signature of Parsec function 'parse' and the class 'Stream'

*爱你&永不变心* 提交于 2019-12-06 21:59:38
问题 What does the constraint (Stream s Identity t) mean in the following type declaration? parse :: (Stream s Identity t) => Parsec s () a -> SourceName -> s -> Either ParseError a What is Stream in the following class declaration, what does it mean. I'm totally lost. class Monad m => Stream s m t | s -> t where When I use Parsec, I get into a jam with the type-signatures ( xxx :: yyy ) all the time. I always skip the signatures, load the src into ghci, and then copy the type-signature back to my

Is there an established way to write parsers that can reconstruct their exact input?

此生再无相见时 提交于 2019-12-06 19:29:58
问题 Say I want to parse a file in language X . Really, I'm only interested in a small part of the information within. It's easy enough to write a parser in one of Haskell's many eDSLs for that purpose (e.g. Megaparsec). data Foo = Foo Int -- the information I'm after. parseFoo :: Parsec Text Foo parseFoo = ... That readily gives rise to a function getFoo :: Text -> Maybe Foo . But now I would also like to modify the source of the Foo information, i.e. basically I want to implement changeFoo ::

Trivial parsec example produces a type error

风格不统一 提交于 2019-12-06 17:24:12
问题 I'm trying to get this trivial parsec code to compile import Text.Parsec simple = letter but I keep getting this error No instance for (Stream s0 m0 Char) arising from a use of `letter' Possible fix: add an instance declaration for (Stream s0 m0 Char) In the expression: letter In an equation for `simple': simple = letter 回答1: I think you have ran against the monomorphism restriction . This restriction means: If a variable is declared with no explicit arguments, its type has to be monomorphic.

Matching bytestrings in Parsec

亡梦爱人 提交于 2019-12-06 06:04:55
问题 I am currently trying to use the Full CSV Parser presented in Real World Haskell . In order to I tried to modify the code to use ByteString instead of String , but there is a string combinator which just works with String . Is there a Parsec combinator similar to string that works with ByteString , without having to do conversions back and forth? I've seen there is an alternative parser that handles ByteString : attoparsec , but I would prefer to stick with Parsec, since I'm just learning how

heap usage for CPS vs non-CPS parsers in Haskell's parsec

主宰稳场 提交于 2019-12-06 04:40:31
I'm trying to write the following parser using parsec : manyLength :: forall s u m a. Monad m => ParsecT s u m a -> ParsecT s u m Int manyLength p = go 0 where go :: Int -> ParsecT s u m Int go !i = (p *> go (i + 1)) <|> pure i This is like the many function, but instead of returning [a] , it returns the number of times Parser a succeeds. This works, but I can't seem to make it run in constant heap space. This makes sense, since the recursive call to go is not in the tail-call position. If parsec would export the constructor to ParsecT , it would be possible to rewrite manyLength in CPS'ed

How to use Parsers from Aeson with IO

非 Y 不嫁゛ 提交于 2019-12-05 20:01:49
I have data types with many fields that, if not being manually specified by a JSON configuration file, should be randomly set. I am using Aeson to parse the config file. What is the best way to do this? Currently, I am setting values equal to some impossible value, and then later checking for said value to edit. data Example = Example { a :: Int, b :: Int } default = Example 1 2 instance FromJSON Example where parseJSON = withObject "Example" $ \v -> Example <$> (v .: "a" <|> return (a default)) <*> (v .: "b" <|> return (b default)) initExample :: Range -> Example -> IO Example initExample

How to parse string into GADT

为君一笑 提交于 2019-12-05 15:52:11
I am trying trying to implement Combinatory Logic in Haskell, and I would like to write to parser for the language. I am having trouble getting a parser to work via Parsec. The basic problem is that I need a way to ensure that the objects returned by the parser are well typed. Does anyone have any creative ideas on how to do this? {-# Language GeneralizedNewtypeDeriving #-} import qualified Data.Map as Map import qualified Text.ParserCombinators.Parsec as P import Text.Parsec.Token (parens) import Text.ParserCombinators.Parsec ((<|>)) import Control.Applicative ((<$>), (<*>), (*>), (<*)) data

Python implementation of Parsec?

ぐ巨炮叔叔 提交于 2019-12-05 13:58:26
问题 I recently wrote a parser in Python using Ply (it's a python reimplementation of yacc). When I was almost done with the parser I discovered that the grammar I need to parse requires me to do some look up during parsing to inform the lexer. Without doing a look up to inform the lexer I cannot correctly parse the strings in the language. Given than I can control the state of the lexer from the grammar rules I think I'll be solving my use case using a look up table in the parser module, but it