2020-03-11 19:27:43 +01:00
|
|
|
/*
|
2021-05-29 12:38:28 +02:00
|
|
|
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
|
2025-11-08 21:21:52 +01:00
|
|
|
* Copyright (c) 2020-2025, Andreas Kling <andreas@ladybird.org>
|
2020-03-11 19:27:43 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-11 19:27:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
2025-08-06 07:18:45 -04:00
|
|
|
#include <AK/Utf16String.h>
|
2025-07-19 13:49:30 -07:00
|
|
|
#include <LibJS/Export.h>
|
2025-11-08 21:21:52 +01:00
|
|
|
#include <LibJS/SourceCode.h>
|
2025-08-06 07:18:45 -04:00
|
|
|
#include <LibJS/Token.h>
|
2020-03-11 19:27:43 +01:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2025-07-19 13:49:30 -07:00
|
|
|
class JS_API Lexer {
|
2020-03-11 19:27:43 +01:00
|
|
|
public:
|
2025-11-08 21:21:52 +01:00
|
|
|
explicit Lexer(NonnullRefPtr<SourceCode const>, size_t line_number = 1, size_t line_column = 0);
|
2020-04-05 16:19:25 +04:30
|
|
|
|
2025-11-08 20:25:53 +01:00
|
|
|
// These both advance the lexer and return a reference to the current token.
|
|
|
|
|
Token const& next();
|
|
|
|
|
Token const& force_slash_as_regex();
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] Token const& current_token() const { return m_current_token; }
|
2020-03-11 19:27:43 +01:00
|
|
|
|
2025-11-08 21:21:52 +01:00
|
|
|
SourceCode const& source_code() const { return m_source; }
|
|
|
|
|
Utf16String const& source() const { return m_source->code(); }
|
|
|
|
|
String const& filename() const { return m_source->filename(); }
|
2020-12-06 14:50:39 +00:00
|
|
|
|
2023-07-07 22:48:11 -04:00
|
|
|
void disallow_html_comments() { m_allow_html_comments = false; }
|
2021-08-14 17:30:37 +02:00
|
|
|
|
2020-03-11 19:27:43 +01:00
|
|
|
private:
|
|
|
|
|
void consume();
|
2020-10-18 15:32:50 +02:00
|
|
|
bool consume_exponent();
|
|
|
|
|
bool consume_octal_number();
|
|
|
|
|
bool consume_hexadecimal_number();
|
|
|
|
|
bool consume_binary_number();
|
2021-06-26 16:30:05 +02:00
|
|
|
bool consume_decimal_number();
|
2021-08-14 17:07:47 +02:00
|
|
|
|
|
|
|
|
u32 current_code_point() const;
|
|
|
|
|
|
2020-03-11 19:27:43 +01:00
|
|
|
bool is_eof() const;
|
2020-10-21 22:16:45 +01:00
|
|
|
bool is_line_terminator() const;
|
2021-08-14 17:07:47 +02:00
|
|
|
bool is_whitespace() const;
|
2021-08-21 11:27:20 +02:00
|
|
|
Optional<u32> is_identifier_unicode_escape(size_t& identifier_length) const;
|
2021-08-18 16:34:25 -04:00
|
|
|
Optional<u32> is_identifier_start(size_t& identifier_length) const;
|
|
|
|
|
Optional<u32> is_identifier_middle(size_t& identifier_length) const;
|
2020-10-29 17:55:24 +00:00
|
|
|
bool is_line_comment_start(bool line_has_token_yet) const;
|
2020-03-11 19:27:43 +01:00
|
|
|
bool is_block_comment_start() const;
|
|
|
|
|
bool is_block_comment_end() const;
|
2020-04-05 14:20:58 +02:00
|
|
|
bool is_numeric_literal_start() const;
|
2025-08-06 07:18:45 -04:00
|
|
|
bool match(char16_t, char16_t) const;
|
|
|
|
|
bool match(char16_t, char16_t, char16_t) const;
|
|
|
|
|
bool match(char16_t, char16_t, char16_t, char16_t) const;
|
2021-06-26 16:30:05 +02:00
|
|
|
template<typename Callback>
|
|
|
|
|
bool match_numeric_literal_separator_followed_by(Callback) const;
|
2020-06-03 16:05:49 -07:00
|
|
|
bool slash_means_division() const;
|
2020-03-11 19:27:43 +01:00
|
|
|
|
2021-07-29 23:28:28 +02:00
|
|
|
TokenType consume_regex_literal();
|
|
|
|
|
|
2025-11-08 21:21:52 +01:00
|
|
|
NonnullRefPtr<SourceCode const> m_source;
|
2020-05-26 13:00:30 -04:00
|
|
|
size_t m_position { 0 };
|
2020-03-11 19:27:43 +01:00
|
|
|
Token m_current_token;
|
2025-08-06 07:18:45 -04:00
|
|
|
char16_t m_current_code_unit { 0 };
|
2021-06-13 09:15:00 +02:00
|
|
|
bool m_eof { false };
|
2025-11-08 20:45:56 +01:00
|
|
|
bool m_regex_is_in_character_class { false };
|
|
|
|
|
bool m_allow_html_comments { true };
|
2021-02-28 10:42:34 +01:00
|
|
|
|
2020-05-26 13:00:30 -04:00
|
|
|
size_t m_line_number { 1 };
|
|
|
|
|
size_t m_line_column { 0 };
|
2020-03-11 19:27:43 +01:00
|
|
|
|
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 15:41:14 -07:00
|
|
|
struct TemplateState {
|
|
|
|
|
bool in_expr;
|
|
|
|
|
u8 open_bracket_count;
|
|
|
|
|
};
|
|
|
|
|
Vector<TemplateState> m_template_states;
|
|
|
|
|
|
2025-08-06 07:18:45 -04:00
|
|
|
static HashMap<Utf16FlyString, TokenType> s_keywords;
|
2020-03-11 19:27:43 +01:00
|
|
|
};
|
|
|
|
|
|
2024-12-04 16:41:02 -05:00
|
|
|
bool is_syntax_character(u32 code_point);
|
|
|
|
|
bool is_whitespace(u32 code_point);
|
|
|
|
|
bool is_line_terminator(u32 code_point);
|
|
|
|
|
|
2020-03-11 19:27:43 +01:00
|
|
|
}
|