コード例 #1
0
ファイル: printer_test.go プロジェクト: grncdr/godef
// TestLineComments, using a simple test case, checks that consequtive line
// comments are properly terminated with a newline even if the AST position
// information is incorrect.
//
func TestLineComments(t *testing.T) {
	const src = `// comment 1
	// comment 2
	// comment 3
	package main
	`

	fset := token.NewFileSet()
	ast1, err1 := parser.ParseFile(fset, "", src, parser.ParseComments, nil)
	if err1 != nil {
		panic(err1)
	}

	var buf bytes.Buffer
	fset = token.NewFileSet() // use the wrong file set
	Fprint(&buf, fset, ast1)

	nlines := 0
	for _, ch := range buf.Bytes() {
		if ch == '\n' {
			nlines++
		}
	}

	const expected = 3
	if nlines < expected {
		t.Errorf("got %d, expected %d\n", nlines, expected)
	}
}
コード例 #2
0
ファイル: sym.go プロジェクト: grncdr/godef
func NewContext() *Context {
	ctxt := &Context{
		pkgCache:     make(map[string]*ast.Package),
		FileSet:      token.NewFileSet(),
		ChangedFiles: make(map[string]*ast.File),
	}
	ctxt.importer = ctxt.importerFunc()
	return ctxt
}
コード例 #3
0
ファイル: types.go プロジェクト: grncdr/godef
var falseIdent = predecl("false")
var trueIdent = predecl("true")
var iotaIdent = predecl("iota")
var boolIdent = predecl("bool")
var intIdent = predecl("int")
var floatIdent = predecl("float")
var stringIdent = predecl("string")

func predecl(name string) *ast.Ident {
	return &ast.Ident{Name: name, Obj: parser.Universe.Lookup(name)}
}

type Importer func(path string) *ast.Package

// When _any_ Importer is called, it adds any files to FileSet.
var FileSet = token.NewFileSet()

// GoPath is used by DefaultImporter to find packages.
var GoPath = []string{filepath.Join(os.Getenv("GOROOT"), "src", "pkg")}

// The Importer looks for the package; if it finds it,
// it parses and returns it. If no package was found, it returns nil.
func NewImporter(srcDir string) Importer {
	return func(path string) *ast.Package {
		bpkg, err := build.Default.Import(path, srcDir, 0)
		if err != nil {
			return nil
		}
		pkgs, err := parser.ParseDir(FileSet, bpkg.Dir, isGoFile, 0)
		if err != nil {
			if Debug {
コード例 #4
0
ファイル: sym.go プロジェクト: grncdr/godef
	if err != nil {
		panic("cannot unquote")
	}
	return v
}

type astVisitor func(n ast.Node) bool

func (f astVisitor) Visit(n ast.Node) ast.Visitor {
	if f(n) {
		return f
	}
	return nil
}

var emptyFileSet = token.NewFileSet()

func pretty(n ast.Node) string {
	var b bytes.Buffer
	printer.Fprint(&b, emptyFileSet, n)
	return b.String()
}

var printConfig = &printer.Config{
	Mode:     printer.TabIndent | printer.UseSpaces,
	Tabwidth: 8,
}

func (ctxt *Context) gofmtFile(f *ast.File) ([]byte, error) {
	var buf bytes.Buffer
	_, err := printConfig.Fprint(&buf, ctxt.FileSet, f)
コード例 #5
0
ファイル: parser_test.go プロジェクト: grncdr/godef
// 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 (
	"os"
	"testing"

	"github.com/grncdr/godef/go/token"
)

var fset = token.NewFileSet()

var illegalInputs = []interface{}{
	nil,
	3.14,
	[]byte(nil),
	"foo!",
	`package p; func f() { if /* should have condition */ {} };`,
	`package p; func f() { if ; /* should have condition */ {} };`,
	`package p; func f() { if f(); /* should have condition */ {} };`,
}

func TestParseIllegalInputs(t *testing.T) {
	for _, src := range illegalInputs {
		_, err := ParseFile(fset, "", src, 0, nil)
		if err == nil {
			t.Errorf("ParseFile(%v) should have failed", src)
		}