Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions internal/poet/reserved.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,37 @@ import "slices"

// TODO(quentin@escape.tech): check if this is complete
var reservedKeywords = []string{
"class",
"if",
"else",
"elif",
"not",
"for",
"and",
"in",
"is",
"or",
"with",
"as",
"assert",
"async",
"await",
"break",
"class",
"continue",
"def",
"del",
"elif",
"else",
"except",
"finally",
"try",
"for",
"from",
"global",
"if",
"import",
"in",
"is",
"lambda",
"nonlocal",
"not",
"or",
"pass",
"raise",
"return",
"try",
"while",
"with",
"yield",
}

Expand Down
59 changes: 47 additions & 12 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ func (w *writer) printIndent(indent int32) {
}
}

func (w *writer) printCommentText(text string, indent int32) {
lines := strings.Split(text, "\n")
for _, line := range lines {
w.print("#")
// trim right space which is usually unintended,
// but leave left space untouched in case if it's intentionally formatted.
trimmed := strings.TrimRight(line, " ")
if trimmed != "" {
w.print(" ")
w.print(trimmed)
}
w.print("\n")
w.printIndent(indent)
}
}

func (w *writer) printNode(node *ast.Node, indent int32) {
switch n := node.Node.(type) {

Expand Down Expand Up @@ -132,10 +148,7 @@ func (w *writer) printNode(node *ast.Node, indent int32) {

func (w *writer) printAnnAssign(aa *ast.AnnAssign, indent int32) {
if aa.Comment != "" {
w.print("# ")
w.print(aa.Comment)
w.print("\n")
w.printIndent(indent)
w.printCommentText(aa.Comment, indent)
}
w.printName(aa.Target, indent)
w.print(": ")
Expand Down Expand Up @@ -255,10 +268,7 @@ func (w *writer) printClassDef(cd *ast.ClassDef, indent int32) {
if i == 0 {
if e, ok := node.Node.(*ast.Node_Expr); ok {
if c, ok := e.Expr.Value.Node.(*ast.Node_Constant); ok {
w.print(`""`)
w.printConstant(c.Constant, indent)
w.print(`""`)
w.print("\n")
w.printDocString(c.Constant, indent)
continue
}
}
Expand All @@ -268,6 +278,33 @@ func (w *writer) printClassDef(cd *ast.ClassDef, indent int32) {
}
}

func (w *writer) printDocString(c *ast.Constant, indent int32) {
switch n := c.Value.(type) {
case *ast.Constant_Str:
w.print(`"""`)
lines := strings.Split(n.Str, "\n")
printedN := 0
for n, line := range lines {
// trim right space which is usually unintended,
// but leave left space untouched in case if it's intentionally formatted.
trimmed := strings.TrimRight(line, " ")
if trimmed == "" {
continue
}
if printedN > 0 && n < len(lines)-1 {
w.print("\n")
w.printIndent(indent)
}
w.print(strings.ReplaceAll(trimmed, `"`, `\"`))
printedN++
}
w.print(`"""`)
w.print("\n")
default:
panic(n)
}
}

func (w *writer) printConstant(c *ast.Constant, indent int32) {
switch n := c.Value.(type) {
case *ast.Constant_Int:
Expand All @@ -282,7 +319,7 @@ func (w *writer) printConstant(c *ast.Constant, indent int32) {
str = `"""`
}
w.print(str)
w.print(n.Str)
w.print(strings.ReplaceAll(n.Str, `"`, `\"`))
w.print(str)

default:
Expand All @@ -291,9 +328,7 @@ func (w *writer) printConstant(c *ast.Constant, indent int32) {
}

func (w *writer) printComment(c *ast.Comment, indent int32) {
w.print("# ")
w.print(c.Text)
w.print("\n")
w.printCommentText(c.Text, 0)
}

func (w *writer) printCompare(c *ast.Compare, indent int32) {
Expand Down