120 lines
4.0 KiB
Python
120 lines
4.0 KiB
Python
import tree_sitter_skill
|
|
from tree_sitter import Language, Parser
|
|
from lsprotocol.types import (
|
|
Diagnostic,
|
|
DiagnosticSeverity,
|
|
Range,
|
|
Position,
|
|
DocumentSymbol,
|
|
SymbolKind,
|
|
)
|
|
from pygls.workspace import TextDocument
|
|
from skillls.constants import ERROR_NODE_TYPES, IDENTIFIER_NODE_TYPES, SYMBOLIC_NODE_TYPES
|
|
|
|
class SkillParser:
|
|
"""
|
|
A Tree-sitter based parser for the Skill language.
|
|
Provides diagnostics and document symbols by traversing the Concrete Syntax Tree (CST).
|
|
"""
|
|
|
|
def __init__(self):
|
|
# Initialize the language and parser using tree_sitter_skill bindings
|
|
self.language = Language(tree_sitter_skill.language())
|
|
self.parser = Parser(self.language)
|
|
|
|
def parse_document(self, text_document: TextDocument) -> tuple[list[Diagnostic], list[DocumentSymbol]]:
|
|
"""
|
|
Parses the document content and returns both diagnostics (errors)
|
|
and a list of DocumentSymbols (outline).
|
|
"""
|
|
content = text_document.source
|
|
if not content:
|
|
return [], []
|
|
|
|
# Tree-sitter parsing
|
|
tree = self.parser.parse(bytes(content, "utf8"))
|
|
|
|
diagnostics: list[Diagnostic] = []
|
|
symbols: list[DocumentSymbol] = []
|
|
|
|
# Traverse the root node to collect errors and symbols
|
|
self._traverse_tree(tree.root_node, content, diagnostics, symbols)
|
|
|
|
return diagnostics, symbols
|
|
|
|
def _traverse_tree(
|
|
self,
|
|
root_node,
|
|
content: str,
|
|
diagnostics: list[Diagnostic],
|
|
symbols: list[DocumentSymbol]
|
|
) -> None:
|
|
"""Iteratively traverses the AST to find errors and symbols."""
|
|
stack = [root_node]
|
|
|
|
while stack:
|
|
node = stack.pop()
|
|
|
|
# 1. Handle Errors (Diagnostics)
|
|
if node.type in ERROR_NODE_TYPES:
|
|
start_point = node.start_point
|
|
end_point = node.end_point
|
|
|
|
diagnostics.append(
|
|
Diagnostic(
|
|
range=Range(
|
|
start=Position(start_point[0], start_point[1]),
|
|
end=Position(end_point[0], end_point[1])
|
|
),
|
|
message=f"Syntax error: unexpected {node.type} token",
|
|
severity=DiagnosticSeverity.Error,
|
|
)
|
|
)
|
|
|
|
# 2. Handle Symbols (Document Symbols / Outline)
|
|
if self._is_symbol_node(node):
|
|
symbol = self._create_document_symbol(node, content)
|
|
if symbol:
|
|
|
|
symbols.append(symbol)
|
|
|
|
# 3. Continue traversal - push children in reverse order to maintain original DFS order
|
|
for child in reversed(node.children):
|
|
stack.append(child)
|
|
|
|
def _is_symbol_node(self, node) -> bool:
|
|
"""Determines if a node is significant enough to be an outline symbol."""
|
|
return node.type in SYMBOLIC_NODE_TYPES or node.type.endswith("_def")
|
|
|
|
def _create_document_symbol(self, node, content: str) -> DocumentSymbol | None:
|
|
"""Extracts a name and range for an AST node to create an LSP symbol."""
|
|
name = None
|
|
for child in node.children:
|
|
if child.type in IDENTIFIER_NODE_TYPES:
|
|
start_byte = child.start_byte
|
|
end_byte = child.end_byte
|
|
name = content[start_byte:end_byte]
|
|
break
|
|
|
|
if not name:
|
|
name = node.type
|
|
|
|
# Ensure name is a string for DocumentSymbol
|
|
symbol_name = str(name)
|
|
|
|
start_pt = node.start_point
|
|
end_pt = node.end_point
|
|
|
|
return DocumentSymbol(
|
|
name=symbol_name,
|
|
kind=SymbolKind.Function,
|
|
range=Range(
|
|
start=Position(start_pt[0], start_pt[1]),
|
|
end=Position(end_pt[0], end_pt[1])
|
|
),
|
|
selection_range=Range(
|
|
start=Position(start_pt[0], start_pt[1]),
|
|
end=Position(end_pt[0], end_pt[1])
|
|
)
|
|
)
|