-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty.h
77 lines (58 loc) · 1.43 KB
/
pretty.h
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once
// (c) Robert Muth - see LICENSE for more info
// see pretty.py for documentation
#include <cstddef>
#include <string>
#include <string_view>
#include <vector>
namespace PP {
const size_t INFINITE_WIDTH = 1000000;
enum class BreakType {
INVALID = 0,
FITS,
INCONSISTENT,
CONSISTENT,
FORCE_LINE_BREAK
};
enum class TokenType {
INVALID,
BEG,
END,
BRK,
STR,
};
struct Token {
TokenType type;
union {
std::string_view str;
struct {
ssize_t num_spaces;
ssize_t offset;
bool nobreak;
} brk;
struct {
BreakType break_type;
ssize_t offset;
} beg;
struct {
} end; // silly but avoids a warning
};
};
inline Token Brk(ssize_t num_space = 1, ssize_t offset = 0,
bool nobreak = false) {
return {.type = TokenType::BRK, .brk = {num_space, offset, nobreak}};
}
inline Token End() { return {.type = TokenType::END, .end = {}}; }
inline Token Str(std::string_view str) {
return {.type = TokenType::STR, .str = str};
}
inline Token Beg(BreakType break_type, ssize_t offset) {
return {.type = TokenType::BEG, .beg = {break_type, offset}};
}
inline Token NoBreak(size_t num_spaces) { return Brk(num_spaces, 0, true); }
inline Token LineBreak(size_t offset = 0) {
return Brk(INFINITE_WIDTH, offset, false);
}
extern std::string PrettyPrint(const std::vector<Token>& tokens,
size_t line_width);
} // namespace PP