[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
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) {
// !
case 33: return new Token(Token::BANG, $position, $position + 1, $line, $col, $prev);
// #
case 35: return $this->readComment($position, $line, $col, $prev);
// $
case 36: return new Token(Token::DOLLAR, $position, $position + 1, $line, $col, $prev);
// (
case 40: return new Token(Token::PAREN_L, $position, $position + 1, $line, $col, $prev);
// )
case 41: return new Token(Token::PAREN_R, $position, $position + 1, $line, $col, $prev);
// .
case 46:
case 33: // !
return new Token(Token::BANG, $position, $position + 1, $line, $col, $prev);
case 35: // #
return $this->readComment($position, $line, $col, $prev);
case 36: // $
return new Token(Token::DOLLAR, $position, $position + 1, $line, $col, $prev);
case 40: // (
return new Token(Token::PAREN_L, $position, $position + 1, $line, $col, $prev);
case 41: // )
return new Token(Token::PAREN_R, $position, $position + 1, $line, $col, $prev);
case 46: // .
if (Utils::charCodeAt($body, $position+1) === 46 &&
Utils::charCodeAt($body, $position+2) === 46) {
return new Token(Token::SPREAD, $position, $position + 3, $line, $col, $prev);
}
break;
// :
case 58: return new Token(Token::COLON, $position, $position + 1, $line, $col, $prev);
// =
case 61: return new Token(Token::EQUALS, $position, $position + 1, $line, $col, $prev);
// @
case 64: return new Token(Token::AT, $position, $position + 1, $line, $col, $prev);
// [
case 91: return new Token(Token::BRACKET_L, $position, $position + 1, $line, $col, $prev);
// ]
case 93: return new Token(Token::BRACKET_R, $position, $position + 1, $line, $col, $prev);
// {
case 123: return new Token(Token::BRACE_L, $position, $position + 1, $line, $col, $prev);
// |
case 124: return new Token(Token::PIPE, $position, $position + 1, $line, $col, $prev);
// }
case 125: return new Token(Token::BRACE_R, $position, $position + 1, $line, $col, $prev);
case 58: // :
return new Token(Token::COLON, $position, $position + 1, $line, $col, $prev);
case 61: // =
return new Token(Token::EQUALS, $position, $position + 1, $line, $col, $prev);
case 64: // @
return new Token(Token::AT, $position, $position + 1, $line, $col, $prev);
case 91: // [
return new Token(Token::BRACKET_L, $position, $position + 1, $line, $col, $prev);
case 93: // ]
return new Token(Token::BRACKET_R, $position, $position + 1, $line, $col, $prev);
case 123: // {
return new Token(Token::BRACE_L, $position, $position + 1, $line, $col, $prev);
case 124: // |
return new Token(Token::PIPE, $position, $position + 1, $line, $col, $prev);
case 125: // }
return new Token(Token::BRACE_R, $position, $position + 1, $line, $col, $prev);
// A-Z
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:
@ -167,10 +170,15 @@ class Lexer
case 53: case 54: case 55: case 56: case 57:
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);
}
// guard against leading zero's
if ($code === 48) { // 0
$code = Utils::charCodeAt($body, ++$position);
@ -267,6 +276,7 @@ class Lexer
}
$position = $this->readDigits($position, $code);
}
return new Token(
$isFloat ? Token::FLOAT : Token::INT,
$start,
@ -286,6 +296,7 @@ class Lexer
$body = $this->source->body;
$position = $start;
$code = $firstCode;
if ($code >= 48 && $code <= 57) { // 0 - 9
do {
$code = Utils::charCodeAt($body, ++$position);
@ -293,10 +304,16 @@ class Lexer
return $position;
}
if ($position > $this->source->length - 1) {
$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:
$hex = mb_substr($body, $position + 1, 4, 'UTF-8');
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);
$this->assertValidStringCharacterCode($code, $position - 1);
@ -351,7 +372,11 @@ class Lexer
$position += 4;
break;
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;
$chunkStart = $position;
@ -359,11 +384,24 @@ class Lexer
}
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');
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)
@ -373,7 +411,7 @@ class Lexer
throw new SyntaxError(
$this->source,
$position,
"Invalid character within String: " . Utils::printCharCode($code)
'Invalid character within String: ' . Utils::printCharCode($code)
);
}
}
@ -415,6 +453,7 @@ class Lexer
break;
}
}
return $position;
}