hoshi-lang
dev
Yet another programming language
Loading...
Searching...
No Matches
formatter.hpp
Go to the documentation of this file.
1
//
2
// Created by XIaokang00010 on 2025/2/16.
3
//
4
#ifndef HOSHI_LANG_FORMATTER_HPP
5
#define HOSHI_LANG_FORMATTER_HPP
6
7
#include "
compiler/compilerContext.h
"
8
#include "
lexer.hpp
"
9
#include <
share/def.hpp
>
10
11
namespace
yoi
{
12
struct
FormatOption
{
13
enum class
IndentType
{
14
Space
,
15
Tab
16
} indentType{
FormatOption::IndentType::Space
};
17
size_t
indentSize
{4};
18
enum class
BraceType
{
19
Attached
,
20
NewLine
21
} braceType{
FormatOption::BraceType::Attached
};
22
size_t
maxWidth
{80};
23
24
FormatOption
() =
default
;
25
26
FormatOption
(
IndentType
indentType,
size_t
indentSize
,
BraceType
braceType,
size_t
maxWidth
= 80);
27
};
28
29
void
formatToken
(std::wostream &os,
FormatOption
option,
const
lexer::token
&token);
30
31
class
Formatter
{
32
public
:
33
FormatOption
option
;
34
std::wostream &
os
;
35
vec<lexer::Comment>
comments
;
36
37
Formatter
(std::wostream &
os
,
FormatOption
option
);
38
39
Formatter
(std::wostream &
os
,
FormatOption
option
,
vec<lexer::Comment>
comments
);
40
41
private
:
42
size_t
indentLevel
{0};
43
size_t
currentColumn
{0};
44
size_t
lastCommentIdx
{0};
45
uint64_t
lastLine
{0};
46
void
indent
();
47
void
newLine
();
48
void
write
(
const
yoi::wstr
&s);
49
bool
printComments
(
AST
*node);
50
bool
printComments
(uint64_t line, uint64_t col);
51
bool
willFit
(
invocationArguments
*node);
52
53
public
:
54
void
format
(
const
lexer::token
&token);
55
void
format
(
basicLiterals
*node);
56
57
void
format
(
identifier
*node);
58
59
void
format
(
identifierWithTypeSpec
*node);
60
61
void
format
(
defTemplateArgSpec
*node);
62
63
void
format
(
defTemplateArg
*node);
64
65
void
format
(
templateArgSpec
*node);
66
67
void
format
(
templateArg
*node);
68
69
void
format
(
invocationArguments
*node);
70
71
void
format
(
definitionArguments
*node);
72
73
void
format
(
funcTypeSpec
*node);
74
75
void
format
(
typeSpec
*node);
76
77
void
format
(
decltypeExpr
*node);
78
79
void
format
(
subscript
*node);
80
81
void
format
(
identifierWithTemplateArg
*node);
82
83
void
format
(
identifierWithDefTemplateArg
*node);
84
85
void
format
(
subscriptExpr
*node);
86
87
void
format
(
memberExpr
*node);
88
89
void
format
(
primary
*node);
90
91
void
format
(
uniqueExpr
*node);
92
93
void
format
(
mulExpr
*node);
94
95
void
format
(
addExpr
*node);
96
97
void
format
(
shiftExpr
*node);
98
99
void
format
(
relationalExpr
*node);
100
101
void
format
(
equalityExpr
*node);
102
103
void
format
(
andExpr
*node);
104
105
void
format
(
exclusiveExpr
*node);
106
107
void
format
(
inclusiveExpr
*node);
108
109
void
format
(
logicalAndExpr
*node);
110
111
void
format
(
logicalOrExpr
*node);
112
113
void
format
(
rExpr
*node);
114
115
void
format
(
codeBlock
*node);
116
117
void
format
(
useStmt
*node);
118
119
void
format
(
funcDefStmt
*node);
120
121
void
format
(
interfaceDefInnerPair
*node);
122
123
void
format
(
interfaceDefInner
*node);
124
125
void
format
(
interfaceDefStmt
*node);
126
127
void
format
(
structDefInnerPair
*node);
128
129
void
format
(
structDefInner
*node);
130
131
void
format
(
structDefStmt
*node);
132
133
void
format
(
dataStructDefStmt
*node);
134
135
void
format
(
implInnerPair
*node);
136
137
void
format
(
implInner
*node);
138
139
void
format
(
implStmt
*node);
140
141
void
format
(
letAssignmentPair
*node);
142
143
void
format
(
letStmt
*node);
144
145
void
format
(
globalStmt
*node);
146
147
void
format
(
ifStmt
*node);
148
149
void
format
(
whileStmt
*node);
150
151
void
format
(
forStmt
*node);
152
153
void
format
(
forEachStmt
*node);
154
155
void
format
(
returnStmt
*node);
156
157
void
format
(
continueStmt
*node);
158
159
void
format
(
breakStmt
*node);
160
161
void
format
(
inCodeBlockStmt
*node);
162
163
void
format
(
leftExpr
*node);
164
165
void
format
(
externModuleAccessExpression
*node);
166
167
void
format
(
exportDecl
*node);
168
169
void
format
(
importDecl
*node);
170
171
void
format
(importInner *node);
172
173
void
format
(
throwStmt
*node);
174
175
void
format
(
catchParam
*node);
176
177
void
format
(
tryCatchStmt
*node);
178
179
void
format
(
dynCastExpression
*node);
180
181
void
format
(
typeIdExpression
*node);
182
183
void
format
(
newExpression
*node);
184
185
void
format
(
abstractExpr
*node);
186
187
void
format
(
lambdaExpr
*node);
188
189
void
format
(
unnamedDefinitionArguments
*node);
190
191
void
format
(
marcoPair
*node);
192
193
void
format
(
marcoDescriptor
*node);
194
195
void
format
(
typeAliasStmt
*node);
196
197
void
format
(
finalizerDef
*node);
198
199
void
format
(
finalizerDecl
*node);
200
201
void
format
(
funcExpr
*node);
202
203
void
format
(
letAssignmentPairLHS
*node);
204
205
void
format
(
enumerationDefinition
*node);
206
207
void
format
(
enumerationPair
*node);
208
209
void
format
(
bracedInitalizerList
*node);
210
211
void
format
(
hoshiModule
*node);
212
213
void
format
(
innerMethodDecl
*node);
214
215
void
format
(
innerMethodDef
*node);
216
217
void
format
(
constructorDecl
*node);
218
219
void
format
(
constructorDef
*node);
220
221
void
format
(
yieldStmt
*node);
222
223
void
format
(
conceptStmt
*node);
224
225
void
format
(
satisfyStmt
*node);
226
227
void
format
(
satisfyClause
*node);
228
229
void
format
(
conceptDefinition
*node);
230
};
231
}
232
233
#endif
yoi::AST
Definition
ast.hpp:15
yoi::Formatter
Definition
formatter.hpp:31
yoi::Formatter::option
FormatOption option
Definition
formatter.hpp:33
yoi::Formatter::format
void format(relationalExpr *node)
yoi::Formatter::indent
void indent()
Definition
formatter.cpp:316
yoi::Formatter::format
void format(logicalAndExpr *node)
yoi::Formatter::currentColumn
size_t currentColumn
Definition
formatter.hpp:43
yoi::Formatter::newLine
void newLine()
Definition
formatter.cpp:324
yoi::Formatter::format
void format(mulExpr *node)
yoi::Formatter::lastCommentIdx
size_t lastCommentIdx
Definition
formatter.hpp:44
yoi::Formatter::format
void format(const lexer::token &token)
Definition
formatter.cpp:367
yoi::Formatter::format
void format(logicalOrExpr *node)
yoi::Formatter::format
void format(shiftExpr *node)
yoi::Formatter::format
void format(andExpr *node)
yoi::Formatter::comments
vec< lexer::Comment > comments
Definition
formatter.hpp:35
yoi::Formatter::format
void format(inclusiveExpr *node)
yoi::Formatter::willFit
bool willFit(invocationArguments *node)
Definition
formatter.cpp:373
yoi::Formatter::write
void write(const yoi::wstr &s)
Definition
formatter.cpp:330
yoi::Formatter::os
std::wostream & os
Definition
formatter.hpp:34
yoi::Formatter::format
void format(equalityExpr *node)
yoi::Formatter::lastLine
uint64_t lastLine
Definition
formatter.hpp:45
yoi::Formatter::printComments
bool printComments(AST *node)
Definition
formatter.cpp:335
yoi::Formatter::format
void format(addExpr *node)
yoi::Formatter::indentLevel
size_t indentLevel
Definition
formatter.hpp:42
yoi::Formatter::format
void format(exclusiveExpr *node)
yoi::abstractExpr
Definition
ast.hpp:442
yoi::addExpr
Definition
ast.hpp:494
yoi::andExpr
Definition
ast.hpp:542
yoi::basicLiterals
Definition
ast.hpp:251
yoi::bracedInitalizerList
Definition
ast.hpp:214
yoi::breakStmt
Definition
ast.hpp:908
yoi::catchParam
Definition
ast.hpp:1054
yoi::codeBlock
Definition
ast.hpp:1013
yoi::conceptDefinition
Definition
ast.hpp:1102
yoi::conceptStmt
Definition
ast.hpp:1111
yoi::constructorDecl
Definition
ast.hpp:983
yoi::constructorDef
Definition
ast.hpp:995
yoi::continueStmt
Definition
ast.hpp:906
yoi::dataStructDefStmt
Definition
ast.hpp:713
yoi::decltypeExpr
Definition
ast.hpp:619
yoi::defTemplateArgSpec
Definition
ast.hpp:275
yoi::defTemplateArg
Definition
ast.hpp:283
yoi::definitionArguments
Definition
ast.hpp:311
yoi::dynCastExpression
Definition
ast.hpp:1074
yoi::enumerationDefinition
Definition
ast.hpp:1085
yoi::enumerationPair
Definition
ast.hpp:1091
yoi::equalityExpr
Definition
ast.hpp:530
yoi::exclusiveExpr
Definition
ast.hpp:554
yoi::exportDecl
Definition
ast.hpp:1036
yoi::externModuleAccessExpression
Definition
ast.hpp:1027
yoi::finalizerDecl
Definition
ast.hpp:991
yoi::finalizerDef
Definition
ast.hpp:1006
yoi::forEachStmt
Definition
ast.hpp:884
yoi::forStmt
Definition
ast.hpp:868
yoi::funcDefStmt
Definition
ast.hpp:624
yoi::funcExpr
Definition
ast.hpp:245
yoi::funcTypeSpec
Definition
ast.hpp:318
yoi::globalStmt
Definition
ast.hpp:792
yoi::hoshiModule
Definition
ast.hpp:1020
yoi::identifierWithDefTemplateArg
Definition
ast.hpp:373
yoi::identifierWithTemplateArg
Definition
ast.hpp:361
yoi::identifierWithTypeSpec
Definition
ast.hpp:265
yoi::identifier
Definition
ast.hpp:258
yoi::ifStmt
Definition
ast.hpp:834
yoi::implInnerPair
Definition
ast.hpp:723
yoi::implInner
Definition
ast.hpp:745
yoi::implStmt
Definition
ast.hpp:752
yoi::importDecl
Definition
ast.hpp:1043
yoi::inCodeBlockStmt
Definition
ast.hpp:910
yoi::inclusiveExpr
Definition
ast.hpp:566
yoi::innerMethodDecl
Definition
ast.hpp:952
yoi::innerMethodDef
Definition
ast.hpp:966
yoi::interfaceDefInnerPair
Definition
ast.hpp:641
yoi::interfaceDefInner
Definition
ast.hpp:656
yoi::interfaceDefStmt
Definition
ast.hpp:663
yoi::invocationArguments
Definition
ast.hpp:304
yoi::lambdaExpr
Definition
ast.hpp:237
yoi::leftExpr
Definition
ast.hpp:467
yoi::letAssignmentPairLHS
Definition
ast.hpp:767
yoi::letAssignmentPair
Definition
ast.hpp:774
yoi::letStmt
Definition
ast.hpp:785
yoi::logicalAndExpr
Definition
ast.hpp:578
yoi::logicalOrExpr
Definition
ast.hpp:590
yoi::marcoDescriptor
Definition
ast.hpp:232
yoi::marcoPair
Definition
ast.hpp:225
yoi::memberExpr
Definition
ast.hpp:395
yoi::mulExpr
Definition
ast.hpp:482
yoi::newExpression
Definition
ast.hpp:402
yoi::primary
Definition
ast.hpp:409
yoi::rExpr
Definition
ast.hpp:602
yoi::relationalExpr
Definition
ast.hpp:518
yoi::returnStmt
Definition
ast.hpp:897
yoi::satisfyClause
Definition
ast.hpp:1132
yoi::satisfyStmt
Definition
ast.hpp:1127
yoi::shiftExpr
Definition
ast.hpp:506
yoi::structDefInnerPair
Definition
ast.hpp:673
yoi::structDefInner
Definition
ast.hpp:696
yoi::structDefStmt
Definition
ast.hpp:703
yoi::subscriptExpr
Definition
ast.hpp:385
yoi::subscript
Definition
ast.hpp:349
yoi::templateArgSpec
Definition
ast.hpp:290
yoi::templateArg
Definition
ast.hpp:297
yoi::throwStmt
Definition
ast.hpp:1049
yoi::tryCatchStmt
Definition
ast.hpp:1061
yoi::typeAliasStmt
Definition
ast.hpp:219
yoi::typeIdExpression
Definition
ast.hpp:1068
yoi::typeSpec
Definition
ast.hpp:328
yoi::uniqueExpr
Definition
ast.hpp:455
yoi::unnamedDefinitionArguments
Definition
ast.hpp:1080
yoi::useStmt
Definition
ast.hpp:609
yoi::whileStmt
Definition
ast.hpp:858
yoi::yieldStmt
Definition
ast.hpp:1097
compilerContext.h
def.hpp
lexer.hpp
yoi
Definition
builtinModule.cpp:7
yoi::vec
std::vector< t > vec
Definition
def.hpp:53
yoi::formatToken
void formatToken(std::wostream &os, FormatOption option, const lexer::token &token)
Definition
formatter.cpp:14
yoi::wstr
std::wstring wstr
Definition
def.hpp:48
yoi::FormatOption
Definition
formatter.hpp:12
yoi::FormatOption::maxWidth
size_t maxWidth
Definition
formatter.hpp:22
yoi::FormatOption::BraceType
BraceType
Definition
formatter.hpp:18
yoi::FormatOption::BraceType::NewLine
@ NewLine
yoi::FormatOption::BraceType::Attached
@ Attached
yoi::FormatOption::indentSize
size_t indentSize
Definition
formatter.hpp:17
yoi::FormatOption::Space
enum yoi::FormatOption::IndentType Space
yoi::FormatOption::Attached
enum yoi::FormatOption::BraceType Attached
yoi::FormatOption::FormatOption
FormatOption()=default
yoi::FormatOption::IndentType
IndentType
Definition
formatter.hpp:13
yoi::FormatOption::IndentType::Tab
@ Tab
yoi::FormatOption::IndentType::Space
@ Space
yoi::lexer::token
Definition
lexer.hpp:25
compiler
frontend
formatter.hpp
Generated by
1.9.8