Skip to content

Commit

Permalink
code style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tmilos committed Dec 21, 2016
1 parent dacb205 commit fce45ad
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 60 deletions.
3 changes: 2 additions & 1 deletion src/Tmilos/GoldParser/Content/DFAStateRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Tmilos/GoldParser/Content/LALRStateRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion src/Tmilos/GoldParser/Content/RuleRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/Tmilos/GoldParser/Dfa/Dfa.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,17 @@

class Dfa implements DfaInterface
{
/** @var StateCollection */
private $states;

/** @var State */
private $startState;

/** @var State */
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;
}
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Tmilos/GoldParser/Dfa/DfaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function reset();
*
* @return State|null
*/
public function GotoNext($char);
public function gotoNext($char);

/**
* The current state in the DFA.
Expand Down
62 changes: 25 additions & 37 deletions src/Tmilos/GoldParser/LalrParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -48,9 +46,6 @@ class LalrParser
/** @var Tokenizer */
private $tokenizer;

/** @var StateCollection */
private $states;

/** @var State */
private $startState;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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()));
Expand All @@ -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();
}
Expand All @@ -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) {
Expand All @@ -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()) {
Expand All @@ -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) {
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -425,7 +413,7 @@ protected function processError(TerminalToken $token)
/**
* @return SymbolCollection
*/
protected function findExpectedTokens()
private function findExpectedTokens()
{
$symbols = new SymbolCollection();
$state = $this->stateStack->peek();
Expand All @@ -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());
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Tmilos/GoldParser/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -138,7 +138,6 @@ public function createNewParser()

return new LalrParser(
$this->createNewTokenizer(),
$this->parserStates,
$startState,
$this->symbols
);
Expand Down
4 changes: 2 additions & 2 deletions src/Tmilos/GoldParser/Symbol/SymbolCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SymbolCollection implements \Countable, \IteratorAggregate
/**
* @return SymbolEnd
*/
public static function Eof()
public static function eof()
{
static $value = null;
if (!$value) {
Expand All @@ -32,7 +32,7 @@ public static function Eof()
/**
* @return SymbolError
*/
public static function Error()
public static function error()
{
static $value = null;
if (!$value) {
Expand Down
4 changes: 2 additions & 2 deletions src/Tmilos/GoldParser/Symbol/SymbolFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/Tmilos/GoldParser/Tokenizer/StringTokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit fce45ad

Please sign in to comment.