瀏覽代碼

feat(parser): add kubernetes v1.27.4

iwanhae 1 年之前
父節點
當前提交
e41f32062d
共有 6 個文件被更改,包括 200 次插入0 次删除
  1. 3 0
      .gitmodules
  2. 50 0
      data/kubernetes_v1.27.4.jsonl
  3. 2 0
      parser/Makefile
  4. 3 0
      parser/go.mod
  5. 102 0
      parser/main.go
  6. 40 0
      parser/types.go

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
+[submodule "parser/kubernetes"]
+	path = parser/kubernetes
+	url = https://github.com/kubernetes/kubernetes.git

File diff suppressed because it is too large
+ 50 - 0
data/kubernetes_v1.27.4.jsonl


+ 2 - 0
parser/Makefile

@@ -0,0 +1,2 @@
+run:
+	go run . > ../data/kubernetes_v1.27.4.jsonl

+ 3 - 0
parser/go.mod

@@ -0,0 +1,3 @@
+module github.com/iwanhae/kuberian/parser
+
+go 1.20

+ 102 - 0
parser/main.go

@@ -0,0 +1,102 @@
+package main
+
+import (
+	"encoding/json"
+	"go/ast"
+	"go/parser"
+	"go/token"
+	"io/fs"
+	"log"
+	"os"
+	"path"
+	"path/filepath"
+	"sort"
+	"strings"
+)
+
+func main() {
+	results := Functions{}
+
+	// Parse
+	filepath.Walk("./kubernetes", func(file string, info fs.FileInfo, err error) error {
+		if err != nil {
+			return err
+		}
+
+		if info.IsDir() {
+			return nil
+		}
+		if path.Ext(file) != ".go" {
+			return nil
+		}
+		for _, s := range []string{
+			"fake",         // test code
+			"test",         // test code
+			"zz_generated", // generated code
+			"generated",    // generated code
+			"api.pb.go",    // generated code
+			"vendor",       // external libs
+			"third_party",  // external libs
+			"sample",       // example code
+			"example",
+		} {
+			if strings.Contains(file, s) {
+				return nil
+			}
+		}
+		results = append(results, parseSrc(file)...)
+		return nil
+	})
+
+	// Sort
+	sort.Sort(results)
+
+	// Print
+	repoId := 1 /* id of the kubernetes/kubernetes@v1.27.4 */
+	id := repoId * 100_000_000
+	for _, f := range results {
+		f.ID = uint64(id)
+		id += 1
+		json.NewEncoder(os.Stdout).Encode(f)
+	}
+}
+
+func parseSrc(filename string) []Function {
+	s, err := os.ReadFile(filename)
+	if err != nil {
+		panic(err) // there will be no err
+	}
+	lines := strings.Split(string(s), "\n")
+
+	f := []Function{}
+	fset := token.NewFileSet()
+	node, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
+	if err != nil {
+		log.Fatalf("Error parsing file: %v", err)
+	}
+
+	for _, decl := range node.Decls {
+		switch d := decl.(type) {
+		case *ast.FuncDecl:
+			start := fset.Position(d.Pos()).Line
+			if d.Doc != nil {
+				start = fset.Position(d.Doc.Pos()).Line
+			}
+			end := fset.Position(d.End()).Line
+			name_pos := fset.Position(d.Name.Pos()).Line
+			name, _ := strings.CutSuffix(lines[name_pos-1], " {")
+
+			f = append(f, Function{
+				Name: name,
+				Line: FunctionPos{
+					From: start,
+					To:   end,
+				},
+				File: filename[11:],
+				Code: strings.Join(lines[start-1:end], "\n"),
+			})
+		}
+	}
+
+	return f
+}

+ 40 - 0
parser/types.go

@@ -0,0 +1,40 @@
+package main
+
+import "sort"
+
+type Functions []Function
+
+var _ sort.Interface = Functions{}
+
+// Len implements sort.Interface.
+func (f Functions) Len() int {
+	return len(f)
+}
+
+// Less implements sort.Interface.
+func (f Functions) Less(i int, j int) bool {
+	if f[i].File < f[j].File {
+		return true
+	} else if f[i].File > f[j].File {
+		return false
+	}
+	return f[i].Line.From < f[j].Line.From
+}
+
+// Swap implements sort.Interface.
+func (f Functions) Swap(i int, j int) {
+	f[i], f[j] = f[j], f[i]
+}
+
+type Function struct {
+	ID   uint64      `json:"id"`
+	Name string      `json:"name"`
+	File string      `json:"file"`
+	Code string      `json:"code"`
+	Line FunctionPos `json:"line"`
+}
+
+type FunctionPos struct {
+	From int `json:"from"`
+	To   int `json:"to"`
+}

Some files were not shown because too many files changed in this diff