Пример #1
0
func (pkg *Package) readHeaderFile(location string) {
	//	println("READING FUNCTION HEADER FILE")
	// TODO : plug in new go parser to do this
	input_file := filepath.Join(location, "headers.tf")

	if _, err := os.Open(input_file); err != nil {
		pkg.Log.Infof("Warning -- found no headers.tf")
		return
	}

	stubs := parser.ParseFile(location, ".", "headers.tf", false, make([]string, 0))
	for _, function := range stubs.Functions {
		// Verify that signatures for primitives refer to things that actually exist
		// println("HEY, LINKING BUILTIN:", function.Stub(pkg.Package))
		stubStr := strings.Replace(function.Stub(pkg.Package), ",", ".", -1)
		if whale.LookupBuiltIn(stubStr) == nil {
			// TODO: figure out why the panic string is suppressed so that we can remove the println
			// println("in " + input_file + " -- attempt to provide signature for nonexistent built-in " + stubStr)
			panic("in " + input_file + " -- attempt to provide signature for nonexistent built-in " + stubStr)
		}

		pkg.resolveHeader(function)

		function.BuiltIn = proto.Bool(true)

		pkg.Package.Functions = append(pkg.Package.Functions, function)
	}

}
Пример #2
0
func (pkgr *Packager) resolveFunctions(dirName, fileName string) {
	// the existence of `fileName` should be verified by the caller
	relPath := filepath.Join(dirName, fileName)
	defs := parser.ParseFile(pkgr.MixerDir, dirName, fileName, true, make([]string, 0))

	for _, f := range defs.Functions {
		// if it's an import stub ...
		if f.GetName() == "@import" {
			importPath := f.GetDescription()
			importExists, _ := fileutil.Exists(filepath.Join(pkgr.MixerDir, importPath))
			if !importExists {
				errURL := "http://help.moovweb.com/entries/22335641-importing-non-existent-files-in-functions-main-ts"
				msg := fmt.Sprintf("\n********\nin file %s:\nattempting to import nonexistent file %s\nPlease consult %s for more information about this error.\n********\n", relPath, importPath, errURL)
				panic(msg)
			}
			pkgr.resolveFunctions(filepath.Dir(importPath), filepath.Base(importPath))
			continue // to forgo appending it to the comprehensive list of functions

			// otherwise it's a proper function definition -- see if it's native
		} else if f.GetBuiltIn() {
			pkgr.resolveNativeDeclaration(f, relPath)

			// otherwise it's a user-defined function
		} else {
			pkgr.resolveUserDefinition(f, relPath)
		}

		pkgr.Package.Functions = append(pkgr.Package.Functions, f)
	}
}
Пример #3
0
func ReadPackageDefinitions(pkg *tp.Package, projectPath, scriptPath, fileName string) {
	//pkg.Println(" -- reading definitions")
	_, err := ioutil.ReadFile(filepath.Join(projectPath, scriptPath, fileName))
	//()("READING DEFINITIONS:", location)

	if err != nil {
		//pkg.Log.Infof("\t -- no user defined functions found")
		// msg := fmt.Sprintf("unable to open function definition file: %s", location)
		// println(msg)
		// panic(msg)
		return
	}
	definitions := parser.ParseFile(projectPath, scriptPath, fileName, false, make([]string, 0))

	// Create a map of pre-packaged function signatures
	prepackaged := make(map[string]bool)
	for _, f := range pkg.Functions {
		var sig string
		baseSig := f.Stub(pkg)
		if baseSig == "name,Text" ||
			baseSig == "text" {
			sig = fmt.Sprintf("%s.%s", f.ScopeTypeString(pkg), f.Stub(pkg))
		} else {
			sig = baseSig
		}
		prepackaged[sig] = true
	}
	// println("*****************")
	// println("*****************")
	// println()
	// println()

	for _, function := range definitions.Functions {
		if function.GetName() == "@import" { // check if it's an import stub first ...
			importPath := function.GetDescription()
			// Verify the existence of the imported file from here, so that we can
			// report the name of the file that contains the import statement.
			importExists, existsErr := exists(filepath.Join(projectPath, importPath))
			if !importExists || (existsErr != nil) {
				errURL := "http://help.moovweb.com/entries/22335641-importing-non-existent-files-in-functions-main-ts"
				msg := fmt.Sprintf("\n********\nin file %s:\nattempting to import nonexistent file %s\nPlease consult %s for more information about this error.\n********\n", filepath.Join(scriptPath, fileName), importPath, errURL)
				panic(msg)
			}
			ReadPackageDefinitions(pkg, projectPath, filepath.Dir(importPath), filepath.Base(importPath))
		} else { // otherwise if it's not an import stub ...
			//pkg.Log.Infof("\t -- function: %v", function)
			resolveDefinition(pkg, function, filepath.Join(scriptPath, fileName))

			// After resolving a user-defined function, see if its fully resolved signature
			// is the same as the signature of a prepackaged function. If so, throw an error.
			// var newSig string
			// newBaseSig := function.Stub(pkg)
			// if newBaseSig == "name,Text" ||
			// 	newBaseSig == "text" {
			// 	newSig = fmt.Sprintf("%s.%s", function.ScopeTypeString(pkg), function.Stub(pkg))
			// } else {
			// 	newSig = newBaseSig
			// }
			// // present := false
			// _, present := prepackaged[newSig]
			// if present {
			// 	msg := fmt.Sprintf("Attempt to redefine prepackaged function: %s", strings.Replace(newSig, ",", "(", 1)+")")
			// 	println(msg)
			// 	panic(msg)
			// }

			pkg.Functions = append(pkg.Functions, function)
		}
	}

}