mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibDiff: Implement ability to parse a patch header
This is a somewhat naive implementation, but it is enough to parse a simple unified patch header. After parsing the patch header, the parser will be at the beginning of the first hunks range, ready for that hunk to be parsed.
This commit is contained in:
parent
ef45221c21
commit
efb26b1781
Notes:
sideshowbarker
2024-07-17 01:23:08 +09:00
Author: https://github.com/shannonbooth Commit: https://github.com/SerenityOS/serenity/commit/efb26b1781 Pull-request: https://github.com/SerenityOS/serenity/pull/19844 Reviewed-by: https://github.com/AtkinsSJ ✅
3 changed files with 44 additions and 0 deletions
|
@ -8,8 +8,11 @@
|
|||
|
||||
namespace Diff {
|
||||
|
||||
enum class Format;
|
||||
|
||||
class Parser;
|
||||
|
||||
struct Header;
|
||||
struct Hunk;
|
||||
struct HunkLocation;
|
||||
struct Line;
|
||||
|
|
|
@ -57,6 +57,33 @@ bool Parser::consume_line_number(size_t& number)
|
|||
return true;
|
||||
}
|
||||
|
||||
ErrorOr<Header> Parser::parse_header()
|
||||
{
|
||||
Header header;
|
||||
|
||||
while (!is_eof()) {
|
||||
|
||||
if (consume_specific("+++ ")) {
|
||||
header.new_file_path = TRY(String::from_utf8(consume_line()));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (consume_specific("--- ")) {
|
||||
header.old_file_path = TRY(String::from_utf8(consume_line()));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (next_is("@@ ")) {
|
||||
header.format = Format::Unified;
|
||||
return header;
|
||||
}
|
||||
|
||||
consume_line();
|
||||
}
|
||||
|
||||
return Error::from_string_literal("Unable to find any patch");
|
||||
}
|
||||
|
||||
ErrorOr<Vector<Hunk>> Parser::parse_hunks()
|
||||
{
|
||||
Vector<Hunk> hunks;
|
||||
|
|
|
@ -59,12 +59,26 @@ struct Hunk {
|
|||
Vector<Line> lines;
|
||||
};
|
||||
|
||||
enum class Format {
|
||||
Unified,
|
||||
Unknown,
|
||||
};
|
||||
|
||||
struct Header {
|
||||
Format format { Format::Unknown };
|
||||
|
||||
String old_file_path;
|
||||
String new_file_path;
|
||||
};
|
||||
|
||||
class Parser : public GenericLexer {
|
||||
public:
|
||||
using GenericLexer::GenericLexer;
|
||||
|
||||
ErrorOr<Vector<Hunk>> parse_hunks();
|
||||
|
||||
ErrorOr<Header> parse_header();
|
||||
|
||||
private:
|
||||
Optional<HunkLocation> consume_unified_location();
|
||||
bool consume_line_number(size_t& number);
|
||||
|
|
Loading…
Reference in a new issue