2020-03-11 18:27:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Token.h"
|
2020-03-12 22:02:41 +00:00
|
|
|
#include <AK/Assertions.h>
|
2020-03-11 18:27:43 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
const char* Token::name(TokenType type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case TokenType::Ampersand:
|
|
|
|
return "Ampersand";
|
|
|
|
case TokenType::AmpersandEquals:
|
|
|
|
return "AmpersandEquals";
|
|
|
|
case TokenType::Asterisk:
|
|
|
|
return "Asterisk";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::AsteriskAsteriskEquals:
|
|
|
|
return "AsteriskAsteriskEquals";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::AsteriskEquals:
|
|
|
|
return "AsteriskEquals";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::Await:
|
|
|
|
return "Await";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::BoolLiteral:
|
|
|
|
return "BoolLiteral";
|
|
|
|
case TokenType::BracketOpen:
|
|
|
|
return "BracketOpen";
|
|
|
|
case TokenType::BracketClose:
|
|
|
|
return "BracketClose";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::Caret:
|
|
|
|
return "Caret";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::Catch:
|
|
|
|
return "Catch";
|
|
|
|
case TokenType::Class:
|
|
|
|
return "Class";
|
|
|
|
case TokenType::Comma:
|
|
|
|
return "Comma";
|
|
|
|
case TokenType::Const:
|
|
|
|
return "Const";
|
|
|
|
case TokenType::CurlyClose:
|
|
|
|
return "CurlyClose";
|
|
|
|
case TokenType::CurlyOpen:
|
|
|
|
return "CurlyOpen";
|
|
|
|
case TokenType::Delete:
|
|
|
|
return "Delete";
|
|
|
|
case TokenType::Do:
|
|
|
|
return "Do";
|
|
|
|
case TokenType::DoubleAmpersand:
|
|
|
|
return "DoubleAmpersand";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::DoubleAsterisk:
|
|
|
|
return "DoubleAsterisk";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::DoublePipe:
|
|
|
|
return "DoublePipe";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::DoubleQuestionMark:
|
|
|
|
return "DoubleQuestionMark";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::Else:
|
|
|
|
return "Else";
|
|
|
|
case TokenType::Eof:
|
|
|
|
return "Eof";
|
|
|
|
case TokenType::Equals:
|
|
|
|
return "Equals";
|
|
|
|
case TokenType::EqualsEquals:
|
|
|
|
return "EqualsEquals";
|
2020-03-13 23:47:49 +00:00
|
|
|
case TokenType::EqualsEqualsEquals:
|
|
|
|
return "EqualsEqualsEquals";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::ExclamationMark:
|
|
|
|
return "ExclamationMark";
|
|
|
|
case TokenType::ExclamationMarkEquals:
|
|
|
|
return "ExclamationMarkEquals";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::ExclamationMarkEqualsEquals:
|
|
|
|
return "ExclamationMarkEqualsEquals";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::Finally:
|
|
|
|
return "Finally";
|
2020-03-13 23:47:49 +00:00
|
|
|
case TokenType::For:
|
|
|
|
return "For";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::Function:
|
|
|
|
return "Function";
|
|
|
|
case TokenType::GreaterThan:
|
|
|
|
return "GreaterThan";
|
2020-03-13 23:47:49 +00:00
|
|
|
case TokenType::GreaterThanEquals:
|
|
|
|
return "GreaterThanEquals";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::Identifier:
|
|
|
|
return "Identifier";
|
|
|
|
case TokenType::If:
|
|
|
|
return "If";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::In:
|
|
|
|
return "In";
|
|
|
|
case TokenType::Instanceof:
|
|
|
|
return "Instanceof";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::Interface:
|
|
|
|
return "Interface";
|
|
|
|
case TokenType::Invalid:
|
|
|
|
return "Invalid";
|
|
|
|
case TokenType::LessThan:
|
|
|
|
return "LessThan";
|
2020-03-13 23:47:49 +00:00
|
|
|
case TokenType::LessThanEquals:
|
|
|
|
return "LessThanEquals";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::Let:
|
|
|
|
return "Let";
|
|
|
|
case TokenType::Minus:
|
|
|
|
return "Minus";
|
|
|
|
case TokenType::MinusEquals:
|
|
|
|
return "MinusEquals";
|
|
|
|
case TokenType::MinusMinus:
|
|
|
|
return "MinusMinus";
|
|
|
|
case TokenType::New:
|
|
|
|
return "New";
|
|
|
|
case TokenType::NullLiteral:
|
|
|
|
return "NullLiteral";
|
|
|
|
case TokenType::NumericLiteral:
|
|
|
|
return "NumericLiteral";
|
|
|
|
case TokenType::ParenClose:
|
|
|
|
return "ParenClose";
|
|
|
|
case TokenType::ParenOpen:
|
|
|
|
return "ParenOpen";
|
|
|
|
case TokenType::Percent:
|
|
|
|
return "Percent";
|
|
|
|
case TokenType::PercentEquals:
|
|
|
|
return "PercentEquals";
|
|
|
|
case TokenType::Period:
|
|
|
|
return "Period";
|
|
|
|
case TokenType::Pipe:
|
|
|
|
return "Pipe";
|
|
|
|
case TokenType::PipeEquals:
|
|
|
|
return "PipeEquals";
|
|
|
|
case TokenType::Plus:
|
|
|
|
return "Plus";
|
|
|
|
case TokenType::PlusEquals:
|
|
|
|
return "PlusEquals";
|
|
|
|
case TokenType::PlusPlus:
|
|
|
|
return "PlusPlus";
|
|
|
|
case TokenType::QuestionMark:
|
|
|
|
return "QuestionMark";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::QuestionMarkPeriod:
|
|
|
|
return "QuestionMarkPeriod";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::RegexLiteral:
|
|
|
|
return "RegexLiteral";
|
|
|
|
case TokenType::Return:
|
|
|
|
return "Return";
|
|
|
|
case TokenType::Semicolon:
|
|
|
|
return "Semicolon";
|
|
|
|
case TokenType::ShiftLeft:
|
|
|
|
return "ShiftLeft";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::ShiftLeftEquals:
|
|
|
|
return "ShiftLeftEquals";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::ShiftRight:
|
|
|
|
return "ShiftRight";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::ShiftRightEquals:
|
|
|
|
return "ShiftRightEquals";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::Slash:
|
|
|
|
return "Slash";
|
|
|
|
case TokenType::SlashEquals:
|
|
|
|
return "SlashEquals";
|
|
|
|
case TokenType::StringLiteral:
|
|
|
|
return "StringLiteral";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::Tilde:
|
|
|
|
return "Tilde";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::Try:
|
|
|
|
return "Try";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::Typeof:
|
|
|
|
return "Typeof";
|
|
|
|
case TokenType::UnsignedShiftRight:
|
|
|
|
return "UnsignedShiftRight";
|
|
|
|
case TokenType::UnsignedShiftRightEquals:
|
|
|
|
return "UnsignedShiftRightEquals";
|
2020-03-14 10:45:25 +00:00
|
|
|
case TokenType::UnterminatedStringLiteral:
|
|
|
|
return "UnterminatedStringLiteral";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::Var:
|
|
|
|
return "Var";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::Void:
|
|
|
|
return "Void";
|
2020-03-11 18:27:43 +00:00
|
|
|
case TokenType::While:
|
|
|
|
return "While";
|
2020-03-12 22:02:41 +00:00
|
|
|
case TokenType::Yield:
|
|
|
|
return "Yield";
|
2020-03-11 18:27:43 +00:00
|
|
|
default:
|
2020-03-12 22:02:41 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
2020-03-11 18:27:43 +00:00
|
|
|
return "<Unknown>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* Token::name() const
|
|
|
|
{
|
|
|
|
return name(m_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Token::double_value() const
|
|
|
|
{
|
|
|
|
// FIXME: need to parse double instead of int
|
|
|
|
bool ok;
|
|
|
|
return m_value.to_int(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
String Token::string_value() const
|
|
|
|
{
|
|
|
|
// FIXME: unescape the string and remove quotes
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Token::bool_value() const
|
|
|
|
{
|
|
|
|
return m_value == "true";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|