Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /Consiliency_treesitter-chunker /chunker /interfaces /grammar.py
| """Grammar management interfaces. | |
| Interfaces for managing Tree-sitter language grammars, | |
| including fetching, building, and versioning. | |
| """ | |
| from abc import ABC, abstractmethod | |
| from dataclasses import dataclass | |
| from enum import Enum | |
| from pathlib import Path | |
| class GrammarStatus(Enum): | |
| """Status of a grammar.""" | |
| NOT_FOUND = "not_found" | |
| NOT_BUILT = "not_built" | |
| BUILDING = "building" | |
| READY = "ready" | |
| ERROR = "error" | |
| OUTDATED = "outdated" | |
| class GrammarInfo: | |
| """Information about a language grammar. | |
| Attributes: | |
| name: Language name (e.g., 'python', 'javascript') | |
| repository_url: Git repository URL | |
| commit_hash: Specific commit to use | |
| abi_version: Tree-sitter ABI version required | |
| status: Current status of the grammar | |
| path: Path to compiled grammar file | |
| error: Error message if status is ERROR | |
| """ | |
| name: str | |
| repository_url: str | |
| commit_hash: str | None = None | |
| abi_version: int | None = None | |
| status: GrammarStatus = GrammarStatus.NOT_FOUND | |
| path: Path | None = None | |
| error: str | None = None | |
| def is_available(self) -> bool: | |
| """Check if grammar is ready to use.""" | |
| return self.status == GrammarStatus.READY | |
| class NodeTypeInfo: | |
| """Information about a node type in a grammar. | |
| Attributes: | |
| name: Node type name (e.g., 'function_definition') | |
| is_named: Whether this is a named node type | |
| has_children: Whether this node type can have children | |
| fields: List of field names this node type has | |
| supertypes: Parent types in the grammar hierarchy | |
| """ | |
| name: str | |
| is_named: bool | |
| has_children: bool | |
| fields: list[str] | |
| supertypes: list[str] | |
| class GrammarManager(ABC): | |
| """Manages Tree-sitter language grammars.""" | |
| def add_grammar( | |
| name: str, | |
| repository_url: str, | |
| commit_hash: str | None = None, | |
| ) -> GrammarInfo: | |
| """Add a new grammar to manage. | |
| Args: | |
| name: Language name | |
| repository_url: Git repository URL | |
| commit_hash: Specific commit (None for latest) | |
| Returns: | |
| Grammar information | |
| """ | |
| def fetch_grammar(name: str) -> bool: | |
| """Fetch grammar source from repository. | |
| Args: | |
| name: Language name | |
| Returns: | |
| True if successful | |
| """ | |
| def build_grammar(name: str) -> bool: | |
| """Build grammar from source. | |
| Args: | |
| name: Language name | |
| Returns: | |
| True if successful | |
| """ | |
| def get_grammar_info(name: str) -> GrammarInfo | None: | |
| """Get information about a grammar. | |
| Args: | |
| name: Language name | |
| Returns: | |
| Grammar info or None if not found | |
| """ | |
| def list_grammars(status: GrammarStatus | None = None) -> list[GrammarInfo]: | |
| """List all managed grammars. | |
| Args: | |
| status: Filter by status (None for all) | |
| Returns: | |
| List of grammar information | |
| """ | |
| def update_grammar(name: str) -> bool: | |
| """Update grammar to latest version. | |
| Args: | |
| name: Language name | |
| Returns: | |
| True if updated | |
| """ | |
| def remove_grammar(name: str) -> bool: | |
| """Remove a grammar. | |
| Args: | |
| name: Language name | |
| Returns: | |
| True if removed | |
| """ | |
| def get_node_types(language: str) -> list[NodeTypeInfo]: | |
| """Get all node types for a language. | |
| Args: | |
| language: Language name | |
| Returns: | |
| List of node type information | |
| """ | |
| def validate_grammar(name: str) -> tuple[bool, str | None]: | |
| """Validate a grammar is working correctly. | |
| Args: | |
| name: Language name | |
| Returns: | |
| Tuple of (is_valid, error_message) | |
| """ | |
| class GrammarBuilder(ABC): | |
| """Builds Tree-sitter grammars from source.""" | |
| def set_build_directory(path: Path) -> None: | |
| """Set directory for build output. | |
| Args: | |
| path: Build output directory | |
| """ | |
| def set_source_directory(path: Path) -> None: | |
| """Set directory containing grammar sources. | |
| Args: | |
| path: Source directory | |
| """ | |
| def build(languages: list[str]) -> dict[str, bool]: | |
| """Build specified languages. | |
| Args: | |
| languages: List of language names | |
| Returns: | |
| Dictionary mapping language to build success | |
| """ | |
| def clean(language: str | None = None) -> None: | |
| """Clean build artifacts. | |
| Args: | |
| language: Specific language (None for all) | |
| """ | |
| def get_build_log(language: str) -> str | None: | |
| """Get build log for a language. | |
| Args: | |
| language: Language name | |
| Returns: | |
| Build log or None | |
| """ | |
| class GrammarRepository(ABC): | |
| """Repository of known grammar sources.""" | |
| def search(query: str) -> list[GrammarInfo]: | |
| """Search for grammars. | |
| Args: | |
| query: Search query | |
| Returns: | |
| List of matching grammars | |
| """ | |
| def get_popular_grammars(limit: int = 20) -> list[GrammarInfo]: | |
| """Get most popular grammars. | |
| Args: | |
| limit: Maximum number to return | |
| Returns: | |
| List of popular grammars | |
| """ | |
| def get_grammar_by_extension(extension: str) -> GrammarInfo | None: | |
| """Find grammar for a file extension. | |
| Args: | |
| extension: File extension (e.g., '.py') | |
| Returns: | |
| Grammar info or None | |
| """ | |
| def refresh_repository() -> bool: | |
| """Refresh repository data. | |
| Returns: | |
| True if successful | |
| """ | |
| class GrammarValidator(ABC): | |
| """Validates grammar compatibility and correctness.""" | |
| def check_abi_compatibility(grammar_path: Path) -> tuple[bool, str | None]: | |
| """Check if grammar ABI is compatible. | |
| Args: | |
| grammar_path: Path to compiled grammar | |
| Returns: | |
| Tuple of (is_compatible, error_message) | |
| """ | |
| def validate_node_types(language: str, expected_types: set[str]) -> list[str]: | |
| """Validate expected node types exist. | |
| Args: | |
| language: Language name | |
| expected_types: Set of expected node type names | |
| Returns: | |
| List of missing node types | |
| """ | |
| def test_parse(language: str, sample_code: str) -> tuple[bool, str | None]: | |
| """Test parsing with sample code. | |
| Args: | |
| language: Language name | |
| sample_code: Sample code to parse | |
| Returns: | |
| Tuple of (success, error_message) | |
| """ | |
Xet Storage Details
- Size:
- 7.65 kB
- Xet hash:
- 49cdb29c5b8fd49eb62e3999f28a46ecd4b27b0c011caecde9de821ec017a254
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.