Serenity Operating System
1/*
2 * Copyright (c) 2022, Brian Gianforcaro <bgianf@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/StringView.h>
10
11namespace GUI {
12
13#define FOR_EACH_TOKEN_TYPE \
14 __TOKEN(Comment) \
15 __TOKEN(Unknown)
16
17struct GitCommitPosition {
18 size_t line;
19 size_t column;
20};
21
22struct GitCommitToken {
23 enum class Type {
24#define __TOKEN(x) x,
25 FOR_EACH_TOKEN_TYPE
26#undef __TOKEN
27 };
28
29 char const* to_string() const
30 {
31 switch (m_type) {
32#define __TOKEN(x) \
33 case Type::x: \
34 return #x;
35 FOR_EACH_TOKEN_TYPE
36#undef __TOKEN
37 }
38 VERIFY_NOT_REACHED();
39 }
40
41 Type m_type { Type::Unknown };
42 StringView m_view;
43 GitCommitPosition m_start;
44 GitCommitPosition m_end;
45};
46
47class GitCommitLexer {
48public:
49 GitCommitLexer(StringView);
50
51 Vector<GitCommitToken> lex();
52
53private:
54 char peek(size_t offset = 0) const;
55 char consume();
56
57 StringView m_input;
58 size_t m_index { 0 };
59 GitCommitPosition m_position { 0, 0 };
60};
61
62}
63
64#undef FOR_EACH_TOKEN_TYPE