Add parsers module
Some checks failed
CI / build (push) Has been cancelled
CI / test (push) Has been cancelled

This commit is contained in:
2026-02-02 02:38:27 +00:00
parent 18a27a57f6
commit 15e580f23e

70
src/parsers/base.py Normal file
View File

@@ -0,0 +1,70 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import Optional
class EntityType(Enum):
FILE = "file"
FUNCTION = "function"
CLASS = "class"
METHOD = "method"
IMPORT = "import"
VARIABLE = "variable"
CONSTANT = "constant"
@dataclass
class Entity:
name: str
entity_type: EntityType
file_path: Path
start_line: int
end_line: int
code: str = ""
attributes: dict = field(default_factory=dict)
parent: Optional["Entity"] = None
children: list["Entity"] = field(default_factory=list)
imports: list[str] = field(default_factory=list)
calls: list[str] = field(default_factory=list)
@dataclass
class ParserResult:
file_path: Path
entities: list[Entity] = field(default_factory=list)
imports: list[str] = field(default_factory=list)
errors: list[str] = field(default_factory=list)
language: str = ""
class BaseParser(ABC):
SUPPORTED_EXTENSIONS: list[str] = []
@abstractmethod
def parse(self, file_path: Path, content: str) -> ParserResult:
pass
@abstractmethod
def extract_entities(self, tree, file_path: Path) -> list[Entity]:
pass
@abstractmethod
def extract_imports(self, tree, file_path: Path) -> list[str]:
pass
@abstractmethod
def extract_calls(self, tree, file_path: Path) -> list[str]:
pass
@classmethod
def can_parse(cls, file_path: Path) -> bool:
return file_path.suffix in cls.SUPPORTED_EXTENSIONS
@classmethod
def get_parser_for_file(cls, file_path: Path) -> Optional["BaseParser"]:
for parser_class in cls.__subclasses__():
if parser_class.can_parse(file_path):
return parser_class()
return None