2020-03-11 18:27:43 +00:00
|
|
|
/*
|
2021-05-29 10:38:28 +00:00
|
|
|
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
|
2020-03-11 18:27:43 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-11 18:27:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Token.h"
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-03-11 18:27:43 +00:00
|
|
|
#include <AK/HashMap.h>
|
2023-01-26 13:40:21 +00:00
|
|
|
#include <AK/String.h>
|
2020-03-11 18:27:43 +00:00
|
|
|
#include <AK/StringView.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
class Lexer {
|
|
|
|
public:
|
2022-07-11 17:32:29 +00:00
|
|
|
explicit Lexer(StringView source, StringView filename = "(unknown)"sv, size_t line_number = 1, size_t line_column = 0);
|
2020-04-05 11:49:25 +00:00
|
|
|
|
2020-03-11 18:27:43 +00:00
|
|
|
Token next();
|
|
|
|
|
2023-07-08 02:48:11 +00:00
|
|
|
DeprecatedString const& source() const { return m_source; }
|
|
|
|
String const& filename() const { return m_filename; }
|
2020-12-06 14:50:39 +00:00
|
|
|
|
2023-07-08 02:48:11 +00:00
|
|
|
void disallow_html_comments() { m_allow_html_comments = false; }
|
2021-08-14 15:30:37 +00:00
|
|
|
|
2021-07-29 21:28:28 +00:00
|
|
|
Token force_slash_as_regex();
|
|
|
|
|
2020-03-11 18:27:43 +00:00
|
|
|
private:
|
|
|
|
void consume();
|
2020-10-18 13:32:50 +00:00
|
|
|
bool consume_exponent();
|
|
|
|
bool consume_octal_number();
|
|
|
|
bool consume_hexadecimal_number();
|
|
|
|
bool consume_binary_number();
|
2021-06-26 14:30:05 +00:00
|
|
|
bool consume_decimal_number();
|
2021-08-14 15:07:47 +00:00
|
|
|
|
|
|
|
bool is_unicode_character() const;
|
|
|
|
u32 current_code_point() const;
|
|
|
|
|
2020-03-11 18:27:43 +00:00
|
|
|
bool is_eof() const;
|
2020-10-21 21:16:45 +00:00
|
|
|
bool is_line_terminator() const;
|
2021-08-14 15:07:47 +00:00
|
|
|
bool is_whitespace() const;
|
2021-08-21 09:27:20 +00:00
|
|
|
Optional<u32> is_identifier_unicode_escape(size_t& identifier_length) const;
|
2021-08-18 20:34:25 +00: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 18:27:43 +00:00
|
|
|
bool is_block_comment_start() const;
|
|
|
|
bool is_block_comment_end() const;
|
2020-04-05 12:20:58 +00:00
|
|
|
bool is_numeric_literal_start() const;
|
2020-04-13 17:50:58 +00:00
|
|
|
bool match(char, char) const;
|
|
|
|
bool match(char, char, char) const;
|
|
|
|
bool match(char, char, char, char) const;
|
2021-06-26 14:30:05 +00:00
|
|
|
template<typename Callback>
|
|
|
|
bool match_numeric_literal_separator_followed_by(Callback) const;
|
2020-06-03 23:05:49 +00:00
|
|
|
bool slash_means_division() const;
|
2020-03-11 18:27:43 +00:00
|
|
|
|
2021-07-29 21:28:28 +00:00
|
|
|
TokenType consume_regex_literal();
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString m_source;
|
2020-05-26 17:00:30 +00:00
|
|
|
size_t m_position { 0 };
|
2020-03-11 18:27:43 +00:00
|
|
|
Token m_current_token;
|
2020-10-21 21:16:45 +00:00
|
|
|
char m_current_char { 0 };
|
2021-06-13 07:15:00 +00:00
|
|
|
bool m_eof { false };
|
2021-02-28 09:42:34 +00:00
|
|
|
|
2023-01-26 13:40:21 +00:00
|
|
|
String m_filename;
|
2020-05-26 17:00:30 +00:00
|
|
|
size_t m_line_number { 1 };
|
|
|
|
size_t m_line_column { 0 };
|
2020-03-11 18:27:43 +00:00
|
|
|
|
2020-06-03 23:05:49 +00:00
|
|
|
bool m_regex_is_in_character_class { false };
|
|
|
|
|
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
|
|
|
struct TemplateState {
|
|
|
|
bool in_expr;
|
|
|
|
u8 open_bracket_count;
|
|
|
|
};
|
|
|
|
Vector<TemplateState> m_template_states;
|
|
|
|
|
2021-08-14 15:30:37 +00:00
|
|
|
bool m_allow_html_comments { true };
|
|
|
|
|
2021-12-29 10:55:06 +00:00
|
|
|
Optional<size_t> m_hit_invalid_unicode;
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
static HashMap<DeprecatedFlyString, TokenType> s_keywords;
|
2022-12-04 18:02:33 +00:00
|
|
|
static HashMap<DeprecatedString, TokenType> s_three_char_tokens;
|
|
|
|
static HashMap<DeprecatedString, TokenType> s_two_char_tokens;
|
2020-03-11 18:27:43 +00:00
|
|
|
static HashMap<char, TokenType> s_single_char_tokens;
|
2021-08-18 20:34:25 +00:00
|
|
|
|
2021-09-10 21:18:00 +00:00
|
|
|
struct ParsedIdentifiers : public RefCounted<ParsedIdentifiers> {
|
|
|
|
// Resolved identifiers must be kept alive for the duration of the parsing stage, otherwise
|
|
|
|
// the only references to these strings are deleted by the Token destructor.
|
2023-01-09 00:23:00 +00:00
|
|
|
HashTable<DeprecatedFlyString> identifiers;
|
2021-09-10 21:18:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RefPtr<ParsedIdentifiers> m_parsed_identifiers;
|
2020-03-11 18:27:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|