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
4
votes
1 answer

State with Megaparsec ParsecT is not backtracking

I have a parser defined as a slightly more complicated version of the following: data X = X { getX :: State ([Int], [X]) Bool } type Parser = ParsecT Void String (State ([Int], [X])) The idea is that I can build up a stack of actions I want to do…
otah007
  • 505
  • 1
  • 4
  • 17
4
votes
1 answer

Parse between quotes with Haskell

The requirements are taken from the DOT language specification, more precisely I'm trying to parse the [ID] attribute, which can be e.g., any double-quoted string ("...") possibly containing escaped quotes (\")1; The following should be a minimal…
Vey
  • 435
  • 5
  • 15
4
votes
1 answer

Left and right recursive parser

This is an evolution of this question. I need to parse with megaparsec a data structure like data Foo = Simple String Dotted Foo String Paren String Foo and I would like to parse to it strings like foo ::= alphanum | foo "."…
marcosh
  • 8,780
  • 5
  • 44
  • 74
4
votes
0 answers

Parser library for Haskell-like expression syntax

I want to extend an toy functional programming language to accept Haskell-like infix operators. Here are the characteristics I am interested in: Parser dynamically updates itself to accept newly defined operators. Infix operators can be converted…
user3368561
  • 779
  • 6
  • 18
4
votes
0 answers

Propagation of (Mega)Parsec errors through continuations

I have a somewhat techinical question about the inner workings of (Mega)Parsec, the informal context of which is: What is the idea governing the accumulation/propagation of error messages through continuations in Parsec? Specifically: Consider the…
fmg
  • 813
  • 8
  • 18
4
votes
1 answer

how to write a parser that does not consume space?

I'm writing a program to modify source code files. I need to parse the file (e.g. with megaparsec), modify its Abstract Syntax Tree AST (e.g. with Uniplate), and regenerate the file with as little changes as possible (e.g. preserving spaces,…
Pierre Carbonnelle
  • 2,305
  • 19
  • 25
4
votes
1 answer

Indentation-aware parsing of expression trees

As a follow-up to this question, I am now trying to parse an expression language that has variables and case ... of ... expressions. The syntax should be indentation-based: Expressions can span multiple lines, as long as every line is indented…
Cactus
  • 27,075
  • 9
  • 69
  • 149
3
votes
1 answer

Parse many terms not followed by symbol

I am attempting to develop a lambda calculus interpreter, which supports definitions of terms. For that, I'm using the Megaparsec library in Haskell. I have the following grammar: term := var | lambda var . term | term term def := var =…
JPYamamoto
  • 496
  • 6
  • 17
3
votes
1 answer

Quasiquotes escaping

I would like to add my new language to Haskell using the Quasiquotes, but the language itself uses |] as a keyword. Is there some way, how to: a) Escape |], so it is passed to my language b) Let the parser of my language decide, when the…
Přemysl Šťastný
  • 1,676
  • 2
  • 18
  • 39
3
votes
3 answers

How to fail a nested megaparsec parser?

I am stuck at the following parsing problem: Parse some text string that may contain zero or more elements from a limited character set, up to but not including one of a set of termination characters. Content/no content should be indicated through…
Ulrich Schuster
  • 1,670
  • 15
  • 24
3
votes
1 answer

Correctly parsing nested data using megaparsec

I am trying to get more familiar with megaparsec, and I am running into some issues with presedences. By 'nested data' in the title I refer to the fact that I am trying to parse types, which in turn could contain other types. If someone could…
Robert
  • 165
  • 10
3
votes
1 answer

How do I turn this regex into a Megaparsec parser without making a mess?

Consider this regex: ^foo/[^=]+/baz=(.*),[^,]*$ If I run it on foo/bar/baz=one,two, it matches and the subgroup captures one. If I run it on foo/bar/baz/bar/baz=three,four,five, it matches and the subgroup captures three,four. I know how to turn…
3
votes
1 answer

Parsing with Haskell/Megaparsec: StateT for building up local, lexical scope?

So I'm trying to do the standard "write yourself a parser for a scheme-like language" exercise to figure out MegaParsec and monad transformers. Following the suggestions of many tutorials and blog posts, I'm using ReaderT and local to implement…
fmg
  • 813
  • 8
  • 18
3
votes
1 answer

Recursive Parser

I need to parse, using Megaparsec a data structure like data Foo = Simple String | Dotted Foo String where I can have alphanumeric strings separated by dots. For example abc should be parsed to Simple "abc" and abc.def to Dotted (Simple…
marcosh
  • 8,780
  • 5
  • 44
  • 74
3
votes
1 answer

Operator precedence in megaparsec

I'm having trouble using Megaparsec 6's makeExprParser helper. I can't seem to figure out how to bind both binary ^ and unary - at the precedence levels I'd expect. Using this makeExprParser expression parser: expressionParser :: Parser…
david king
  • 738
  • 5
  • 9