This repository has been archived on 2024-02-27. You can view files and clone it, but cannot push or open issues or pull requests.
arf/analyzer/table.go

91 lines
2.3 KiB
Go
Raw Permalink Normal View History

package analyzer
2022-09-29 22:25:56 +00:00
import "os"
import "sort"
import "path/filepath"
2022-10-11 22:31:37 +00:00
import "git.tebibyte.media/arf/arf/file"
2022-10-01 21:21:17 +00:00
import "git.tebibyte.media/arf/arf/types"
2022-10-11 22:31:37 +00:00
import "git.tebibyte.media/arf/arf/infoerr"
// locator uniquely identifies a section in the section table.
type locator struct {
modulePath string
name string
}
2022-09-10 23:50:18 +00:00
func (where locator) ToString () (output string) {
2022-09-29 22:25:56 +00:00
cwd, _ := os.Getwd()
modulePath, err := filepath.Rel(cwd, where.modulePath)
if err != nil {
panic("cant get relative path: " + err.Error())
}
output += modulePath + "." + where.name
2022-09-10 23:50:18 +00:00
return
}
// SectionTable stores a list of semantically analized sections from one module,
// and all sections that it requires from other modules.
type SectionTable map[locator] Section
2022-09-18 03:29:11 +00:00
// ToString returns the data stored in the table as a string.
func (table SectionTable) ToString (indent int) (output string) {
sortedKeys := make(locatorArray, len(table))
index := 0
for key, _ := range table {
sortedKeys[index] = key
index ++
}
sort.Sort(sortedKeys)
for _, name := range sortedKeys {
section := table[name]
2022-09-18 03:29:11 +00:00
output += section.ToString(indent)
}
return
}
// locatorArray holds a sortable array of locators
type locatorArray []locator
// Len returns the length of the locator array
func (array locatorArray) Len () (length int) {
length = len(array)
return
}
// Less returns whether item at index left is less than item at index right.
func (array locatorArray) Less (left, right int) (less bool) {
leftLocator := array[left]
rightLocator := array[right]
less =
leftLocator.modulePath + leftLocator.name <
rightLocator.modulePath + rightLocator.name
return
}
// Swap swaps the elments at indices left and right.
func (array locatorArray) Swap (left, right int) {
temp := array[left]
array[left] = array[right]
array[right] = temp
}
2022-09-18 03:29:11 +00:00
// Section is a semantically analyzed section.
type Section interface {
// Provided by sectionBase
Name () (name string)
Complete () (complete bool)
ModulePath () (path string)
ModuleName () (path string)
2022-10-01 21:21:17 +00:00
Permission () (permission types.Permission)
2022-10-11 22:31:37 +00:00
Location () (location file.Location)
NewError (message string, kind infoerr.ErrorKind) (err error)
2022-09-29 22:09:52 +00:00
locator () (where locator)
// Must be implemented by each individual section
ToString (indent int) (output string)
}