lang: Pass through the fs and be consistent in usage

This simplifies the API by passing through the filesystem so that
function signatures don't need to be as complicated, and furthermore use
that consistently throughout.
This commit is contained in:
James Shubin
2024-02-21 12:31:31 -05:00
parent a05b6a927e
commit b7efd94147
5 changed files with 37 additions and 33 deletions

View File

@@ -3474,7 +3474,7 @@ func (obj *StmtProg) importSystemScope(name string) (*interfaces.Scope, error) {
// importScopeWithInputs returns a local or remote scope from an inputs string.
// The inputs string is the common frontend for a lot of our parsing decisions.
func (obj *StmtProg) importScopeWithInputs(s string, scope *interfaces.Scope, parentVertex *pgraph.SelfVertex) (*interfaces.Scope, error) {
output, err := inputs.ParseInput(s, obj.data.Fs)
input, err := inputs.ParseInput(s, obj.data.Fs)
if err != nil {
return nil, errwrap.Wrapf(err, "could not activate an input parser")
}
@@ -3484,12 +3484,12 @@ func (obj *StmtProg) importScopeWithInputs(s string, scope *interfaces.Scope, pa
// run recursion detection by checking for duplicates in the seen files
// TODO: do the paths need to be cleaned for "../", etc before compare?
//for _, name := range obj.data.Files { // existing seen files
// if util.StrInList(name, output.Files) {
// if util.StrInList(name, input.Files) {
// return nil, fmt.Errorf("recursive import of: `%s`", name)
// }
//}
reader := bytes.NewReader(output.Main)
reader := bytes.NewReader(input.Main)
// nested logger
logf := func(format string, v ...interface{}) {
@@ -3498,11 +3498,11 @@ func (obj *StmtProg) importScopeWithInputs(s string, scope *interfaces.Scope, pa
// build new list of files
files := []string{}
files = append(files, output.Files...)
files = append(files, input.Files...)
files = append(files, obj.data.Files...)
// store a reference to the parent metadata
metadata := output.Metadata
metadata := input.Metadata
metadata.Metadata = obj.data.Metadata
// now run the lexer/parser to do the import
@@ -3518,9 +3518,9 @@ func (obj *StmtProg) importScopeWithInputs(s string, scope *interfaces.Scope, pa
// init and validate the structure of the AST
data := &interfaces.Data{
// TODO: add missing fields here if/when needed
Fs: obj.data.Fs,
FsURI: obj.data.FsURI,
Base: output.Base, // new base dir (absolute path)
Fs: input.FS, // formerly: obj.data.Fs,
FsURI: input.FS.URI(), // formerly: obj.data.FsURI,
Base: input.Base, // new base dir (absolute path)
Files: files,
Imports: parentVertex, // the parent vertex that imported me
Metadata: metadata,
@@ -3593,7 +3593,7 @@ func (obj *StmtProg) importScopeWithInputs(s string, scope *interfaces.Scope, pa
obj.importProgs = append(obj.importProgs, prog)
// collecting these here is more elegant (and possibly more efficient!)
obj.importFiles = append(obj.importFiles, output.Files...) // save for CollectFiles
obj.importFiles = append(obj.importFiles, input.Files...) // save for CollectFiles
return prog.scope, nil
}

View File

@@ -263,9 +263,9 @@ func (obj *GAPI) Cli(cliInfo *gapi.CliInfo) (*gapi.Deploy, error) {
// init and validate the structure of the AST
data := &interfaces.Data{
// TODO: add missing fields here if/when needed
Fs: localFs, // the local fs!
FsURI: localFs.URI(), // TODO: is this right?
Base: output.Base, // base dir (absolute path) that this is rooted in
Fs: output.FS, // formerly: localFs // the local fs!
FsURI: output.FS.URI(), // formerly: localFs.URI() // TODO: is this right?
Base: output.Base, // base dir (absolute path) that this is rooted in
Files: output.Files,
Imports: importVertex,
Metadata: output.Metadata,
@@ -770,9 +770,9 @@ func (obj *GAPI) Get(getInfo *gapi.GetInfo) error {
// init and validate the structure of the AST
data := &interfaces.Data{
// TODO: add missing fields here if/when needed
Fs: localFs, // the local fs!
FsURI: localFs.URI(), // TODO: is this right?
Base: output.Base, // base dir (absolute path) that this is rooted in
Fs: output.FS, // formerly: localFs // the local fs!
FsURI: output.FS.URI(), // formerly: localFs.URI() // TODO: is this right?
Base: output.Base, // base dir (absolute path) that this is rooted in
Files: output.Files,
Imports: importVertex,
Metadata: output.Metadata,

View File

@@ -66,9 +66,10 @@ var (
// ParsedInput is the output struct which contains all the information we need.
type ParsedInput struct {
//activated bool // if struct is not nil we're activated
Base string // base path (abs path with trailing slash)
Main []byte // contents of main entry mcl code
Files []string // files and dirs to copy to fs (abs paths)
FS engine.Fs // reference to the engine.Fs used to call the parse
Base string // base path (abs path with trailing slash)
Main []byte // contents of main entry mcl code
Files []string // files and dirs to copy to fs (abs paths)
Metadata *interfaces.Metadata
Workers []func(engine.Fs) error // copy files here that aren't listed!
}
@@ -214,6 +215,7 @@ func inputMetadata(s string, fs engine.Fs) (*ParsedInput, error) {
return nil, errwrap.Wrapf(err, "could not build metadata")
}
return &ParsedInput{
FS: fs,
Base: basePath,
Main: b,
Files: files,
@@ -261,6 +263,7 @@ func inputMcl(s string, fs engine.Fs) (*ParsedInput, error) {
},
}
return &ParsedInput{
FS: fs,
Base: dirify(filepath.Dir(s)), // base path with trailing slash
Main: b,
Files: []string{
@@ -363,6 +366,7 @@ func inputCode(s string, fs engine.Fs) (*ParsedInput, error) {
}
return &ParsedInput{
FS: fs,
Base: dirify(wd),
Main: b,
Files: []string{}, // they're already copied in

View File

@@ -374,10 +374,10 @@ func TestAstFunc1(t *testing.T) {
data := &interfaces.Data{
// TODO: add missing fields here if/when needed
Fs: fs,
FsURI: fs.URI(), // TODO: is this right?
Base: output.Base, // base dir (absolute path) the metadata file is in
Files: output.Files, // no really needed here afaict
Fs: output.FS, // formerly: fs
FsURI: output.FS.URI(), // formerly: fs.URI() // TODO: is this right?
Base: output.Base, // base dir (absolute path) the metadata file is in
Files: output.Files, // no really needed here afaict
Imports: importVertex,
Metadata: output.Metadata,
Modules: "/" + interfaces.ModuleDirectory, // not really needed here afaict
@@ -904,10 +904,10 @@ func TestAstFunc2(t *testing.T) {
data := &interfaces.Data{
// TODO: add missing fields here if/when needed
Fs: fs,
FsURI: "memmapfs:///", // we're in standalone mode
Base: output.Base, // base dir (absolute path) the metadata file is in
Files: output.Files, // no really needed here afaict
Fs: output.FS, // formerly: fs
FsURI: output.FS.URI(),
Base: output.Base, // base dir (absolute path) the metadata file is in
Files: output.Files, // no really needed here afaict
Imports: importVertex,
Metadata: output.Metadata,
Modules: "/" + interfaces.ModuleDirectory, // not really needed here afaict
@@ -1706,10 +1706,10 @@ func TestAstFunc3(t *testing.T) {
data := &interfaces.Data{
// TODO: add missing fields here if/when needed
Fs: fs,
FsURI: "memmapfs:///", // we're in standalone mode
Base: output.Base, // base dir (absolute path) the metadata file is in
Files: output.Files, // no really needed here afaict
Fs: output.FS, // formerly: fs
FsURI: output.FS.URI(),
Base: output.Base, // base dir (absolute path) the metadata file is in
Files: output.Files, // no really needed here afaict
Imports: importVertex,
Metadata: output.Metadata,
Modules: "/" + interfaces.ModuleDirectory, // not really needed here afaict

View File

@@ -139,9 +139,9 @@ func (obj *Lang) Init() error {
// init and validate the structure of the AST
data := &interfaces.Data{
// TODO: add missing fields here if/when needed
Fs: obj.Fs,
FsURI: obj.FsURI,
Base: output.Base, // base dir (absolute path) the metadata file is in
Fs: output.FS, // formerly: obj.Fs
FsURI: output.FS.URI(), // formerly: obj.FsURI
Base: output.Base, // base dir (absolute path) the metadata file is in
Files: output.Files,
Imports: importVertex,
Metadata: output.Metadata,