From ae523d738953b3f509134f007ef7daa7ec055c26 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Fri, 30 Jan 2026 12:19:02 +0000 Subject: [PATCH] Initial commit: CodeMap v0.1.0 - CLI tool for code analysis and diagram generation --- codemap/parsers/go_parser.py | 85 ++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 codemap/parsers/go_parser.py diff --git a/codemap/parsers/go_parser.py b/codemap/parsers/go_parser.py new file mode 100644 index 0000000..8a430b1 --- /dev/null +++ b/codemap/parsers/go_parser.py @@ -0,0 +1,85 @@ +import re +from pathlib import Path +from typing import List +from codemap.parsers.base import BaseParser, Dependency, ParsedFile + + +class GoParser(BaseParser): + supported_extensions = [".go"] + + IMPORT_PATTERN = re.compile(r'^\s*import\s*\(?\s*(?:"([^"]+)"|`([^`]+)`)\s*\)?') + ALIASED_IMPORT_PATTERN = re.compile(r'(\w+)\s*[""]([^""]+)[""]') + MULTI_IMPORT_PATTERN = re.compile(r'^\s*import\s*\(\s*$') + + def parse(self, file_path: Path) -> ParsedFile: + module_name = self.extract_module_name(file_path) + dependencies: List[Dependency] = [] + + try: + with open(file_path, "r", encoding="utf-8") as f: + content = f.read() + except (UnicodeDecodeError, OSError) as e: + return ParsedFile( + file_path=file_path, + module_name=module_name, + dependencies=[], + file_type="go" + ) + + lines = content.split('\n') + in_import_block = False + import_start_line = 0 + + for line_num, line in enumerate(lines, 1): + stripped = line.strip() + + if self.MULTI_IMPORT_PATTERN.match(stripped): + in_import_block = True + import_start_line = line_num + continue + + if in_import_block: + if stripped == ')': + in_import_block = False + continue + + alias_match = self.ALIASED_IMPORT_PATTERN.match(stripped) + if alias_match: + dep = Dependency( + module_name=alias_match.group(2), + file_path=file_path, + line_number=line_num, + alias=alias_match.group(1) + ) + dependencies.append(dep) + else: + match = self.IMPORT_PATTERN.match(stripped) + if match: + import_path = match.group(1) or match.group(2) + dep = Dependency( + module_name=import_path, + file_path=file_path, + line_number=line_num + ) + dependencies.append(dep) + else: + match = self.IMPORT_PATTERN.match(stripped) + if match: + import_path = match.group(1) or match.group(2) + if import_path: + dep = Dependency( + module_name=import_path, + file_path=file_path, + line_number=line_num + ) + dependencies.append(dep) + + return ParsedFile( + file_path=file_path, + module_name=module_name, + dependencies=dependencies, + file_type="go" + ) + + def extract_module_name(self, file_path: Path) -> str: + return file_path.stem