Skip to content

Commit add6a96

Browse files
committed
added eof token.
1 parent 11fb4f3 commit add6a96

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

parser.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private function R()
8383

8484
private function I()
8585
{
86-
if ($this->position < count($this->tokens) && $this->lookahead()->token != Token::CLOSE_OBJECT) {
86+
if ($this->lookahead()->token != Token::EOF && $this->lookahead()->token != Token::CLOSE_OBJECT) {
8787
return $this->P();
8888
}
8989
return [];
@@ -129,6 +129,7 @@ class Token
129129
const ARRAYS = 'array';
130130
const OPEN_OBJECT = 'open_object';
131131
const CLOSE_OBJECT = 'close_object';
132+
const EOF = 'eof';
132133
}
133134

134135
class Tokenizer
@@ -149,7 +150,7 @@ public function lex($string)
149150
foreach (self::$tokenMap as $regex => $token) {
150151
if (preg_match($regex, $string, $matches, null, $offset)) {
151152
$tokenDetails = new stdClass();
152-
$tokenDetails->token = trim($token);
153+
$tokenDetails->token = $token;
153154
$tokenDetails->value = trim($matches[0]);
154155
$tokens[] = $tokenDetails;
155156
$offset += strlen($matches[0]);
@@ -158,6 +159,10 @@ public function lex($string)
158159
}
159160
throw new Exception(sprintf('Unexpected character: >%s< offset >%d<', $string[$offset], $offset));
160161
}
162+
$eofToken = new stdClass();
163+
$eofToken->token = Token::EOF;
164+
$eofToken->value = 'eof';
165+
$tokens[] = $eofToken;
161166
return $tokens;
162167
}
163168
}

0 commit comments

Comments
 (0)