Skip to content

Commit d2b6c84

Browse files
authored
Merge pull request #101 from xushiwei/q
modfile parseExt: Class, Project add FullExt
2 parents c9e677a + bee3121 commit d2b6c84

File tree

4 files changed

+76
-28
lines changed

4 files changed

+76
-28
lines changed

modfile/ext.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,34 @@ import (
2424
"github.com/qiniu/x/errors"
2525
)
2626

27-
// can be "_[class].gox" or ".[class]"
28-
func isExt(s string) bool {
29-
return len(s) > 1 && (s[0] == '_' || s[0] == '.')
27+
// can be "_[class].gox", "*_[class].gox", ".[class]" or "*.[class]"
28+
// can be "main_[class].gox", "main.[class]" if isProj is true
29+
func isExt(s string, isProj bool) bool {
30+
return len(s) > 1 && (s[0] == '*' || s[0] == '_' || s[0] == '.') ||
31+
isProj && len(s) > 4 && s[:4] == "main" && (s[4] == '_' || s[4] == '.')
3032
}
3133

32-
func parseExt(s *string) (t string, err error) {
33-
t, err = parseString(s)
34+
func getExt(s string) string {
35+
if len(s) > 1 && s[0] == '*' {
36+
return s[1:]
37+
}
38+
if len(s) > 4 && s[:4] == "main" {
39+
return s[4:]
40+
}
41+
return s
42+
}
43+
44+
func parseExt(s *string, isProj bool) (ext, fullExt string, err error) {
45+
t, err := parseString(s)
3446
if err != nil {
3547
goto failed
3648
}
37-
if isExt(t) {
38-
return
49+
if isExt(t, isProj) {
50+
return getExt(t), t, nil
3951
}
4052
err = errors.New("invalid ext format")
4153
failed:
42-
return "", &InvalidExtError{
54+
return "", "", &InvalidExtError{
4355
Ext: *s,
4456
Err: err,
4557
}

modfile/gop_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ gop 1.1 1.2
269269
doTestParseErr(t, `gop.mod:2: invalid gop version '1.x': must match format 1.23`, `
270270
gop 1.x
271271
`)
272-
doTestParseErr(t, `gop.mod:2: usage: project [.projExt ProjClass] classFilePkgPath ...`, `
272+
doTestParseErr(t, `gop.mod:2: usage: project [*.projExt ProjClass] classFilePkgPath ...`, `
273273
project
274274
`)
275-
doTestParseErr(t, `gop.mod:2: usage: project [.projExt ProjClass] classFilePkgPath ...`, `
275+
doTestParseErr(t, `gop.mod:2: usage: project [*.projExt ProjClass] classFilePkgPath ...`, `
276276
project .gmx Game
277277
`)
278278
doTestParseErr(t, `gop.mod:2: ext ." invalid: unquoted string cannot contain quote`, `

modfile/rule.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ type Gop = modfile.Go
5959

6060
// A Class is the work class statement.
6161
type Class struct {
62-
Ext string // can be "_[class].gox" or ".[class]", eg. "_yap.gox" or ".spx"
63-
Class string // "Sprite"
64-
Proto string // prototype of the work class (not empty if multiple work classes)
65-
Syntax *Line
62+
Ext string // can be "_[class].gox" or ".[class]", eg. "_yap.gox" or ".spx"
63+
FullExt string // can be "*_[class].gox", "_[class].gox", "*.[class]" or ".[class]"
64+
Class string // "Sprite"
65+
Proto string // prototype of the work class (not empty if multiple work classes)
66+
Embedded bool // if true, the class instance will be embedded in the project
67+
Syntax *Line
6668
}
6769

6870
// A Import is the import statement.
@@ -75,6 +77,7 @@ type Import struct {
7577
// A Project is the project statement.
7678
type Project struct {
7779
Ext string // can be "_[class].gox" or ".[class]", eg. "_yap.gox" or ".gmx"
80+
FullExt string // can be "main_[class].gox", "*_[class].gox", "_[class].gox", "main.[class]", "*.[class]" or ".[class]"
7881
Class string // "Game"
7982
Works []*Class // work class of classfile
8083
PkgPaths []string // package paths of classfile and optional inline-imported packages.
@@ -199,15 +202,15 @@ func (f *File) parseVerb(errs *ErrorList, verb string, line *Line, args []string
199202
f.Gop.Version = args[0]
200203
case "project":
201204
if len(args) < 1 {
202-
errorf("usage: project [.projExt ProjClass] classFilePkgPath ...")
205+
errorf("usage: project [*.projExt ProjClass] classFilePkgPath ...")
203206
return
204207
}
205-
if isExt(args[0]) {
208+
if isExt(args[0], true) {
206209
if len(args) < 3 || strings.Contains(args[1], "/") {
207-
errorf("usage: project [.projExt ProjClass] classFilePkgPath ...")
210+
errorf("usage: project [*.projExt ProjClass] classFilePkgPath ...")
208211
return
209212
}
210-
ext, err := parseExt(&args[0])
213+
ext, fullExt, err := parseExt(&args[0], true)
211214
if err != nil {
212215
wrapError(err)
213216
return
@@ -223,7 +226,7 @@ func (f *File) parseVerb(errs *ErrorList, verb string, line *Line, args []string
223226
return
224227
}
225228
f.addProj(&Project{
226-
Ext: ext, Class: class, PkgPaths: pkgPaths, Syntax: line,
229+
Ext: ext, FullExt: fullExt, Class: class, PkgPaths: pkgPaths, Syntax: line,
227230
})
228231
return
229232
}
@@ -245,7 +248,7 @@ func (f *File) parseVerb(errs *ErrorList, verb string, line *Line, args []string
245248
errorf("usage: class .workExt WorkClass [ProjClass]")
246249
return
247250
}
248-
workExt, err := parseExt(&args[0])
251+
workExt, fullExt, err := parseExt(&args[0], false)
249252
if err != nil {
250253
wrapError(err)
251254
return
@@ -264,10 +267,11 @@ func (f *File) parseVerb(errs *ErrorList, verb string, line *Line, args []string
264267
}
265268
}
266269
proj.Works = append(proj.Works, &Class{
267-
Ext: workExt,
268-
Class: class,
269-
Proto: protoClass,
270-
Syntax: line,
270+
Ext: workExt,
271+
FullExt: fullExt,
272+
Class: class,
273+
Proto: protoClass,
274+
Syntax: line,
271275
})
272276
case "import":
273277
proj := f.proj()
@@ -329,7 +333,7 @@ func AutoQuote(s string) string {
329333
}
330334

331335
var (
332-
symbolRE = regexp.MustCompile("\\*?[A-Z]\\w*")
336+
symbolRE = regexp.MustCompile(`\*?[A-Z]\w*`)
333337
)
334338

335339
// TODO: to be optimized

modfile/rule_test.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,57 @@ var addParseExtTests = []struct {
2626
desc string
2727
ext string
2828
want string
29+
wantF string
2930
wantErr string
31+
isProj bool
3032
}{
3133
{
3234
"spx ok",
3335
".spx",
3436
".spx",
37+
".spx",
3538
"",
39+
false,
3640
},
3741
{
3842
"yap ok",
3943
"_yap.gox",
4044
"_yap.gox",
45+
"_yap.gox",
46+
"",
47+
false,
48+
},
49+
{
50+
"yap ok",
51+
"*_yap.gox",
52+
"_yap.gox",
53+
"*_yap.gox",
54+
"",
55+
false,
56+
},
57+
{
58+
"yap ok",
59+
"main_yap.gox",
60+
"_yap.gox",
61+
"main_yap.gox",
4162
"",
63+
true,
64+
},
65+
{
66+
"yap ok",
67+
"main_yap.gox",
68+
"",
69+
"",
70+
"ext main_yap.gox invalid: invalid ext format",
71+
false,
4272
},
4373
{
4474
"not a ext",
4575
"gmx",
4676
"",
77+
"",
4778
"ext gmx invalid: invalid ext format",
79+
false,
4880
},
4981
}
5082

@@ -57,14 +89,14 @@ func TestParseExt(t *testing.T) {
5789
}
5890
for _, tt := range addParseExtTests {
5991
t.Run(tt.desc, func(t *testing.T) {
60-
ext, err := parseExt(&tt.ext)
92+
ext, extF, err := parseExt(&tt.ext, tt.isProj)
6193
if err != nil {
6294
if err.Error() != tt.wantErr {
6395
t.Fatalf("wanterr: %s, but got: %s", tt.wantErr, err)
6496
}
6597
}
66-
if ext != tt.want {
67-
t.Fatalf("want: %s, but got: %s", tt.want, ext)
98+
if ext != tt.want || extF != tt.wantF {
99+
t.Fatalf("want: %s %s, but got: %s %s", tt.want, tt.wantF, ext, extF)
68100
}
69101
})
70102
}

0 commit comments

Comments
 (0)