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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-03-30 11:11:07 +00:00
|
|
|
#define ENUMERATE_JS_TOKENS \
|
|
|
|
__ENUMERATE_JS_TOKEN(Ampersand) \
|
|
|
|
__ENUMERATE_JS_TOKEN(AmpersandEquals) \
|
2020-03-30 13:26:09 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Arrow) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Asterisk) \
|
2020-05-04 22:03:35 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(DoubleAsteriskEquals) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(AsteriskEquals) \
|
2020-05-12 16:43:35 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Async) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Await) \
|
2020-06-06 00:14:10 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(BigIntLiteral) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(BoolLiteral) \
|
|
|
|
__ENUMERATE_JS_TOKEN(BracketClose) \
|
|
|
|
__ENUMERATE_JS_TOKEN(BracketOpen) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Break) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Caret) \
|
2020-05-04 21:34:45 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(CaretEquals) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Case) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Catch) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Class) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Colon) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Comma) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Const) \
|
2020-04-04 22:22:42 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Continue) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(CurlyClose) \
|
|
|
|
__ENUMERATE_JS_TOKEN(CurlyOpen) \
|
2020-04-30 16:26:27 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Debugger) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__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) \
|
2020-05-12 16:43:35 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Enum) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__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) \
|
2020-05-12 16:43:35 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Export) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Extends) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__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) \
|
2020-05-12 16:43:35 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Implements) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Import) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__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) \
|
2020-05-12 16:43:35 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Package) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__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) \
|
2020-05-12 16:43:35 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Private) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Protected) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Public) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(QuestionMark) \
|
|
|
|
__ENUMERATE_JS_TOKEN(QuestionMarkPeriod) \
|
|
|
|
__ENUMERATE_JS_TOKEN(RegexLiteral) \
|
2020-06-03 23:05:49 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(RegexFlags) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__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) \
|
2020-05-12 16:43:35 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Static) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(StringLiteral) \
|
2020-05-12 16:43:35 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Super) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__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) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Throw) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Tilde) \
|
2020-04-27 06:05:37 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(TripleDot) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Try) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Typeof) \
|
|
|
|
__ENUMERATE_JS_TOKEN(UnsignedShiftRight) \
|
|
|
|
__ENUMERATE_JS_TOKEN(UnsignedShiftRightEquals) \
|
2020-06-03 23:05:49 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(UnterminatedRegexLiteral) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__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) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Var) \
|
|
|
|
__ENUMERATE_JS_TOKEN(Void) \
|
|
|
|
__ENUMERATE_JS_TOKEN(While) \
|
2020-05-12 16:43:35 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(With) \
|
2020-03-30 11:11:07 +00:00
|
|
|
__ENUMERATE_JS_TOKEN(Yield)
|
|
|
|
|
2020-03-11 18:27:43 +00:00
|
|
|
enum class TokenType {
|
2020-03-30 11:11:07 +00:00
|
|
|
#define __ENUMERATE_JS_TOKEN(x) x,
|
|
|
|
ENUMERATE_JS_TOKENS
|
|
|
|
#undef __ENUMERATE_JS_TOKEN
|
2020-08-18 16:46:36 +00:00
|
|
|
_COUNT_OF_TOKENS
|
2020-03-11 18:27:43 +00:00
|
|
|
};
|
2020-08-18 16:46:36 +00:00
|
|
|
constexpr size_t cs_num_of_js_tokens = static_cast<size_t>(TokenType::_COUNT_OF_TOKENS);
|
2020-03-11 18:27:43 +00:00
|
|
|
|
|
|
|
class Token {
|
|
|
|
public:
|
2020-04-05 09:34:03 +00:00
|
|
|
Token(TokenType type, StringView trivia, StringView value, size_t line_number, size_t line_column)
|
2020-03-11 18:27:43 +00:00
|
|
|
: m_type(type)
|
|
|
|
, m_trivia(trivia)
|
|
|
|
, m_value(value)
|
2020-04-05 09:34:03 +00:00
|
|
|
, m_line_number(line_number)
|
|
|
|
, m_line_column(line_column)
|
2020-03-11 18:27:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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; }
|
2020-04-05 09:34:03 +00:00
|
|
|
size_t line_number() const { return m_line_number; }
|
|
|
|
size_t line_column() const { return m_line_column; }
|
2020-03-11 18:27:43 +00:00
|
|
|
double double_value() const;
|
|
|
|
bool bool_value() const;
|
|
|
|
|
2020-05-17 06:27:25 +00:00
|
|
|
enum class StringValueStatus {
|
|
|
|
Ok,
|
|
|
|
MalformedHexEscape,
|
|
|
|
MalformedUnicodeEscape,
|
|
|
|
UnicodeEscapeOverflow,
|
|
|
|
};
|
|
|
|
String string_value(StringValueStatus& status) const;
|
|
|
|
|
2020-04-18 18:31:27 +00:00
|
|
|
bool is_identifier_name() const;
|
|
|
|
|
2020-03-11 18:27:43 +00:00
|
|
|
private:
|
|
|
|
TokenType m_type;
|
|
|
|
StringView m_trivia;
|
|
|
|
StringView m_value;
|
2020-04-05 09:34:03 +00:00
|
|
|
size_t m_line_number;
|
|
|
|
size_t m_line_column;
|
2020-03-11 18:27:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|