Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
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
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
39 deletions
+49
-39
nodes.go
src/pkg/go/printer/nodes.go
+13
-9
printer.go
src/pkg/go/printer/printer.go
+23
-18
printer_test.go
src/pkg/go/printer/printer_test.go
+13
-12
No files found.
src/pkg/go/printer/nodes.go
View file @
10242e80
...
...
@@ -37,8 +37,10 @@ const (
func
(
p
*
printer
)
linebreak
(
line
,
min
,
max
int
,
ws
whiteSpace
,
newSection
bool
)
(
printedBreak
bool
)
{
n
:=
line
-
p
.
pos
.
Line
;
switch
{
case
n
<
min
:
n
=
min
;
case
n
>
max
:
n
=
max
;
case
n
<
min
:
n
=
min
;
case
n
>
max
:
n
=
max
;
}
if
n
>
0
{
p
.
print
(
ws
);
...
...
@@ -118,9 +120,10 @@ func (p *printer) stringList(list []*ast.BasicLit, multiLine *bool) {
}
type
exprListMode
uint
;
type
exprListMode
uint
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
commaTerm
;
// elements are terminated by comma
noIndent
;
// no extra indentation in multi-line lists
...
...
@@ -150,7 +153,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo
// all list entries on a single line
for
i
,
x
:=
range
list
{
if
i
>
0
{
if
mode
&
commaSep
!=
0
{
if
mode
&
commaSep
!=
0
{
p
.
print
(
token
.
COMMA
);
}
p
.
print
(
blank
);
...
...
@@ -179,7 +182,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo
prev
:=
line
;
line
=
x
.
Pos
()
.
Line
;
if
i
>
0
{
if
mode
&
commaSep
!=
0
{
if
mode
&
commaSep
!=
0
{
p
.
print
(
token
.
COMMA
);
}
if
prev
<
line
{
...
...
@@ -353,7 +356,7 @@ func needsBlanks(expr ast.Expr) bool {
return
false
;
case
*
ast
.
IndexExpr
:
// index expressions don't need blanks if the indexed expressions are simple
return
needsBlanks
(
x
.
X
)
return
needsBlanks
(
x
.
X
)
;
case
*
ast
.
CallExpr
:
// call expressions need blanks if they have more than one
// 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
p
.
print
(
blank
);
}
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
);
case
*
ast
.
Ellipsis
:
...
...
@@ -862,7 +865,8 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
// ----------------------------------------------------------------------------
// Declarations
type
declContext
uint
;
type
declContext
uint
const
(
atTop
declContext
=
iota
;
inGroup
;
...
...
src/pkg/go/printer/printer.go
View file @
10242e80
...
...
@@ -57,7 +57,7 @@ var noPos token.Position
// Use ignoreMultiLine if the multiLine information is not important.
var
ignoreMultiLine
=
new
(
bool
)
;
var
ignoreMultiLine
=
new
(
bool
)
type
printer
struct
{
...
...
@@ -126,7 +126,7 @@ func (p *printer) write(data []byte) {
p
.
write0
(
data
[
i0
:
i
+
1
]);
// update p.pos
p
.
pos
.
Offset
+=
i
+
1
-
i0
;
p
.
pos
.
Offset
+=
i
+
1
-
i0
;
p
.
pos
.
Line
++
;
p
.
pos
.
Column
=
1
;
...
...
@@ -151,21 +151,26 @@ func (p *printer) write(data []byte) {
case
'"'
,
'\'
'
,
'&'
,
'<'
,
'>'
:
if
p
.
Mode
&
GenHTML
!=
0
{
// write segment ending in b
p
.
write0
(
data
[
i0
:
i
]);
p
.
write0
(
data
[
i0
:
i
]);
// write HTML-escaped b
var
esc
[]
byte
;
switch
b
{
case
'"'
:
esc
=
esc_quot
;
case
'\'
'
:
esc
=
esc_apos
;
case
'&'
:
esc
=
esc_amp
;
case
'<'
:
esc
=
esc_lt
;
case
'>'
:
esc
=
esc_gt
;
case
'"'
:
esc
=
esc_quot
;
case
'\'
'
:
esc
=
esc_apos
;
case
'&'
:
esc
=
esc_amp
;
case
'<'
:
esc
=
esc_lt
;
case
'>'
:
esc
=
esc_gt
;
}
p
.
write0
(
esc
);
// update p.pos
d
:=
i
+
1
-
i0
;
d
:=
i
+
1
-
i0
;
p
.
pos
.
Offset
+=
d
;
p
.
pos
.
Column
+=
d
;
...
...
@@ -179,10 +184,10 @@ func (p *printer) write(data []byte) {
}
// write remaining segment
p
.
write0
(
data
[
i0
:
len
(
data
)]);
p
.
write0
(
data
[
i0
:
len
(
data
)]);
// update p.pos
d
:=
len
(
data
)
-
i0
;
d
:=
len
(
data
)
-
i0
;
p
.
pos
.
Offset
+=
d
;
p
.
pos
.
Column
+=
d
;
}
...
...
@@ -193,7 +198,7 @@ func (p *printer) writeNewlines(n int) {
if
n
>
maxNewlines
{
n
=
maxNewlines
;
}
p
.
write
(
newlines
[
0
:
n
]);
p
.
write
(
newlines
[
0
:
n
]);
}
}
...
...
@@ -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
]
==
'*'
)
{
i
++
;
}
return
a
[
0
:
i
];
return
a
[
0
:
i
];
}
...
...
@@ -447,7 +452,7 @@ func stripCommonPrefix(lines [][]byte) {
// for the opening /*, assume up to 3 blanks or a tab. This
// whitespace may be found as suffix in the common prefix.
first
:=
lines
[
0
];
if
isBlank
(
first
[
2
:
len
(
first
)])
{
if
isBlank
(
first
[
2
:
len
(
first
)])
{
// no comment text on the first line:
// reduce prefix by up to 3 blanks or a tab
// if present - this keeps comment text indented
...
...
@@ -480,7 +485,7 @@ func stripCommonPrefix(lines [][]byte) {
// Shorten the computed common prefix by the length of
// suffix, if it is found as suffix of the prefix.
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) {
// Remove the common prefix from all but the first and empty lines.
for
i
,
line
:=
range
lines
{
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 ...) {
// (note that valid Go programs cannot contain esc ('\xff')
// bytes since they do not appear in legal UTF-8 sequences)
// TODO(gri): this this more efficiently.
data
=
strings
.
Bytes
(
"
\xff
"
+
string
(
data
)
+
"
\xff
"
);
data
=
strings
.
Bytes
(
"
\xff
"
+
string
(
data
)
+
"
\xff
"
);
case
token
.
Token
:
if
p
.
Styler
!=
nil
{
data
,
tag
=
p
.
Styler
.
Token
(
x
);
...
...
@@ -865,7 +870,7 @@ func (p *trimmer) Write(data []byte) (n int, err os.Error) {
// General printing is controlled with these Config.Mode flags.
const
(
GenHTML
uint
=
1
<<
iota
;
// generate HTML
GenHTML
uint
=
1
<<
iota
;
// generate HTML
RawFormat
;
// do not use a tabwriter; if set, UseSpaces is ignored
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 (
)
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
{
...
...
@@ -29,11 +29,12 @@ func lineString(text []byte, i int) string {
for
i
<
len
(
text
)
&&
text
[
i
]
!=
'\n'
{
i
++
;
}
return
string
(
text
[
i0
:
i
]);
return
string
(
text
[
i0
:
i
]);
}
type
checkMode
uint
;
type
checkMode
uint
const
(
export
checkMode
=
1
<<
iota
;
rawFormat
;
...
...
@@ -56,7 +57,7 @@ func check(t *testing.T, source, golden string, mode checkMode) {
// determine printer configuration
cfg
:=
Config
{
Tabwidth
:
tabwidth
};
if
mode
&
rawFormat
!=
0
{
if
mode
&
rawFormat
!=
0
{
cfg
.
Mode
|=
RawFormat
;
}
...
...
@@ -111,14 +112,14 @@ type entry struct {
// Use gotest -update to create/update the respective golden files.
var
data
=
[]
entry
{
entry
{
"empty.input"
,
"empty.golden"
,
0
},
entry
{
"comments.input"
,
"comments.golden"
,
0
},
entry
{
"comments.input"
,
"comments.x"
,
export
},
entry
{
"linebreaks.input"
,
"linebreaks.golden"
,
0
},
entry
{
"expressions.input"
,
"expressions.golden"
,
0
},
entry
{
"expressions.input"
,
"expressions.raw"
,
rawFormat
},
entry
{
"declarations.input"
,
"declarations.golden"
,
0
},
entry
{
"statements.input"
,
"statements.golden"
,
0
},
entry
{
"empty.input"
,
"empty.golden"
,
0
},
entry
{
"comments.input"
,
"comments.golden"
,
0
},
entry
{
"comments.input"
,
"comments.x"
,
export
},
entry
{
"linebreaks.input"
,
"linebreaks.golden"
,
0
},
entry
{
"expressions.input"
,
"expressions.golden"
,
0
},
entry
{
"expressions.input"
,
"expressions.raw"
,
rawFormat
},
entry
{
"declarations.input"
,
"declarations.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