@@ -95,6 +95,12 @@ func (conv *converter) ConvertFile(f *ast.File) *ir.File {
95
95
conv .dslPkgname = imp .Name .Name
96
96
}
97
97
}
98
+ // Right now this list is hardcoded from the knowledge of which
99
+ // stdlib packages are supported inside the bytecode.
100
+ switch importPath {
101
+ case "fmt" , "strings" , "strconv" :
102
+ conv .addCustomImport (result , importPath )
103
+ }
98
104
}
99
105
100
106
for _ , decl := range f .Decls {
@@ -161,6 +167,10 @@ func (conv *converter) convertInitFunc(dst *ir.File, decl *ast.FuncDecl) {
161
167
}
162
168
}
163
169
170
+ func (conv * converter ) addCustomImport (dst * ir.File , pkgPath string ) {
171
+ dst .CustomDecls = append (dst .CustomDecls , `import "` + pkgPath + `"` )
172
+ }
173
+
164
174
func (conv * converter ) addCustomDecl (dst * ir.File , decl ast.Decl ) {
165
175
begin := conv .fset .Position (decl .Pos ())
166
176
end := conv .fset .Position (decl .End ())
@@ -436,6 +446,7 @@ func (conv *converter) convertRuleExpr(call *ast.CallExpr) {
436
446
suggestArgs * []ast.Expr
437
447
reportArgs * []ast.Expr
438
448
atArgs * []ast.Expr
449
+ doArgs * []ast.Expr
439
450
)
440
451
441
452
for {
@@ -475,6 +486,8 @@ func (conv *converter) convertRuleExpr(call *ast.CallExpr) {
475
486
panic (conv .errorf (chain .Sel , "Report() can't be repeated" ))
476
487
}
477
488
reportArgs = & call .Args
489
+ case "Do" :
490
+ doArgs = & call .Args
478
491
case "At" :
479
492
if atArgs != nil {
480
493
panic (conv .errorf (chain .Sel , "At() can't be repeated" ))
@@ -527,13 +540,27 @@ func (conv *converter) convertRuleExpr(call *ast.CallExpr) {
527
540
rule .SuggestTemplate = conv .parseStringArg ((* suggestArgs )[0 ])
528
541
}
529
542
530
- if suggestArgs == nil && reportArgs == nil {
531
- panic (conv .errorf (origCall , "missing Report() or Suggest () call" ))
543
+ if suggestArgs == nil && reportArgs == nil && doArgs == nil {
544
+ panic (conv .errorf (origCall , "missing Report(), Suggest() or Do () call" ))
532
545
}
533
- if reportArgs == nil {
534
- rule .ReportTemplate = "suggestion: " + rule .SuggestTemplate
546
+ if doArgs != nil {
547
+ if suggestArgs != nil || reportArgs != nil {
548
+ panic (conv .errorf (origCall , "can't combine Report/Suggest with Do yet" ))
549
+ }
550
+ if matchCommentArgs != nil {
551
+ panic (conv .errorf (origCall , "can't use Do() with MatchComment() yet" ))
552
+ }
553
+ funcName , ok := (* doArgs )[0 ].(* ast.Ident )
554
+ if ! ok {
555
+ panic (conv .errorf ((* doArgs )[0 ], "only named function args are supported" ))
556
+ }
557
+ rule .DoFuncName = funcName .String ()
535
558
} else {
536
- rule .ReportTemplate = conv .parseStringArg ((* reportArgs )[0 ])
559
+ if reportArgs == nil {
560
+ rule .ReportTemplate = "suggestion: " + rule .SuggestTemplate
561
+ } else {
562
+ rule .ReportTemplate = conv .parseStringArg ((* reportArgs )[0 ])
563
+ }
537
564
}
538
565
539
566
for i , alt := range alternatives {
0 commit comments