add cache, try parsing using pygls
This commit is contained in:
+38
-49
@@ -1,40 +1,38 @@
|
||||
from logging import INFO, basicConfig, getLogger
|
||||
from pathlib import Path
|
||||
from urllib.parse import unquote
|
||||
from lsprotocol.types import (
|
||||
TEXT_DOCUMENT_DOCUMENT_SYMBOL,
|
||||
CompletionItem,
|
||||
Diagnostic,
|
||||
DiagnosticSeverity,
|
||||
DocumentSymbol,
|
||||
DocumentSymbolParams,
|
||||
Position,
|
||||
Range,
|
||||
SymbolKind,
|
||||
)
|
||||
|
||||
from pygls.server import LanguageServer
|
||||
from parsimonious import Grammar
|
||||
from pygls.uris import urlparse
|
||||
|
||||
# from skillls.parsing.location import Range
|
||||
from parsimonious import Grammar, IncompleteParseError
|
||||
|
||||
from .cache import Cache
|
||||
from .parsing.tokenize import Locator, SkillVisitor
|
||||
|
||||
|
||||
example = """
|
||||
(skillist siomfpwqmqwepfomkjnbkjb
|
||||
'(rawlist token)
|
||||
clist(qwerfwf)
|
||||
)
|
||||
"""
|
||||
URI = str
|
||||
|
||||
cache = {}
|
||||
basicConfig(filename="skillls.log", level=INFO)
|
||||
cache: Cache[str, CompletionItem] = Cache()
|
||||
|
||||
logger = getLogger(__name__)
|
||||
server = LanguageServer("skillls", "v0.1")
|
||||
|
||||
|
||||
def parse(path: Path):
|
||||
# path = Path(__file__).parent / "grammar.peg"
|
||||
def parse(content: str):
|
||||
path = Path(__file__).parent / "grammar.peg"
|
||||
grammar = Grammar(path.read_text())
|
||||
|
||||
locator = Locator(example)
|
||||
tree = grammar.parse(example)
|
||||
locator = Locator(content)
|
||||
tree = grammar.parse(content)
|
||||
|
||||
iv = SkillVisitor(locator)
|
||||
output = iv.visit(tree)
|
||||
@@ -42,42 +40,33 @@ def parse(path: Path):
|
||||
return output
|
||||
|
||||
|
||||
def parse_and_cache(uri: str) -> list[DocumentSymbol]:
|
||||
path = Path(unquote(urlparse(uri).path))
|
||||
if not path.exists():
|
||||
logger.error("could not find %s", path)
|
||||
return []
|
||||
|
||||
if not cache.get(path):
|
||||
logger.info("%s not yet cached, parsing...")
|
||||
out = parse(path)
|
||||
logger.info("%s", out)
|
||||
|
||||
return []
|
||||
|
||||
|
||||
basicConfig(filename="skillls.log", level=INFO)
|
||||
|
||||
logger = getLogger(__name__)
|
||||
server = LanguageServer("skillls", "v0.1")
|
||||
|
||||
|
||||
@server.feature(TEXT_DOCUMENT_DOCUMENT_SYMBOL)
|
||||
def document_symbols(params: DocumentSymbolParams) -> list[DocumentSymbol]:
|
||||
logger.info("requested document symbols for %s", params.text_document.uri)
|
||||
doc = server.workspace.documents[params.text_document.uri]
|
||||
return [
|
||||
DocumentSymbol(
|
||||
"~global_scope",
|
||||
kind=SymbolKind.Namespace,
|
||||
range=Range(
|
||||
start=Position(0, 0),
|
||||
end=Position(len(doc.lines) - 1, len(doc.lines[-1])),
|
||||
),
|
||||
selection_range=Range(Position(0, 0), Position(0, 0)),
|
||||
logger.warning("requested document symbols for %s", params.text_document.uri)
|
||||
doc = server.workspace.get_text_document(params.text_document.uri)
|
||||
try:
|
||||
logger.warning(parse(doc.source))
|
||||
except IncompleteParseError as e:
|
||||
server.publish_diagnostics(
|
||||
params.text_document.uri,
|
||||
[
|
||||
Diagnostic(
|
||||
Range(
|
||||
Position(e.line() - 1, e.column() - 1),
|
||||
Position(len(doc.lines), 0),
|
||||
),
|
||||
str(e),
|
||||
severity=DiagnosticSeverity.Error,
|
||||
)
|
||||
],
|
||||
)
|
||||
]
|
||||
return []
|
||||
|
||||
|
||||
def main():
|
||||
server.start_io()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
file = Path(__file__).parent.parent / "examples" / "example.il"
|
||||
parse(file.read_text())
|
||||
|
||||
Reference in New Issue
Block a user