Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
akkie committed Jan 5, 2012
0 parents commit abe6332
Show file tree
Hide file tree
Showing 61 changed files with 3,833 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.idea
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "vendor/mohiva-common"]
path = vendor/mohiva-common
url = git://github.com/mohiva/common.git
13 changes: 13 additions & 0 deletions .travis.yml
@@ -0,0 +1,13 @@
language: php

php:
- 5.4

script: phpunit --configuration phpunit.travis.xml ./tests/com/mohiva/test/pyramid

before_install:
git submodule init && git submodule --quiet update

notifications:
email:
- travis.mohiva.pyramid@mohiva.com
14 changes: 14 additions & 0 deletions LICENSE.textile
@@ -0,0 +1,14 @@
h1. License

Copyright (c) 2007-2012, Christian Kaps
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of Christian Kaps nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11 changes: 11 additions & 0 deletions README.textile
@@ -0,0 +1,11 @@
h2. About

The Mohiva Pyramid project is an operator precedence parser based on the "Precedence climbing" algorithm.

This project is Open Source and released under the terms of the New BSD License.

h2. Links

http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm#climbing
http://en.wikipedia.org/wiki/Operator-precedence_parser
http://en.wikipedia.org/wiki/Interpreter_pattern
13 changes: 13 additions & 0 deletions phpunit.idea.xml
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/com/mohiva/test/pyramid/Bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true">

<logging>
<log type="coverage-clover" target="/tmp/clover.xml" />
</logging>

</phpunit>
15 changes: 15 additions & 0 deletions phpunit.travis.xml
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/com/mohiva/test/pyramid/Bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">

<testsuites>
<testsuite name="Mohiva Common test suite">
<directory suffix="Test.php">./tests/com/mohiva/test/pyramid</directory>
</testsuite>
</testsuites>

</phpunit>
96 changes: 96 additions & 0 deletions src/com/mohiva/pyramid/Grammar.php
@@ -0,0 +1,96 @@
<?php
/**
* Mohiva Pyramid
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.textile.
* It is also available through the world-wide-web at this URL:
* https://github.com/mohiva/pyramid/blob/master/LICENSE.textile
*
* @category Mohiva/Pyramid
* @package Mohiva/Pyramid
* @author Christian Kaps <christian.kaps@mohiva.com>
* @copyright Copyright (c) 2007-2012 Christian Kaps (http://www.mohiva.com)
* @license https://github.com/mohiva/pyramid/blob/master/LICENSE.textile New BSD License
* @link https://github.com/mohiva/pyramid
*/
namespace com\mohiva\pyramid;

/**
* Represents the grammar for the parser.
*
* @category Mohiva/Pyramid
* @package Mohiva/Pyramid
* @author Christian Kaps <christian.kaps@mohiva.com>
* @copyright Copyright (c) 2007-2012 Christian Kaps (http://www.mohiva.com)
* @license https://github.com/mohiva/pyramid/blob/master/LICENSE.textile New BSD License
* @link https://github.com/mohiva/pyramid
*/
class Grammar {

/**
* The operator table.
*
* @var OperatorTable
*/
private $operatorTable = null;

/**
* The operand table.
*
* @var OperandTable
*/
private $operandTable = null;

/**
* The class constructor.
*/
public function __construct() {

$this->operatorTable = new OperatorTable();
$this->operandTable = new OperandTable();
}

/**
* Adds a new operator to the operator table.
*
* @param Operator $operator The operator to add.
* @throws UnsupportedOperatorException if an unsupported operator type was given.
*/
public function addOperator(Operator $operator) {

$this->operatorTable->addOperator($operator);
}

/**
* Adds a new operand to the operand table.
*
* @param Operand $operand The operand to add.
*/
public function addOperand(Operand $operand) {

$this->operandTable->addOperand($operand);
}

/**
* Returns the instance of the operator table.
*
* @return OperatorTable The instance of the operator table.
*/
public function getOperatorTable() {

return $this->operatorTable;
}

/**
* Returns the instance of the operand table.
*
* @return OperandTable The instance of the operand table.
*/
public function getOperandTable() {

return $this->operandTable;
}
}
42 changes: 42 additions & 0 deletions src/com/mohiva/pyramid/Node.php
@@ -0,0 +1,42 @@
<?php
/**
* Mohiva Pyramid
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.textile.
* It is also available through the world-wide-web at this URL:
* https://github.com/mohiva/pyramid/blob/master/LICENSE.textile
*
* @category Mohiva/Pyramid
* @package Mohiva/Pyramid
* @author Christian Kaps <christian.kaps@mohiva.com>
* @copyright Copyright (c) 2007-2012 Christian Kaps (http://www.mohiva.com)
* @license https://github.com/mohiva/pyramid/blob/master/LICENSE.textile New BSD License
* @link https://github.com/mohiva/pyramid
*/
namespace com\mohiva\pyramid;

