-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a file based example and tweak excel example to be reuseable
- Loading branch information
1 parent
3588c33
commit 1b32450
Showing
5 changed files
with
146 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Tests\Verraes\Parsica\Examples; | ||
|
||
class Cell | ||
{ | ||
private $col; | ||
private $row; | ||
|
||
function __construct($col, $row) | ||
{ | ||
$this->col = $col; | ||
$this->row = $row; | ||
} | ||
} | ||
class Range | ||
{ | ||
private Cell $from; | ||
private Cell $to; | ||
|
||
function __construct(Cell $from, Cell $to) | ||
{ | ||
$this->from = $from; | ||
$this->to = $to; | ||
} | ||
} | ||
class Intersection | ||
{ | ||
private Range $l; | ||
private Range $r; | ||
|
||
function __construct(Range $l, Range $r) | ||
{ | ||
$this->l = $l; | ||
$this->r = $r; | ||
} | ||
} | ||
class Sum | ||
{ | ||
private Intersection $intersection; | ||
|
||
function __construct(Intersection $intersection) | ||
{ | ||
$this->intersection = $intersection; | ||
} | ||
} | ||
class Ampersand | ||
{ | ||
private Cell $l; | ||
private Cell $r; | ||
|
||
function __construct(Cell $l, Cell $r) | ||
{ | ||
$this->l = $l; | ||
$this->r = $r; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of the Parsica library. | ||
* | ||
* Copyright (c) 2020 Mathias Verraes <mathias@verraes.net> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Tests\Verraes\Parsica\Examples; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Verraes\Parsica\Parser; | ||
use Verraes\Parsica\PHPUnit\ParserAssertions; | ||
use Verraes\Parsica\TextFileStream; | ||
use function Verraes\Parsica\{alphaChar, between, char, collect, digitChar, skipHSpace1, space, string}; | ||
|
||
final class ExcelTextFileStreamTest extends TestCase | ||
{ | ||
use ParserAssertions; | ||
|
||
private string $stubBasePath; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); // TODO: Change the autogenerated stub | ||
$this->stubBasePath = dirname(__DIR__) . "/stubs/"; | ||
} | ||
|
||
/** @test */ | ||
public function spaceOrOperatorDependingOnContext() | ||
{ | ||
// https://twitter.com/Mark_Baker/status/1309919606887374849?s=20 | ||
// and https://twitter.com/Mark_Baker/status/1309960902482026498?s=20 | ||
// `=SUM(B7:D7 C6:C8)` where space is the intersection operator for the | ||
// intersection between the two ranges B7:D7 and C6:C8 (ie. C7), | ||
// and `=A1 & B1` where the space is simply whitespace and should be ignored | ||
|
||
$parser = $this->excelParser(); | ||
|
||
$input = new TextFileStream($this->stubBasePath . 'example-excel-1.txt'); | ||
$expected = new Sum( | ||
new Intersection( | ||
new Range(new Cell("B", "7"), new Cell("D", "7")), | ||
new Range(new Cell("C", "6"), new Cell("C", "8")), | ||
) | ||
); | ||
$this->assertParsesStream($input, $parser, $expected); | ||
|
||
$input = new TextFileStream($this->stubBasePath . 'example-excel-2.txt'); | ||
$expected = new Ampersand( | ||
new Cell("A", "1"), | ||
new Cell("B", "1"), | ||
); | ||
$this->assertParsesStream($input, $parser, $expected); | ||
} | ||
|
||
private function excelParser(): Parser | ||
{ | ||
$parens = fn(Parser $p): Parser => between(char('('), char(')'), $p); | ||
$cell = collect(alphaChar(), digitChar()) | ||
->map(fn($o) => new Cell($o[0], $o[1])); | ||
$range = collect($cell, char(':'), $cell) | ||
->map(fn($o) => new Range($o[0], $o[2])); | ||
$intersection = collect($range, space(), $range) | ||
->map(fn($o) => new Intersection($o[0], $o[2])); | ||
$sum = (string('=SUM')->followedBy($parens($intersection))) | ||
->map(fn($o) => new Sum($o)); | ||
|
||
|
||
// consumes space before and after Parser $p | ||
$token = fn(Parser $p): Parser => between(skipHSpace1(), skipHSpace1(), $p); | ||
$ampersand = char('=')->followedBy(collect( | ||
$cell, | ||
$token(char('&')), | ||
$cell | ||
))->map(fn($o) => new Ampersand($o[0], $o[2])); | ||
|
||
|
||
return $sum->or($ampersand); | ||
} | ||
|
||
} | ||
|
||
require_once __DIR__ . '/ExcelClasses.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
=SUM(B7:D7 C6:C8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
=A1 & B1 |