ladybird/Libraries/LibJS/Token.h

201 lines
9.1 KiB
C
Raw Normal View History

/*
* 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.
*/
#pragma once
#include <AK/String.h>
#include <AK/StringView.h>
namespace JS {
#define ENUMERATE_JS_TOKENS \
__ENUMERATE_JS_TOKEN(Ampersand) \
__ENUMERATE_JS_TOKEN(AmpersandEquals) \
2020-03-30 13:26:09 +00:00
__ENUMERATE_JS_TOKEN(Arrow) \
__ENUMERATE_JS_TOKEN(Asterisk) \
__ENUMERATE_JS_TOKEN(DoubleAsteriskEquals) \
__ENUMERATE_JS_TOKEN(AsteriskEquals) \
__ENUMERATE_JS_TOKEN(Async) \
__ENUMERATE_JS_TOKEN(Await) \
2020-06-06 00:14:10 +00:00
__ENUMERATE_JS_TOKEN(BigIntLiteral) \
__ENUMERATE_JS_TOKEN(BoolLiteral) \
__ENUMERATE_JS_TOKEN(BracketClose) \
__ENUMERATE_JS_TOKEN(BracketOpen) \
__ENUMERATE_JS_TOKEN(Break) \
__ENUMERATE_JS_TOKEN(Caret) \
__ENUMERATE_JS_TOKEN(CaretEquals) \
__ENUMERATE_JS_TOKEN(Case) \
__ENUMERATE_JS_TOKEN(Catch) \
__ENUMERATE_JS_TOKEN(Class) \
__ENUMERATE_JS_TOKEN(Colon) \
__ENUMERATE_JS_TOKEN(Comma) \
__ENUMERATE_JS_TOKEN(Const) \
__ENUMERATE_JS_TOKEN(Continue) \
__ENUMERATE_JS_TOKEN(CurlyClose) \
__ENUMERATE_JS_TOKEN(CurlyOpen) \
__ENUMERATE_JS_TOKEN(Debugger) \
__ENUMERATE_JS_TOKEN(Default) \
__ENUMERATE_JS_TOKEN(Delete) \
__ENUMERATE_JS_TOKEN(Do) \
__ENUMERATE_JS_TOKEN(DoubleAmpersand) \
__ENUMERATE_JS_TOKEN(DoubleAsterisk) \
__ENUMERATE_JS_TOKEN(DoublePipe) \
__ENUMERATE_JS_TOKEN(DoubleQuestionMark) \
__ENUMERATE_JS_TOKEN(Else) \
__ENUMERATE_JS_TOKEN(Enum) \
__ENUMERATE_JS_TOKEN(Eof) \
__ENUMERATE_JS_TOKEN(Equals) \
__ENUMERATE_JS_TOKEN(EqualsEquals) \
__ENUMERATE_JS_TOKEN(EqualsEqualsEquals) \
__ENUMERATE_JS_TOKEN(ExclamationMark) \
__ENUMERATE_JS_TOKEN(ExclamationMarkEquals) \
__ENUMERATE_JS_TOKEN(ExclamationMarkEqualsEquals) \
__ENUMERATE_JS_TOKEN(Export) \
__ENUMERATE_JS_TOKEN(Extends) \
__ENUMERATE_JS_TOKEN(Finally) \
__ENUMERATE_JS_TOKEN(For) \
__ENUMERATE_JS_TOKEN(Function) \
__ENUMERATE_JS_TOKEN(GreaterThan) \
__ENUMERATE_JS_TOKEN(GreaterThanEquals) \
__ENUMERATE_JS_TOKEN(Identifier) \
__ENUMERATE_JS_TOKEN(If) \
__ENUMERATE_JS_TOKEN(Implements) \
__ENUMERATE_JS_TOKEN(Import) \
__ENUMERATE_JS_TOKEN(In) \
__ENUMERATE_JS_TOKEN(Instanceof) \
__ENUMERATE_JS_TOKEN(Interface) \
__ENUMERATE_JS_TOKEN(Invalid) \
__ENUMERATE_JS_TOKEN(LessThan) \
__ENUMERATE_JS_TOKEN(LessThanEquals) \
__ENUMERATE_JS_TOKEN(Let) \
__ENUMERATE_JS_TOKEN(Minus) \
__ENUMERATE_JS_TOKEN(MinusEquals) \
__ENUMERATE_JS_TOKEN(MinusMinus) \
__ENUMERATE_JS_TOKEN(New) \
__ENUMERATE_JS_TOKEN(NullLiteral) \
__ENUMERATE_JS_TOKEN(NumericLiteral) \
__ENUMERATE_JS_TOKEN(Package) \
__ENUMERATE_JS_TOKEN(ParenClose) \
__ENUMERATE_JS_TOKEN(ParenOpen) \
__ENUMERATE_JS_TOKEN(Percent) \
__ENUMERATE_JS_TOKEN(PercentEquals) \
__ENUMERATE_JS_TOKEN(Period) \
__ENUMERATE_JS_TOKEN(Pipe) \
__ENUMERATE_JS_TOKEN(PipeEquals) \
__ENUMERATE_JS_TOKEN(Plus) \
__ENUMERATE_JS_TOKEN(PlusEquals) \
__ENUMERATE_JS_TOKEN(PlusPlus) \
__ENUMERATE_JS_TOKEN(Private) \
__ENUMERATE_JS_TOKEN(Protected) \
__ENUMERATE_JS_TOKEN(Public) \
__ENUMERATE_JS_TOKEN(QuestionMark) \
__ENUMERATE_JS_TOKEN(QuestionMarkPeriod) \
__ENUMERATE_JS_TOKEN(RegexLiteral) \
__ENUMERATE_JS_TOKEN(RegexFlags) \
__ENUMERATE_JS_TOKEN(Return) \
__ENUMERATE_JS_TOKEN(Semicolon) \
__ENUMERATE_JS_TOKEN(ShiftLeft) \
__ENUMERATE_JS_TOKEN(ShiftLeftEquals) \
__ENUMERATE_JS_TOKEN(ShiftRight) \
__ENUMERATE_JS_TOKEN(ShiftRightEquals) \
__ENUMERATE_JS_TOKEN(Slash) \
__ENUMERATE_JS_TOKEN(SlashEquals) \
__ENUMERATE_JS_TOKEN(Static) \
__ENUMERATE_JS_TOKEN(StringLiteral) \
__ENUMERATE_JS_TOKEN(Super) \
__ENUMERATE_JS_TOKEN(Switch) \
LibJS: Add template literals Adds fully functioning template literals. Because template literals contain expressions, most of the work has to be done in the Lexer rather than the Parser. And because of the complexity of template literals (expressions, nesting, escapes, etc), the Lexer needs to have some template-related state. When entering a new template literal, a TemplateLiteralStart token is emitted. When inside a literal, all text will be parsed up until a '${' or '`' (or EOF, but that's a syntax error) is seen, and then a TemplateLiteralExprStart token is emitted. At this point, the Lexer proceeds as normal, however it keeps track of the number of opening and closing curly braces it has seen in order to determine the close of the expression. Once it finds a matching curly brace for the '${', a TemplateLiteralExprEnd token is emitted and the state is updated accordingly. When the Lexer is inside of a template literal, but not an expression, and sees a '`', this must be the closing grave: a TemplateLiteralEnd token is emitted. The state required to correctly parse template strings consists of a vector (for nesting) of two pieces of information: whether or not we are in a template expression (as opposed to a template string); and the count of the number of unmatched open curly braces we have seen (only applicable if the Lexer is currently in a template expression). TODO: Add support for template literal newlines in the JS REPL (this will cause a syntax error currently): > `foo > bar` 'foo bar'
2020-05-03 22:41:14 +00:00
__ENUMERATE_JS_TOKEN(TemplateLiteralEnd) \
__ENUMERATE_JS_TOKEN(TemplateLiteralExprEnd) \
__ENUMERATE_JS_TOKEN(TemplateLiteralExprStart) \
__ENUMERATE_JS_TOKEN(TemplateLiteralStart) \
__ENUMERATE_JS_TOKEN(TemplateLiteralString) \
2020-04-12 22:42:14 +00:00
__ENUMERATE_JS_TOKEN(This) \
__ENUMERATE_JS_TOKEN(Throw) \
__ENUMERATE_JS_TOKEN(Tilde) \
__ENUMERATE_JS_TOKEN(TripleDot) \
__ENUMERATE_JS_TOKEN(Try) \
__ENUMERATE_JS_TOKEN(Typeof) \
__ENUMERATE_JS_TOKEN(UnsignedShiftRight) \
__ENUMERATE_JS_TOKEN(UnsignedShiftRightEquals) \
__ENUMERATE_JS_TOKEN(UnterminatedRegexLiteral) \
__ENUMERATE_JS_TOKEN(UnterminatedStringLiteral) \
LibJS: Add template literals Adds fully functioning template literals. Because template literals contain expressions, most of the work has to be done in the Lexer rather than the Parser. And because of the complexity of template literals (expressions, nesting, escapes, etc), the Lexer needs to have some template-related state. When entering a new template literal, a TemplateLiteralStart token is emitted. When inside a literal, all text will be parsed up until a '${' or '`' (or EOF, but that's a syntax error) is seen, and then a TemplateLiteralExprStart token is emitted. At this point, the Lexer proceeds as normal, however it keeps track of the number of opening and closing curly braces it has seen in order to determine the close of the expression. Once it finds a matching curly brace for the '${', a TemplateLiteralExprEnd token is emitted and the state is updated accordingly. When the Lexer is inside of a template literal, but not an expression, and sees a '`', this must be the closing grave: a TemplateLiteralEnd token is emitted. The state required to correctly parse template strings consists of a vector (for nesting) of two pieces of information: whether or not we are in a template expression (as opposed to a template string); and the count of the number of unmatched open curly braces we have seen (only applicable if the Lexer is currently in a template expression). TODO: Add support for template literal newlines in the JS REPL (this will cause a syntax error currently): > `foo > bar` 'foo bar'
2020-05-03 22:41:14 +00:00
__ENUMERATE_JS_TOKEN(UnterminatedTemplateLiteral) \
__ENUMERATE_JS_TOKEN(Var) \
__ENUMERATE_JS_TOKEN(Void) \
__ENUMERATE_JS_TOKEN(While) \
__ENUMERATE_JS_TOKEN(With) \
__ENUMERATE_JS_TOKEN(Yield)
enum class TokenType {
#define __ENUMERATE_JS_TOKEN(x) x,
ENUMERATE_JS_TOKENS
#undef __ENUMERATE_JS_TOKEN
_COUNT_OF_TOKENS
};
constexpr size_t cs_num_of_js_tokens = static_cast<size_t>(TokenType::_COUNT_OF_TOKENS);
class Token {
public:
Token(TokenType type, StringView trivia, StringView value, size_t line_number, size_t line_column)
: m_type(type)
, m_trivia(trivia)
, m_value(value)
, m_line_number(line_number)
, m_line_column(line_column)
{
}
TokenType type() const { return m_type; }
const char* name() const;
static const char* name(TokenType);
const StringView& trivia() const { return m_trivia; }
const StringView& value() const { return m_value; }
size_t line_number() const { return m_line_number; }
size_t line_column() const { return m_line_column; }
double double_value() const;
bool bool_value() const;
enum class StringValueStatus {
Ok,
MalformedHexEscape,
MalformedUnicodeEscape,
UnicodeEscapeOverflow,
};
String string_value(StringValueStatus& status) const;
bool is_identifier_name() const;
private:
TokenType m_type;
StringView m_trivia;
StringView m_value;
size_t m_line_number;
size_t m_line_column;
};
}