Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <AK/StringView.h>
30#include <AK/Vector.h>
31
32namespace GUI {
33
34#define FOR_EACH_TOKEN_TYPE \
35 __TOKEN(Unknown) \
36 __TOKEN(Whitespace) \
37 __TOKEN(PreprocessorStatement) \
38 __TOKEN(IncludeStatement) \
39 __TOKEN(IncludePath) \
40 __TOKEN(LeftParen) \
41 __TOKEN(RightParen) \
42 __TOKEN(LeftCurly) \
43 __TOKEN(RightCurly) \
44 __TOKEN(LeftBracket) \
45 __TOKEN(RightBracket) \
46 __TOKEN(Comma) \
47 __TOKEN(Asterisk) \
48 __TOKEN(Semicolon) \
49 __TOKEN(DoubleQuotedString) \
50 __TOKEN(SingleQuotedString) \
51 __TOKEN(EscapeSequence) \
52 __TOKEN(Comment) \
53 __TOKEN(Integer) \
54 __TOKEN(Float) \
55 __TOKEN(Keyword) \
56 __TOKEN(KnownType) \
57 __TOKEN(Identifier)
58
59struct CppPosition {
60 size_t line;
61 size_t column;
62};
63
64struct CppToken {
65 enum class Type {
66#define __TOKEN(x) x,
67 FOR_EACH_TOKEN_TYPE
68#undef __TOKEN
69 };
70
71 const char* to_string() const
72 {
73 switch (m_type) {
74#define __TOKEN(x) \
75 case Type::x: \
76 return #x;
77 FOR_EACH_TOKEN_TYPE
78#undef __TOKEN
79 }
80 ASSERT_NOT_REACHED();
81 }
82
83 Type m_type { Type::Unknown };
84 CppPosition m_start;
85 CppPosition m_end;
86};
87
88class CppLexer {
89public:
90 CppLexer(const StringView&);
91
92 Vector<CppToken> lex();
93
94private:
95 char peek(size_t offset = 0) const;
96 char consume();
97
98 StringView m_input;
99 size_t m_index { 0 };
100 CppPosition m_previous_position { 0, 0 };
101 CppPosition m_position { 0, 0 };
102};
103
104}