webapper/parzrkit

1.0.0-a 2016-04-24 00:29 UTC

This package is not auto-updated.

Last update: 2024-04-24 11:30:07 UTC


README

ParzRKit is an abstract parser library written in PHP for future purposes

This is a part of a light-weight framework, Prometheus: http://webapper.vallalatiszolgaltatasok.hu/#!/prometheus (language only in hungarian, sorry)

Creative Commons License
ParzRKit by Assarte D'Raven is licensed under a Creative Commons Attribution 4.0 International License.

How you help me

Feel free to use my lib, I hope that you may enjoy that and may help you on your better efficiency. Well, you should donate me some credits via PayPal if my help counts for you on your work:

PayPal - The safer, easier way to pay online!

...or give me some positive feedback on my e-mail adress (you can see that in my profile).

Thanks anyway!

How it works

It's a quite easy way to use this lib basically:

  1. Create a Lexer class which extends ParzRKit's Lexer, and create its Node classes
  2. Create Linker class(es) which are extends ParzRKit's Linker - keep in mind that your Linker classes must fits your needs of your Node classes!
  3. Nearly just Parser->parse()->compile()->link(); I said: nearly

This is a working example of usage:

use ParzRKit\Parser\Lexer\AbstractToken;
use ParzRKit\Parser\Lexer;
use ParzRKit\Linker;
use ParzRKit\Parser;
use ParzRKit\Compiler;
use ParzRKit\Parser\Lexer\DataStreamToken;

define('SRC', realpath(dirname(dirname(__FILE__))).'/src/vendor/assarte/parzrkit/lib/');

require SRC.'Parser/Lexer/AbstractToken.php';
require SRC.'Parser/Lexer/ComposedToken.php';
require SRC.'Parser/Lexer/DataStreamToken.php';
require SRC.'Parser/ReturnToParentException.php';
require SRC.'Parser/Lexer.php';
require SRC.'Parser.php';
require SRC.'Linker.php';
require SRC.'Compiler/CompileException.php';
require SRC.'Compiler/Exception/NotAllowedException.php';
require SRC.'Compiler/Exception/NotMetException.php';
require SRC.'Compiler/BasicNode.php';
require SRC.'Compiler/StrictNode.php';
require SRC.'Compiler/RecursiveNodeIterator.php';
require SRC.'Compiler.php';

class TestToken extends DataStreamToken
{
	public function isDataStream()
	{
		return false;
	}
	
	public function identifyOpenTag()
	{
		if ($this->stream{0} == '/') {
			$this->openTag = '/';
		}
	}
}

class TestSubToken extends AbstractToken
{
	public function identifyOpenTag()
	{
		if ($this->stream{0} == '[') {
			$this->openTag = '[';
		}
	}

	public function identifyCloseTag($inStream=null)
	{
		$pos = $this->getCursor();
		if ($inStream !== null) {
			$pos = 0;
		} else {
			$inStream = $this->processed;
		}
		
		if ($inStream{$pos} == ']') {
			$this->closeTag = ']';
			return true;
		}

		return false;
	}

	public function guessCloseTag()
	{
		return ']';
	}
}

class TestLexer extends Lexer
{
	protected function registerTokeners()
	{
		$this->addTokener('TestToken');
		$this->addTokener('TestSubToken');
	}
}

class TestLinker extends Linker
{
	public function link()
	{
		$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($this));
		foreach ($it as $link) {
			echo str_repeat('| ', $it->getDepth() - 1).'|-'.json_encode($link).'<br>';
		}
	}
}

$stream = '[876/t687][aa[bb[dd]]cc]';
$parser = new Parser($stream, TestLexer::getLexer());
$parser->parse();
$compiler = new Compiler(new TestLinker(), $parser->getToken()->getNode());
$compiler->compile();
echo $stream.'<br>';
$compiler->getLinker()->link();

The example above will print:

[876/t687][aa[bb[dd]]cc]
|-"[876\/t687]"
| |-"876"
| |-"\/t687"
|-"[aa[bb[dd]]cc]"
| |-"aa"
| |-"[bb[dd]]"
| | |-"bb"
| | |-"[dd]"
| |-"cc"