From eb9c3651614dd5e5aef067880092e9f622c264df Mon Sep 17 00:00:00 2001 From: zhukv Date: Sat, 3 Aug 2013 13:47:47 +0300 Subject: Fix to PSR standart, fix tokenizer, fix function executor. --- src/NXP/Classes/Func.php | 45 +++++++++++ src/NXP/Classes/Operand.php | 89 ++++++++++++++++++++++ src/NXP/Classes/Token.php | 56 ++++++++++++++ src/NXP/Classes/TokenParser.php | 165 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 355 insertions(+) create mode 100644 src/NXP/Classes/Func.php create mode 100644 src/NXP/Classes/Operand.php create mode 100644 src/NXP/Classes/Token.php create mode 100644 src/NXP/Classes/TokenParser.php (limited to 'src/NXP/Classes') diff --git a/src/NXP/Classes/Func.php b/src/NXP/Classes/Func.php new file mode 100644 index 0000000..e8c0fa2 --- /dev/null +++ b/src/NXP/Classes/Func.php @@ -0,0 +1,45 @@ +name = $name; + $this->callback = $callback; + } + + public function getName() + { + return $this->name; + } + + public function getCallback() + { + return $this->callback; + } +} diff --git a/src/NXP/Classes/Operand.php b/src/NXP/Classes/Operand.php new file mode 100644 index 0000000..fae0c69 --- /dev/null +++ b/src/NXP/Classes/Operand.php @@ -0,0 +1,89 @@ +association = $association; + $this->symbol = $symbol; + $this->type = $type; + $this->priority = $priority; + $this->callback = $callback; + } + + public function getAssociation() + { + return $this->association; + } + + public function getSymbol() + { + return $this->symbol; + } + + public function getType() + { + return $this->type; + } + + public function getCallback() + { + return $this->callback; + } + + public function getPriority() + { + return $this->priority; + } + +} diff --git a/src/NXP/Classes/Token.php b/src/NXP/Classes/Token.php new file mode 100644 index 0000000..bbcd5bc --- /dev/null +++ b/src/NXP/Classes/Token.php @@ -0,0 +1,56 @@ +type = $type; + $this->value = $value; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @return string + */ + public function getValue() + { + return $this->value; + } + +} diff --git a/src/NXP/Classes/TokenParser.php b/src/NXP/Classes/TokenParser.php new file mode 100644 index 0000000..f498184 --- /dev/null +++ b/src/NXP/Classes/TokenParser.php @@ -0,0 +1,165 @@ + '[0-9\.]', + self::CHAR => '[a-z_]', + self::SPECIAL_CHAR => '[\!\@\#\$\%\^\&\*\/\|\-\+\=\~]', + self::LEFT_BRACKET => '\(', + self::RIGHT_BRACKET => '\)', + self::SPACE => '\s' + ); + + const ERROR_STATE = 'ERROR_STATE'; + + private $transitions = array( + Token::NOTHING => array( + self::DIGIT => Token::NUMBER, + self::CHAR => Token::STRING, + self::SPECIAL_CHAR => Token::OPERATOR, + self::LEFT_BRACKET => Token::LEFT_BRACKET, + self::RIGHT_BRACKET => Token::RIGHT_BRACKET, + self::SPACE => Token::NOTHING + ), + Token::STRING => array( + self::DIGIT => Token::STRING, + self::CHAR => Token::STRING, + self::SPECIAL_CHAR => Token::OPERATOR, + self::LEFT_BRACKET => Token::LEFT_BRACKET, + self::RIGHT_BRACKET => Token::RIGHT_BRACKET, + self::SPACE => Token::NOTHING + ), + Token::NUMBER => array( + self::DIGIT => Token::NUMBER, + self::CHAR => self::ERROR_STATE, + self::SPECIAL_CHAR => Token::OPERATOR, + self::LEFT_BRACKET => Token::LEFT_BRACKET, + self::RIGHT_BRACKET => Token::RIGHT_BRACKET, + self::SPACE => Token::NOTHING + ), + Token::OPERATOR => array( + self::DIGIT => Token::NUMBER, + self::CHAR => Token::STRING, + self::SPECIAL_CHAR => Token::OPERATOR, + self::LEFT_BRACKET => Token::LEFT_BRACKET, + self::RIGHT_BRACKET => Token::RIGHT_BRACKET, + self::SPACE => Token::NOTHING + ), + self::ERROR_STATE => array( + self::DIGIT => self::ERROR_STATE, + self::CHAR => self::ERROR_STATE, + self::SPECIAL_CHAR => self::ERROR_STATE, + self::LEFT_BRACKET => self::ERROR_STATE, + self::RIGHT_BRACKET => self::ERROR_STATE, + self::SPACE => self::ERROR_STATE + ), + Token::LEFT_BRACKET => array( + self::DIGIT => Token::NUMBER, + self::CHAR => Token::STRING, + self::SPECIAL_CHAR => Token::OPERATOR, + self::LEFT_BRACKET => Token::LEFT_BRACKET, + self::RIGHT_BRACKET => Token::RIGHT_BRACKET, + self::SPACE => Token::NOTHING + ), + Token::RIGHT_BRACKET => array( + self::DIGIT => Token::NUMBER, + self::CHAR => Token::STRING, + self::SPECIAL_CHAR => Token::OPERATOR, + self::LEFT_BRACKET => Token::LEFT_BRACKET, + self::RIGHT_BRACKET => Token::RIGHT_BRACKET, + self::SPACE => Token::NOTHING + ), + ); + + private $accumulator = ''; + + private $state = Token::NOTHING; + + private $queue = null; + + public function __construct() + { + $this->queue = new \SplQueue(); + } + + /** + * Tokenize math expression + * @param $expression + * @return \SplQueue + * @throws \Exception + */ + public function tokenize($expression) + { + $oldState = null; + for ($i=0; $igetSymbolType($char); + $oldState = $this->state; + $this->state = $this->transitions[$this->state][$class]; + if ($this->state == self::ERROR_STATE) { + throw new \Exception("Parse expression error at $i column (symbol '$char')"); + } + $this->addToQueue($oldState); + $this->accumulator .= $char; + } + if (!empty($this->accumulator)) { + $token = new Token($this->state, $this->accumulator); + $this->queue->push($token); + } + + return $this->queue; + } + + /** + * @param $symbol + * @return string + * @throws \Exception + */ + private function getSymbolType($symbol) + { + foreach ($this->terms as $class => $regex) { + if (preg_match("/$regex/i", $symbol)) { + return $class; + } + } + + throw new \Exception("Unknown char '$symbol'"); + } + + /** + * @param $oldState + */ + private function addToQueue($oldState) + { + if ($oldState == Token::NOTHING) { + $this->accumulator = ''; + + return; + } + + if (($this->state != $oldState) || ($oldState == Token::LEFT_BRACKET) || ($oldState == Token::RIGHT_BRACKET)) { + $token = new Token($oldState, $this->accumulator); + $this->queue->push($token); + $this->accumulator = ''; + } + } +} -- cgit v1.2.3