Have dumbindent handle multi-line strings/comments

Fixes #31
This commit is contained in:
Nigel Tao committed 2020-06-06 22:32:39 +10:00
1 parent 3d7af06727
commit 6424bae087
4 files changed
+89 -42

No files matched your search

+1 -7
View File
@@ -44,9 +44,6 @@
// ---- // ----
// //
// There are no configuration options (e.g. tabs versus spaces). // There are no configuration options (e.g. tabs versus spaces).
//
// Known bug: it cannot handle /* slash-star comments */ or multi-line strings
// yet. This is tracked at https://github.com/google/wuffs/issues/31
package main package main
import ( import (
@@ -145,10 +142,7 @@ func do(r io.Reader, filename string) error {
return err return err
} }
dst, err := dumbindent.FormatBytes(nil, src) dst := dumbindent.FormatBytes(nil, src)
if err != nil {
return err
}
if r != nil { if r != nil {
if _, err := os.Stdout.Write(dst); err != nil { if _, err := os.Stdout.Write(dst); err != nil {
+1 -1
View File
@@ -182,7 +182,7 @@ func Do(args []string) error {
return unformatted, nil return unformatted, nil
} }
return dumbindent.FormatBytes(nil, unformatted) return dumbindent.FormatBytes(nil, unformatted), nil
}) })
} }
+74 -31
View File
@@ -27,20 +27,18 @@
// `dumbindent` was 80 times faster than `clang-format`. // `dumbindent` was 80 times faster than `clang-format`.
// //
// There are no configuration options (e.g. tabs versus spaces). // There are no configuration options (e.g. tabs versus spaces).
//
// Known bug: it cannot handle /* slash-star comments */ or multi-line strings
// yet. This is tracked at https://github.com/google/wuffs/issues/31
package dumbindent package dumbindent
import ( import (
"bytes" "bytes"
"errors"
) )
// 'Constants', but their type is []byte, not string. // 'Constants', but their type is []byte, not string.
var ( var (
externC = []byte("extern \"C\"") backTick = []byte("`")
spaces = []byte(" ") externC = []byte("extern \"C\"")
spaces = []byte(" ")
starSlash = []byte("*/")
) )
// hangingBytes is a look-up table for updating the hanging variable. // hangingBytes is a look-up table for updating the hanging variable.
@@ -55,9 +53,9 @@ var hangingBytes = [256]bool{
// It is valid to pass a dst slice (such as nil) whose spare capacity (not // It is valid to pass a dst slice (such as nil) whose spare capacity (not
// including its existing elements) is too short to hold the formatted program. // including its existing elements) is too short to hold the formatted program.
// In this case, a new slice will be allocated and returned. // In this case, a new slice will be allocated and returned.
func FormatBytes(dst []byte, src []byte) ([]byte, error) { func FormatBytes(dst []byte, src []byte) []byte {
if len(src) == 0 { if len(src) == 0 {
return dst, nil return dst
} else if len(dst) == 0 { } else if len(dst) == 0 {
dst = make([]byte, 0, len(src)+(len(src)/2)) dst = make([]byte, 0, len(src)+(len(src)/2))
} }
@@ -69,11 +67,12 @@ func FormatBytes(dst []byte, src []byte) ([]byte, error) {
blankLine := false // Whether the previous line was blank. blankLine := false // Whether the previous line was blank.
for line, remaining := src, []byte(nil); len(src) > 0; src = remaining { for line, remaining := src, []byte(nil); len(src) > 0; src = remaining {
src = trimLeadingWhiteSpace(src)
line, remaining = src, nil line, remaining = src, nil
if i := bytes.IndexByte(line, '\n'); i >= 0 { if i := bytes.IndexByte(line, '\n'); i >= 0 {
line, remaining = line[:i], line[i+1:] line, remaining = line[:i], line[i+1:]
} }
line = trimSpace(line) lineLength := len(line)
// Collapse 2 or more consecutive blank lines into 1. Also strip any // Collapse 2 or more consecutive blank lines into 1. Also strip any
// blank lines: // blank lines:
@@ -99,6 +98,7 @@ func FormatBytes(dst []byte, src []byte) ([]byte, error) {
if (line[0] == '#') || if (line[0] == '#') ||
((line[0] == 'e') && bytes.HasPrefix(line, externC)) || ((line[0] == 'e') && bytes.HasPrefix(line, externC)) ||
((line[0] == '}') && bytes.HasSuffix(line, externC)) { ((line[0] == '}') && bytes.HasSuffix(line, externC)) {
line = trimTrailingWhiteSpace(line)
dst = append(dst, line...) dst = append(dst, line...)
dst = append(dst, '\n') dst = append(dst, '\n')
openBrace = false openBrace = false
@@ -150,16 +150,16 @@ func FormatBytes(dst []byte, src []byte) ([]byte, error) {
indent -= n indent -= n
} }
// Output the line itself. // Output the leading '}'s.
dst = append(dst, line...) dst = append(dst, line[:closeBraces]...)
dst = append(dst, "\n"...) line = line[closeBraces:]
// Adjust the state according to the braces and parentheses within the // Adjust the state according to the braces and parentheses within the
// line (except for those in comments and strings). // line (except for those in comments and strings).
last := lastNonWhiteSpace(line) last := lastNonWhiteSpace(line)
loop: loop:
for s := line[closeBraces:]; ; { for {
for i, c := range s { for i, c := range line {
switch c { switch c {
case '{': case '{':
nBraces++ nBraces++
@@ -169,23 +169,39 @@ func FormatBytes(dst []byte, src []byte) ([]byte, error) {
nParens++ nParens++
case ')': case ')':
nParens-- nParens--
case '/': case '/':
if (i + 1) >= len(s) { if (i + 1) >= len(line) {
break break
} }
if s[i+1] == '/' { if line[i+1] == '/' {
// A slash-slash comment. Skip the rest of the line. // A slash-slash comment. Skip the rest of the line.
last = lastNonWhiteSpace(s[:i]) last = lastNonWhiteSpace(line[:i])
break loop break loop
} else if s[i+1] == '*' { } else if line[i+1] == '*' {
return nil, errors.New("dumbindent: TODO: support slash-star comments") // A slash-star comment.
dst = append(dst, line[:i+2]...)
restOfLine := line[i+2:]
restOfSrc := src[lineLength-len(restOfLine):]
dst, line, remaining = handleRaw(dst, restOfSrc, starSlash)
last = lastNonWhiteSpace(line)
continue loop
} }
case '"', '\'': case '"', '\'':
if suffix, err := skipString(s[i+1:], c); err != nil { // A cooked string, whose contents are backslash-escaped.
return nil, err suffix := skipCooked(line[i+1:], c)
} else { dst = append(dst, line[:len(line)-len(suffix)]...)
s = suffix line = suffix
} continue loop
case '`':
// A raw string.
dst = append(dst, line[:i+1]...)
restOfLine := line[i+1:]
restOfSrc := src[lineLength-len(restOfLine):]
dst, line, remaining = handleRaw(dst, restOfSrc, backTick)
last = lastNonWhiteSpace(line)
continue loop continue loop
} }
} }
@@ -193,15 +209,25 @@ func FormatBytes(dst []byte, src []byte) ([]byte, error) {
} }
openBrace = last == '{' openBrace = last == '{'
hanging = hangingBytes[last] hanging = hangingBytes[last]
// Output the line (minus any trailing space).
line = trimTrailingWhiteSpace(line)
dst = append(dst, line...)
dst = append(dst, "\n"...)
} }
return dst, nil return dst
} }
// trimSpace converts "\t foo bar " to "foo bar". // trimLeadingWhiteSpace converts "\t foo bar " to "foo bar ".
func trimSpace(s []byte) []byte { func trimLeadingWhiteSpace(s []byte) []byte {
for (len(s) > 0) && ((s[0] == ' ') || (s[0] == '\t')) { for (len(s) > 0) && ((s[0] == ' ') || (s[0] == '\t')) {
s = s[1:] s = s[1:]
} }
return s
}
// trimTrailingWhiteSpace converts "\t foo bar " to "\t foo bar".
func trimTrailingWhiteSpace(s []byte) []byte {
for (len(s) > 0) && ((s[len(s)-1] == ' ') || (s[len(s)-1] == '\t')) { for (len(s) > 0) && ((s[len(s)-1] == ' ') || (s[len(s)-1] == '\t')) {
s = s[:len(s)-1] s = s[:len(s)-1]
} }
@@ -241,11 +267,11 @@ func lastNonWhiteSpace(s []byte) byte {
return 0 return 0
} }
// skipString converts `ijk \" lmn" pqr` to ` pqr`. // skipCooked converts `ijk \" lmn" pqr` to ` pqr`.
func skipString(s []byte, quote byte) (suffix []byte, retErr error) { func skipCooked(s []byte, quote byte) (suffix []byte) {
for i := 0; i < len(s); { for i := 0; i < len(s); {
if x := s[i]; x == quote { if x := s[i]; x == quote {
return s[i+1:], nil return s[i+1:]
} else if x != '\\' { } else if x != '\\' {
i += 1 i += 1
} else if (i + 1) < len(s) { } else if (i + 1) < len(s) {
@@ -254,5 +280,22 @@ func skipString(s []byte, quote byte) (suffix []byte, retErr error) {
break break
} }
} }
return nil, errors.New("dumbindent: TODO: support multi-line strings") return nil
}
// handleRaw copies a raw string from restOfSrc to dst, re-calculating the
// (line, remaining) pair afterwards.
func handleRaw(dst []byte, restOfSrc []byte, endQuote []byte) (retDst []byte, line []byte, remaining []byte) {
end := bytes.Index(restOfSrc, endQuote)
if end < 0 {
end = len(restOfSrc)
} else {
end += len(endQuote)
}
dst = append(dst, restOfSrc[:end]...)
line, remaining = restOfSrc[end:], nil
if i := bytes.IndexByte(line, '\n'); i >= 0 {
line, remaining = line[:i], line[i+1:]
}
return dst, line, remaining
} }
+13 -3
View File
@@ -23,6 +23,10 @@ func TestFormatBytes(tt *testing.T) {
src string src string
want string want string
}{{ }{{
// Leading and trailing space.
src: "\t\tx y \n",
want: "x y\n",
}, {
// Braces. // Braces.
src: "foo{\nbar\n }\nbaz\n", src: "foo{\nbar\n }\nbaz\n",
want: "foo{\n bar\n}\nbaz\n", want: "foo{\n bar\n}\nbaz\n",
@@ -50,6 +54,14 @@ func TestFormatBytes(tt *testing.T) {
// Double-quote string. // Double-quote string.
src: "a = \"{\"\nb = {\nc = 0\n", src: "a = \"{\"\nb = {\nc = 0\n",
want: "a = \"{\"\nb = {\n c = 0\n", want: "a = \"{\"\nb = {\n c = 0\n",
}, {
// Back-tick string.
src: "a['key'] = `{\n\n\nX` ; \nb = {\nc = 0\n",
want: "a['key'] = `{\n\n\nX` ;\nb = {\n c = 0\n",
}, {
// Slash-star comment.
src: " a['key'] = /*{\n\n\nX*/ ; \nb = {\nc = 0\n",
want: "a['key'] = /*{\n\n\nX*/ ;\nb = {\n c = 0\n",
}, { }, {
// Label. // Label.
src: "if (b) {\nlabel:\nswitch (i) {\ncase 0:\nj = k\nbreak;\n}\n}\n", src: "if (b) {\nlabel:\nswitch (i) {\ncase 0:\nj = k\nbreak;\n}\n}\n",
@@ -57,9 +69,7 @@ func TestFormatBytes(tt *testing.T) {
}} }}
for i, tc := range testCases { for i, tc := range testCases {
if g, err := FormatBytes(nil, []byte(tc.src)); err != nil { if got := string(FormatBytes(nil, []byte(tc.src))); got != tc.want {
tt.Fatalf("i=%d, src=%q: %v", i, tc.src, err)
} else if got := string(g); got != tc.want {
tt.Fatalf("i=%d, src=%q:\ngot %q\nwant %q", i, tc.src, got, tc.want) tt.Fatalf("i=%d, src=%q:\ngot %q\nwant %q", i, tc.src, got, tc.want)
} }
} }