Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

classExt: support _[class].gox or .[class] #42

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions modfile/gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ project
doTestParseErr(t, `gop.mod:2: usage: project [.projExt ProjClass] classFilePkgPath ...`, `
project .gmx Game
`)
doTestParseErr(t, `gop.mod:2: ext . invalid: invalid ext format`, `
doTestParseErr(t, `gop.mod:2: ext ." invalid: unquoted string cannot contain quote`, `
project ." Game math
`)
doTestParseErr(t, `gop.mod:2: "." is not a valid package path`, `
project . Game math
`)
doTestParseErr(t, `gop.mod:2: symbol game invalid: invalid Go export symbol format`, `
Expand Down Expand Up @@ -330,16 +333,17 @@ unknown .spx
}

func doTestParseErr(t *testing.T, errMsg string, gopmod string) {
t.Run(errMsg, func(t *testing.T) {
_, err := Parse("gop.mod", []byte(gopmod), nil)
if err == nil || err.Error() == "" {
t.Fatal("Parse: no error?")
return
}
if errRet := errors.Summary(err); errRet != errMsg {
t.Error("Parse got:", errRet, "\nExpected:", errMsg)
}
})
t.Helper()
// t.Run(errMsg, func(t *testing.T) {
_, err := Parse("gop.mod", []byte(gopmod), nil)
if err == nil || err.Error() == "" {
t.Fatal("Parse: no error?")
return
}
if errRet := errors.Summary(err); errRet != errMsg {
t.Error("Parse got:", errRet, "\nExpected:", errMsg)
}
// })
}

// -----------------------------------------------------------------------------
37 changes: 29 additions & 8 deletions modfile/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Import struct {

// A Project is the project statement.
type Project struct {
Ext string // ".gmx"
Ext string // can be "_[class].gox" or ".[class]", eg "_yap.gox" or ".gmx"
Class string // "Game"
Works []*Class // work class of classfile
PkgPaths []string // package paths of classfile
Expand All @@ -72,7 +72,7 @@ type Project struct {

// A Class is the work class statement.
type Class struct {
Ext string // ".spx"
Ext string // can be "_[class].gox" or ".[class]", eg "_yap.gox" or ".spx"
Class string // "Sprite"
Syntax *Line
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func (f *File) parseVerb(errs *ErrorList, verb string, line *Line, args []string
errorf("usage: project [.projExt ProjClass] classFilePkgPath ...")
return
}
if strings.HasPrefix(args[0], ".") {
if isExt(args[0]) {
if len(args) < 3 || strings.Contains(args[1], "/") {
errorf("usage: project [.projExt ProjClass] classFilePkgPath ...")
return
Expand All @@ -202,19 +202,19 @@ func (f *File) parseVerb(errs *ErrorList, verb string, line *Line, args []string
wrapError(err)
return
}
pkgPaths, err := parseStrings(args[2:])
pkgPaths, err := parsePkgPaths(args[2:])
if err != nil {
errorf("invalid quoted string: %v", err)
wrapError(err)
return
}
f.addProj(&Project{
Ext: ext, Class: class, PkgPaths: pkgPaths, Syntax: line,
})
return
}
pkgPaths, err := parseStrings(args)
pkgPaths, err := parsePkgPaths(args)
if err != nil {
errorf("invalid quoted string: %v", err)
wrapError(err)
return
}
f.addProj(&Project{
Expand Down Expand Up @@ -324,12 +324,33 @@ func parseStrings(args []string) (arr []string, err error) {
return
}

func parsePkgPaths(args []string) (paths []string, err error) {
if paths, err = parseStrings(args); err != nil {
return nil, fmt.Errorf("invalid quoted string: %v", err)
}
for _, pkg := range paths {
if !isPkgPath(pkg) {
return nil, fmt.Errorf(`"%s" is not a valid package path`, pkg)
}
}
return
}

func isPkgPath(s string) bool {
return s != "" && (s[0] != '.' && s[0] != '_')
}

// can be "_[class].gox" or ".[class]"
func isExt(s string) bool {
return len(s) > 1 && (s[0] == '_' || s[0] == '.')
}

func parseExt(s *string) (t string, err error) {
t, err = parseString(s)
if err != nil {
goto failed
}
if len(t) > 1 && t[0] == '.' || t == "" {
if isExt(t) {
return
}
err = errors.New("invalid ext format")
Expand Down
6 changes: 3 additions & 3 deletions modfile/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ var addParseExtTests = []struct {
"",
},
{
"no ext",
"",
"",
"yap ok",
"_yap.gox",
"_yap.gox",
"",
},
{
Expand Down