Skip to content

Commit 0bb1ac1

Browse files
authored
fix(printer): Underscore at the end of the line (#77)
"\n" was missing when printing underscore on the last line
1 parent 50c1fd4 commit 0bb1ac1

File tree

3 files changed

+117
-7
lines changed

3 files changed

+117
-7
lines changed

cpp/printer.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,13 @@ inline void DocPrinter::Append(const Node &doc, const PrinterConfig &cfg) {
476476
}
477477

478478
inline ::mlc::Str DocPrinter::GetString() const {
479-
mlc::Str text = output_.str();
479+
std::string text = output_.str();
480480
// Remove any trailing indentation
481-
while (!text->empty() && std::isspace(text->back())) {
482-
text->pop_back();
481+
while (!text.empty() && text.back() == ' ') {
482+
text.pop_back();
483+
}
484+
if (!text.empty() && text.back() != '\n') {
485+
text.push_back('\n');
483486
}
484487
return DecorateText(text, line_starts_, options_, MergeAndExemptSpans(underlines_, underlines_exempted_));
485488
}

python/mlc/testing/toy_ir/ir.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class Add(Expr):
3838
rhs: Expr
3939

4040
def __ir_print__(self, printer: mlcp.IRPrinter, path: mlcp.ObjectPath) -> mlt.Node:
41-
lhs: mlt.Expr = printer(self.lhs, path=path["a"])
42-
rhs: mlt.Expr = printer(self.rhs, path=path["b"])
41+
lhs: mlt.Expr = printer(self.lhs, path=path["lhs"])
42+
rhs: mlt.Expr = printer(self.rhs, path=path["rhs"])
4343
return lhs + rhs
4444

4545

@@ -49,9 +49,9 @@ class Assign(Stmt):
4949
lhs: Var = mlcd.field(structure="bind")
5050

5151
def __ir_print__(self, printer: mlcp.IRPrinter, path: mlcp.ObjectPath) -> mlt.Node:
52-
rhs: mlt.Expr = printer(self.rhs, path=path["b"])
52+
rhs: mlt.Expr = printer(self.rhs, path=path["rhs"])
5353
printer.var_def(self.lhs.name, obj=self.lhs)
54-
lhs: mlt.Expr = printer(self.lhs, path=path["a"])
54+
lhs: mlt.Expr = printer(self.lhs, path=path["lhs"])
5555
return mlt.Assign(lhs=lhs, rhs=rhs)
5656

5757

tests/python/test_printer_ir_printer.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import re
22

33
import mlc.printer as mlcp
4+
import pytest
5+
from mlc.printer import ObjectPath
46
from mlc.testing.toy_ir import Add, Assign, Func, Var
57

68

@@ -105,3 +107,108 @@ def f(a):
105107
r" return a_0x[0-9A-Fa-f]+$",
106108
mlcp.to_python(f, mlcp.PrinterConfig(print_addr_on_dup_var=True)),
107109
)
110+
111+
112+
@pytest.mark.parametrize(
113+
"path, expected",
114+
[
115+
(
116+
ObjectPath.root()["args"][0],
117+
"""
118+
def f(a, b):
119+
^
120+
c = a + b
121+
return c
122+
""",
123+
),
124+
(
125+
ObjectPath.root()["args"][1],
126+
"""
127+
def f(a, b):
128+
^
129+
c = a + b
130+
return c
131+
""",
132+
),
133+
(
134+
ObjectPath.root()["stmts"][0],
135+
"""
136+
def f(a, b):
137+
c = a + b
138+
^^^^^^^^^
139+
return c
140+
""",
141+
),
142+
(
143+
ObjectPath.root()["stmts"][0],
144+
"""
145+
def f(a, b):
146+
c = a + b
147+
^^^^^^^^^
148+
return c
149+
""",
150+
),
151+
(
152+
ObjectPath.root()["stmts"][0]["lhs"],
153+
"""
154+
def f(a, b):
155+
c = a + b
156+
^
157+
return c
158+
""",
159+
),
160+
(
161+
ObjectPath.root()["stmts"][0]["rhs"],
162+
"""
163+
def f(a, b):
164+
c = a + b
165+
^^^^^
166+
return c
167+
""",
168+
),
169+
(
170+
ObjectPath.root()["stmts"][0]["rhs"]["lhs"],
171+
"""
172+
def f(a, b):
173+
c = a + b
174+
^
175+
return c
176+
""",
177+
),
178+
(
179+
ObjectPath.root()["stmts"][0]["rhs"]["rhs"],
180+
"""
181+
def f(a, b):
182+
c = a + b
183+
^
184+
return c
185+
""",
186+
),
187+
(
188+
ObjectPath.root()["ret"],
189+
"""
190+
def f(a, b):
191+
c = a + b
192+
return c
193+
^
194+
""",
195+
),
196+
],
197+
)
198+
def test_print_underscore(path: ObjectPath, expected: str) -> None:
199+
a = Var(name="a")
200+
b = Var(name="b")
201+
c = Var(name="c")
202+
f = Func(
203+
name="f",
204+
args=[a, b],
205+
stmts=[
206+
Assign(lhs=c, rhs=Add(a, b)),
207+
],
208+
ret=c,
209+
)
210+
actual = mlcp.to_python(
211+
f,
212+
mlcp.PrinterConfig(path_to_underline=[path]),
213+
)
214+
assert actual.strip() == expected.strip()

0 commit comments

Comments
 (0)