Skip to content

Commit

Permalink
LibJS: Keep track of file names, lines and columns inside the AST
Browse files Browse the repository at this point in the history
  • Loading branch information
boricj authored and awesomekling committed Mar 1, 2021
1 parent 007b6ed commit 0039ecb
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 122 deletions.
8 changes: 6 additions & 2 deletions Userland/Libraries/LibJS/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ HashMap<String, TokenType> Lexer::s_three_char_tokens;
HashMap<String, TokenType> Lexer::s_two_char_tokens;
HashMap<char, TokenType> Lexer::s_single_char_tokens;

Lexer::Lexer(StringView source)
Lexer::Lexer(StringView source, StringView filename, size_t line_number, size_t line_column)
: m_source(source)
, m_current_token(TokenType::Eof, {}, StringView(nullptr), StringView(nullptr), 0, 0)
, m_current_token(TokenType::Eof, {}, StringView(nullptr), StringView(nullptr), filename, 0, 0)
, m_filename(filename)
, m_line_number(line_number)
, m_line_column(line_column)
{
if (s_keywords.is_empty()) {
s_keywords.set("await", TokenType::Await);
Expand Down Expand Up @@ -635,6 +638,7 @@ Token Lexer::next()
token_message,
m_source.substring_view(trivia_start - 1, value_start - trivia_start),
m_source.substring_view(value_start - 1, m_position - value_start),
m_filename,
value_start_line_number,
value_start_column_number);

Expand Down
5 changes: 4 additions & 1 deletion Userland/Libraries/LibJS/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ namespace JS {

class Lexer {
public:
explicit Lexer(StringView source);
explicit Lexer(StringView source, StringView filename = "(unknown)", size_t line_number = 1, size_t line_column = 0);

Token next();

const StringView& source() const { return m_source; };
const StringView& filename() const { return m_filename; };

private:
void consume();
Expand All @@ -65,6 +66,8 @@ class Lexer {
size_t m_position { 0 };
Token m_current_token;
char m_current_char { 0 };

StringView m_filename;
size_t m_line_number { 1 };
size_t m_line_column { 0 };

Expand Down
Loading

0 comments on commit 0039ecb

Please sign in to comment.