Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
add fail core function
Browse files Browse the repository at this point in the history
  • Loading branch information
ylixir committed Feb 20, 2019
1 parent 98622f1 commit c339f31
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
22 changes: 17 additions & 5 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ $success = $parser("foo bar");
$fail = $parser("foobar");
```

### `end`
## `end`

This will check to see if we are at the end of input. Success means there is nothing left to parse.

Expand All @@ -94,6 +94,18 @@ $success = $parser("foo");
$failure = $parser("foobar");
```

## `fail`

Always fails.

#### OOP and FP

```php
$parser = p::fail();

assert(null === $parser("foo"));
```

## `fold`

Similar to `array_reduce`, this function can be used to combine values. For example, you might want to turn the array `["1","2","3"]` into the integer `123`.
Expand Down Expand Up @@ -128,7 +140,7 @@ $parser = p::fold(
assert(["flowers"] === $parser("flowerflower"));
```

### `lit`
## `lit`

Checks to see if the unparsed data starts with the *lit*eral.

Expand All @@ -141,7 +153,7 @@ $success = $parser("foobar");
$fail = $parser("bar");
```

### `map`
## `map`

This is used to convert raw data to more useful types. For example you might wish to convert a string containing an integer into an actual integer.

Expand All @@ -167,7 +179,7 @@ $parser = p::map(function (string $s): bool {
assert([true] == $parser("yes")->parsed);
```

### `not`
## `not`

Fails if the given parser is successful. Succeeds if not.

Expand All @@ -181,7 +193,7 @@ assert([] === $parser("bar")->parsed);
assert("bar" === $parser("bar")->unparsed);
```

### `or`
## `or`

Tries a list of parsers in order until one succeeds.

Expand Down
11 changes: 11 additions & 0 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class Functions
const drop = self::class . "::drop";
const end = self::class . "::end";
const eol = self::class . "::eol";
const fail = self::class . "::fail";
const float = self::class . "::float";
const fold = self::class . "::fold";
const hex = self::class . "::hex";
Expand Down Expand Up @@ -122,6 +123,16 @@ public static function eol(): callable
return self::or(self::lit("\n"), self::lit("\r\n"), self::lit("\r"));
}

/**
* @return callable(string):null
*/
public static function fail(): callable
{
return function (string $s): ?r {
return null;
};
}

/**
* @return callable(string):?r<float>
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Oop.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class Oop
//convenience constants for passing functions to functions
const binary = self::class . "::binary";
const eol = self::class . "::eol";
const fail = self::class . "::fail";
const float = self::class . "::float";
const hex = self::class . "::hex";
const int = self::class . "::int";
Expand Down Expand Up @@ -69,6 +70,11 @@ public static function eol(): self
return new self(p::eol());
}

public static function fail(): self
{
return new self(p::fail());
}

public static function float(): self
{
return new self(p::float());
Expand Down
7 changes: 7 additions & 0 deletions test/Unit/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ public function test_eol(string $input, ?r $expected): void
self::assertEquals($expected, $p($input));
}

public function test_fail(): void
{
$parser = p::fail();

self::assertEquals(null, $parser("foo"));
}

public function float_provider(): array
{
return [
Expand Down
7 changes: 7 additions & 0 deletions test/Unit/OopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ public function testEol(string $input, ?r $expected): void
self::assertEquals($expected, $p($input));
}

public function testFail(): void
{
$parser = p::fail();

self::assertEquals(null, $parser("foo"));
}

public function floatProvider(): array
{
return [
Expand Down

0 comments on commit c339f31

Please sign in to comment.