From fce45ad3dbd14b400913a75e348d5b6a395bb90a Mon Sep 17 00:00:00 2001 From: Milos Tomic Date: Wed, 21 Dec 2016 12:13:08 +0100 Subject: [PATCH] code style fixes --- .../GoldParser/Content/DFAStateRecord.php | 3 +- .../GoldParser/Content/LALRStateRecord.php | 3 +- src/Tmilos/GoldParser/Content/RuleRecord.php | 3 +- src/Tmilos/GoldParser/Dfa/Dfa.php | 9 +-- src/Tmilos/GoldParser/Dfa/DfaInterface.php | 2 +- src/Tmilos/GoldParser/LalrParser.php | 62 ++++++++----------- src/Tmilos/GoldParser/Loader.php | 3 +- .../GoldParser/Symbol/SymbolCollection.php | 4 +- .../GoldParser/Symbol/SymbolFactory.php | 4 +- .../GoldParser/Tokenizer/StringTokenizer.php | 12 ++-- 10 files changed, 45 insertions(+), 60 deletions(-) diff --git a/src/Tmilos/GoldParser/Content/DFAStateRecord.php b/src/Tmilos/GoldParser/Content/DFAStateRecord.php index c5bd14f..1e72af4 100644 --- a/src/Tmilos/GoldParser/Content/DFAStateRecord.php +++ b/src/Tmilos/GoldParser/Content/DFAStateRecord.php @@ -45,7 +45,8 @@ public function __construct(Record $record) if (($record->getEntries()->count() - 5) % 3 !== 0) { throw new \RuntimeException('Invalid number of entries for edges in DFA state'); } - for ($i = 5; $i < $record->getEntries()->count(); $i += 3) { + $count = $record->getEntries()->count(); + for ($i = 5; $i < $count; $i += 3) { $this->edgeSubRecords[] = new EdgeSubRecord($record->getEntries()->item($i)->toIntValue(), $record->getEntries()->item($i + 1)->toIntValue()); } } diff --git a/src/Tmilos/GoldParser/Content/LALRStateRecord.php b/src/Tmilos/GoldParser/Content/LALRStateRecord.php index 2d2de49..1d5e97e 100644 --- a/src/Tmilos/GoldParser/Content/LALRStateRecord.php +++ b/src/Tmilos/GoldParser/Content/LALRStateRecord.php @@ -36,7 +36,8 @@ public function __construct(Record $record) if (($record->getEntries()->count() - 3) % 4 !== 0) { throw new \RuntimeException('Invalid number of entries for actions in LALR state'); } - for ($i = 3; $i < $record->getEntries()->count(); $i += 4) { + $count = $record->getEntries()->count(); + for ($i = 3; $i < $count; $i += 4) { $this->subActions[] = new ActionSubRecord( $record->getEntries()->item($i)->toIntValue(), $record->getEntries()->item($i + 1)->toIntValue(), diff --git a/src/Tmilos/GoldParser/Content/RuleRecord.php b/src/Tmilos/GoldParser/Content/RuleRecord.php index d8fdcca..345765b 100644 --- a/src/Tmilos/GoldParser/Content/RuleRecord.php +++ b/src/Tmilos/GoldParser/Content/RuleRecord.php @@ -39,7 +39,8 @@ public function __construct(Record $record) $this->nonTerminal = $record->getEntries()->item(2)->toIntValue(); //skip reserved empty entry $this->symbols = []; - for ($i = 4; $i < $record->getEntries()->count(); ++$i) { + $count = $record->getEntries()->count(); + for ($i = 4; $i < $count; ++$i) { $this->symbols[] = $record->getEntries()->item($i)->toIntValue(); } } diff --git a/src/Tmilos/GoldParser/Dfa/Dfa.php b/src/Tmilos/GoldParser/Dfa/Dfa.php index 62a6aa3..0c6d26b 100644 --- a/src/Tmilos/GoldParser/Dfa/Dfa.php +++ b/src/Tmilos/GoldParser/Dfa/Dfa.php @@ -13,9 +13,6 @@ class Dfa implements DfaInterface { - /** @var StateCollection */ - private $states; - /** @var State */ private $startState; @@ -23,12 +20,10 @@ class Dfa implements DfaInterface private $currentState; /** - * @param StateCollection $states * @param State $startState */ - public function __construct(StateCollection $states, State $startState) + public function __construct(State $startState) { - $this->states = $states; $this->startState = $startState; $this->currentState = $startState; } @@ -38,7 +33,7 @@ public function reset() $this->currentState = $this->startState; } - public function GotoNext($char) + public function gotoNext($char) { $transition = $this->currentState->getTransitions()->find($char); if ($transition) { diff --git a/src/Tmilos/GoldParser/Dfa/DfaInterface.php b/src/Tmilos/GoldParser/Dfa/DfaInterface.php index 39aa65c..e2a38ef 100644 --- a/src/Tmilos/GoldParser/Dfa/DfaInterface.php +++ b/src/Tmilos/GoldParser/Dfa/DfaInterface.php @@ -27,7 +27,7 @@ public function reset(); * * @return State|null */ - public function GotoNext($char); + public function gotoNext($char); /** * The current state in the DFA. diff --git a/src/Tmilos/GoldParser/LalrParser.php b/src/Tmilos/GoldParser/LalrParser.php index 87ae38b..840d80e 100644 --- a/src/Tmilos/GoldParser/LalrParser.php +++ b/src/Tmilos/GoldParser/LalrParser.php @@ -27,7 +27,6 @@ use Tmilos\GoldParser\Lalr\ReduceAction; use Tmilos\GoldParser\Lalr\ShiftAction; use Tmilos\GoldParser\Lalr\State; -use Tmilos\GoldParser\Lalr\StateCollection; use Tmilos\GoldParser\Lalr\StateStack; use Tmilos\GoldParser\Symbol\SymbolCollection; use Tmilos\GoldParser\Symbol\SymbolCommentEnd; @@ -39,7 +38,6 @@ use Tmilos\GoldParser\Symbol\SymbolWhiteSpace; use Tmilos\GoldParser\Tokenizer\NonTerminalToken; use Tmilos\GoldParser\Tokenizer\TerminalToken; -use Tmilos\GoldParser\Tokenizer\Token; use Tmilos\GoldParser\Tokenizer\Tokenizer; use Tmilos\GoldParser\Tokenizer\TokenStack; @@ -48,9 +46,6 @@ class LalrParser /** @var Tokenizer */ private $tokenizer; - /** @var StateCollection */ - private $states; - /** @var State */ private $startState; @@ -86,14 +81,12 @@ class LalrParser /** * @param Tokenizer $tokenizer - * @param StateCollection $states * @param State $startState * @param SymbolCollection $symbols */ - public function __construct(Tokenizer $tokenizer, StateCollection $states, State $startState, SymbolCollection $symbols) + public function __construct(Tokenizer $tokenizer, State $startState, SymbolCollection $symbols) { $this->tokenizer = $tokenizer; - $this->states = $states; $this->startState = $startState; $this->symbols = $symbols; $this->storeTokens = StoreTokensMode::NO_USER_OBJECT(); @@ -209,7 +202,7 @@ public function parse($input) $this->tokenizer->setInput($input); while ($this->continueParsing) { $token = $this->getLookAhead(); - if ($token != null) { + if ($token) { $this->parseTerminal($token); } } @@ -234,9 +227,9 @@ private function parseTerminal(TerminalToken $token) if ($action instanceof ShiftAction) { $this->doShift($token, $action); } elseif ($action instanceof ReduceAction) { - $this->doReduce($token, $action); + $this->doReduce($action); } elseif ($action instanceof AcceptAction) { - $this->doAccept($token, $action); + $this->doAccept(); } else { $this->continueParsing = false; $this->fireParseError($token); @@ -251,7 +244,7 @@ private function doShift(TerminalToken $token, ShiftAction $action) $this->eventDispatcher->dispatch(Events::SHIFT, new ShiftEvent($token, $action->getState())); } - private function doReduce(Token $token, ReduceAction $action) + private function doReduce(ReduceAction $action) { $rhs = $action->getRule()->getRhs(); $reduceLength = count($rhs); @@ -278,13 +271,13 @@ private function doReduce(Token $token, ReduceAction $action) $gotoAction = $currentState->getActions()->item($action->getRule()->getLhs()); if ($gotoAction instanceof GotoAction) { - $this->doGoto($token, $gotoAction); + $this->doGoto($gotoAction); } else { throw new \RuntimeException('Invalid action table in state'); } } - private function doAccept(Token $token, AcceptAction $action) + private function doAccept() { $this->continueParsing = false; $this->accepted = true; @@ -296,7 +289,7 @@ private function doAccept(Token $token, AcceptAction $action) } } - private function doGoto(Token $token, GotoAction $action) + private function doGoto(GotoAction $action) { $this->stateStack->push($action->getState()); $this->eventDispatcher->dispatch(Events::GOTO_EVENT, new GotoEvent($action->getSymbol(), $this->stateStack->peek())); @@ -305,7 +298,7 @@ private function doGoto(Token $token, GotoAction $action) private function doReleaseTokens(NonTerminalToken $token) { if (StoreTokensMode::NEVER()->equals($this->storeTokens) || - (StoreTokensMode::NO_USER_OBJECT()->equals($this->storeTokens) && $token->getUserObject() != null) + (StoreTokensMode::NO_USER_OBJECT()->equals($this->storeTokens) && $token->getUserObject()) ) { $token->clearTokens(); } @@ -316,22 +309,22 @@ private function doReleaseTokens(NonTerminalToken $token) */ private function getLookAhead() { - if ($this->lookAhead != null) { + if ($this->lookAhead) { return $this->lookAhead; } do { $token = $this->tokenizer->retrieveToken(); if ($token->getSymbol() instanceof SymbolCommentLine) { - if (!$this->processCommentLine($token)) { + if (!$this->processCommentLine()) { $this->continueParsing = false; } } elseif ($token->getSymbol() instanceof SymbolCommentStart) { - if (!$this->processCommentStart($token)) { + if (!$this->processCommentStart()) { $this->continueParsing = false; } } elseif ($token->getSymbol() instanceof SymbolWhiteSpace) { - if (!$this->processWhiteSpace($token)) { + if (!$this->processWhiteSpace()) { $this->continueParsing = false; } } elseif ($token->getSymbol() instanceof SymbolError) { @@ -345,9 +338,9 @@ private function getLookAhead() if (!$this->continueParsing) { break; } - } while ($this->lookAhead == null); + } while (!$this->lookAhead); - if ($this->lookAhead != null) { + if ($this->lookAhead) { $event = new TokenReadEvent($this->lookAhead); $this->eventDispatcher->dispatch(Events::TOKEN_READ, $event); if (!$event->isContinue()) { @@ -359,19 +352,16 @@ private function getLookAhead() return $this->lookAhead; } - protected function processWhiteSpace(TerminalToken $token) + private function processWhiteSpace() { return true; } /** - * @param TerminalToken $token - * * @return bool */ - protected function processCommentStart(TerminalToken $token) + private function processCommentStart() { - // return (SkipAfterCommentEnd() != null); $commentDepth = 1; $token = null; while ($commentDepth > 0) { @@ -394,11 +384,9 @@ protected function processCommentStart(TerminalToken $token) } /** - * @param TerminalToken $token - * * @return bool */ - protected function processCommentLine(TerminalToken $token) + private function processCommentLine() { // skip to end of line $result = $this->tokenizer->skipAfterChar("\n"); @@ -414,7 +402,7 @@ protected function processCommentLine(TerminalToken $token) * * @return bool */ - protected function processError(TerminalToken $token) + private function processError(TerminalToken $token) { $event = new TokenErrorEvent($token); $this->eventDispatcher->dispatch(Events::TOKEN_ERROR, $event); @@ -425,7 +413,7 @@ protected function processError(TerminalToken $token) /** * @return SymbolCollection */ - protected function findExpectedTokens() + private function findExpectedTokens() { $symbols = new SymbolCollection(); $state = $this->stateStack->peek(); @@ -438,24 +426,24 @@ protected function findExpectedTokens() return $symbols; } - protected function fireEofError() + private function fireEofError() { $eofToken = new TerminalToken( - SymbolCollection::Eof(), - SymbolCollection::Eof()->getName(), + SymbolCollection::eof(), + SymbolCollection::eof()->getName(), $this->tokenizer->getCurrentLocation() ); $this->fireParseError($eofToken); } - protected function fireParseError(TerminalToken $token) + private function fireParseError(TerminalToken $token) { $event = new ParseErrorEvent($token, $this->findExpectedTokens()); $this->eventDispatcher->dispatch(Events::PARSE_ERROR, $event); $this->continueParsing = ContinueMode::STOP()->equals($event->getContinue()); $this->lookAhead = $event->getNextToken(); - if ($event->getNextToken() != null && ContinueMode::INSERT()->equals($event->getContinue())) { + if ($event->getNextToken() && ContinueMode::INSERT()->equals($event->getContinue())) { $this->tokenizer->setCurrentLocation($token->getLocation()); } } diff --git a/src/Tmilos/GoldParser/Loader.php b/src/Tmilos/GoldParser/Loader.php index 3dc98cd..4142d30 100644 --- a/src/Tmilos/GoldParser/Loader.php +++ b/src/Tmilos/GoldParser/Loader.php @@ -127,7 +127,7 @@ public function getRules() public function createNewTokenizer() { $startState = $this->dfaStates->item($this->content->getInitialStatesRecord()->getDfa()); - $dfa = new Dfa\Dfa($this->dfaStates, $startState); + $dfa = new Dfa\Dfa($startState); return new StringTokenizer($dfa); } @@ -138,7 +138,6 @@ public function createNewParser() return new LalrParser( $this->createNewTokenizer(), - $this->parserStates, $startState, $this->symbols ); diff --git a/src/Tmilos/GoldParser/Symbol/SymbolCollection.php b/src/Tmilos/GoldParser/Symbol/SymbolCollection.php index 46e3e16..6fbbbdd 100644 --- a/src/Tmilos/GoldParser/Symbol/SymbolCollection.php +++ b/src/Tmilos/GoldParser/Symbol/SymbolCollection.php @@ -19,7 +19,7 @@ class SymbolCollection implements \Countable, \IteratorAggregate /** * @return SymbolEnd */ - public static function Eof() + public static function eof() { static $value = null; if (!$value) { @@ -32,7 +32,7 @@ public static function Eof() /** * @return SymbolError */ - public static function Error() + public static function error() { static $value = null; if (!$value) { diff --git a/src/Tmilos/GoldParser/Symbol/SymbolFactory.php b/src/Tmilos/GoldParser/Symbol/SymbolFactory.php index 235ebd4..b5761e2 100644 --- a/src/Tmilos/GoldParser/Symbol/SymbolFactory.php +++ b/src/Tmilos/GoldParser/Symbol/SymbolFactory.php @@ -33,7 +33,7 @@ public static function createSymbol(SymbolRecord $record) return new SymbolWhiteSpace($record->getIndex()); break; case 3: - return SymbolCollection::Eof(); + return SymbolCollection::eof(); break; case 4: return new SymbolCommentStart($record->getIndex()); @@ -45,7 +45,7 @@ public static function createSymbol(SymbolRecord $record) return new SymbolCommentLine($record->getIndex()); break; case 7: - return SymbolCollection::Error(); + return SymbolCollection::error(); break; default: return new SymbolError(-1); diff --git a/src/Tmilos/GoldParser/Tokenizer/StringTokenizer.php b/src/Tmilos/GoldParser/Tokenizer/StringTokenizer.php index f89d90b..1b5054d 100644 --- a/src/Tmilos/GoldParser/Tokenizer/StringTokenizer.php +++ b/src/Tmilos/GoldParser/Tokenizer/StringTokenizer.php @@ -58,26 +58,26 @@ public function retrieveToken() $acceptInfo = null; if ($this->input->getPosition() >= strlen($this->input->getText())) { - return new TerminalToken(SymbolCollection::Eof(), SymbolCollection::Eof()->getName(), $startLocation); + return new TerminalToken(SymbolCollection::eof(), SymbolCollection::eof()->getName(), $startLocation); } - $newState = $this->dfa->GotoNext($this->input->readChar()); - while ($newState != null) { + $newState = $this->dfa->gotoNext($this->input->readChar()); + while ($newState) { if ($newState instanceof EndState) { $acceptInfo = new AcceptInfo($newState, Location::copy($this->input->getLocation())); } if ($this->input->isEof()) { $newState = null; } else { - $newState = $this->dfa->GotoNext($this->input->readChar()); + $newState = $this->dfa->gotoNext($this->input->readChar()); } } - if ($acceptInfo == null) { + if (!$acceptInfo) { $len = $this->input->getPosition() - $startLocation->getPosition(); $text = substr($this->input->getText(), $startLocation->getPosition(), $len); - return new TerminalToken(SymbolCollection::Error(), $text, $startLocation); + return new TerminalToken(SymbolCollection::error(), $text, $startLocation); } else { $this->input->setLocation($acceptInfo->getLocation()); $len = $acceptInfo->getLocation()->getPosition() - $startLocation->getPosition();