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
bf04eefd
Commit
bf04eefd
authored
Sep 19, 2008
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- simplified parser by better factoring
R=r OCL=15539 CL=15542
parent
72e3b204
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
65 deletions
+52
-65
parser.go
usr/gri/pretty/parser.go
+41
-65
pretty.go
usr/gri/pretty/pretty.go
+11
-0
No files found.
usr/gri/pretty/parser.go
View file @
bf04eefd
...
...
@@ -292,14 +292,14 @@ func (P *Parser) ParseResult() {
}
//
Signatur
es
//
Function typ
es
//
// (params)
// (params) type
// (params) (results)
func
(
P
*
Parser
)
Parse
Signatur
e
()
{
P
.
Trace
(
"
Signatur
e"
);
func
(
P
*
Parser
)
Parse
FunctionTyp
e
()
{
P
.
Trace
(
"
FunctionTyp
e"
);
P
.
OpenScope
();
P
.
level
--
;
...
...
@@ -314,66 +314,11 @@ func (P *Parser) ParseSignature() {
}
// Named signatures
//
// ident (params)
// ident (params) type
// ident (params) (results)
// (recv) ident (params)
// (recv) ident (params) type
// (recv) ident (params) (results)
func
(
P
*
Parser
)
ParseNamedSignature
()
*
AST
.
Ident
{
P
.
Trace
(
"NamedSignature"
);
P
.
OpenScope
();
P
.
level
--
;
if
P
.
tok
==
Scanner
.
LPAREN
{
recv_pos
:=
P
.
pos
;
n
:=
P
.
ParseParameters
();
if
n
!=
1
{
P
.
Error
(
recv_pos
,
"must have exactly one receiver"
);
panic
(
"UNIMPLEMENTED (ParseNamedSignature)"
);
// TODO do something useful here
}
}
ident
:=
P
.
ParseIdent
();
P
.
ParseParameters
();
P
.
ParseResult
();
P
.
level
++
;
P
.
CloseScope
();
P
.
Ecart
();
return
ident
;
}
func
(
P
*
Parser
)
ParseFunctionType
()
{
P
.
Trace
(
"FunctionType"
);
typ
:=
P
.
ParseSignature
();
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseMethodDecl
()
{
P
.
Trace
(
"MethodDecl"
);
ident
:=
P
.
ParseIdent
();
P
.
OpenScope
();
P
.
level
--
;
P
.
ParseParameters
();
//r0 := sig.entries.len;
P
.
ParseResult
();
P
.
level
++
;
P
.
CloseScope
();
P
.
ParseFunctionType
();
P
.
Optional
(
Scanner
.
SEMICOLON
);
P
.
Ecart
();
...
...
@@ -453,7 +398,7 @@ func (P *Parser) TryType() bool {
case
Scanner
.
LBRACK
:
P
.
ParseArrayType
();
case
Scanner
.
CHAN
,
Scanner
.
ARROW
:
P
.
ParseChannelType
();
case
Scanner
.
INTERFACE
:
P
.
ParseInterfaceType
();
case
Scanner
.
LPAREN
:
P
.
Parse
Signatur
e
();
case
Scanner
.
LPAREN
:
P
.
Parse
FunctionTyp
e
();
case
Scanner
.
MAP
:
P
.
ParseMapType
();
case
Scanner
.
STRUCT
:
P
.
ParseStructType
();
case
Scanner
.
MUL
:
P
.
ParsePointerType
();
...
...
@@ -523,7 +468,7 @@ func (P *Parser) ParseFunctionLit() AST.Expr {
P
.
Trace
(
"FunctionLit"
);
P
.
Expect
(
Scanner
.
FUNC
);
P
.
Parse
Signature
();
// replace this with ParseFunctionType() and it won't work - 6g bug?
P
.
Parse
FunctionType
();
P
.
ParseBlock
();
P
.
Ecart
();
...
...
@@ -639,8 +584,8 @@ func (P *Parser) ParseOperand(ident *AST.Ident) AST.Expr {
}
func
(
P
*
Parser
)
ParseSelectorOrType
Assertion
(
x
AST
.
Expr
)
AST
.
Expr
{
P
.
Trace
(
"SelectorOrType
Assertion
"
);
func
(
P
*
Parser
)
ParseSelectorOrType
Guard
(
x
AST
.
Expr
)
AST
.
Expr
{
P
.
Trace
(
"SelectorOrType
Guard
"
);
P
.
Expect
(
Scanner
.
PERIOD
);
pos
:=
P
.
pos
;
...
...
@@ -682,6 +627,8 @@ func (P *Parser) ParseCall(x AST.Expr) AST.Expr {
// first arguments could be a type if the call is to "new"
// - exclude type names because they could be expression starts
// - exclude "("'s because function types are not allowed and they indicate an expression
// - still a problem for "new(*T)" (the "*")
// - possibility: make "new" a keyword again (or disallow "*" types in new)
if
P
.
tok
!=
Scanner
.
IDENT
&&
P
.
tok
!=
Scanner
.
LPAREN
&&
P
.
TryType
()
{
if
P
.
tok
==
Scanner
.
COMMA
{
P
.
Next
();
...
...
@@ -706,7 +653,7 @@ func (P *Parser) ParsePrimaryExpr(ident *AST.Ident) AST.Expr {
x
:=
P
.
ParseOperand
(
ident
);
for
{
switch
P
.
tok
{
case
Scanner
.
PERIOD
:
x
=
P
.
ParseSelectorOrType
Assertion
(
x
);
case
Scanner
.
PERIOD
:
x
=
P
.
ParseSelectorOrType
Guard
(
x
);
case
Scanner
.
LBRACK
:
x
=
P
.
ParseIndexOrSlice
(
x
);
case
Scanner
.
LPAREN
:
x
=
P
.
ParseCall
(
x
);
default
:
goto
exit
;
...
...
@@ -1221,11 +1168,40 @@ func (P *Parser) ParseDecl(exported bool, keyword int) {
}
// Function declarations
//
// func ident (params)
// func ident (params) type
// func ident (params) (results)
// func (recv) ident (params)
// func (recv) ident (params) type
// func (recv) ident (params) (results)
func
(
P
*
Parser
)
ParseFuncDecl
(
exported
bool
)
{
P
.
Trace
(
"FuncDecl"
);
P
.
Expect
(
Scanner
.
FUNC
);
ident
:=
P
.
ParseNamedSignature
();
P
.
OpenScope
();
P
.
level
--
;
if
P
.
tok
==
Scanner
.
LPAREN
{
recv_pos
:=
P
.
pos
;
n
:=
P
.
ParseParameters
();
if
n
!=
1
{
P
.
Error
(
recv_pos
,
"must have exactly one receiver"
);
}
}
ident
:=
P
.
ParseIdent
();
P
.
ParseFunctionType
();
P
.
level
++
;
P
.
CloseScope
();
if
P
.
tok
==
Scanner
.
SEMICOLON
{
// forward declaration
P
.
Next
();
...
...
usr/gri/pretty/pretty.go
View file @
bf04eefd
...
...
@@ -19,9 +19,20 @@ var (
)
func
Usage
()
{
print
(
"usage: pretty { flags } { files }
\n
"
);
Flag
.
PrintDefaults
();
sys
.
exit
(
0
);
}
func
main
()
{
Flag
.
Parse
();
if
Flag
.
NFlag
()
==
0
&&
Flag
.
NArg
()
==
0
{
Usage
();
}
// process files
for
i
:=
0
;
i
<
Flag
.
NArg
();
i
++
{
src_file
:=
Flag
.
Arg
(
i
);
...
...
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