예제 #1
0
func parsePack(srcDir string, specialFiles []string) {

	fileSet, packs, err := getAstTree(srcDir, specialFiles)
	if err != nil {
		fmt.Printf("Warning: some errors occured during parsing package %s:\n %v\n", srcDir, err)
	}

	_, d := path.Split(srcDir)
	if packTree, ok := packs[d]; !ok {
		panic("Couldn't find a package " + d + " in directory \"" + srcDir + "\"")
	} else {
		pack := st.NewPackage(srcDir, packages[srcDir], fileSet, packTree)
		program.Packages[srcDir] = pack
	}
}
예제 #2
0
func (iv *importsVisitor) Visit(node ast.Node) (w ast.Visitor) {
	w = iv
	if is, ok := node.(*ast.ImportSpec); ok {

		Path := string(is.Path.Value)
		Path = Path[1 : len(Path)-1] //remove quotes

		var (
			name     string
			found    bool
			pack     *st.Package
			packTree *ast.Package
		)

		if is.Name != nil {
			switch is.Name.Name {
			case "_":
				return // package imported only for side-effects
			case ".":
				name = "." //, hasLocalName = ".", false
			default:
				name = is.Name.Name //, hasLocalName =, true
			}
		} else {
			_, name = path.Split(Path)
			//hasLocalName = false
		}

		pack, found = program.FindPackageByGoPath(Path)

		if !found {
			_, f := path.Split(Path)
			fileSet, dirTree, _ := getAstTree(path.Join(goSrcDir, Path), iv.specialPackages[Path])
			if dirTree != nil {
				if packTree, found = dirTree[f]; found {
					pack = st.NewPackage(path.Join(goSrcDir, Path), Path, fileSet, packTree)
					program.Packages[pack.QualifiedPath] = pack
					parseImports(pack, iv.specialPackages)
				} else {
					panic("package not found where expected: " + path.Join(goSrcDir, Path))
				}
			}
			for dir, goPath := range packages {
				if goPath == Path {
					fileSet, dirTree, _ := getAstTree(dir, iv.specialPackages[Path])
					if dirTree != nil {
						if packTree, found = dirTree[f]; found {
							pack = st.NewPackage(dir, Path, fileSet, packTree)
							program.Packages[pack.QualifiedPath] = pack
							parseImports(pack, iv.specialPackages)
							break
						} else {
							panic("package not found where expected: " + dir)
						}
					} else {
						panic("package not found where expected: " + dir)
					}
				}
			}
		}
		if _, isIn := iv.Package.Imports[iv.FileName]; !isIn {
			iv.Package.Imports[iv.FileName] = new(vector.Vector)
		}

		sym := st.MakePackage(name, iv.Package.Symbols, Path, pack)

		if is.Name != nil {

			sym.AddIdent(is.Name)
			program.IdentMap.AddIdent(is.Name, sym)

			sym.AddPosition(iv.Package.FileSet.Position(is.Name.Pos()))

			if is.Name.Name == "." {
				iv.Package.Symbols.AddOpenedScope(pack.Symbols)
			}
		}

		iv.Package.Imports[iv.FileName].Push(sym)
		//------------

		return

	}
	return
}