2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-10-25 17:52:44 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
2020-09-28 13:21:25 +00:00
|
|
|
namespace Cpp {
|
2020-02-07 19:07:15 +00:00
|
|
|
|
2019-10-25 17:52:44 +00:00
|
|
|
#define FOR_EACH_TOKEN_TYPE \
|
2019-10-25 19:58:40 +00:00
|
|
|
__TOKEN(Unknown) \
|
2019-10-25 17:52:44 +00:00
|
|
|
__TOKEN(Whitespace) \
|
|
|
|
__TOKEN(PreprocessorStatement) \
|
2020-03-11 22:17:29 +00:00
|
|
|
__TOKEN(IncludeStatement) \
|
|
|
|
__TOKEN(IncludePath) \
|
2019-10-25 17:52:44 +00:00
|
|
|
__TOKEN(LeftParen) \
|
|
|
|
__TOKEN(RightParen) \
|
|
|
|
__TOKEN(LeftCurly) \
|
|
|
|
__TOKEN(RightCurly) \
|
|
|
|
__TOKEN(LeftBracket) \
|
|
|
|
__TOKEN(RightBracket) \
|
2020-07-26 20:37:23 +00:00
|
|
|
__TOKEN(Less) \
|
|
|
|
__TOKEN(Greater) \
|
|
|
|
__TOKEN(LessEquals) \
|
|
|
|
__TOKEN(GreaterEquals) \
|
|
|
|
__TOKEN(LessLess) \
|
|
|
|
__TOKEN(GreaterGreater) \
|
|
|
|
__TOKEN(LessLessEquals) \
|
|
|
|
__TOKEN(GreaterGreaterEquals) \
|
|
|
|
__TOKEN(LessGreater) \
|
2019-10-25 17:52:44 +00:00
|
|
|
__TOKEN(Comma) \
|
2020-07-26 17:39:43 +00:00
|
|
|
__TOKEN(Plus) \
|
2020-07-26 22:13:13 +00:00
|
|
|
__TOKEN(PlusPlus) \
|
2020-07-26 17:39:43 +00:00
|
|
|
__TOKEN(PlusEquals) \
|
|
|
|
__TOKEN(Minus) \
|
2020-07-26 22:13:13 +00:00
|
|
|
__TOKEN(MinusMinus) \
|
2020-07-26 17:39:43 +00:00
|
|
|
__TOKEN(MinusEquals) \
|
2019-10-25 17:52:44 +00:00
|
|
|
__TOKEN(Asterisk) \
|
2020-07-26 17:35:47 +00:00
|
|
|
__TOKEN(AsteriskEquals) \
|
2020-07-26 17:39:43 +00:00
|
|
|
__TOKEN(Slash) \
|
|
|
|
__TOKEN(SlashEquals) \
|
2020-07-26 20:37:23 +00:00
|
|
|
__TOKEN(Percent) \
|
|
|
|
__TOKEN(PercentEquals) \
|
2020-07-26 22:16:22 +00:00
|
|
|
__TOKEN(Caret) \
|
|
|
|
__TOKEN(CaretEquals) \
|
2020-07-26 22:20:50 +00:00
|
|
|
__TOKEN(ExclamationMark) \
|
|
|
|
__TOKEN(ExclamationMarkEquals) \
|
2020-07-26 17:39:43 +00:00
|
|
|
__TOKEN(Equals) \
|
|
|
|
__TOKEN(EqualsEquals) \
|
2020-07-26 20:40:57 +00:00
|
|
|
__TOKEN(And) \
|
|
|
|
__TOKEN(AndAnd) \
|
|
|
|
__TOKEN(AndEquals) \
|
|
|
|
__TOKEN(Pipe) \
|
|
|
|
__TOKEN(PipePipe) \
|
|
|
|
__TOKEN(PipeEquals) \
|
2020-07-26 22:20:50 +00:00
|
|
|
__TOKEN(Tilde) \
|
|
|
|
__TOKEN(QuestionMark) \
|
|
|
|
__TOKEN(Colon) \
|
2020-07-26 22:35:17 +00:00
|
|
|
__TOKEN(ColonColon) \
|
|
|
|
__TOKEN(ColonColonAsterisk) \
|
2019-10-25 17:52:44 +00:00
|
|
|
__TOKEN(Semicolon) \
|
2020-07-26 22:26:04 +00:00
|
|
|
__TOKEN(Dot) \
|
2020-07-26 22:35:17 +00:00
|
|
|
__TOKEN(DotAsterisk) \
|
2020-07-26 22:26:04 +00:00
|
|
|
__TOKEN(Arrow) \
|
2020-07-26 22:35:17 +00:00
|
|
|
__TOKEN(ArrowAsterisk) \
|
2019-10-25 17:52:44 +00:00
|
|
|
__TOKEN(DoubleQuotedString) \
|
|
|
|
__TOKEN(SingleQuotedString) \
|
2020-07-27 00:49:17 +00:00
|
|
|
__TOKEN(RawString) \
|
2020-03-10 20:26:11 +00:00
|
|
|
__TOKEN(EscapeSequence) \
|
2019-10-25 17:52:44 +00:00
|
|
|
__TOKEN(Comment) \
|
2020-03-11 18:31:25 +00:00
|
|
|
__TOKEN(Integer) \
|
|
|
|
__TOKEN(Float) \
|
2019-10-25 17:52:44 +00:00
|
|
|
__TOKEN(Keyword) \
|
2019-10-26 08:32:12 +00:00
|
|
|
__TOKEN(KnownType) \
|
2019-10-25 17:52:44 +00:00
|
|
|
__TOKEN(Identifier)
|
|
|
|
|
2020-09-28 13:21:25 +00:00
|
|
|
struct Position {
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t line;
|
|
|
|
size_t column;
|
2019-10-25 19:07:45 +00:00
|
|
|
};
|
|
|
|
|
2020-09-28 13:21:25 +00:00
|
|
|
struct Token {
|
2019-10-25 17:52:44 +00:00
|
|
|
enum class Type {
|
|
|
|
#define __TOKEN(x) x,
|
|
|
|
FOR_EACH_TOKEN_TYPE
|
|
|
|
#undef __TOKEN
|
|
|
|
};
|
|
|
|
|
|
|
|
const char* to_string() const
|
|
|
|
{
|
|
|
|
switch (m_type) {
|
|
|
|
#define __TOKEN(x) \
|
|
|
|
case Type::x: \
|
|
|
|
return #x;
|
|
|
|
FOR_EACH_TOKEN_TYPE
|
|
|
|
#undef __TOKEN
|
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-10-25 19:58:40 +00:00
|
|
|
Type m_type { Type::Unknown };
|
2020-09-28 13:21:25 +00:00
|
|
|
Position m_start;
|
|
|
|
Position m_end;
|
2019-10-25 17:52:44 +00:00
|
|
|
};
|
|
|
|
|
2020-09-28 13:21:25 +00:00
|
|
|
class Lexer {
|
2019-10-25 17:52:44 +00:00
|
|
|
public:
|
2020-09-28 13:21:25 +00:00
|
|
|
Lexer(const StringView&);
|
2019-10-25 17:52:44 +00:00
|
|
|
|
2020-09-28 13:21:25 +00:00
|
|
|
Vector<Token> lex();
|
2019-10-25 17:52:44 +00:00
|
|
|
|
|
|
|
private:
|
2019-12-09 16:45:40 +00:00
|
|
|
char peek(size_t offset = 0) const;
|
2019-10-25 17:52:44 +00:00
|
|
|
char consume();
|
|
|
|
|
|
|
|
StringView m_input;
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t m_index { 0 };
|
2020-09-28 13:21:25 +00:00
|
|
|
Position m_previous_position { 0, 0 };
|
|
|
|
Position m_position { 0, 0 };
|
2019-10-25 17:52:44 +00:00
|
|
|
};
|
2020-02-07 19:07:15 +00:00
|
|
|
|
|
|
|
}
|