1
0
mirror of https://github.com/golang/go synced 2024-11-26 20:41:24 -07:00

1) Move parser.go into src/lib/go

- minor adjustments as suggested by rsc
2) Added parser_test fragment
3) Renamed some types in AST.go per rsc request

R=rsc
DELTA=2053  (2027 added, 0 deleted, 26 changed)
OCL=26963
CL=26971
This commit is contained in:
Robert Griesemer 2009-03-31 16:53:37 -07:00
parent 4702c0e5ef
commit 07513c2599
5 changed files with 2054 additions and 25 deletions

View File

@ -101,7 +101,7 @@ strings.6: utf8.install
testing.6: flag.install fmt.dirinstall testing.6: flag.install fmt.dirinstall
fmt.dirinstall: io.dirinstall reflect.dirinstall strconv.dirinstall fmt.dirinstall: io.dirinstall reflect.dirinstall strconv.dirinstall
go.dirinstall: strconv.dirinstall utf8.install unicode.dirinstall go.dirinstall: strconv.dirinstall utf8.install unicode.dirinstall fmt.dirinstall
hash.dirinstall: os.dirinstall hash.dirinstall: os.dirinstall
http.dirinstall: bufio.install io.dirinstall net.dirinstall os.dirinstall strings.install log.install http.dirinstall: bufio.install io.dirinstall net.dirinstall os.dirinstall strings.install log.install
io.dirinstall: os.dirinstall sync.dirinstall io.dirinstall: os.dirinstall sync.dirinstall

View File

@ -38,9 +38,13 @@ O2=\
ast.$O\ ast.$O\
scanner.$O\ scanner.$O\
ast.a: a1 a2 O3=\
scanner.a: a1 a2 parser.$O\
token.a: a1 a2
ast.a: a1 a2 a3
parser.a: a1 a2 a3
scanner.a: a1 a2 a3
token.a: a1 a2 a3
a1: $(O1) a1: $(O1)
$(AR) grc token.a token.$O $(AR) grc token.a token.$O
@ -51,21 +55,28 @@ a2: $(O2)
$(AR) grc scanner.a scanner.$O $(AR) grc scanner.a scanner.$O
rm -f $(O2) rm -f $(O2)
a3: $(O3)
$(AR) grc parser.a parser.$O
rm -f $(O3)
newpkg: clean newpkg: clean
$(AR) grc ast.a $(AR) grc ast.a
$(AR) grc parser.a
$(AR) grc scanner.a $(AR) grc scanner.a
$(AR) grc token.a $(AR) grc token.a
$(O1): newpkg $(O1): newpkg
$(O2): a1 $(O2): a1
$(O3): a2
nuke: clean nuke: clean
rm -f $(GOROOT)/pkg/ast.a $(GOROOT)/pkg/scanner.a $(GOROOT)/pkg/token.a rm -f $(GOROOT)/pkg/ast.a $(GOROOT)/pkg/parser.a $(GOROOT)/pkg/scanner.a $(GOROOT)/pkg/token.a
packages: ast.a scanner.a token.a packages: ast.a parser.a scanner.a token.a
install: packages install: packages
cp ast.a $(GOROOT)/pkg/ast.a cp ast.a $(GOROOT)/pkg/ast.a
cp parser.a $(GOROOT)/pkg/parser.a
cp scanner.a $(GOROOT)/pkg/scanner.a cp scanner.a $(GOROOT)/pkg/scanner.a
cp token.a $(GOROOT)/pkg/token.a cp token.a $(GOROOT)/pkg/token.a

View File

