Skip to content

Commit

Permalink
Merge pull request #42 from xushiwei/class
Browse files Browse the repository at this point in the history
classExt: support _[class].gox or .[class]
  • Loading branch information
xushiwei authored Jan 7, 2024
2 parents 206ed31 + 429ed29 commit 899d8f8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
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

0 comments on commit 899d8f8

Please sign in to comment.