コード例 #1
0
ファイル: main.go プロジェクト: DarthSim/party
func fixImportCheck(body []byte, importPath string) ([]byte, error) {
	fset := token.NewFileSet()
	// todo: see if we can restrict the mode some more
	f, err := parser.ParseFile(fset, "", body, parser.ParseComments)
	if err != nil {
		log.Fatal(err)
	}
	var after *ast.CommentGroup
	var pos token.Pos = token.Pos(len(body))
	for _, v := range f.Comments {
		text := strings.TrimSpace(v.Text())
		if v.Pos() > f.Package && v.Pos() < pos && strings.HasPrefix(text, "import") {
			pos = v.Pos()
			after = v
		}
	}
	if after != nil && bytes.IndexByte(body[f.Package:pos], '\n') == -1 {
		comment := fmt.Sprintf(`// import "%s"`, importPath)
		buf := new(bytes.Buffer)
		buf.Write(body[:after.Pos()-1])
		buf.WriteString(comment)
		buf.Write(body[after.End()-1:])
		body = buf.Bytes()
	}
	return body, nil
}
コード例 #2
0
ファイル: parse.go プロジェクト: josharian/debugo
func parseBreakpoint(fset *token.FileSet, filename string, cg *ast.CommentGroup) (Breakpoint, error) {
	var t Test
	bp := Breakpoint{Filename: filename, Line: fset.Position(cg.Pos()).Line}

	appendTest := func() {
		if t.Debugger != "" {
			bp.Tests = append(bp.Tests, t)
		}
	}

	for _, comment := range cg.List[1:] {
		line := strings.TrimSpace(comment.Text)
		lineno := fset.Position(comment.Pos()).Line

		if !strings.HasPrefix(line, "// ") {
			continue
		}
		line = strings.TrimSpace(line[len("//"):])

		// Check whether this is a new test. If so,
		// save the previous test and start a new one.
		switch {
		case strings.HasPrefix(line, "(gdb) "):
			appendTest()
			t = Test{
				Debugger: "gdb",
				Command:  strings.TrimSpace(line[len("(gdb)"):]),
				Line:     lineno,
			}
			continue
		case strings.HasPrefix(line, "(lldb) "):
			appendTest()
			t = Test{
				Debugger: "lldb",
				Command:  strings.TrimSpace(line[len("(lldb)"):]),
				Line:     lineno,
			}
			continue
		}

		// Not a new test; must be a Want from the current test.

		if t.Debugger == "" {
			// Oops, no current test
			return bp, fmt.Errorf("%s:%d expected a (gdb) or (lldb) command", filename, lineno)
		}

		t.Want = append(t.Want, line)
	}

	// Save the last test.
	appendTest()
	return bp, nil
}