@ -99,7 +99,7 @@ type Comments []*Comment
type ( type (
Ident struct; Ident struct;
StringLit struct; StringLit struct;
FunctionType struct; FuncType struct;
BlockStmt struct; BlockStmt struct;
// A Field represents a Field declaration list in a struct type, // A Field represents a Field declaration list in a struct type,
@ -172,9 +172,9 @@ type (
Strings []*StringLit; // list of strings, len(Strings) > 1 Strings []*StringLit; // list of strings, len(Strings) > 1
}; };
// A FunctionLit node represents a function literal. // A FuncLit node represents a function literal.
FunctionLit struct { FuncLit struct {
Type *FunctionType; // function type Type *FuncType; // function type
Body *BlockStmt; // function body Body *BlockStmt; // function body
}; };
@ -302,8 +302,8 @@ type (
// Pointer types are represented via StarExpr nodes. // Pointer types are represented via StarExpr nodes.
// A FunctionType node represents a function type. // A FuncType node represents a function type.
FunctionType struct { FuncType struct {
token.Position; // position of "func" keyword token.Position; // position of "func" keyword
Params []*Field; // (incoming) parameters Params []*Field; // (incoming) parameters
Results []*Field; // (outgoing) results Results []*Field; // (outgoing) results
@ -324,8 +324,8 @@ type (
Value Expr; Value Expr;
}; };
// A ChannelType node represents a channel type. // A ChanType node represents a channel type.
ChannelType struct { ChanType struct {
token.Position; // position of "chan" keyword or "<-" (whichever comes first) token.Position; // position of "chan" keyword or "<-" (whichever comes first)
Dir ChanDir; // channel direction Dir ChanDir; // channel direction
Value Expr; // value type Value Expr; // value type
@ -337,7 +337,7 @@ type (
// corresponds to the position of a sub-node. // corresponds to the position of a sub-node.
// //
func (x *StringList) Pos() token.Position { return x.Strings[0].Pos(); } func (x *StringList) Pos() token.Position { return x.Strings[0].Pos(); }
func (x *FunctionLit) Pos() token.Position { return x.Type.Pos(); } func (x *FuncLit) Pos() token.Position { return x.Type.Pos(); }
func (x *CompositeLit) 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 *SelectorExpr) Pos() token.Position { return x.X.Pos(); }
func (x *IndexExpr) Pos() token.Position { return x.X.Pos(); } func (x *IndexExpr) Pos() token.Position { return x.X.Pos(); }
@ -362,7 +362,7 @@ type ExprVisitor interface {
DoCharLit(x *CharLit); DoCharLit(x *CharLit);
DoStringLit(x *StringLit); DoStringLit(x *StringLit);
DoStringList(x *StringList); DoStringList(x *StringList);
DoFunctionLit(x *FunctionLit); DoFuncLit(x *FuncLit);
DoCompositeLit(x *CompositeLit); DoCompositeLit(x *CompositeLit);
DoParenExpr(x *ParenExpr); DoParenExpr(x *ParenExpr);
DoSelectorExpr(x *SelectorExpr); DoSelectorExpr(x *SelectorExpr);
@ -380,10 +380,10 @@ type ExprVisitor interface {
DoArrayType(x *ArrayType); DoArrayType(x *ArrayType);
DoSliceType(x *SliceType); DoSliceType(x *SliceType);
DoStructType(x *StructType); DoStructType(x *StructType);
DoFunctionType(x *FunctionType); DoFuncType(x *FuncType);
DoInterfaceType(x *InterfaceType); DoInterfaceType(x *InterfaceType);
DoMapType(x *MapType); DoMapType(x *MapType);
DoChannelType(x *ChannelType); DoChanType(x *ChanType);
} }
@ -397,7 +397,7 @@ func (x *FloatLit) Visit(v ExprVisitor) { v.DoFloatLit(x); }
func (x *CharLit) Visit(v ExprVisitor) { v.DoCharLit(x); } func (x *CharLit) Visit(v ExprVisitor) { v.DoCharLit(x); }
func (x *StringLit) Visit(v ExprVisitor) { v.DoStringLit(x); } func (x *StringLit) Visit(v ExprVisitor) { v.DoStringLit(x); }
func (x *StringList) Visit(v ExprVisitor) { v.DoStringList(x); } func (x *StringList) Visit(v ExprVisitor) { v.DoStringList(x); }
func (x *FunctionLit) Visit(v ExprVisitor) { v.DoFunctionLit(x); } func (x *FuncLit) Visit(v ExprVisitor) { v.DoFuncLit(x); }
func (x *CompositeLit) Visit(v ExprVisitor) { v.DoCompositeLit(x); } func (x *CompositeLit) Visit(v ExprVisitor) { v.DoCompositeLit(x); }
func (x *ParenExpr) Visit(v ExprVisitor) { v.DoParenExpr(x); } func (x *ParenExpr) Visit(v ExprVisitor) { v.DoParenExpr(x); }
func (x *SelectorExpr) Visit(v ExprVisitor) { v.DoSelectorExpr(x); } func (x *SelectorExpr) Visit(v ExprVisitor) { v.DoSelectorExpr(x); }
@ -413,10 +413,10 @@ func (x *KeyValueExpr) Visit(v ExprVisitor) { v.DoKeyValueExpr(x); }
func (x *ArrayType) Visit(v ExprVisitor) { v.DoArrayType(x); } func (x *ArrayType) Visit(v ExprVisitor) { v.DoArrayType(x); }
func (x *SliceType) Visit(v ExprVisitor) { v.DoSliceType(x); } func (x *SliceType) Visit(v ExprVisitor) { v.DoSliceType(x); }
func (x *StructType) Visit(v ExprVisitor) { v.DoStructType(x); } func (x *StructType) Visit(v ExprVisitor) { v.DoStructType(x); }
func (x *FunctionType) Visit(v ExprVisitor) { v.DoFunctionType(x); } func (x *FuncType) Visit(v ExprVisitor) { v.DoFuncType(x); }
func (x *InterfaceType) Visit(v ExprVisitor) { v.DoInterfaceType(x); } func (x *InterfaceType) Visit(v ExprVisitor) { v.DoInterfaceType(x); }
func (x *MapType) Visit(v ExprVisitor) { v.DoMapType(x); } func (x *MapType) Visit(v ExprVisitor) { v.DoMapType(x); }
func (x *ChannelType) Visit(v ExprVisitor) { v.DoChannelType(x); } func (x *ChanType) Visit(v ExprVisitor) { v.DoChanType(x); }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -699,7 +699,7 @@ type (
Doc Comments; // associated documentation; or nil Doc Comments; // associated documentation; or nil
Recv *Field; // receiver (methods) or nil (functions) Recv *Field; // receiver (methods) or nil (functions)
Name *Ident; // function/method name Name *Ident; // function/method name
Type *FunctionType; // position of Func keyword, parameters and results Type *FuncType; // position of Func keyword, parameters and results
Body *BlockStmt; // function body or nil (forward declaration) Body *BlockStmt; // function body or nil (forward declaration)
}; };
@ -746,10 +746,12 @@ func (d *DeclList) Visit(v DeclVisitor) { v.DoDeclList(d); }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Packages // Programs
// A Package node represents the root node of an AST. // A Program node represents the root node of an AST
type Package struct { // for an entire source file.
//
type Program struct {
Doc Comments; // associated documentation; or nil Doc Comments; // associated documentation; or nil
token.Position; // position of "package" keyword token.Position; // position of "package" keyword
Name *Ident; // package name Name *Ident; // package name

1969
src/lib/go/parser.go Normal file

File diff suppressed because it is too large Load Diff

47
src/lib/go/parser_test.go Normal file
View File

@ -0,0 +1,47 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package parser
import (
"ast";
"os";
"parser";
"testing";
)
func TestParse0(t *testing.T) {
// test nil []bytes source
var src []byte;
prog, ok := Parse(src, nil, 0);
if ok {
t.Errorf("parse should have failed");
}
}
func TestParse1(t *testing.T) {
// test string source
src := `package main import "fmt" func main() { fmt.Println("Hello, World!") }`;
prog, ok := Parse(src, nil, 0);
if !ok {
t.Errorf("parse failed");
}
}
func TestParse2(t *testing.T) {
// test io.Read source
filename := "parser_test.go";
src, err := os.Open(filename, os.O_RDONLY, 0);
defer src.Close();
if err != nil {
t.Errorf("cannot open %s (%s)\n", filename, err.String());
}
prog, ok := Parse(src, nil, 0);
if !ok {
t.Errorf("parse failed");
}
}