Skip to content

Commit

Permalink
Add more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros committed May 1, 2021
1 parent a8aeaac commit d48ce40
Showing 1 changed file with 76 additions and 19 deletions.
95 changes: 76 additions & 19 deletions docs/parsers.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ Result:
[1, 2]
```

## Between
### Between

Matches a parser when between two other ones.

Expand All @@ -482,7 +482,7 @@ Result:
0 // failure
```

## Deferred
### Deferred

Creates a parser that can be references before it is actually defined. This is used when there is a cyclic dependency between parsers.

Expand All @@ -508,7 +508,7 @@ Result:
1
```

## Recursive
### Recursive

Creates a parser that can reference itself.

Expand Down Expand Up @@ -537,7 +537,7 @@ Result:
1
```

## Capture
### Capture

Ignores the individual result of a parser and returns the whole matched string instead.

Expand All @@ -562,39 +562,59 @@ Result:
"age = 12"
```

#### Other parsers
## Flow parsers

### AnyCharBefore
### Then

Convert the result of a parser. This is usually used to create custom data structures when a parser succeeds, or to convert it to another type.

```c#
Parser<TextSpan> AnyCharBefore<T>(Parser<T> parser, bool canBeEmpty = false, bool failOnEof = false, bool consumeDelimiter = false)
Parser<U> Then<U>(Func<T, U> conversion)
Parser<U> Then<U>(Func<ParseContext, T, U> conversion)
```

### Empty
Usage:

```c#
Parser<T> Empty<T>()
Parser<object> Empty()
Parser<T> Empty<T>(T value)
var parser =
Terms.Integer()
.AndSkip(Terms.Char(','))
.And(Terms.Integer())
.Then(x => new Point(x.Item1, y.Item2));

parser.Parse("1,2");
```

### OneOf
Result:

```c#
Parser<T> OneOf<T>(params Parser<T>[] parsers)
```
Point { x: 1, y: 2}
```

### Then
### ElseError

Fails parsing with a custom error message.

```c#
Parser<U> Then<U>(Func<T, U> conversion)
Parser<U> Then<U>(Func<ParseContext, T, U> conversion)
Parser<T> ElseError(string message)
```

### ElseError
Usage:

```c#
Parser<T> ElseError(string message)
var parser =
Terms.Integer().ElseError("Expected an integer")
.AndSkip(Terms.Char(',').ElseError("Expected a comma"))
.And(Terms.Integer().ElseError("Expected an integer"))
.Then(x => new Point(x.Item1, y.Item2));

parser.Parse("1,");
```

Result:

```
failure: "Expected an integer at (1:3)
```

### Error
Expand All @@ -604,27 +624,64 @@ Parser<T> Error(string message)
Parser<U> Error<U>(string message)
```


### When

Adds some additional logic for a parser to succeed.

```c#
Parser<T> When(Func<T, bool> predicate)
```

### Switch

Returns the next parser based on some custom logic.

```c#
Parser<U> Switch<U>(Func<ParseContext, T, Parser<U>> action)
```

### Eof

Expects the end of the string.

```c#
Parser<T> Eof()
```

### Discard

Discards the previous result and replaces it with the default value or a custom one.

```c#
Parser<U> Discard<U>()
Parser<U> Discard<U>(U value)
```

#### Other parsers

### AnyCharBefore

Returns any characters until the specified parser is matched.

```c#
Parser<TextSpan> AnyCharBefore<T>(Parser<T> parser, bool canBeEmpty = false, bool failOnEof = false, bool consumeDelimiter = false)
```

### Empty

Always returns successfully, with an optional return type or value.

```c#
Parser<T> Empty<T>()
Parser<object> Empty()
Parser<T> Empty<T>(T value)
```

### OneOf

Like [Or](#Or), with an unlimited list of parsers.

```c#
Parser<T> OneOf<T>(params Parser<T>[] parsers)
```

0 comments on commit d48ce40

Please sign in to comment.