Skip to content

Commit

Permalink
Merge cca0b6a into 215d1c3
Browse files Browse the repository at this point in the history
  • Loading branch information
maximw committed Nov 17, 2018
2 parents 215d1c3 + cca0b6a commit 81534e6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
@@ -1,5 +1,5 @@
{
"name": "tmilos/lexer",
"name": "maximw/lexer",
"type": "library",
"description": "Lexical analyzer with individual token definition with regular expressions",
"keywords": ["lexer", "parser"],
Expand Down
4 changes: 2 additions & 2 deletions src/Config/LexerArrayConfig.php
Expand Up @@ -14,7 +14,7 @@
class LexerArrayConfig implements LexerConfig
{
/** @var TokenDefn[] */
private $definitions = [];
protected $definitions = [];

/**
* @param TokenDefn[] $tokenDefinitions
Expand All @@ -24,7 +24,7 @@ public function __construct(array $tokenDefinitions)
foreach ($tokenDefinitions as $k => $v) {
if ($v instanceof TokenDefn) {
$this->addTokenDefinition($v);
} elseif (is_string($k) && is_string($v)) {
} elseif (is_string($k) && is_scalar($v)) {
$this->addTokenDefinition(new TokenDefn($v, $k));
}
}
Expand Down
32 changes: 24 additions & 8 deletions src/Lexer.php
Expand Up @@ -17,25 +17,25 @@
class Lexer
{
/** @var LexerConfig */
private $config;
protected $config;

/** @var string */
private $input;
protected $input;

/** @var int */
private $position;
protected $position;

/** @var int */
private $peek;
protected $peek;

/** @var Token[] */
private $tokens;
protected $tokens;

/** @var Token */
private $lookahead;
protected $lookahead;

/** @var Token */
private $token;
protected $token;

/**
* @param LexerConfig $config
Expand Down Expand Up @@ -118,24 +118,28 @@ public function setInput($input)
$this->input = $input;
$this->reset();
$this->tokens = static::scan($this->config, $input);
return $this;
}

public function reset()
{
$this->position = 0;
$this->peek = 0;
$this->token = null;
$this->lookahead = null;
return $this;
}

public function resetPeek()
{
$this->peek = 0;
return $this;
}

public function resetPosition($position = 0)
{
$this->position = $position;
return $this;
}

/**
Expand Down Expand Up @@ -180,6 +184,7 @@ public function skipUntil($tokenName)
while ($this->lookahead !== null && $this->lookahead->getName() !== $tokenName) {
$this->moveNext();
}
return $this;
}

/**
Expand All @@ -190,6 +195,7 @@ public function skipTokens(array $tokenNames)
while ($this->lookahead !== null && in_array($this->lookahead->getName(), $tokenNames, true)) {
$this->moveNext();
}
return $this;
}

/**
Expand Down Expand Up @@ -234,4 +240,14 @@ public function glimpse()

return $peek;
}

/**
* Returns all the Tokens
*
* @return Token[]
*/
public function getTokens()
{
return $this->tokens;
}
}
2 changes: 1 addition & 1 deletion src/Token.php
Expand Up @@ -75,7 +75,7 @@ public function is($token)
{
if ($token instanceof self) {
return $this->name === $token->getName();
} elseif (is_string($token)) {
} elseif (is_scalar($token)) {
return $this->name === $token;
} else {
throw new \InvalidArgumentException('Expected string or Token');
Expand Down

0 comments on commit 81534e6

Please sign in to comment.