/**
* Represents a expression node.
*
* A node can be the root node or a node which contains multiple
* nodes or a leaf.
*
* @category Mohiva/Pyramid
* @package Mohiva/Pyramid
* @author Christian Kaps <christian.kaps@mohiva.com>
* @copyright Copyright (c) 2007-2012 Christian Kaps (http://www.mohiva.com)
* @license https://github.com/mohiva/pyramid/blob/master/LICENSE.textile New BSD License
* @link https://github.com/mohiva/pyramid
*/
interface Node {

/**
* Evaluates the node.
*
* @return mixed The result of the evaluation.
*/
public function evaluate();
}
52 changes: 52 additions & 0 deletions src/com/mohiva/pyramid/Operand.php
@@ -0,0 +1,52 @@
<?php
/**
* Mohiva Pyramid
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.textile.
* It is also available through the world-wide-web at this URL:
* https://github.com/mohiva/pyramid/blob/master/LICENSE.textile
*
* @category Mohiva/Pyramid
* @package Mohiva/Pyramid
* @author Christian Kaps <christian.kaps@mohiva.com>
* @copyright Copyright (c) 2007-2012 Christian Kaps (http://www.mohiva.com)
* @license https://github.com/mohiva/pyramid/blob/master/LICENSE.textile New BSD License
* @link https://github.com/mohiva/pyramid
*/
namespace com\mohiva\pyramid;

use com\mohiva\common\parser\TokenStream;

/**
* Represents an operand.
*
* @category Mohiva/Pyramid
* @package Mohiva/Pyramid
* @author Christian Kaps <christian.kaps@mohiva.com>
* @copyright Copyright (c) 2007-2012 Christian Kaps (http://www.mohiva.com)
* @license https://github.com/mohiva/pyramid/blob/master/LICENSE.textile New BSD License
* @link https://github.com/mohiva/pyramid
*/
interface Operand {

/**
* Returns the identifiers for an operand.
*
* Identifiers are token codes which are defined by the lexer. And they where needed to
* recognize if a token is a part of an operand.
*
* @return array The identifiers for an operand.
*/
public function getIdentifiers();

/**
* Parse the operand.
*
* @param Grammar $grammar The grammar of the parser.
* @param \com\mohiva\common\parser\TokenStream $stream The token stream to parse.
*/
public function parse(Grammar $grammar, TokenStream $stream);
}
99 changes: 99 additions & 0 deletions src/com/mohiva/pyramid/OperandTable.php
@@ -0,0 +1,99 @@
<?php
/**
* Mohiva Pyramid
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.textile.
* It is also available through the world-wide-web at this URL:
* https://github.com/mohiva/pyramid/blob/master/LICENSE.textile
*
* @category Mohiva/Pyramid
* @package Mohiva/Pyramid
* @author Christian Kaps <christian.kaps@mohiva.com>
* @copyright Copyright (c) 2007-2012 Christian Kaps (http://www.mohiva.com)
* @license https://github.com/mohiva/pyramid/blob/master/LICENSE.textile New BSD License
* @link https://github.com/mohiva/pyramid
*/
namespace com\mohiva\pyramid;

use com\mohiva\pyramid\exceptions\DoubleIdentifierUsageException;
use com\mohiva\pyramid\exceptions\InvalidIdentifierException;

/**
* Contains a list of operands.
*
* @category Mohiva/Pyramid
* @package Mohiva/Pyramid
* @author Christian Kaps <christian.kaps@mohiva.com>
* @copyright Copyright (c) 2007-2012 Christian Kaps (http://www.mohiva.com)
* @license https://github.com/mohiva/pyramid/blob/master/LICENSE.textile New BSD License
* @link https://github.com/mohiva/pyramid
*/
class OperandTable {

/**
* The list with registered operands.
*
* The key is the identifier of the operand and the value
* is the operand object itself.
*
* If as example an operand has two identifiers than this array
* contains also two entries for this operand. All objects are
* stored as reference, so this should not be problem.
*
* @var Operand[]
*/
private $operands = array();

/**
* Adds a new operand to the table.
*
* @param Operand $operand The operand to add.
* @throws DoubleIdentifierUsageException if an identifier of the given operand is registered for an other operand.
*/
public function addOperand(Operand $operand) {

foreach ($operand->getIdentifiers() as $identifier) {
if (isset($this->operands[$identifier])) {
$operand = get_class($this->operands[$identifier]);
$message = "The identifier `{$identifier}` is already in use for operand `{$operand}`";
throw new DoubleIdentifierUsageException($message);
}

$this->operands[$identifier] = $operand;
}
}

/**
* Check if an operand is registered for the given token.
*
* @param Token $token The token to check for.
* @return boolean True if a operand for the given token exists, false otherwise.
*/
public function isRegistered(Token $token) {

if (isset($this->operands[$token->getCode()])) {
return true;
}

return false;
}

/**
* Get the operand for the given token.
*
* @param Token $token The token which must be defined(as identifier) for the operand.
* @return Operand The operand for the given token.
* @throws InvalidIdentifierException if no operand for the given token exists.
*/
public function getOperand(Token $token) {

if ($this->isRegistered($token)) {
return $this->operands[$token->getCode()];
}

throw new InvalidIdentifierException("No operand for identifier `{$token->getCode()}` exists in table");
}
}

0 comments on commit abe6332

Please sign in to comment.