1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:17:45 +00:00

AK+LibXML+JSSpecCompiler: Move LineTrackingLexer to AK

This is a simple extension of GenericLexer, and is used in more than
just LibXML, so let's move it into AK.
The move also resolves a FIXME, which is removed in this commit.
This commit is contained in:
Ali Mohammad Pur 2024-02-16 04:55:17 +03:30 committed by Andreas Kling
parent 08c02ad888
commit bc301b6f40
8 changed files with 79 additions and 78 deletions

View file

@ -39,29 +39,6 @@ struct Listener {
virtual void error(ParseError const&) { }
};
// FIXME: This is also used in JSSpecCompiler, so should probably live in AK or even merged with
// AK::GenericLexer.
class LineTrackingLexer : public GenericLexer {
public:
using GenericLexer::GenericLexer;
LineTrackingLexer(StringView input, XML::Offset start_offset)
: GenericLexer(input)
, m_cached_offset {
.line = start_offset.line,
.column = start_offset.column,
}
{
}
Offset cached_offset() const { return m_cached_offset; }
void restore_cached_offset(Offset cached_offset) { m_cached_offset = cached_offset; }
Offset offset_for(size_t) const;
protected:
mutable Offset m_cached_offset;
};
class Parser {
public:
struct Options {
@ -96,8 +73,8 @@ private:
ErrorOr<void, ParseError> parse_internal();
void append_node(NonnullOwnPtr<Node>);
void append_text(StringView, Offset);
void append_comment(StringView, Offset);
void append_text(StringView, LineTrackingLexer::Position);
void append_comment(StringView, LineTrackingLexer::Position);
void enter_node(Node&);
void leave_node();
@ -170,9 +147,9 @@ private:
[[nodiscard]] auto rollback_point(SourceLocation location = SourceLocation::current())
{
return ArmedScopeGuard {
[this, position = m_lexer.tell(), cached_offset = m_lexer.cached_offset(), location] {
[this, position = m_lexer.tell(), cached_position = m_lexer.cached_position(), location] {
m_lexer.retreat(m_lexer.tell() - position);
m_lexer.restore_cached_offset(cached_offset);
m_lexer.restore_cached_offset(cached_position);
(void)location;
dbgln_if(XML_PARSER_DEBUG, "{:->{}}FAIL @ {} -- \x1b[31m{}\x1b[0m", " ", s_debug_indent_level * 2, location, m_lexer.remaining().substring_view(0, min(16, m_lexer.tell_remaining())).replace("\n"sv, "\\n"sv, ReplaceMode::All));
}