> For the complete documentation index, see [llms.txt](https://glados.guillaume-hein.fr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://glados.guillaume-hein.fr/developpers/parsing.md).

# Parsing

{% hint style="info" %}
This page is dedicated to the documentation of the internal code of the **GoMelan** parser.
{% endhint %}

## Type instances

### Application Parser instance:

* Pure : takes a value and returns a parser that always succeeds with that value.
* <\*> : applies a function inside a parser to a value inside a parser.

### Alternative Parser instance:

* empty : represents a parser that always fails.
* <|> : try the first parser and, if it fails, try the second parser.

### Monad Parser instance

* Pure : already define.
* \>>= : applies a parser to a value, then applies a function to the result.

## Functions and Operators

```haskell
(<?>) :: Parser a -> (Remaining -> ParseError) -> Parser a
```

* Operator similar to \<?> for custom error messages.
* Add error message in stack error.<br>

## **Utilization**

### &#x20;Applicative Instance Examples:

**`pure`**

```haskell
haskellCopy code-- Creating a parser that succeeds with the value 42
resultParser :: Parser Int
resultParser = pure 42
```

**`<*>`**

```haskell
haskellCopy code-- Combining parsers to apply a function within a parser to a value within a parser
additionParser :: Parser Int
additionParser = (+) <$> parseNumber <*> parseNumber
```

### Alternative Instance Examples:

**`empty`**

```haskell
haskellCopy code-- Creating a parser that always fails
failParser :: Parser Int
failParser = empty
```

**`<|>`**

```haskell
haskellCopy code-- Combining parsers to try one parser, and if it fails, try the second one
alternativeParser :: Parser Int
alternativeParser = successfulParser <|> failParser
```

### Monad Instance Examples:

**`>>=`**

```haskell
haskellCopy code-- Chaining parsers to apply a parser to a value, and then apply a function to the result
chainParser :: Parser Int
chainParser = parseNumber >>= \x -> parseNumber >>= \y -> return (x * y)
```

**`(<?>)`**

```haskell
haskellCopy code-- Applying a custom error message in case of failure
customErrorParser :: Parser Int
customErrorParser = (parseNumber <?> CustomErrorFunction) >>= \x -> ...
```
