-
Notifications
You must be signed in to change notification settings - Fork 20
/
context.go
91 lines (71 loc) · 2.03 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package parser
import (
contextpkg "context"
"sync"
"github.com/tliron/exturl"
"github.com/tliron/go-ard"
"github.com/tliron/kutil/problems"
"github.com/tliron/kutil/terminal"
"github.com/tliron/kutil/util"
"github.com/tliron/puccini/tosca/grammars"
"github.com/tliron/puccini/tosca/parsing"
)
//
// Context
//
type Context struct {
Parser *Parser
URL exturl.URL
Bases []exturl.URL
Quirks parsing.Quirks
Inputs map[string]ard.Value
Stylist *terminal.Stylist
Root *File
Files Files
readWork sync.WaitGroup
filesLock util.RWLocker
}
func (self *Parser) NewContext() *Context {
return &Context{
Parser: self,
filesLock: util.NewDefaultRWLocker(),
}
}
func (self *Context) GetProblems() *problems.Problems {
return self.Root.GetContext().Problems
}
func (self *Context) MergeProblems() {
self.filesLock.RLock()
defer self.filesLock.RUnlock()
// Note: This could happen many times, but because problems are de-duped, everything is OK :)
for _, file := range self.Files {
self.GetProblems().Merge(file.GetContext().Problems)
}
}
func (self *Context) AddFile(file *File) {
self.filesLock.Lock()
defer self.filesLock.Unlock()
self.Files = append(self.Files, file)
}
func (self *Context) AddImportFile(context contextpkg.Context, entityPtr parsing.EntityPtr, container *File, nameTransformer parsing.NameTransformer) *File {
file := NewFile(entityPtr, container, nameTransformer)
if container != nil {
containerContext := container.GetContext()
if !containerContext.HasQuirk(parsing.QuirkImportsVersionPermissive) {
fileContext := file.GetContext()
if !grammars.CompatibleGrammars(containerContext, fileContext) {
containerContext.ReportImportIncompatible(fileContext.URL)
return file
}
}
}
self.AddFile(file)
self.goReadImports(context, file)
return file
}
// Print
func (self *Context) PrintImports(indent int) {
terminal.PrintIndent(indent)
terminal.Printf("%s\n", terminal.StdoutStylist.Value(self.Root.GetContext().URL.String()))
self.Root.PrintImports(indent, terminal.TreePrefix{})
}