summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-05-14 16:08:40 +0100
committerAndreas Kling <kling@serenityos.org>2020-05-15 09:53:52 +0200
commit00b61a212fba0ca83f2682b9bc0a136536e55be7 (patch)
tree32526399159c4d2f5270ba1d3aac20909506db89
parent3485613f4ad2e766378c6a503879d04a8884bf89 (diff)
downloadserenity-00b61a212fba0ca83f2682b9bc0a136536e55be7.zip
LibJS: Remove syntax errors from lexer
Giving the lexer the ability to generate errors adds unnecessary complexity - also it only calls its syntax_error() function in one place anyway ("unterminated string literal"). But since the lexer *also* emits tokens like Eof or UnterminatedStringLiteral, it should be up to the consumer of these tokens to decide what to do. Also remove the option to not print errors to stderr as that's not relevant anymore.
-rw-r--r--Libraries/LibJS/Lexer.cpp8
-rw-r--r--Libraries/LibJS/Lexer.h10
-rw-r--r--Libraries/LibJS/Parser.h2
-rw-r--r--Userland/js.cpp2
4 files changed, 2 insertions, 20 deletions
diff --git a/Libraries/LibJS/Lexer.cpp b/Libraries/LibJS/Lexer.cpp
index dd5bae4f7c..dc094f4f5d 100644
--- a/Libraries/LibJS/Lexer.cpp
+++ b/Libraries/LibJS/Lexer.cpp
@@ -240,13 +240,6 @@ bool Lexer::is_numeric_literal_start() const
return isdigit(m_current_char) || (m_current_char == '.' && m_position < m_source.length() && isdigit(m_source[m_position]));
}
-void Lexer::syntax_error(const char* msg)
-{
- m_has_errors = true;
- if (m_log_errors)
- fprintf(stderr, "Syntax Error: %s (line: %zu, column: %zu)\n", msg, m_line_number, m_line_column);
-}
-
Token Lexer::next()
{
size_t trivia_start = m_position;
@@ -395,7 +388,6 @@ Token Lexer::next()
consume();
}
if (m_current_char != stop_char) {
- syntax_error("unterminated string literal");
token_type = TokenType::UnterminatedStringLiteral;
} else {
consume();
diff --git a/Libraries/LibJS/Lexer.h b/Libraries/LibJS/Lexer.h
index f5a4ae51cd..e60c2dd654 100644
--- a/Libraries/LibJS/Lexer.h
+++ b/Libraries/LibJS/Lexer.h
@@ -37,14 +37,8 @@ namespace JS {
class Lexer {
public:
explicit Lexer(StringView source);
- Lexer(StringView source, bool log_errors)
- : Lexer(source)
- {
- m_log_errors = log_errors;
- }
Token next();
- bool has_errors() const { return m_has_errors; }
private:
void consume();
@@ -60,16 +54,12 @@ private:
bool match(char, char, char) const;
bool match(char, char, char, char) const;
- void syntax_error(const char*);
-
StringView m_source;
size_t m_position = 0;
Token m_current_token;
int m_current_char = 0;
- bool m_has_errors = false;
size_t m_line_number = 1;
size_t m_line_column = 1;
- bool m_log_errors = true;
struct TemplateState {
bool in_expr;
diff --git a/Libraries/LibJS/Parser.h b/Libraries/LibJS/Parser.h
index 90eb95ed09..7cbd0b2b80 100644
--- a/Libraries/LibJS/Parser.h
+++ b/Libraries/LibJS/Parser.h
@@ -75,7 +75,7 @@ public:
NonnullRefPtr<NewExpression> parse_new_expression();
RefPtr<FunctionExpression> try_parse_arrow_function_expression(bool expect_parens);
- bool has_errors() const { return m_parser_state.m_lexer.has_errors() || m_parser_state.m_has_errors; }
+ bool has_errors() const { m_parser_state.m_has_errors; }
private:
friend class ScopePusher;
diff --git a/Userland/js.cpp b/Userland/js.cpp
index ed5ddb3c56..acfe17d87e 100644
--- a/Userland/js.cpp
+++ b/Userland/js.cpp
@@ -524,7 +524,7 @@ int main(int argc, char** argv)
size_t open_indents = s_repl_line_level;
- JS::Lexer lexer(str, false);
+ JS::Lexer lexer(str);
bool indenters_starting_line = true;
for (JS::Token token = lexer.next(); token.type() != JS::TokenType::Eof; token = lexer.next()) {
auto length = token.value().length();