Add parsers module
This commit is contained in:
51
i18n_guardian/parsers/base.py
Normal file
51
i18n_guardian/parsers/base.py
Normal file
@@ -0,0 +1,51 @@
|
||||
"""Base parser interface."""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
class StringLiteral:
|
||||
"""Represents a string literal found in code."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
value: str,
|
||||
file_path: Path,
|
||||
line: int,
|
||||
column: int,
|
||||
end_line: Optional[int] = None,
|
||||
end_column: Optional[int] = None,
|
||||
is_template: bool = False,
|
||||
) -> None:
|
||||
self.value = value
|
||||
self.file_path = file_path
|
||||
self.line = line
|
||||
self.column = column
|
||||
self.end_line = end_line
|
||||
self.end_column = end_column
|
||||
self.is_template = is_template
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"StringLiteral({self.value!r}, {self.file_path}:{self.line})"
|
||||
|
||||
|
||||
class Parser(ABC):
|
||||
"""Base class for code parsers."""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def name(self) -> str:
|
||||
"""Parser name."""
|
||||
pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def extensions(self) -> List[str]:
|
||||
"""File extensions this parser handles."""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def parse(self, file_path: Path) -> List[StringLiteral]:
|
||||
"""Parse a file and extract string literals."""
|
||||
pass
|
||||
Reference in New Issue
Block a user