summaryrefslogtreecommitdiff
path: root/Libraries/LibJS
diff options
context:
space:
mode:
Diffstat (limited to 'Libraries/LibJS')
-rw-r--r--Libraries/LibJS/Lexer.cpp56
-rw-r--r--Libraries/LibJS/Lexer.h3
-rw-r--r--Libraries/LibJS/Tests/comments-basic.js20
3 files changed, 63 insertions, 16 deletions
diff --git a/Libraries/LibJS/Lexer.cpp b/Libraries/LibJS/Lexer.cpp
index fc16cb239e..f8bfce245a 100644
--- a/Libraries/LibJS/Lexer.cpp
+++ b/Libraries/LibJS/Lexer.cpp
@@ -167,6 +167,36 @@ void Lexer::consume_exponent()
}
}
+bool Lexer::match(char a, char b) const
+{
+ if (m_position >= m_source.length())
+ return false;
+
+ return m_current_char == a
+ && m_source[m_position] == b;
+}
+
+bool Lexer::match(char a, char b, char c) const
+{
+ if (m_position + 1 >= m_source.length())
+ return false;
+
+ return m_current_char == a
+ && m_source[m_position] == b
+ && m_source[m_position + 1] == c;
+}
+
+bool Lexer::match(char a, char b, char c, char d) const
+{
+ if (m_position + 2 >= m_source.length())
+ return false;
+
+ return m_current_char == a
+ && m_source[m_position] == b
+ && m_source[m_position + 1] == c
+ && m_source[m_position + 2] == d;
+}
+
bool Lexer::is_eof() const
{
return m_current_char == EOF;
@@ -184,17 +214,17 @@ bool Lexer::is_identifier_middle() const
bool Lexer::is_line_comment_start() const
{
- return m_current_char == '/' && m_position < m_source.length() && m_source[m_position] == '/';
+ return match('/', '/') || match('<', '!', '-', '-') || match('-', '-', '>');
}
bool Lexer::is_block_comment_start() const
{
- return m_current_char == '/' && m_position < m_source.length() && m_source[m_position] == '*';
+ return match('/', '*');
}
bool Lexer::is_block_comment_end() const
{
- return m_current_char == '*' && m_position < m_source.length() && m_source[m_position] == '/';
+ return match('*', '/');
}
bool Lexer::is_numeric_literal_start() const
@@ -328,19 +358,13 @@ Token Lexer::next()
} else {
// There is only one four-char operator: >>>=
bool found_four_char_token = false;
- if (m_position + 2 < m_source.length()) {
- if (m_current_char == '>'
- && m_source[m_position] == '>'
- && m_source[m_position + 1] == '>'
- && m_source[m_position + 2] == '=') {
-
- found_four_char_token = true;
- consume();
- consume();
- consume();
- consume();
- token_type = TokenType::UnsignedShiftRightEquals;
- }
+ if (match('>', '>', '>', '=')) {
+ found_four_char_token = true;
+ consume();
+ consume();
+ consume();
+ consume();
+ token_type = TokenType::UnsignedShiftRightEquals;
}
bool found_three_char_token = false;
diff --git a/Libraries/LibJS/Lexer.h b/Libraries/LibJS/Lexer.h
index e896d3ca2a..d9be445eb6 100644
--- a/Libraries/LibJS/Lexer.h
+++ b/Libraries/LibJS/Lexer.h
@@ -56,6 +56,9 @@ private:
bool is_block_comment_start() const;
bool is_block_comment_end() const;
bool is_numeric_literal_start() const;
+ bool match(char, char) const;
+ bool match(char, char, char) const;
+ bool match(char, char, char, char) const;
void syntax_error(const char*);
diff --git a/Libraries/LibJS/Tests/comments-basic.js b/Libraries/LibJS/Tests/comments-basic.js
new file mode 100644
index 0000000000..c819f61545
--- /dev/null
+++ b/Libraries/LibJS/Tests/comments-basic.js
@@ -0,0 +1,20 @@
+try {
+ var i = 0;
+
+ // i++;
+ /* i++; */
+ /*
+ i++;
+ */
+ <!-- i++; --> i++;
+ <!-- i++;
+ i++;
+ --> i++;
+
+ assert(i === 1);
+
+ console.log('PASS');
+} catch (e) {
+ console.log('FAIL: ' + e);
+}
+