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. // 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. // 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) { 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 { if err != nil {
return nil, errwrap.Wrapf(err, "could not activate an input parser") 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 // run recursion detection by checking for duplicates in the seen files
// TODO: do the paths need to be cleaned for "../", etc before compare? // TODO: do the paths need to be cleaned for "../", etc before compare?
//for _, name := range obj.data.Files { // existing seen files //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) // return nil, fmt.Errorf("recursive import of: `%s`", name)
// } // }
//} //}
reader := bytes.NewReader(output.Main) reader := bytes.NewReader(input.Main)
// nested logger // nested logger
logf := func(format string, v ...interface{}) { 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 // build new list of files
files := []string{} files := []string{}
files = append(files, output.Files...) files = append(files, input.Files...)
files = append(files, obj.data.Files...) files = append(files, obj.data.Files...)
// store a reference to the parent metadata // store a reference to the parent metadata
metadata := output.Metadata metadata := input.Metadata
metadata.Metadata = obj.data.Metadata metadata.Metadata = obj.data.Metadata
// now run the lexer/parser to do the import // 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 // init and validate the structure of the AST
data := &interfaces.Data{ data := &interfaces.Data{
// TODO: add missing fields here if/when needed // TODO: add missing fields here if/when needed
Fs: obj.data.Fs, Fs: input.FS, // formerly: obj.data.Fs,
FsURI: obj.data.FsURI, FsURI: input.FS.URI(), // formerly: obj.data.FsURI,
Base: output.Base, // new base dir (absolute path) Base: input.Base, // new base dir (absolute path)
Files: files, Files: files,
Imports: parentVertex, // the parent vertex that imported me Imports: parentVertex, // the parent vertex that imported me
Metadata: metadata, Metadata: metadata,
@@ -3593,7 +3593,7 @@ func (obj *StmtProg) importScopeWithInputs(s string, scope *interfaces.Scope, pa
obj.importProgs = append(obj.importProgs, prog) obj.importProgs = append(obj.importProgs, prog)
// collecting these here is more elegant (and possibly more efficient!) // 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 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 // init and validate the structure of the AST
data := &interfaces.Data{ data := &interfaces.Data{
// TODO: add missing fields here if/when needed // TODO: add missing fields here if/when needed
Fs: localFs, // the local fs! Fs: output.FS, // formerly: localFs // the local fs!
FsURI: localFs.URI(), // TODO: is this right? FsURI: output.FS.URI(), // formerly: localFs.URI() // TODO: is this right?
Base: output.Base, // base dir (absolute path) that this is rooted in Base: output.Base, // base dir (absolute path) that this is rooted in
Files: output.Files, Files: output.Files,
Imports: importVertex, Imports: importVertex,
Metadata: output.Metadata, Metadata: output.Metadata,
@@ -770,9 +770,9 @@ func (obj *GAPI) Get(getInfo *gapi.GetInfo) error {
// init and validate the structure of the AST // init and validate the structure of the AST
data := &interfaces.Data{ data := &interfaces.Data{
// TODO: add missing fields here if/when needed // TODO: add missing fields here if/when needed
Fs: localFs, // the local fs! Fs: output.FS, // formerly: localFs // the local fs!
FsURI: localFs.URI(), // TODO: is this right? FsURI: output.FS.URI(), // formerly: localFs.URI() // TODO: is this right?
Base: output.Base, // base dir (absolute path) that this is rooted in Base: output.Base, // base dir (absolute path) that this is rooted in
Files: output.Files, Files: output.Files,
Imports: importVertex, Imports: importVertex,
Metadata: output.Metadata, Metadata: output.Metadata,

View File

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

View File

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