Skip to content

Commit 6b14d61

Browse files
committed
Add keepSpace to improve formatting of the AST printer
Signed-off-by: Roberto Raggi <[email protected]>
1 parent de26177 commit 6b14d61

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed

packages/cxx-gen-ast/src/gen_ast_pretty_printer_cc.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export function gen_ast_pretty_printer_cc({
7878
emit(`}`);
7979
emit(`void space() { accept.space(); }`);
8080
emit(`void nospace() { accept.nospace(); }`);
81+
emit(`void keepSpace() { accept.keepSpace(); }`);
8182
emit(`void newline() { accept.newline(); }`);
8283
emit(`void nonewline() { accept.nonewline(); }`);
8384
emit(`void indent() { accept.indent(); }`);
@@ -99,6 +100,11 @@ export function gen_ast_pretty_printer_cc({
99100
}
100101
break;
101102

103+
case "opLoc":
104+
emit(`space();`);
105+
emit(`keepSpace();`);
106+
break;
107+
102108
case "closeLoc":
103109
case "colonLoc":
104110
case "dotLoc":
@@ -149,6 +155,15 @@ export function gen_ast_pretty_printer_cc({
149155
}
150156
break;
151157

158+
case "equalLoc":
159+
emit(`keepSpace();`);
160+
break;
161+
162+
case "opLoc":
163+
emit(`space();`);
164+
emit(`keepSpace();`);
165+
break;
166+
152167
case "closeLoc":
153168
case "lbracketLoc":
154169
case "lbracket2Loc":
@@ -379,11 +394,17 @@ void ASTPrettyPrinter::space() {
379394
}
380395
381396
void ASTPrettyPrinter::nospace() {
397+
if (keepSpace_) return;
382398
space_ = false;
383399
}
384400
401+
void ASTPrettyPrinter::keepSpace() {
402+
keepSpace_ = true;
403+
}
404+
385405
void ASTPrettyPrinter::newline() {
386406
space_ = false;
407+
keepSpace_ = false;
387408
newline_ = true;
388409
}
389410

packages/cxx-gen-ast/src/gen_ast_pretty_printer_h.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ ${code.join("\n")}
9999
}
100100
newline_ = false;
101101
space_ = false;
102+
keepSpace_ = false;
102103
std::format_to(output_, fmt, std::forward<Args>(args)...);
103104
space();
104105
}
@@ -107,6 +108,7 @@ ${code.join("\n")}
107108
108109
void space();
109110
void nospace();
111+
void keepSpace();
110112
void newline();
111113
void nonewline();
112114
void indent();
@@ -117,6 +119,7 @@ private:
117119
std::ostream_iterator<char> output_;
118120
int depth_ = 0;
119121
bool space_ = false;
122+
bool keepSpace_ = false;
120123
bool newline_ = false;
121124
};
122125

0 commit comments

Comments
 (0)