[style changes] Lexer

This commit is contained in:
Andreas Heiberg 2016-11-11 13:19:42 +00:00 committed by vladar
parent d8ca5f4183
commit b2c8d8eeb1

View File

@ -110,43 +110,46 @@ class Lexer
// SourceCharacter // SourceCharacter
if ($code < 0x0020 && $code !== 0x0009 && $code !== 0x000A && $code !== 0x000D) { if ($code < 0x0020 && $code !== 0x0009 && $code !== 0x000A && $code !== 0x000D) {
throw new SyntaxError($this->source, $position, 'Invalid character ' . Utils::printCharCode($code)); throw new SyntaxError(
$this->source,
$position,
'Invalid character ' . Utils::printCharCode($code)
);
} }
switch ($code) { switch ($code) {
// ! case 33: // !
case 33: return new Token(Token::BANG, $position, $position + 1, $line, $col, $prev); return new Token(Token::BANG, $position, $position + 1, $line, $col, $prev);
// # case 35: // #
case 35: return $this->readComment($position, $line, $col, $prev); return $this->readComment($position, $line, $col, $prev);
// $ case 36: // $
case 36: return new Token(Token::DOLLAR, $position, $position + 1, $line, $col, $prev); return new Token(Token::DOLLAR, $position, $position + 1, $line, $col, $prev);
// ( case 40: // (
case 40: return new Token(Token::PAREN_L, $position, $position + 1, $line, $col, $prev); return new Token(Token::PAREN_L, $position, $position + 1, $line, $col, $prev);
// ) case 41: // )
case 41: return new Token(Token::PAREN_R, $position, $position + 1, $line, $col, $prev); return new Token(Token::PAREN_R, $position, $position + 1, $line, $col, $prev);
// . case 46: // .
case 46:
if (Utils::charCodeAt($body, $position+1) === 46 && if (Utils::charCodeAt($body, $position+1) === 46 &&
Utils::charCodeAt($body, $position+2) === 46) { Utils::charCodeAt($body, $position+2) === 46) {
return new Token(Token::SPREAD, $position, $position + 3, $line, $col, $prev); return new Token(Token::SPREAD, $position, $position + 3, $line, $col, $prev);
} }
break; break;
// : case 58: // :
case 58: return new Token(Token::COLON, $position, $position + 1, $line, $col, $prev); return new Token(Token::COLON, $position, $position + 1, $line, $col, $prev);
// = case 61: // =
case 61: return new Token(Token::EQUALS, $position, $position + 1, $line, $col, $prev); return new Token(Token::EQUALS, $position, $position + 1, $line, $col, $prev);
// @ case 64: // @
case 64: return new Token(Token::AT, $position, $position + 1, $line, $col, $prev); return new Token(Token::AT, $position, $position + 1, $line, $col, $prev);
// [ case 91: // [
case 91: return new Token(Token::BRACKET_L, $position, $position + 1, $line, $col, $prev); return new Token(Token::BRACKET_L, $position, $position + 1, $line, $col, $prev);
// ] case 93: // ]
case 93: return new Token(Token::BRACKET_R, $position, $position + 1, $line, $col, $prev); return new Token(Token::BRACKET_R, $position, $position + 1, $line, $col, $prev);
// { case 123: // {
case 123: return new Token(Token::BRACE_L, $position, $position + 1, $line, $col, $prev); return new Token(Token::BRACE_L, $position, $position + 1, $line, $col, $prev);
// | case 124: // |
case 124: return new Token(Token::PIPE, $position, $position + 1, $line, $col, $prev); return new Token(Token::PIPE, $position, $position + 1, $line, $col, $prev);
// } case 125: // }
case 125: return new Token(Token::BRACE_R, $position, $position + 1, $line, $col, $prev); return new Token(Token::BRACE_R, $position, $position + 1, $line, $col, $prev);
// A-Z // A-Z
case 65: case 66: case 67: case 68: case 69: case 70: case 71: case 72: case 65: case 66: case 67: case 68: case 69: case 70: case 71: case 72:
case 73: case 74: case 75: case 76: case 77: case 78: case 79: case 80: case 73: case 74: case 75: case 76: case 77: case 78: case 79: case 80:
@ -159,18 +162,23 @@ class Lexer
case 105: case 106: case 107: case 108: case 109: case 110: case 111: case 105: case 106: case 107: case 108: case 109: case 110: case 111:
case 112: case 113: case 114: case 115: case 116: case 117: case 118: case 112: case 113: case 114: case 115: case 116: case 117: case 118:
case 119: case 120: case 121: case 122: case 119: case 120: case 121: case 122:
return $this->readName($position, $line, $col, $prev); return $this->readName($position, $line, $col, $prev);
// - // -
case 45: case 45:
// 0-9 // 0-9
case 48: case 49: case 50: case 51: case 52: case 48: case 49: case 50: case 51: case 52:
case 53: case 54: case 55: case 56: case 57: case 53: case 54: case 55: case 56: case 57:
return $this->readNumber($position, $code, $line, $col, $prev); return $this->readNumber($position, $code, $line, $col, $prev);
// " // "
case 34: return $this->readString($position, $line, $col, $prev); case 34:
return $this->readString($position, $line, $col, $prev);
} }
throw new SyntaxError($this->source, $position, 'Unexpected character ' . Utils::printCharCode($code)); throw new SyntaxError(
$this->source,
$position,
'Unexpected character ' . Utils::printCharCode($code)
);
} }
/** /**
@ -239,6 +247,7 @@ class Lexer
$code = Utils::charCodeAt($body, ++$position); $code = Utils::charCodeAt($body, ++$position);
} }
// guard against leading zero's
if ($code === 48) { // 0 if ($code === 48) { // 0
$code = Utils::charCodeAt($body, ++$position); $code = Utils::charCodeAt($body, ++$position);
@ -267,6 +276,7 @@ class Lexer
} }
$position = $this->readDigits($position, $code); $position = $this->readDigits($position, $code);
} }
return new Token( return new Token(
$isFloat ? Token::FLOAT : Token::INT, $isFloat ? Token::FLOAT : Token::INT,
$start, $start,
@ -286,6 +296,7 @@ class Lexer
$body = $this->source->body; $body = $this->source->body;
$position = $start; $position = $start;
$code = $firstCode; $code = $firstCode;
if ($code >= 48 && $code <= 57) { // 0 - 9 if ($code >= 48 && $code <= 57) { // 0 - 9
do { do {
$code = Utils::charCodeAt($body, ++$position); $code = Utils::charCodeAt($body, ++$position);
@ -293,10 +304,16 @@ class Lexer
return $position; return $position;
} }
if ($position > $this->source->length - 1) { if ($position > $this->source->length - 1) {
$code = null; $code = null;
} }
throw new SyntaxError($this->source, $position, "Invalid number, expected digit but got: " . Utils::printCharCode($code));
throw new SyntaxError(
$this->source,
$position,
'Invalid number, expected digit but got: ' . Utils::printCharCode($code)
);
} }
/** /**
@ -343,7 +360,11 @@ class Lexer
case 117: case 117:
$hex = mb_substr($body, $position + 1, 4, 'UTF-8'); $hex = mb_substr($body, $position + 1, 4, 'UTF-8');
if (!preg_match('/[0-9a-fA-F]{4}/', $hex)) { if (!preg_match('/[0-9a-fA-F]{4}/', $hex)) {
throw new SyntaxError($this->source, $position, 'Invalid character escape sequence: \\u' . $hex); throw new SyntaxError(
$this->source,
$position,
'Invalid character escape sequence: \\u' . $hex
);
} }
$code = hexdec($hex); $code = hexdec($hex);
$this->assertValidStringCharacterCode($code, $position - 1); $this->assertValidStringCharacterCode($code, $position - 1);
@ -351,7 +372,11 @@ class Lexer
$position += 4; $position += 4;
break; break;
default: default:
throw new SyntaxError($this->source, $position, 'Invalid character escape sequence: \\' . Utils::chr($code)); throw new SyntaxError(
$this->source,
$position,
'Invalid character escape sequence: \\' . Utils::chr($code)
);
} }
++$position; ++$position;
$chunkStart = $position; $chunkStart = $position;
@ -359,11 +384,24 @@ class Lexer
} }
if ($code !== 34) { if ($code !== 34) {
throw new SyntaxError($this->source, $position, 'Unterminated string'); throw new SyntaxError(
$this->source,
$position,
'Unterminated string'
);
} }
$value .= mb_substr($body, $chunkStart, $position - $chunkStart, 'UTF-8'); $value .= mb_substr($body, $chunkStart, $position - $chunkStart, 'UTF-8');
return new Token(Token::STRING, $start, $position + 1, $line, $col, $prev, $value);
return new Token(
Token::STRING,
$start,
$position + 1,
$line,
$col,
$prev,
$value
);
} }
private function assertValidStringCharacterCode($code, $position) private function assertValidStringCharacterCode($code, $position)
@ -373,7 +411,7 @@ class Lexer
throw new SyntaxError( throw new SyntaxError(
$this->source, $this->source,
$position, $position,
"Invalid character within String: " . Utils::printCharCode($code) 'Invalid character within String: ' . Utils::printCharCode($code)
); );
} }
} }
@ -415,6 +453,7 @@ class Lexer
break; break;
} }
} }
return $position; return $position;
} }