|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "flag" |
4 | 5 | "fmt" |
| 6 | + "io" |
5 | 7 | "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
6 | 10 |
|
7 | | - "github.com/shopware/shopware-lsp/internal/php" |
| 11 | + tree_sitter_twig "github.com/shopware/shopware-lsp/internal/tree_sitter_grammars/twig/bindings/go" |
| 12 | + tree_sitter "github.com/tree-sitter/go-tree-sitter" |
| 13 | + tree_sitter_javascript "github.com/tree-sitter/tree-sitter-javascript/bindings/go" |
| 14 | + tree_sitter_json "github.com/tree-sitter/tree-sitter-json/bindings/go" |
| 15 | + tree_sitter_php "github.com/tree-sitter/tree-sitter-php/bindings/go" |
8 | 16 | ) |
9 | 17 |
|
10 | 18 | func main() { |
11 | | - if len(os.Args) < 2 { |
12 | | - fmt.Println("Usage: go run cmd/debug_ast/main.go <php_file_path>") |
| 19 | + lang := flag.String("lang", "", "Language to parse (php, js, twig, json). Auto-detected from extension if not specified.") |
| 20 | + flag.Parse() |
| 21 | + |
| 22 | + args := flag.Args() |
| 23 | + if len(args) < 1 { |
| 24 | + fmt.Println("Usage: go run cmd/debug_ast/main.go [-lang=php|js|twig|json] <file_path>") |
| 25 | + fmt.Println(" go run cmd/debug_ast/main.go [-lang=php|js|twig|json] - < input.txt") |
| 26 | + fmt.Println("") |
| 27 | + fmt.Println("Options:") |
| 28 | + fmt.Println(" -lang Language to parse (php, js, twig, json)") |
| 29 | + fmt.Println(" Auto-detected from file extension if not specified") |
| 30 | + fmt.Println("") |
| 31 | + fmt.Println("Examples:") |
| 32 | + fmt.Println(" go run cmd/debug_ast/main.go example.php") |
| 33 | + fmt.Println(" go run cmd/debug_ast/main.go -lang=js example.vue") |
| 34 | + fmt.Println(" echo \"this.\\$tc('key')\" | go run cmd/debug_ast/main.go -lang=js -") |
| 35 | + os.Exit(1) |
| 36 | + } |
| 37 | + |
| 38 | + filePath := args[0] |
| 39 | + |
| 40 | + var fileContent []byte |
| 41 | + var err error |
| 42 | + |
| 43 | + if filePath == "-" { |
| 44 | + // Read from stdin |
| 45 | + fileContent, err = io.ReadAll(os.Stdin) |
| 46 | + if err != nil { |
| 47 | + fmt.Printf("Error reading stdin: %v\n", err) |
| 48 | + os.Exit(1) |
| 49 | + } |
| 50 | + fmt.Println("Analyzing AST from stdin") |
| 51 | + } else { |
| 52 | + fileContent, err = os.ReadFile(filePath) |
| 53 | + if err != nil { |
| 54 | + fmt.Printf("Error reading file: %v\n", err) |
| 55 | + os.Exit(1) |
| 56 | + } |
| 57 | + fmt.Printf("Analyzing AST for file: %s\n\n", filePath) |
| 58 | + } |
| 59 | + |
| 60 | + // Determine language |
| 61 | + language := *lang |
| 62 | + if language == "" && filePath != "-" { |
| 63 | + language = detectLanguage(filePath) |
| 64 | + } |
| 65 | + |
| 66 | + if language == "" { |
| 67 | + fmt.Println("Error: Could not detect language. Please specify -lang flag.") |
| 68 | + os.Exit(1) |
| 69 | + } |
| 70 | + |
| 71 | + parser := tree_sitter.NewParser() |
| 72 | + defer parser.Close() |
| 73 | + |
| 74 | + var langErr error |
| 75 | + switch strings.ToLower(language) { |
| 76 | + case "php": |
| 77 | + langErr = parser.SetLanguage(tree_sitter.NewLanguage(tree_sitter_php.LanguagePHP())) |
| 78 | + case "js", "javascript": |
| 79 | + langErr = parser.SetLanguage(tree_sitter.NewLanguage(tree_sitter_javascript.Language())) |
| 80 | + case "twig": |
| 81 | + langErr = parser.SetLanguage(tree_sitter.NewLanguage(tree_sitter_twig.Language())) |
| 82 | + case "json": |
| 83 | + langErr = parser.SetLanguage(tree_sitter.NewLanguage(tree_sitter_json.Language())) |
| 84 | + default: |
| 85 | + fmt.Printf("Error: Unsupported language '%s'. Supported: php, js, twig, json\n", language) |
| 86 | + os.Exit(1) |
| 87 | + } |
| 88 | + |
| 89 | + if langErr != nil { |
| 90 | + fmt.Printf("Error setting language: %v\n", langErr) |
13 | 91 | os.Exit(1) |
14 | 92 | } |
15 | 93 |
|
16 | | - filePath := os.Args[1] |
17 | | - fmt.Printf("Analyzing AST for file: %s\n\n", filePath) |
| 94 | + tree := parser.Parse(fileContent, nil) |
| 95 | + if tree == nil { |
| 96 | + fmt.Println("Error: Failed to parse content") |
| 97 | + os.Exit(1) |
| 98 | + } |
| 99 | + defer tree.Close() |
| 100 | + |
| 101 | + fmt.Printf("Language: %s\n", language) |
| 102 | + fmt.Printf("Content:\n---\n%s\n---\n\n", string(fileContent)) |
| 103 | + |
| 104 | + printNodeStructure(tree.RootNode(), fileContent, 0) |
| 105 | +} |
18 | 106 |
|
19 | | - php.DebugAST(filePath) |
| 107 | +func detectLanguage(filePath string) string { |
| 108 | + ext := strings.ToLower(filepath.Ext(filePath)) |
| 109 | + switch ext { |
| 110 | + case ".php": |
| 111 | + return "php" |
| 112 | + case ".js", ".mjs", ".cjs": |
| 113 | + return "js" |
| 114 | + case ".twig": |
| 115 | + return "twig" |
| 116 | + case ".json": |
| 117 | + return "json" |
| 118 | + default: |
| 119 | + return "" |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func printNodeStructure(node *tree_sitter.Node, fileContent []byte, depth int) { |
| 124 | + if node == nil { |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + indent := "" |
| 129 | + for i := 0; i < depth; i++ { |
| 130 | + indent += " " |
| 131 | + } |
| 132 | + |
| 133 | + // Get position info |
| 134 | + startPos := node.StartPosition() |
| 135 | + endPos := node.EndPosition() |
| 136 | + |
| 137 | + nodeText := "" |
| 138 | + if node.NamedChildCount() == 0 { |
| 139 | + text := string(node.Utf8Text(fileContent)) |
| 140 | + // Truncate long text |
| 141 | + if len(text) > 50 { |
| 142 | + text = text[:47] + "..." |
| 143 | + } |
| 144 | + // Escape newlines for display |
| 145 | + text = strings.ReplaceAll(text, "\n", "\\n") |
| 146 | + text = strings.ReplaceAll(text, "\r", "\\r") |
| 147 | + text = strings.ReplaceAll(text, "\t", "\\t") |
| 148 | + nodeText = fmt.Sprintf(" = %q", text) |
| 149 | + } |
| 150 | + |
| 151 | + fmt.Printf("%s%s [%d:%d-%d:%d]%s\n", |
| 152 | + indent, |
| 153 | + node.Kind(), |
| 154 | + startPos.Row, startPos.Column, |
| 155 | + endPos.Row, endPos.Column, |
| 156 | + nodeText, |
| 157 | + ) |
| 158 | + |
| 159 | + // Recursively print child nodes |
| 160 | + for i := uint(0); i < node.NamedChildCount(); i++ { |
| 161 | + printNodeStructure(node.NamedChild(i), fileContent, depth+1) |
| 162 | + } |
20 | 163 | } |
0 commit comments