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

Parsing expressions inside arithmetic expressions

I would like to parse arithmetic expressions. Here is my current parser: data AExpr = ExprAsAExpr Expr | IntConst Integer | Neg AExpr | ABinary ABinOp AExpr AExpr deriving (Show, Eq) aExpr :: Parser AExpr aExpr =…
Michael
  • 3,411
  • 4
  • 25
  • 56
2
votes
0 answers

Megaparsec: Unexpected input consumption when using try

I'm currently writing my simple programming language parser in Haskell with megaparsec library. I found this megaparsec tutorial, and I wrote following parser code: import Data.Void import Text.Megaparsec import Text.Megaparsec.Char import…
suhdonghwi
  • 955
  • 1
  • 7
  • 20
2
votes
1 answer

parsec how to recursively parse simple expression?

I have a strings like that: ***, **(*)*, ****(**(**)*)** And I want to parse it in the data structure like that: data Tree = Node [S] Tree Tree | Empty where S is * (* doesn't mean any char it is just star symbol) I tried to build parser (I use…
Sergey Sosnin
  • 1,313
  • 13
  • 30
2
votes
1 answer

Parse EBNF with Megaparsec nested sepBy

As an exercise I try to parse a EBNF/ABNF grammar with Megaparsec. I got trivial stuff like terminals and optionals working, but I'm struggling with alternatives. With this grammar: S ::= 'hello' ['world'] IDENTIFIER LITERAL | 'test'; And this…
Jan van Brügge
  • 742
  • 8
  • 13
1
vote
1 answer

Megaparsec parse many until given marker with backtracking

I'm trying to parse the following input using megaparsec (if this sounds familiar to the advent of code challenge from day 7 2022, it's because it is): $ cd / $ ls dir a 123 foo.txt dir d $ cd a $ ls dir e 4567 f 78901 h.zip $ cd e I want to parse…
l7r7
  • 1,134
  • 1
  • 7
  • 23
1
vote
1 answer

Getting current context error formatting function

I am using Megaparsec to get tree representation of the code, which is later evaluated by separated functions. I would like to add to the nodes of the tree representation the parsec function with the current context to format the error. Why? Eg. the…
Přemysl Šťastný
  • 1,676
  • 2
  • 18
  • 39
1
vote
0 answers

How can I parse hexadecimal string into Char in Haskell?

I am writing a parser using megaparsec and want to parse hex number to Char. The hex number parser should take exactly two chars and return a char that has the same numeric value. Right now I have something like this. type Parser = Parsec Void…
Dmitry
  • 11
  • 2
1
vote
1 answer

Adding string literals support in parser

Now I'w working on my university homework and one of the tasks is to add string literals support in my Haskell parser of dummy programming language (named "Hi"). I solved this task with that code: parseString = do res <- char '\"' *> manyTill…
ppv
  • 21
  • 3
1
vote
1 answer

How to parse multiple lines with megaparsec? many . many runs into space leak

I'd like to parse some very simple text for example, "abcxyzzzz\nhello\n" into ["abcxyzzz", "hello"] :: String. Not looking for a simpler function to do this (like words) as I need to parse something more complex and I'm just laying the foundations…
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
1
vote
0 answers

How to retrieve comments using Megaparsec?

I'm printing an AST in order to format the source file, from which the AST is generated using Megaparsec. Comments are missing from the AST, but I intend to keep them after reformatting. Is there a way in Megaparsec to obtain all comments (for both…
sinoTrinity
  • 1,125
  • 2
  • 15
  • 27
1
vote
1 answer

Operator precedence issue when parsing with Megaparsec

I was parsing a C-like language with array and struct. Following C operator precedence, . and [] are made of equal precedence. opTable :: [[Operator Parser Expr]] opTable = [[ InfixL $ Access <$ symbol "." , opSubscript]] opSubscript = Postfix $…
sinoTrinity
  • 1,125
  • 2
  • 15
  • 27
1
vote
1 answer

Chaining unary operators with megaparsec

I have written a parser using megaparsec for a very simple language consisting of integer literals and two unary operators "a" and "b": data ValueExpr = OpA ValueExpr | OpB ValueExpr | Integer Integer valueExpr ::…
Peter
  • 2,919
  • 1
  • 16
  • 35
1
vote
3 answers

How to properly parse indented block with megaparsec?

I'm trying to make an indentation based programming language, and I'm trying to parse something like: expr1 : expr2 expr3 Here, essentially : indicates the start of a new indentation block, so expr1 is completely irrelevant, the idea is that :…
Nick Tchayka
  • 563
  • 3
  • 14
1
vote
0 answers

Megaparsec: nested indented blocks eat too much

I am having trouble with nested indented blocks swallowing new lines. I want to parse a function definition (or rather skip the entire body, func. def. is not necessarily top-level). Example: func F() -> void: pass func m(): pass func f(a:= 4,…
menfon
  • 1,587
  • 1
  • 11
  • 28
1
vote
2 answers

Haskell - intersperse a parser with another one

I have two parsers parser1 :: Parser a and parser2 :: Parser a. I would like now to parse a list of as interspersing them with parser2 The desired signature is something like interspersedParser :: Parser b -> Parser a -> Parser [a] For example, if…
marcosh
  • 8,780
  • 5
  • 44
  • 74