Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
10242e80
Commit
10242e80
authored
Nov 05, 2009
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gofmt'ed parts of go
R=rsc
http://go/go-review/1023001
parent
65063bc6
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
137 additions
and
127 deletions
+137
-127
src/pkg/go/printer/nodes.go
src/pkg/go/printer/nodes.go
+37
-33
src/pkg/go/printer/printer.go
src/pkg/go/printer/printer.go
+80
-75
src/pkg/go/printer/printer_test.go
src/pkg/go/printer/printer_test.go
+20
-19
No files found.
src/pkg/go/printer/nodes.go
View file @
10242e80
...
@@ -37,8 +37,10 @@ const (
...
@@ -37,8 +37,10 @@ const (
func
(
p
*
printer
)
linebreak
(
line
,
min
,
max
int
,
ws
whiteSpace
,
newSection
bool
)
(
printedBreak
bool
)
{
func
(
p
*
printer
)
linebreak
(
line
,
min
,
max
int
,
ws
whiteSpace
,
newSection
bool
)
(
printedBreak
bool
)
{
n
:=
line
-
p
.
pos
.
Line
;
n
:=
line
-
p
.
pos
.
Line
;
switch
{
switch
{
case
n
<
min
:
n
=
min
;
case
n
<
min
:
case
n
>
max
:
n
=
max
;
n
=
min
;
case
n
>
max
:
n
=
max
;
}
}
if
n
>
0
{
if
n
>
0
{
p
.
print
(
ws
);
p
.
print
(
ws
);
...
@@ -118,9 +120,10 @@ func (p *printer) stringList(list []*ast.BasicLit, multiLine *bool) {
...
@@ -118,9 +120,10 @@ func (p *printer) stringList(list []*ast.BasicLit, multiLine *bool) {
}
}
type
exprListMode
uint
;
type
exprListMode
uint
const
(
const
(
blankStart
exprListMode
=
1
<<
iota
;
// print a blank before the list
blankStart
exprListMode
=
1
<<
iota
;
// print a blank before the list
commaSep
;
// elements are separated by commas
commaSep
;
// elements are separated by commas
commaTerm
;
// elements are terminated by comma
commaTerm
;
// elements are terminated by comma
noIndent
;
// no extra indentation in multi-line lists
noIndent
;
// no extra indentation in multi-line lists
...
@@ -150,7 +153,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo
...
@@ -150,7 +153,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo
// all list entries on a single line
// all list entries on a single line
for
i
,
x
:=
range
list
{
for
i
,
x
:=
range
list
{
if
i
>
0
{
if
i
>
0
{
if
mode
&
commaSep
!=
0
{
if
mode
&
commaSep
!=
0
{
p
.
print
(
token
.
COMMA
);
p
.
print
(
token
.
COMMA
);
}
}
p
.
print
(
blank
);
p
.
print
(
blank
);
...
@@ -179,7 +182,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo
...
@@ -179,7 +182,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo
prev
:=
line
;
prev
:=
line
;
line
=
x
.
Pos
()
.
Line
;
line
=
x
.
Pos
()
.
Line
;
if
i
>
0
{
if
i
>
0
{
if
mode
&
commaSep
!=
0
{
if
mode
&
commaSep
!=
0
{
p
.
print
(
token
.
COMMA
);
p
.
print
(
token
.
COMMA
);
}
}
if
prev
<
line
{
if
prev
<
line
{
...
@@ -353,7 +356,7 @@ func needsBlanks(expr ast.Expr) bool {
...
@@ -353,7 +356,7 @@ func needsBlanks(expr ast.Expr) bool {
return
false
;
return
false
;
case
*
ast
.
IndexExpr
:
case
*
ast
.
IndexExpr
:
// index expressions don't need blanks if the indexed expressions are simple
// index expressions don't need blanks if the indexed expressions are simple
return
needsBlanks
(
x
.
X
)
return
needsBlanks
(
x
.
X
)
;
case
*
ast
.
CallExpr
:
case
*
ast
.
CallExpr
:
// call expressions need blanks if they have more than one
// call expressions need blanks if they have more than one
// argument or if the function expression needs blanks
// argument or if the function expression needs blanks
...
@@ -539,7 +542,7 @@ func (p *printer) expr1(expr ast.Expr, prec1 int, multiLine *bool) (optSemi bool
...
@@ -539,7 +542,7 @@ func (p *printer) expr1(expr ast.Expr, prec1 int, multiLine *bool) (optSemi bool
p
.
print
(
blank
);
p
.
print
(
blank
);
}
}
p
.
print
(
x
.
Lbrace
,
token
.
LBRACE
);
p
.
print
(
x
.
Lbrace
,
token
.
LBRACE
);
p
.
exprList
(
x
.
Lbrace
,
x
.
Elts
,
commaSep
|
commaTerm
,
multiLine
);
p
.
exprList
(
x
.
Lbrace
,
x
.
Elts
,
commaSep
|
commaTerm
,
multiLine
);
p
.
print
(
x
.
Rbrace
,
token
.
RBRACE
);
p
.
print
(
x
.
Rbrace
,
token
.
RBRACE
);
case
*
ast
.
Ellipsis
:
case
*
ast
.
Ellipsis
:
...
@@ -862,7 +865,8 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
...
@@ -862,7 +865,8 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Declarations
// Declarations
type
declContext
uint
;
type
declContext
uint
const
(
const
(
atTop
declContext
=
iota
;
atTop
declContext
=
iota
;
inGroup
;
inGroup
;
...
...
src/pkg/go/printer/printer.go
View file @
10242e80
...
@@ -57,7 +57,7 @@ var noPos token.Position
...
@@ -57,7 +57,7 @@ var noPos token.Position
// Use ignoreMultiLine if the multiLine information is not important.
// Use ignoreMultiLine if the multiLine information is not important.
var
ignoreMultiLine
=
new
(
bool
)
;
var
ignoreMultiLine
=
new
(
bool
)
type
printer
struct
{
type
printer
struct
{
...
@@ -126,7 +126,7 @@ func (p *printer) write(data []byte) {
...
@@ -126,7 +126,7 @@ func (p *printer) write(data []byte) {
p
.
write0
(
data
[
i0
:
i
+
1
]);
p
.
write0
(
data
[
i0
:
i
+
1
]);
// update p.pos
// update p.pos
p
.
pos
.
Offset
+=
i
+
1
-
i0
;
p
.
pos
.
Offset
+=
i
+
1
-
i0
;
p
.
pos
.
Line
++
;
p
.
pos
.
Line
++
;
p
.
pos
.
Column
=
1
;
p
.
pos
.
Column
=
1
;
...
@@ -151,21 +151,26 @@ func (p *printer) write(data []byte) {
...
@@ -151,21 +151,26 @@ func (p *printer) write(data []byte) {
case
'"'
,
'\'
'
,
'&'
,
'<'
,
'>'
:
case
'"'
,
'\'
'
,
'&'
,
'<'
,
'>'
:
if
p
.
Mode
&
GenHTML
!=
0
{
if
p
.
Mode
&
GenHTML
!=
0
{
// write segment ending in b
// write segment ending in b
p
.
write0
(
data
[
i0
:
i
]);
p
.
write0
(
data
[
i0
:
i
]);
// write HTML-escaped b
// write HTML-escaped b
var
esc
[]
byte
;
var
esc
[]
byte
;
switch
b
{
switch
b
{
case
'"'
:
esc
=
esc_quot
;
case
'"'
:
case
'\'
'
:
esc
=
esc_apos
;
esc
=
esc_quot
;
case
'&'
:
esc
=
esc_amp
;
case
'\'
'
:
case
'<'
:
esc
=
esc_lt
;
esc
=
esc_apos
;
case
'>'
:
esc
=
esc_gt
;
case
'&'
:
esc
=
esc_amp
;
case
'<'
:
esc
=
esc_lt
;
case
'>'
:
esc
=
esc_gt
;
}
}
p
.
write0
(
esc
);
p
.
write0
(
esc
);
// update p.pos
// update p.pos
d
:=
i
+
1
-
i0
;
d
:=
i
+
1
-
i0
;
p
.
pos
.
Offset
+=
d
;
p
.
pos
.
Offset
+=
d
;
p
.
pos
.
Column
+=
d
;
p
.
pos
.
Column
+=
d
;
...
@@ -179,10 +184,10 @@ func (p *printer) write(data []byte) {
...
@@ -179,10 +184,10 @@ func (p *printer) write(data []byte) {
}
}
// write remaining segment
// write remaining segment
p
.
write0
(
data
[
i0
:
len
(
data
)]);
p
.
write0
(
data
[
i0
:
len
(
data
)]);
// update p.pos
// update p.pos
d
:=
len
(
data
)
-
i0
;
d
:=
len
(
data
)
-
i0
;
p
.
pos
.
Offset
+=
d
;
p
.
pos
.
Offset
+=
d
;
p
.
pos
.
Column
+=
d
;
p
.
pos
.
Column
+=
d
;
}
}
...
@@ -193,7 +198,7 @@ func (p *printer) writeNewlines(n int) {
...
@@ -193,7 +198,7 @@ func (p *printer) writeNewlines(n int) {
if
n
>
maxNewlines
{
if
n
>
maxNewlines
{
n
=
maxNewlines
;
n
=
maxNewlines
;
}
}
p
.
write
(
newlines
[
0
:
n
]);
p
.
write
(
newlines
[
0
:
n
]);
}
}
}
}
...
@@ -393,7 +398,7 @@ func commonPrefix(a, b []byte) []byte {
...
@@ -393,7 +398,7 @@ func commonPrefix(a, b []byte) []byte {
for
i
<
len
(
a
)
&&
i
<
len
(
b
)
&&
a
[
i
]
==
b
[
i
]
&&
(
a
[
i
]
<=
' '
||
a
[
i
]
==
'*'
)
{
for
i
<
len
(
a
)
&&
i
<
len
(
b
)
&&
a
[
i
]
==
b
[
i
]
&&
(
a
[
i
]
<=
' '
||
a
[
i
]
==
'*'
)
{
i
++
;
i
++
;
}
}
return
a
[
0
:
i
];
return
a
[
0
:
i
];
}
}
...
@@ -447,7 +452,7 @@ func stripCommonPrefix(lines [][]byte) {
...
@@ -447,7 +452,7 @@ func stripCommonPrefix(lines [][]byte) {
// for the opening /*, assume up to 3 blanks or a tab. This
// for the opening /*, assume up to 3 blanks or a tab. This
// whitespace may be found as suffix in the common prefix.
// whitespace may be found as suffix in the common prefix.
first
:=
lines
[
0
];
first
:=
lines
[
0
];
if
isBlank
(
first
[
2
:
len
(
first
)])
{
if
isBlank
(
first
[
2
:
len
(
first
)])
{
// no comment text on the first line:
// no comment text on the first line:
// reduce prefix by up to 3 blanks or a tab
// reduce prefix by up to 3 blanks or a tab
// if present - this keeps comment text indented
// if present - this keeps comment text indented
...
@@ -480,7 +485,7 @@ func stripCommonPrefix(lines [][]byte) {
...
@@ -480,7 +485,7 @@ func stripCommonPrefix(lines [][]byte) {
// Shorten the computed common prefix by the length of
// Shorten the computed common prefix by the length of
// suffix, if it is found as suffix of the prefix.
// suffix, if it is found as suffix of the prefix.
if
bytes
.
HasSuffix
(
prefix
,
suffix
)
{
if
bytes
.
HasSuffix
(
prefix
,
suffix
)
{
prefix
=
prefix
[
0
:
len
(
prefix
)
-
len
(
suffix
)];
prefix
=
prefix
[
0
:
len
(
prefix
)
-
len
(
suffix
)];
}
}
}
}
}
}
...
@@ -508,7 +513,7 @@ func stripCommonPrefix(lines [][]byte) {
...
@@ -508,7 +513,7 @@ func stripCommonPrefix(lines [][]byte) {
// Remove the common prefix from all but the first and empty lines.
// Remove the common prefix from all but the first and empty lines.
for
i
,
line
:=
range
lines
{
for
i
,
line
:=
range
lines
{
if
i
>
0
&&
len
(
line
)
!=
0
{
if
i
>
0
&&
len
(
line
)
!=
0
{
lines
[
i
]
=
line
[
len
(
prefix
)
:
len
(
line
)];
lines
[
i
]
=
line
[
len
(
prefix
)
:
len
(
line
)];
}
}
}
}
}
}
...
@@ -721,7 +726,7 @@ func (p *printer) print(args ...) {
...
@@ -721,7 +726,7 @@ func (p *printer) print(args ...) {
// (note that valid Go programs cannot contain esc ('\xff')
// (note that valid Go programs cannot contain esc ('\xff')
// bytes since they do not appear in legal UTF-8 sequences)
// bytes since they do not appear in legal UTF-8 sequences)
// TODO(gri): this this more efficiently.
// TODO(gri): this this more efficiently.
data
=
strings
.
Bytes
(
"
\xff
"
+
string
(
data
)
+
"
\xff
"
);
data
=
strings
.
Bytes
(
"
\xff
"
+
string
(
data
)
+
"
\xff
"
);
case
token
.
Token
:
case
token
.
Token
:
if
p
.
Styler
!=
nil
{
if
p
.
Styler
!=
nil
{
data
,
tag
=
p
.
Styler
.
Token
(
x
);
data
,
tag
=
p
.
Styler
.
Token
(
x
);
...
@@ -865,7 +870,7 @@ func (p *trimmer) Write(data []byte) (n int, err os.Error) {
...
@@ -865,7 +870,7 @@ func (p *trimmer) Write(data []byte) (n int, err os.Error) {
// General printing is controlled with these Config.Mode flags.
// General printing is controlled with these Config.Mode flags.
const
(
const
(
GenHTML
uint
=
1
<<
iota
;
// generate HTML
GenHTML
uint
=
1
<<
iota
;
// generate HTML
RawFormat
;
// do not use a tabwriter; if set, UseSpaces is ignored
RawFormat
;
// do not use a tabwriter; if set, UseSpaces is ignored
UseSpaces
;
// use spaces instead of tabs for indentation and alignment
UseSpaces
;
// use spaces instead of tabs for indentation and alignment
)
)
...
...
src/pkg/go/printer/printer_test.go
View file @
10242e80
...
@@ -21,7 +21,7 @@ const (
...
@@ -21,7 +21,7 @@ const (
)
)
var
update
=
flag
.
Bool
(
"update"
,
false
,
"update golden files"
)
;
var
update
=
flag
.
Bool
(
"update"
,
false
,
"update golden files"
)
func
lineString
(
text
[]
byte
,
i
int
)
string
{
func
lineString
(
text
[]
byte
,
i
int
)
string
{
...
@@ -29,11 +29,12 @@ func lineString(text []byte, i int) string {
...
@@ -29,11 +29,12 @@ func lineString(text []byte, i int) string {
for
i
<
len
(
text
)
&&
text
[
i
]
!=
'\n'
{
for
i
<
len
(
text
)
&&
text
[
i
]
!=
'\n'
{
i
++
;
i
++
;
}
}
return
string
(
text
[
i0
:
i
]);
return
string
(
text
[
i0
:
i
]);
}
}
type
checkMode
uint
;
type
checkMode
uint
const
(
const
(
export
checkMode
=
1
<<
iota
;
export
checkMode
=
1
<<
iota
;
rawFormat
;
rawFormat
;
...
@@ -56,7 +57,7 @@ func check(t *testing.T, source, golden string, mode checkMode) {
...
@@ -56,7 +57,7 @@ func check(t *testing.T, source, golden string, mode checkMode) {
// determine printer configuration
// determine printer configuration
cfg
:=
Config
{
Tabwidth
:
tabwidth
};
cfg
:=
Config
{
Tabwidth
:
tabwidth
};
if
mode
&
rawFormat
!=
0
{
if
mode
&
rawFormat
!=
0
{
cfg
.
Mode
|=
RawFormat
;
cfg
.
Mode
|=
RawFormat
;
}
}
...
@@ -111,14 +112,14 @@ type entry struct {
...
@@ -111,14 +112,14 @@ type entry struct {
// Use gotest -update to create/update the respective golden files.
// Use gotest -update to create/update the respective golden files.
var
data
=
[]
entry
{
var
data
=
[]
entry
{
entry
{
"empty.input"
,
"empty.golden"
,
0
},
entry
{
"empty.input"
,
"empty.golden"
,
0
},
entry
{
"comments.input"
,
"comments.golden"
,
0
},
entry
{
"comments.input"
,
"comments.golden"
,
0
},
entry
{
"comments.input"
,
"comments.x"
,
export
},
entry
{
"comments.input"
,
"comments.x"
,
export
},
entry
{
"linebreaks.input"
,
"linebreaks.golden"
,
0
},
entry
{
"linebreaks.input"
,
"linebreaks.golden"
,
0
},
entry
{
"expressions.input"
,
"expressions.golden"
,
0
},
entry
{
"expressions.input"
,
"expressions.golden"
,
0
},
entry
{
"expressions.input"
,
"expressions.raw"
,
rawFormat
},
entry
{
"expressions.input"
,
"expressions.raw"
,
rawFormat
},
entry
{
"declarations.input"
,
"declarations.golden"
,
0
},
entry
{
"declarations.input"
,
"declarations.golden"
,
0
},
entry
{
"statements.input"
,
"statements.golden"
,
0
},
entry
{
"statements.input"
,
"statements.golden"
,
0
},
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment