Initial commit: CodeMap v0.1.0 - CLI tool for code analysis and diagram generation
Some checks failed
CI / test (push) Has been cancelled
CI / build (push) Has been cancelled

This commit is contained in:
2026-01-30 12:19:02 +00:00
parent c04e75f631
commit ae523d7389

View File

@@ -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