Questions tagged [megaparsec]

Megaparsec is a monadic parser combinator library for Haskell with improved error reporting and indentation-aware parsing

Megaparsec is a monadic parser combinator library for Haskell. It is based on Parsec and provides improved error reporting and indentation-aware parsing.

87 questions
1
vote
2 answers

Record parsing in Haskell

I am building a parser using Megaparsec and I don't know which is the best approach to parse a structure like names a b c surnames d e f g where names and surnames are keywords followed by a list of strings, and each of the two line is optional.…
marcosh
  • 8,780
  • 5
  • 44
  • 74
1
vote
2 answers

Mixing Parser Char (lexer?) vs. Parser String

I've written several compilers and am familiar with lexers, regexs/NFAs/DFAs, parsers and semantic rules in flex/bison, JavaCC, JavaCup, antlr4 and so on. Is there some sort of magical monadic operator that seamlessly grows/combines a token with a…
user246672
1
vote
1 answer

Simple C grammar for Haskell Parsec, MegaParsec or Happy

I develop a preprocessor transpiling subset of C statements into gcc _asm statements. For this project, I will be happy to reuse existing C statements parser written with any popular Haskell technology, or just start with some simple C subset paser…
Bulat
  • 2,435
  • 1
  • 15
  • 15
1
vote
0 answers

After defining the name of a type classes in Haskell, what does the vertical bar followed by a function mean?

I was taking a look at the megaparsec library and noticed that a class is defined as class (Stream s, A.Alternative m, MonadPlus m) => MonadParsec e s m | m -> e s where I know that I am creating a type class named MonadParsec, but what does | m ->…
mtber75
  • 938
  • 1
  • 8
  • 15
1
vote
2 answers

Type Resolution in Haskell: MegaParsec/ Parsing a single space

If I look at the documentation for space it suggests using void spaceChar. However, if I actually try: x :: Parser () x = void spaceChar I get * Couldn't match type `Token s0' with `Char' arising from a use of `spaceChar' The type variable…
Julian Birch
  • 2,605
  • 1
  • 21
  • 36
1
vote
0 answers

Megaparsec: No instance for (ErrorComponent Void) arising from a do statement

I am trying to write a simple parser with megaparsec and have the following issue: type MyParser = Parsec Void String xxx :: MyParser Int xxx = do satisfy (`notElem` "!>") return 1 gbgc :: MyParser [Int] gbgc = many (yyy <|> xxx) I get…
pulcher
  • 115
  • 5
1
vote
1 answer

megaparsec: how to declare the type of `eol` to parse Text and not [Char]

I can't understand what does the type of (for example) eol mean: eol :: (MonadParsec e s m, Token s ~ Char) => m String or, better, I don't understand how to use eol with Text.Megaparsec.Text and not Text.Megaparsec.String. I've been trying to use…
helq
  • 1,451
  • 1
  • 9
  • 12
1
vote
1 answer

How to match megaparsec result?

I wrote a small parser with megaparsec: module App (main) where import Control.Monad (void) import Text.Megaparsec import Text.Megaparsec.String import qualified Text.Megaparsec.Lexer as L sc :: Parser () sc = L.space (void spaceChar) lineCmnt…
1
vote
1 answer

Is it possible to force backtrack all options?

I need to parse this syntax for function declaration foo x = 1 Func "foo" (Ident "x") = 1 foo (x = 1) = 1 Func "foo" (Label "x" 1) = 1 foo x = y = 1 Func "foo" (Ident "x") = (Label "y" 1) I have written this parser module SimpleParser…
ais
  • 2,514
  • 2
  • 17
  • 24
0
votes
1 answer

How can I access the whole input from an arbitrary parser in a sequence?

I'm working through a DNS message parser. I have defined the following using megaparsec: Header data DNSHeader = DNSHeader { hid :: !Word16, hflags :: !Word16, hnumQuestions :: !Word16, hnumAnswers :: !Word16, hnumAuthorities ::…
DavSanchez
  • 831
  • 8
  • 13
0
votes
2 answers

How to parse Number with comma via Megaparsec

Currently I have a parser: pScientific :: Parser Scientific pScientific = lexeme L.scientific This is able to easily parse something like 4087.00 but fails when then number 4,087.00 Is there a way to make megaparsec parse number with comma? PS: I…
Bads
  • 774
  • 3
  • 8
  • 20
0
votes
1 answer

Megaparsec .. Find an Element Before Encountering a Different Element

I am trying to parse (using magaparsec) the XML export of FreePlane (mindmapper). This is my third attempt to really 'learn' (internalize) megaparsec. I've written several parsers before, two worked (after a lot of struggling), and one I gave up and…
TomP
  • 108
  • 6
0
votes
1 answer

Megaparsec: transforming comment syntax into a Record

Using Megaparsec, if I want to parse a string containing comments of the form ~{content} into a Comment record, how would I go about doing that? For instance: data Comment = { id :: Integer, content :: String } parse :: Parser [Comment] parse =…
Niek
  • 1,464
  • 1
  • 17
  • 27
0
votes
2 answers

Megaparsec: skip space and non-alphanumeric

I'm a beginner with Megaparsec and Haskell in general, and trying to write a parser for the following grammar: A word will always be one of: A number composed of one or more ASCII digits (ie "0" or "1234") OR A simple word composed of one or…
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
0
votes
0 answers

Could not find module ‘Text.Megaparsec.Char.Lexer’

I tried "stack install megaparsec", adding megaparsec and parsec as a dependency in the project.yaml file and running stack build, i tried stack build on all levels of the project folder. Also what's weird is that I can load the module which…