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
10
votes
0 answers

Auto-completion suggestions from parse error

I am writing a parser for a custom jupter kernel using megaparsec. I was able to re-use the parser to provide completions too: the custom error message generated from the megaparsec library are transformed to the list of expected symbols. It that…
ttylec
  • 303
  • 1
  • 7
8
votes
1 answer

How to parse ternary expression using Parsec?

buildExpressionParser only deals with unary and binary operators. Can it handle ternary operators like ?:? There are some discussions here and here, but none is conclusive.
sinoTrinity
  • 1,125
  • 2
  • 15
  • 27
8
votes
1 answer

Indentation using Megaparsec

I would like to parse a basic indented language using Megaparsec. Originally I was using Parsec which I managed to get working correctly with indentation but now I'm having quite some trouble. I've been following a tutorial here and here's the code…
Michael
  • 3,411
  • 4
  • 25
  • 56
8
votes
0 answers

Elegant way to parse "line splices" (backslashes followed by a newline) in megaparsec

for a small compiler project we are currently working on implementing a compiler for a subset of C for which we decided to use Haskell and megaparsec. Overall we made good progress but there are still some corner cases that we cannot correctly…
Chirs
  • 567
  • 2
  • 15
8
votes
2 answers

Megaparsec: Unable to parse recursive arithmetic string

I'm working on a small parser using Megaparsec and trying to parse arithmetic. -- Arithmetic expressions data Aexp = N Num | V Var | Mult Aexp Aexp | Add Aexp Aexp | Sub Aexp Aexp …
Bort
  • 231
  • 2
  • 3
  • 11
8
votes
3 answers

Megaparsec, backtracking user state with StateT and ParsecT

Using Megaparsec 5. Following this guide, I can achieve a back-tracking user-state by combining StateT and ParsecT (non-defined types should be obvious/irrelevant): type MyParser a = StateT UserState (ParsecT Dec T.Text Identity) a if I run a…
cornuz
  • 2,678
  • 18
  • 35
6
votes
1 answer

How to report multiple errors using megaparsec?

Per megaparsec docs, "Since version 8, reporting multiple parse errors at once has become much easier." I haven't been able to find a single example of doing it. The only one I find is this. However it only shows how to parse a newline delimited toy…
sinoTrinity
  • 1,125
  • 2
  • 15
  • 27
5
votes
0 answers

How do I know if a (mega)parsec parser may consume input without experimentation - appears not to be documented

When using the <|> combinator in a Parsec or Megaparsec parser, 'try' maybe needed to force backtracking in case the first parser fails having consumed input. In Parsec I need to use 'try' when parsing strings: λ: parse (try (string "abc") <|>…
5
votes
1 answer

How to get source range of AST nodes using megaparsec?

I'm trying to generate a source map for some source file I'm parsing and I want to get the range for each node. getSourcePos only gives the start position of a node (src:line:column). How to get its end position?
sinoTrinity
  • 1,125
  • 2
  • 15
  • 27
5
votes
1 answer

Haskell Megaparsec - Reserved word parsed as identifier

I'm trying to parse a simple language with lamdba expressions. But runParser expr "lamdbda(x) (return x) returns Right (Var "lamdba") instead of Right (Lambda ["x"] (Return (Var "x"))) My guess is, that I have to add a try somewhere, but I can't…
5
votes
1 answer

Parsing block comments with Megaparsec using symbols for start and end

I want to parse text similar to this in Haskell using Megaparsec. # START SKIP def foo(a,b): c = 2*a # Foo return a + b # END SKIP , where # START SKIP and # END SKIP marks the start and end of the block of text to parse. Compared to…
Karl Marklund
  • 485
  • 1
  • 4
  • 10
4
votes
1 answer

Recursing to a function that doesn't exist yet in Haskell

I'm stuck on a problem with writing a parser in Haskell that I hope someone can help out with! It is a bit more complicated than my usual parser because there are two layers of parsing. First a language definition is parsed into an AST, then that…
4
votes
2 answers

Why does this Megaparsec parser work in GHCi but does not compile in a source file?

I am a beginner user of Haskell and the Megaparsec library. While parsing a line of text, I come to a point where I need to parse the remaining text in the line up until the end-of-line (either LF or CRLF). My thought was to use some and noneOf but…
Leo U.
  • 43
  • 3
4
votes
1 answer

Why doesn't "between (char '"') (char '"') (many charLiteral)" work for parsing string literals?

The documentation for Text.Megaparsec.Char.Lexer.charLiteral suggests using char '"' *> manyTill charLiteral (char '"') for parsing string literals (where manyTill is defined in the module Control.Applicative.Combinators in the parser-combinators…
runeks
  • 1,745
  • 2
  • 17
  • 26
4
votes
1 answer

Functional dependency with specialization rule

I want to write a specialization rewrite rule for a Megaparsec combinator so that the rule only fires when the input type is a ByteString. {-# LANGUAGE ExplicitForAll #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeFamilies #-} import…
James Brock
  • 3,236
  • 1
  • 28
  • 33
1
2 3 4 5 6