gsc-tool/src/iw8/xsk/lexer.hpp

73 lines
1.4 KiB
C++
Raw Normal View History

2022-01-26 06:08:28 -05:00
// Copyright 2022 xensik. All rights reserved.
//
// Use of this source code is governed by a GNU GPLv3 license
// that can be found in the LICENSE file.
#pragma once
namespace xsk::gsc::iw8
{
2022-01-28 09:58:41 -05:00
constexpr size_t max_buf_size = 0x2000;
2022-01-26 06:08:28 -05:00
struct buffer
{
char* data;
2022-01-28 09:58:41 -05:00
int length;
2022-01-26 06:08:28 -05:00
buffer();
~buffer();
bool push(char c);
};
struct reader
{
2022-01-28 09:58:41 -05:00
enum state_type : std::uint8_t { end, ok };
2022-01-26 06:08:28 -05:00
const char* buffer_pos;
2022-01-28 09:58:41 -05:00
std::uint32_t bytes_remaining;
2022-01-26 06:08:28 -05:00
char last_byte;
char current_byte;
2022-01-28 09:58:41 -05:00
state_type state;
2022-01-26 06:08:28 -05:00
reader();
reader& operator=(const reader& r)
{
std::memcpy(this, &r, sizeof(reader));
return *this;
}
void init(const char* data, size_t size);
void advance();
};
class lexer
{
2022-01-28 09:58:41 -05:00
enum class state : std::uint8_t { start, string, localize, field, preprocessor };
2022-01-26 06:08:28 -05:00
reader reader_;
buffer buffer_;
location loc_;
std::stack<location> locs_;
std::stack<reader> readers_;
std::uint32_t header_top_;
2022-01-28 09:58:41 -05:00
state state_;
2022-01-30 08:45:07 -05:00
build mode_;
2022-01-28 09:58:41 -05:00
bool indev_;
2022-02-04 09:39:47 -05:00
bool clean_;
2022-01-26 06:08:28 -05:00
public:
lexer(const std::string& name, const char* data, size_t size);
2022-01-30 08:45:07 -05:00
auto lex() -> parser::symbol_type;
2022-01-26 06:08:28 -05:00
void push_header(const std::string& file);
void pop_header();
2022-01-30 08:45:07 -05:00
void ban_header(const location& loc);
2022-01-26 06:08:28 -05:00
private:
2022-02-04 09:39:47 -05:00
void advance();
void preprocessor(parser::token::token_kind_type token);
2022-01-26 06:08:28 -05:00
};
} // namespace xsk::gsc::iw8