gsc-tool/include/xsk/t6/lexer.hpp

79 lines
1.5 KiB
C++
Raw Normal View History

2023-01-23 17:31:08 -05:00
// Copyright 2023 xensik. All rights reserved.
2022-02-18 13:07:37 -05:00
//
// Use of this source code is governed by a GNU GPLv3 license
// that can be found in the LICENSE file.
#pragma once
namespace xsk::arc::t6
{
constexpr size_t max_buf_size = 0x2000;
struct buffer
{
char* data;
2022-04-07 13:22:10 -04:00
size_t length;
2022-02-18 13:07:37 -05:00
buffer();
~buffer();
bool push(char c);
};
struct reader
{
enum state_type : std::uint8_t { end, ok };
const char* buffer_pos;
std::uint32_t bytes_remaining;
char last_byte;
char current_byte;
state_type state;
reader();
2022-03-21 11:36:09 -04:00
reader(const reader& obj)
2022-02-18 13:07:37 -05:00
{
2022-03-21 11:36:09 -04:00
std::memcpy(this, &obj, sizeof(reader));
}
reader& operator=(const reader& obj)
{
std::memcpy(this, &obj, sizeof(reader));
2022-02-18 13:07:37 -05:00
return *this;
}
void init(const char* data, size_t size);
void advance();
};
class lexer
{
2022-02-20 06:10:21 -05:00
enum class state : std::uint8_t { start, string, localize, preprocessor };
2022-02-18 13:07:37 -05:00
reader reader_;
buffer buffer_;
location loc_;
std::stack<location> locs_;
std::stack<reader> readers_;
std::uint32_t header_top_;
state state_;
build mode_;
bool indev_;
bool clean_;
public:
lexer(build mode, const std::string& name, const char* data, size_t size);
auto lex() -> parser::symbol_type;
void push_header(const std::string& file);
void pop_header();
void ban_header(const location& loc);
private:
void advance();
2022-02-20 06:10:21 -05:00
void preprocessor_wrap();
void preprocessor_run(parser::token::token_kind_type token);
2022-02-18 13:07:37 -05:00
};
} // namespace xsk::arc::t6