1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:38:10 +00:00

LibJS: Track source positions all the way down to exceptions

This makes exceptions have a trace of source positions too, which could
probably be helpful in making fancier error tracebacks.
This commit is contained in:
AnotherTest 2020-12-28 20:45:22 +03:30 committed by Andreas Kling
parent f17874ecd2
commit b34b681811
10 changed files with 647 additions and 245 deletions

View file

@ -31,6 +31,7 @@
#include <AK/StringBuilder.h>
#include <LibJS/AST.h>
#include <LibJS/Lexer.h>
#include <LibJS/SourceRange.h>
#include <stdio.h>
namespace JS {
@ -102,11 +103,6 @@ public:
RefPtr<Statement> try_parse_labelled_statement();
RefPtr<MetaProperty> try_parse_new_target_expression();
struct Position {
size_t line;
size_t column;
};
struct Error {
String message;
Optional<Position> position;
@ -175,6 +171,30 @@ private:
void load_state();
Position position() const;
struct RulePosition {
RulePosition(Parser& parser, Position position)
: m_parser(parser)
, m_position(position)
{
m_parser.m_parser_state.m_rule_starts.append(position);
}
~RulePosition()
{
auto last = m_parser.m_parser_state.m_rule_starts.take_last();
ASSERT(last.line == m_position.line);
ASSERT(last.column == m_position.column);
}
const Position& position() const { return m_position; }
private:
Parser& m_parser;
Position m_position;
};
[[nodiscard]] RulePosition push_start() { return { *this, position() }; }
struct ParserState {
Lexer m_lexer;
Token m_current_token;
@ -182,6 +202,7 @@ private:
Vector<NonnullRefPtrVector<VariableDeclaration>> m_var_scopes;
Vector<NonnullRefPtrVector<VariableDeclaration>> m_let_scopes;
Vector<NonnullRefPtrVector<FunctionDeclaration>> m_function_scopes;
Vector<Position> m_rule_starts;
HashTable<StringView> m_labels_in_scope;
bool m_strict_mode { false };
bool m_allow_super_property_lookup { false };