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
b48f7121
Commit
b48f7121
authored
Nov 20, 2009
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Permit omission of hi bound in slices.
R=r, rsc
https://golang.org/cl/157082
parent
61660adc
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
70 additions
and
21 deletions
+70
-21
ast.go
src/cmd/cgo/ast.go
+3
-0
expr.go
src/pkg/exp/eval/expr.go
+13
-8
ast.go
src/pkg/go/ast/ast.go
+10
-2
walk.go
src/pkg/go/ast/walk.go
+4
-0
parser.go
src/pkg/go/parser/parser.go
+13
-7
nodes.go
src/pkg/go/printer/nodes.go
+12
-4
expressions.golden
src/pkg/go/printer/testdata/expressions.golden
+5
-0
expressions.input
src/pkg/go/printer/testdata/expressions.input
+5
-0
expressions.raw
src/pkg/go/printer/testdata/expressions.raw
+5
-0
No files found.
src/cmd/cgo/ast.go
View file @
b48f7121
...
...
@@ -178,6 +178,9 @@ func walk(x interface{}, p *Prog, context string) {
case
*
ast
.
IndexExpr
:
walk
(
&
n
.
X
,
p
,
"expr"
);
walk
(
&
n
.
Index
,
p
,
"expr"
);
case
*
ast
.
SliceExpr
:
walk
(
&
n
.
X
,
p
,
"expr"
);
walk
(
&
n
.
Index
,
p
,
"expr"
);
if
n
.
End
!=
nil
{
walk
(
&
n
.
End
,
p
,
"expr"
)
}
...
...
src/pkg/exp/eval/expr.go
View file @
b48f7121
...
...
@@ -581,20 +581,25 @@ func (a *exprCompiler) compile(x ast.Expr, callCtx bool) *expr {
return
ei
.
compileIdent
(
a
.
block
,
a
.
constant
,
callCtx
,
x
.
Value
)
case
*
ast
.
IndexExpr
:
if
x
.
End
!=
nil
{
l
,
r
:=
a
.
compile
(
x
.
X
,
false
),
a
.
compile
(
x
.
Index
,
false
);
if
l
==
nil
||
r
==
nil
{
return
nil
}
return
ei
.
compileIndexExpr
(
l
,
r
);
case
*
ast
.
SliceExpr
:
end
:=
x
.
End
;
if
end
==
nil
{
// TODO: set end to len(x.X)
panic
(
"unimplemented"
)
}
arr
:=
a
.
compile
(
x
.
X
,
false
);
lo
:=
a
.
compile
(
x
.
Index
,
false
);
hi
:=
a
.
compile
(
x
.
E
nd
,
false
);
hi
:=
a
.
compile
(
e
nd
,
false
);
if
arr
==
nil
||
lo
==
nil
||
hi
==
nil
{
return
nil
}
return
ei
.
compileSliceExpr
(
arr
,
lo
,
hi
);
}
l
,
r
:=
a
.
compile
(
x
.
X
,
false
),
a
.
compile
(
x
.
Index
,
false
);
if
l
==
nil
||
r
==
nil
{
return
nil
}
return
ei
.
compileIndexExpr
(
l
,
r
);
case
*
ast
.
KeyValueExpr
:
goto
notimpl
...
...
src/pkg/go/ast/ast.go
View file @
b48f7121
...
...
@@ -172,10 +172,16 @@ type (
Sel
*
Ident
;
// field selector
};
// An IndexExpr node represents an expression followed by an index
or slice
.
// An IndexExpr node represents an expression followed by an index.
IndexExpr
struct
{
X
Expr
;
// expression
Index
Expr
;
// index expression or beginning of slice range
Index
Expr
;
// index expression
};
// An SliceExpr node represents an expression followed by slice indices.
SliceExpr
struct
{
X
Expr
;
// expression
Index
Expr
;
// beginning of slice range
End
Expr
;
// end of slice range; or nil
};
...
...
@@ -305,6 +311,7 @@ func (x *FuncLit) Pos() token.Position { return x.Type.Pos() }
func
(
x
*
CompositeLit
)
Pos
()
token
.
Position
{
return
x
.
Type
.
Pos
()
}
func
(
x
*
SelectorExpr
)
Pos
()
token
.
Position
{
return
x
.
X
.
Pos
()
}
func
(
x
*
IndexExpr
)
Pos
()
token
.
Position
{
return
x
.
X
.
Pos
()
}
func
(
x
*
SliceExpr
)
Pos
()
token
.
Position
{
return
x
.
X
.
Pos
()
}
func
(
x
*
TypeAssertExpr
)
Pos
()
token
.
Position
{
return
x
.
X
.
Pos
()
}
func
(
x
*
CallExpr
)
Pos
()
token
.
Position
{
return
x
.
Fun
.
Pos
()
}
func
(
x
*
BinaryExpr
)
Pos
()
token
.
Position
{
return
x
.
X
.
Pos
()
}
...
...
@@ -323,6 +330,7 @@ func (x *CompositeLit) exprNode() {}
func
(
x
*
ParenExpr
)
exprNode
()
{}
func
(
x
*
SelectorExpr
)
exprNode
()
{}
func
(
x
*
IndexExpr
)
exprNode
()
{}
func
(
x
*
SliceExpr
)
exprNode
()
{}
func
(
x
*
TypeAssertExpr
)
exprNode
()
{}
func
(
x
*
CallExpr
)
exprNode
()
{}
func
(
x
*
StarExpr
)
exprNode
()
{}
...
...
src/pkg/go/ast/walk.go
View file @
b48f7121
...
...
@@ -129,6 +129,10 @@ func Walk(v Visitor, node interface{}) {
case
*
IndexExpr
:
Walk
(
v
,
n
.
X
);
Walk
(
v
,
n
.
Index
);
case
*
SliceExpr
:
Walk
(
v
,
n
.
X
);
Walk
(
v
,
n
.
Index
);
Walk
(
v
,
n
.
End
);
case
*
TypeAssertExpr
:
...
...
src/pkg/go/parser/parser.go
View file @
b48f7121
...
...
@@ -962,23 +962,28 @@ func (p *parser) parseSelectorOrTypeAssertion(x ast.Expr) ast.Expr {
}
func
(
p
*
parser
)
parseIndex
(
x
ast
.
Expr
)
ast
.
Expr
{
func
(
p
*
parser
)
parseIndex
OrSlice
(
x
ast
.
Expr
)
ast
.
Expr
{
if
p
.
trace
{
defer
un
(
trace
(
p
,
"Index"
))
defer
un
(
trace
(
p
,
"Index
OrSlice
"
))
}
p
.
expect
(
token
.
LBRACK
);
p
.
exprLev
++
;
begin
:=
p
.
parseExpr
();
var
end
ast
.
Expr
;
index
:=
p
.
parseExpr
();
if
p
.
tok
==
token
.
COLON
{
p
.
next
();
end
=
p
.
parseExpr
();
var
end
ast
.
Expr
;
if
p
.
tok
!=
token
.
RBRACK
{
end
=
p
.
parseExpr
()
}
x
=
&
ast
.
SliceExpr
{
x
,
index
,
end
};
}
else
{
x
=
&
ast
.
IndexExpr
{
x
,
index
}
}
p
.
exprLev
--
;
p
.
expect
(
token
.
RBRACK
);
return
&
ast
.
IndexExpr
{
x
,
begin
,
end
}
;
return
x
;
}
...
...
@@ -1072,6 +1077,7 @@ func (p *parser) checkExpr(x ast.Expr) ast.Expr {
case
*
ast
.
ParenExpr
:
case
*
ast
.
SelectorExpr
:
case
*
ast
.
IndexExpr
:
case
*
ast
.
SliceExpr
:
case
*
ast
.
TypeAssertExpr
:
if
t
.
Type
==
nil
{
// the form X.(type) is only allowed in type switch expressions
...
...
@@ -1168,7 +1174,7 @@ L: for {
case
token
.
PERIOD
:
x
=
p
.
parseSelectorOrTypeAssertion
(
p
.
checkExpr
(
x
))
case
token
.
LBRACK
:
x
=
p
.
parseIndex
(
p
.
checkExpr
(
x
))
x
=
p
.
parseIndex
OrSlice
(
p
.
checkExpr
(
x
))
case
token
.
LPAREN
:
x
=
p
.
parseCallOrConversion
(
p
.
checkExprOrType
(
x
))
case
token
.
LBRACE
:
...
...
src/pkg/go/printer/nodes.go
View file @
b48f7121
...
...
@@ -665,17 +665,25 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
p
.
print
(
token
.
RPAREN
);
case
*
ast
.
IndexExpr
:
// TODO(gri): should treat[] like parentheses and undo one level of depth
p
.
expr1
(
x
.
X
,
token
.
HighestPrec
,
1
,
0
,
multiLine
);
p
.
print
(
token
.
LBRACK
);
p
.
expr0
(
x
.
Index
,
depth
+
1
,
multiLine
);
if
x
.
End
!=
nil
{
// blanks around ":" if either side is a binary expression
if
depth
<=
1
&&
(
isBinary
(
x
.
Index
)
||
isBinary
(
x
.
End
))
{
p
.
print
(
token
.
RBRACK
);
case
*
ast
.
SliceExpr
:
// TODO(gri): should treat[] like parentheses and undo one level of depth
p
.
expr1
(
x
.
X
,
token
.
HighestPrec
,
1
,
0
,
multiLine
);
p
.
print
(
token
.
LBRACK
);
p
.
expr0
(
x
.
Index
,
depth
+
1
,
multiLine
);
// blanks around ":" if both sides exist and either side is a binary expression
if
depth
<=
1
&&
x
.
End
!=
nil
&&
(
isBinary
(
x
.
Index
)
||
isBinary
(
x
.
End
))
{
p
.
print
(
blank
,
token
.
COLON
,
blank
)
}
else
{
p
.
print
(
token
.
COLON
)
}
p
.
expr0
(
x
.
End
,
depth
+
1
,
multiLine
);
if
x
.
End
!=
nil
{
p
.
expr0
(
x
.
End
,
depth
+
1
,
multiLine
)
}
p
.
print
(
token
.
RBRACK
);
...
...
src/pkg/go/printer/testdata/expressions.golden
View file @
b48f7121
...
...
@@ -53,6 +53,11 @@ func _() {
_
=
1
+
2
*
3
;
_
=
s
[
1
:
2
*
3
];
_
=
s
[
a
:
b
-
c
];
_
=
s
[
0
:];
_
=
s
[
a
+
b
];
_
=
s
[
a
+
b
:];
_
=
a
[
a
<<
b
+
1
];
_
=
a
[
a
<<
b
+
1
:];
_
=
s
[
a
+
b
:
len
(
s
)];
_
=
s
[
len
(
s
):-
a
];
_
=
s
[
a
:
len
(
s
)+
1
];
...
...
src/pkg/go/printer/testdata/expressions.input
View file @
b48f7121
...
...
@@ -53,6 +53,11 @@ func _() {
_
=
1
+
2
*
3
;
_
=
s
[
1
:
2
*
3
];
_
=
s
[
a
:
b
-
c
];
_
=
s
[
0
:];
_
=
s
[
a
+
b
];
_
=
s
[
a
+
b
:];
_
=
a
[
a
<<
b
+
1
];
_
=
a
[
a
<<
b
+
1
:];
_
=
s
[
a
+
b
:
len
(
s
)];
_
=
s
[
len
(
s
)
:
-
a
];
_
=
s
[
a
:
len
(
s
)+
1
];
...
...
src/pkg/go/printer/testdata/expressions.raw
View file @
b48f7121
...
...
@@ -53,6 +53,11 @@ func _() {
_
=
1
+
2
*
3
;
_
=
s
[
1
:
2
*
3
];
_
=
s
[
a
:
b
-
c
];
_
=
s
[
0
:];
_
=
s
[
a
+
b
];
_
=
s
[
a
+
b
:];
_
=
a
[
a
<<
b
+
1
];
_
=
a
[
a
<<
b
+
1
:];
_
=
s
[
a
+
b
:
len
(
s
)];
_
=
s
[
len
(
s
):-
a
];
_
=
s
[
a
:
len
(
s
)+
1
];
...
...
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