linked-data-python 0.0.4__py3-none-any.whl → 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ldpy/__init__.py +35 -11
- ldpy/__main__.py +75 -250
- ldpy/build.py +91 -0
- ldpy/console.py +116 -0
- ldpy/debug.py +235 -0
- ldpy/formatter.py +329 -0
- ldpy/importer.py +110 -0
- ldpy/lsp/__init__.py +11 -0
- ldpy/lsp/__main__.py +4 -0
- ldpy/lsp/backend.py +118 -0
- ldpy/lsp/rpc.py +104 -0
- ldpy/lsp/server.py +353 -0
- ldpy/lsp/translate.py +140 -0
- ldpy/pygments_lexer.py +613 -0
- ldpy/runtime.py +931 -0
- ldpy/sparql.py +551 -0
- ldpy/transpiler/__init__.py +14 -0
- ldpy/transpiler/core.py +2558 -0
- ldpy/transpiler/errors.py +38 -0
- ldpy/transpiler/linemap.py +292 -0
- linked_data_python-0.2.0.dist-info/METADATA +158 -0
- linked_data_python-0.2.0.dist-info/RECORD +26 -0
- {linked_data_python-0.0.4.dist-info → linked_data_python-0.2.0.dist-info}/WHEEL +1 -1
- linked_data_python-0.2.0.dist-info/entry_points.txt +9 -0
- {linked_data_python-0.0.4.dist-info → linked_data_python-0.2.0.dist-info/licenses}/LICENSE.md +0 -0
- {linked_data_python-0.0.4.dist-info → linked_data_python-0.2.0.dist-info}/top_level.txt +0 -0
- ldpy/grun/lib.py +0 -63
- ldpy/grun/util.py +0 -11
- ldpy/ldpy.py +0 -183
- ldpy/rewriter/IndentedStringWriter.py +0 -54
- ldpy/rewriter/LDPythonRewriter.py +0 -677
- ldpy/rewriter/MultiChannelTokenStream.py +0 -127
- ldpy/rewriter/Result.py +0 -49
- ldpy/rewriter/__init__.py +0 -7
- ldpy/rewriter/antlr/LDPythonLexer.py +0 -870
- ldpy/rewriter/antlr/LDPythonParser.py +0 -9336
- ldpy/rewriter/antlr/LDPythonVisitor.py +0 -573
- ldpy/sparql/builtin.py +0 -326
- linked_data_python-0.0.4.dist-info/METADATA +0 -139
- linked_data_python-0.0.4.dist-info/RECORD +0 -20
- linked_data_python-0.0.4.dist-info/entry_points.txt +0 -3
ldpy/lsp/translate.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""Translating positions, ranges and URIs between a .ldpy document and its
|
|
2
|
+
Python shadow, through the LanguageMap — on raw LSP structures (dicts).
|
|
3
|
+
|
|
4
|
+
The functions are pure and unit-tested (tests/test_lsp_translate.py).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def shadow_uri(uri):
|
|
9
|
+
"""URI of the Python shadow document of a .ldpy (in-memory convention)."""
|
|
10
|
+
return uri + ".shadow.py"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def unshadow_uri(uri):
|
|
14
|
+
"""Original .ldpy URI of a shadow URI (inverse of shadow_uri)."""
|
|
15
|
+
if uri.endswith(".shadow.py"):
|
|
16
|
+
return uri[:-len(".shadow.py")]
|
|
17
|
+
return uri
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def pos_to_py(lmap, pos):
|
|
21
|
+
"""Position LSP .ldpy -> .py (dict {'line','character'})."""
|
|
22
|
+
r = lmap.to_gen(pos["line"], pos["character"])
|
|
23
|
+
if r is None:
|
|
24
|
+
return None
|
|
25
|
+
return {"line": r[0], "character": r[1]}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def pos_to_ldpy(lmap, pos):
|
|
29
|
+
"""Position LSP .py -> .ldpy (None si synthétique)."""
|
|
30
|
+
r = lmap.to_src(pos["line"], pos["character"])
|
|
31
|
+
if r is None:
|
|
32
|
+
return None
|
|
33
|
+
return {"line": r[0], "character": r[1]}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _is_pos(d):
|
|
37
|
+
return isinstance(d, dict) and set(d) >= {"line", "character"} \
|
|
38
|
+
and isinstance(d.get("line"), int)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _is_range(d):
|
|
42
|
+
return isinstance(d, dict) and _is_pos(d.get("start", None) or {}) \
|
|
43
|
+
and _is_pos(d.get("end", None) or {})
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def translate_result(obj, lmap, maps_by_uri=None):
|
|
47
|
+
"""Recursively translate a backend RESULT (.py -> .ldpy):
|
|
48
|
+
|
|
49
|
+
- every range {'start','end'} is translated through the map;
|
|
50
|
+
- every 'uri'/'targetUri' field pointing at a shadow is un-shadowed, and
|
|
51
|
+
the ranges of an object carrying that uri use THAT document's map
|
|
52
|
+
document (maps_by_uri : uri .ldpy -> LanguageMap).
|
|
53
|
+
|
|
54
|
+
An untranslatable range (a synthetic position) is left as it is rather
|
|
55
|
+
than breaking the structure."""
|
|
56
|
+
maps_by_uri = maps_by_uri or {}
|
|
57
|
+
|
|
58
|
+
def walk(node, cur_map):
|
|
59
|
+
if isinstance(node, list):
|
|
60
|
+
return [walk(x, cur_map) for x in node]
|
|
61
|
+
if not isinstance(node, dict):
|
|
62
|
+
return node
|
|
63
|
+
out = {}
|
|
64
|
+
# this node's uri may change which map applies to ITS ranges
|
|
65
|
+
for key in ("uri", "targetUri"):
|
|
66
|
+
u = node.get(key)
|
|
67
|
+
if isinstance(u, str) and u.endswith(".shadow.py"):
|
|
68
|
+
ldpy_uri = unshadow_uri(u)
|
|
69
|
+
cur_map = maps_by_uri.get(ldpy_uri, cur_map)
|
|
70
|
+
for k, v in node.items():
|
|
71
|
+
if k in ("uri", "targetUri") and isinstance(v, str):
|
|
72
|
+
out[k] = unshadow_uri(v)
|
|
73
|
+
elif _is_range(v):
|
|
74
|
+
out[k] = _range_to_ldpy(cur_map, v)
|
|
75
|
+
elif _is_pos(v) and k == "position":
|
|
76
|
+
out[k] = pos_to_ldpy(cur_map, v) or v
|
|
77
|
+
else:
|
|
78
|
+
out[k] = walk(v, cur_map)
|
|
79
|
+
return out
|
|
80
|
+
|
|
81
|
+
return walk(obj, lmap)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _range_to_ldpy(lmap, rng):
|
|
85
|
+
s = pos_to_ldpy(lmap, rng["start"])
|
|
86
|
+
e = pos_to_ldpy(lmap, rng["end"])
|
|
87
|
+
if s is None or e is None:
|
|
88
|
+
return rng
|
|
89
|
+
return {"start": s, "end": e}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def island_at(lmap, line, character):
|
|
93
|
+
"""The island segment covering a source position, or None."""
|
|
94
|
+
for seg in lmap.segments:
|
|
95
|
+
if seg.src is None or not seg.kind.startswith("island:"):
|
|
96
|
+
continue
|
|
97
|
+
sl0, sc0, sl1, sc1 = seg.src
|
|
98
|
+
if (sl0, sc0) <= (line, character) < (sl1, sc1):
|
|
99
|
+
return seg
|
|
100
|
+
return None
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
# ---------------------------------------------------------- semantic tokens
|
|
104
|
+
|
|
105
|
+
TOKEN_TYPES = ["namespace", "string", "type", "variable", "macro", "keyword"]
|
|
106
|
+
_KIND_TO_TYPE = {
|
|
107
|
+
"island:prefix": 5, "island:base": 5, # keyword
|
|
108
|
+
"island:iri": 1, "island:literal": 1, # string
|
|
109
|
+
"island:pname": 2, # type
|
|
110
|
+
"island:var": 3, # variable
|
|
111
|
+
"island:firi": 4, "island:fnode": 4, # macro
|
|
112
|
+
"island:graph": 4, # macro (région entière)
|
|
113
|
+
# fiches 013-019
|
|
114
|
+
"island:import": 5, # keyword (prefix import)
|
|
115
|
+
"island:graph-decl": 5, "island:bindings-decl": 5,
|
|
116
|
+
"island:for-bindings": 5, "island:for-bindings-close": 5,
|
|
117
|
+
"island:match": 4, "island:sparql": 4, # macro (région entière)
|
|
118
|
+
"island:addto": 4, "island:removefrom": 4,
|
|
119
|
+
"island:enode": 4, "island:eiri": 4,
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def semantic_tokens(lmap):
|
|
124
|
+
"""LSP encoding (deltas) of the SINGLE-LINE islands of the source doc."""
|
|
125
|
+
toks = []
|
|
126
|
+
for seg in lmap.segments:
|
|
127
|
+
if seg.src is None or seg.kind not in _KIND_TO_TYPE:
|
|
128
|
+
continue
|
|
129
|
+
sl0, sc0, sl1, sc1 = seg.src
|
|
130
|
+
if sl0 != sl1:
|
|
131
|
+
continue # multiligne : laissé à TextMate
|
|
132
|
+
toks.append((sl0, sc0, sc1 - sc0, _KIND_TO_TYPE[seg.kind]))
|
|
133
|
+
toks.sort()
|
|
134
|
+
data, prev_line, prev_col = [], 0, 0
|
|
135
|
+
for line, col, length, ttype in toks:
|
|
136
|
+
dline = line - prev_line
|
|
137
|
+
dcol = col - prev_col if dline == 0 else col
|
|
138
|
+
data.extend([dline, dcol, length, ttype, 0])
|
|
139
|
+
prev_line, prev_col = line, col
|
|
140
|
+
return data
|