2022-09-07 10:20:34 -06:00
|
|
|
package analyzer
|
|
|
|
|
2022-09-29 16:25:56 -06:00
|
|
|
import "os"
|
2022-09-17 21:40:30 -06:00
|
|
|
import "path/filepath"
|
|
|
|
|
2022-09-07 10:20:34 -06:00
|
|
|
// locator uniquely identifies a section in the section table.
|
|
|
|
type locator struct {
|
|
|
|
modulePath string
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2022-09-10 17:50:18 -06:00
|
|
|
func (where locator) ToString () (output string) {
|
2022-09-29 16:25:56 -06: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 17:50:18 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-07 10:20:34 -06:00
|
|
|
// 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-17 21:29:11 -06:00
|
|
|
// ToString returns the data stored in the table as a string.
|
|
|
|
func (table SectionTable) ToString (indent int) (output string) {
|
|
|
|
for _, section := range table {
|
|
|
|
output += section.ToString(indent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-07 10:20:34 -06:00
|
|
|
// Section is a semantically analyzed section.
|
|
|
|
type Section interface {
|
2022-09-17 21:40:30 -06:00
|
|
|
// Provided by sectionBase
|
|
|
|
Name () (name string)
|
|
|
|
Complete () (complete bool)
|
|
|
|
ModulePath () (path string)
|
|
|
|
ModuleName () (path string)
|
2022-09-29 16:09:52 -06:00
|
|
|
locator () (where locator)
|
2022-09-17 21:40:30 -06:00
|
|
|
|
|
|
|
// Must be implemented by each individual section
|
2022-09-09 18:55:03 -06:00
|
|
|
ToString (indent int) (output string)
|
2022-09-07 10:20:34 -06:00
|
|
|
}
|
2022-09-08 10:59:49 -06:00
|
|
|
|
2022-09-09 18:55:03 -06:00
|
|
|
// sectionBase is a struct that all sections must embed.
|
|
|
|
type sectionBase struct {
|
2022-09-10 17:50:18 -06:00
|
|
|
where locator
|
2022-09-09 18:55:03 -06:00
|
|
|
complete bool
|
|
|
|
}
|
2022-09-08 10:59:49 -06:00
|
|
|
|
2022-09-09 18:55:03 -06:00
|
|
|
// Name returns the name of the section.
|
|
|
|
func (section sectionBase) Name () (name string) {
|
2022-09-10 17:50:18 -06:00
|
|
|
name = section.where.name
|
2022-09-09 18:55:03 -06:00
|
|
|
return
|
2022-09-08 10:59:49 -06:00
|
|
|
}
|
|
|
|
|
2022-09-10 17:50:18 -06:00
|
|
|
// ModulePath returns the full path of the module the section came from.
|
2022-09-17 21:40:30 -06:00
|
|
|
func (section sectionBase) ModulePath () (path string) {
|
|
|
|
path = section.where.modulePath
|
2022-09-10 17:50:18 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-17 21:40:30 -06:00
|
|
|
// ModuleName returns the name of the module where the section came from.
|
|
|
|
func (section sectionBase) ModuleName () (name string) {
|
|
|
|
name = filepath.Base(section.where.modulePath)
|
|
|
|
return
|
|
|
|
}
|
2022-09-10 17:50:18 -06:00
|
|
|
|
2022-09-09 18:55:03 -06:00
|
|
|
// Complete returns wether the section has been completed.
|
|
|
|
func (section sectionBase) Complete () (complete bool) {
|
|
|
|
complete = section.complete
|
|
|
|
return
|
2022-09-08 10:59:49 -06:00
|
|
|
}
|
2022-09-29 16:09:52 -06:00
|
|
|
|
|
|
|
func (section sectionBase) locator () (where locator) {
|
|
|
|
where = section.where
|
|
|
|
return
|
|
|
|
}
|