2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-10-25 19:52:44 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
|
#include <AK/Vector.h>
|
2022-09-14 00:56:13 +02:00
|
|
|
#include <LibCpp/Token.h>
|
2019-10-25 19:52:44 +02:00
|
|
|
|
2020-09-28 16:21:25 +03:00
|
|
|
namespace Cpp {
|
2020-02-07 20:07:15 +01:00
|
|
|
|
2020-09-28 16:21:25 +03:00
|
|
|
class Lexer {
|
2019-10-25 19:52:44 +02:00
|
|
|
public:
|
2021-11-11 00:55:02 +01:00
|
|
|
explicit Lexer(StringView, size_t start_line = 0);
|
2019-10-25 19:52:44 +02:00
|
|
|
|
2020-09-28 16:21:25 +03:00
|
|
|
Vector<Token> lex();
|
2021-08-21 16:36:40 +03:00
|
|
|
template<typename Callback>
|
|
|
|
|
void lex_iterable(Callback);
|
2019-10-25 19:52:44 +02:00
|
|
|
|
2021-08-13 12:11:26 +03:00
|
|
|
void set_ignore_whitespace(bool value) { m_options.ignore_whitespace = value; }
|
|
|
|
|
|
2019-10-25 19:52:44 +02:00
|
|
|
private:
|
2019-12-09 17:45:40 +01:00
|
|
|
char peek(size_t offset = 0) const;
|
2019-10-25 19:52:44 +02:00
|
|
|
char consume();
|
2021-08-21 16:36:40 +03:00
|
|
|
void lex_impl(Function<void(Token)>);
|
2019-10-25 19:52:44 +02:00
|
|
|
|
|
|
|
|
StringView m_input;
|
2019-12-09 17:45:40 +01:00
|
|
|
size_t m_index { 0 };
|
2020-09-28 16:21:25 +03:00
|
|
|
Position m_previous_position { 0, 0 };
|
|
|
|
|
Position m_position { 0, 0 };
|
2021-08-13 12:11:26 +03:00
|
|
|
|
|
|
|
|
struct Options {
|
|
|
|
|
bool ignore_whitespace { false };
|
|
|
|
|
} m_options;
|
2019-10-25 19:52:44 +02:00
|
|
|
};
|
2020-02-07 20:07:15 +01:00
|
|
|
|
2021-08-21 16:36:40 +03:00
|
|
|
template<typename Callback>
|
|
|
|
|
void Lexer::lex_iterable(Callback callback)
|
|
|
|
|
{
|
|
|
|
|
return lex_impl(move(callback));
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-07 20:07:15 +01:00
|
|
|
}
|