aboutsummaryrefslogtreecommitdiff
path: root/src/brussel.codegen.comp/CodegenLexer.hpp
blob: ec8c8b7e19ebdd1be2739f0a19eaec676fa342f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once

#include <LookupTable.hpp>

#include <stb_c_lexer.h>
#include <span>
#include <string>
#include <string_view>
#include <vector>

enum {
	CLEX_ext_single_char = CLEX_first_unused_token,
	CLEX_ext_double_colon,
	CLEX_ext_dot_dot_dot,
	CLEX_ext_COUNT,
};

struct StbLexerToken {
	std::string text;

	union {
		double lexerRealNumber;
		long lexerIntNumber;
	};

	// Can either be CLEX_* or CLEX_ext_* values
	int type;

	int Reamalgamate() const;
};

bool StbTokenIsSingleChar(int lexerToken);
bool StbTokenIsMultiChar(int lexerToken);
std::string CombineTokens(std::span<const StbLexerToken> tokens, std::string_view separator = {});

struct CodegenLexer {
	std::vector<StbLexerToken> tokens;
	size_t idx = 0;

	void InitializeFrom(std::string_view source);

	const StbLexerToken& Current() const;

	const StbLexerToken* TryConsumeToken(int type);
	const StbLexerToken* TryConsumeSingleCharToken(char c);

    void SkipUntilToken(int type);
    void SkipUntilTokenSingleChar(char c);
};