1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 03:27:44 +00:00

Shell: Track line numbers and the positions of some keywords

This commit is contained in:
AnotherTest 2020-09-28 14:27:20 +03:30 committed by Andreas Kling
parent b91be8b9fd
commit a10cfee0d4
4 changed files with 251 additions and 107 deletions

View file

@ -42,6 +42,12 @@ public:
RefPtr<AST::Node> parse();
struct SavedOffset {
size_t offset;
AST::Position::Line line;
};
SavedOffset save_offset() const;
private:
RefPtr<AST::Node> parse_toplevel();
RefPtr<AST::Node> parse_sequence();
@ -76,34 +82,55 @@ private:
bool at_end() const { return m_input.length() <= m_offset; }
char peek();
char consume();
void putback();
bool expect(char);
bool expect(const StringView&);
void restore_to(size_t offset, AST::Position::Line line)
{
m_offset = offset;
m_line = move(line);
}
AST::Position::Line line() const { return m_line; }
StringView consume_while(Function<bool(char)>);
struct ScopedOffset {
ScopedOffset(Vector<size_t>& offsets, size_t offset)
ScopedOffset(Vector<size_t>& offsets, Vector<AST::Position::Line>& lines, size_t offset, size_t lineno, size_t linecol)
: offsets(offsets)
, lines(lines)
, offset(offset)
, line({ lineno, linecol })
{
offsets.append(offset);
lines.append(line);
}
~ScopedOffset()
{
auto last = offsets.take_last();
ASSERT(last == offset);
auto last_line = lines.take_last();
ASSERT(last_line == line);
}
Vector<size_t>& offsets;
Vector<AST::Position::Line>& lines;
size_t offset;
AST::Position::Line line;
};
void restore_to(const ScopedOffset& offset) { restore_to(offset.offset, offset.line); }
OwnPtr<ScopedOffset> push_start();
StringView m_input;
size_t m_offset { 0 };
AST::Position::Line m_line { 0, 0 };
Vector<size_t> m_rule_start_offsets;
Vector<AST::Position::Line> m_rule_start_lines;
};
#if 0