finished default parser

This commit is contained in:
2018-04-24 07:19:17 -04:00
parent ff99a0eaaf
commit 8a1c752045
26 changed files with 1757 additions and 6 deletions

View File

@@ -0,0 +1,40 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Author implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'A') {
$this->builder->addAuthor(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Book implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'B') {
$this->builder->addBook(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Discography implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'D') {
$this->builder->addDiscography(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class EndOfTune implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
// list($key, $data) = $this->getKeyDataFromLine($line);
if ($this->isEndOfFile($context) || $this->isEndOfMusic($context, $line)) {
$this->builder->handleEndOfMusic();
}
return $next($context);
}
protected function isEndOfFile(Context $context)
{
$key = $context->key();
return $context->count() <= $key+1;
}
private function isEndOfMusic(Context $context, string $line)
{
return $context->isState(Context::MODE_TUNE_BODY)
&& preg_match('/^\s*$/', $line);
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Filename implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'F'
&& $context->getState() === $context::MODE_TUNE_HEADER
) {
$this->builder->addFilename(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Group implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'G') {
$this->builder->addGroup(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class History implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
* @throws \Enzyme\Collection\CollectionException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'H'
&& $context->isState($context::MODE_TUNE_HEADER)
) {
$this->builder->addHistory(new StringAtom($data));
$context->setStateInHistory();
} elseif ($context->isState($context::MODE_HISTORY)) {
$this->builder->addHistory(new StringAtom($line));
if ($this->nextLineNotHistory($context)) {
$context->setStateInTuneHeader();
}
}
return $next($context);
}
/**
* @param Context $context
* @return bool
* @throws \Enzyme\Collection\CollectionException
*/
private function nextLineNotHistory(Context $context)
{
$nextLine = $context->get($context->key() +1);
list($key, $data) = $this->getKeyDataFromLine($nextLine);
return $key !== '';
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class KeySignature implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($context->getState() === $context::MODE_TUNE_HEADER && $key === 'K') {
$this->builder->setKey(new StringAtom($data));
$context->setStateInTuneBody();
}
return $next($context);
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Meter implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'M'
&& $context->getState() === $context::MODE_TUNE_HEADER
) {
$this->builder->setMeter(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class MusicLine implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($context->getState() === $context::MODE_TUNE_BODY
&& $key !== 'K'
&& $line !== ''
) {
$this->builder->appendMusic(new StringAtom($line));
}
return $next($context);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class NoteLength implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'L') {
$this->builder->addNoteLength(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Notes implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'N') {
$this->builder->addNote(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Origin implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'O') {
$this->builder->addOrigin(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Parts implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'P'
&& $context->getState() === $context::MODE_TUNE_HEADER
) {
$this->builder->setParts(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Rhythm implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'R'
&& $context->getState() === $context::MODE_TUNE_HEADER
) {
$this->builder->addRhythm(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Source implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'S'
&& $context->getState() === $context::MODE_TUNE_HEADER
) {
$this->builder->addSource(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Tempo implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'Q') {
$this->builder->addTempo(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Transcriber implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'Z') {
$this->builder->addTranscriber(new StringAtom($data));
}
return $next($context);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
use Enzyme\Axiom\Atoms\StringAtom;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
class Words implements Lex
{
use LineKeyDataTrait;
/**
* @var Builder
*/
protected $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @param Context $context
* @param \Closure $next
* @return mixed
* @throws \Enzyme\Axiom\Exceptions\AtomException
*/
public function handle(Context $context, \Closure $next)
{
$line = $context->current();
list($key, $data) = $this->getKeyDataFromLine($line);
if ($key === 'W') {
$this->builder->addWords(new StringAtom($data));
}
return $next($context);
}
}