Initial commit: CodeMap v0.1.0 - CLI tool for code analysis and diagram generation
This commit is contained in:
85
codemap/parsers/go_parser.py
Normal file
85
codemap/parsers/go_parser.py
Normal 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
|
||||||
Reference in New Issue
Block a user