1818import tomllib
1919from bisect import bisect_right
2020
21+ from coretrace_python .source .manager import LINE_BREAK , lines_of
2122from coretrace_python .source .model import FileLocation , SourceId
2223
2324Pointer = tuple [str | int , ...]
@@ -48,7 +49,7 @@ def pointer_text(path: Pointer) -> str:
4849
4950class _Lines :
5051 def __init__ (self , text : str ) -> None :
51- self ._starts = [0 , * (match .end () for match in re .finditer (" \n " , text ))]
52+ self ._starts = [0 , * (match .end () for match in LINE_BREAK .finditer (text ))]
5253
5354 def position (self , offset : int ) -> tuple [int , int ]:
5455 """The one-based line and column of ``offset``."""
@@ -158,7 +159,7 @@ def _space(self, position: int) -> int:
158159class TomlPositions :
159160 """The key lines of each section of a TOML document, by the structural path of its
160161 table: the root, ``[a.b]``, or the element ``[[a]]`` opens, numbered in order. Lines
161- are counted by line feeds, as the rest of the engine counts them; a line that starts
162+ are counted as the rest of the engine counts them (``lines_of``) ; a line that starts
162163 inside a multi-line string or an open array or inline table is a continuation, never
163164 a header or a key."""
164165
@@ -167,9 +168,9 @@ def __init__(self, text: str) -> None:
167168 self ._keys : dict [Pointer , dict [str , list [tuple [int , int , str ]]]] = {(): {}}
168169 arrays : dict [Pointer , int ] = {}
169170 current : Pointer = ()
171+ self ._lines = lines_of (text )
170172 state = _LexState ()
171- for number , line in enumerate (text .split ("\n " ), start = 1 ):
172- line = line .removesuffix ("\r " )
173+ for number , line in enumerate (self ._lines , start = 1 ):
173174 continued = state .inside ()
174175 state .read (line )
175176 if continued :
@@ -205,17 +206,36 @@ def locate(self, path: Pointer, value: object) -> tuple[int, int] | None:
205206 """The line and column ``value`` starts at under ``path``, or None when the
206207 section its path names has no line setting that key to that value."""
207208
208- if not path or isinstance ( path [ - 1 ], int ) :
209+ if not path :
209210 return None
211+ if isinstance (path [- 1 ], int ):
212+ return self ._element (path , value )
210213 for number , column , written in self ._keys .get (path [:- 1 ], {}).get (path [- 1 ], ()):
211- try :
212- found = tomllib .loads (f"v = { written } " )["v" ]
213- except tomllib .TOMLDecodeError :
214- continue
215- if _same (found , value ):
214+ if _same (_value (written ), value ):
216215 return number , column
217216 return None
218217
218+ def _element (self , path : Pointer , value : object ) -> tuple [int , int ] | None :
219+ """An element of an array a key holds, written over one or several lines:
220+ ``dependencies = [\\ n "a",\\ n "b",\\ n]``."""
221+
222+ if len (path ) < 2 or isinstance (path [- 2 ], int ):
223+ return None
224+ index = path [- 1 ]
225+ assert isinstance (index , int )
226+ for number , column , _ in self ._keys .get (path [:- 2 ], {}).get (path [- 2 ], ()):
227+ text = "\n " .join (self ._lines [number - 1 :])[column - 1 :]
228+ elements = _array_elements (text )
229+ if index >= len (elements ):
230+ continue
231+ start , end = elements [index ]
232+ if not _same (_value (text [start :end ]), value ):
233+ continue
234+ before = text [:start ]
235+ line = number + before .count ("\n " )
236+ return line , (start - before .rfind ("\n " ) if "\n " in before else column + start )
237+ return None
238+
219239
220240class _LexState :
221241 """Where a TOML line leaves the reader: inside a multi-line string (its delimiter),
@@ -266,6 +286,75 @@ def read(self, line: str) -> None:
266286 position += 1
267287
268288
289+ def _value (written : str ) -> object :
290+ """The value TOML text denotes, or None when it is not one value."""
291+
292+ try :
293+ return tomllib .loads (f"v = { written } " )["v" ]
294+ except tomllib .TOMLDecodeError :
295+ return None
296+
297+
298+ def _array_elements (text : str ) -> list [tuple [int , int ]]:
299+ """The start and end offsets of the elements of the array ``text`` opens, skipping
300+ strings and comments; empty when ``text`` does not open an array."""
301+
302+ if not text .startswith ("[" ):
303+ return []
304+ elements : list [tuple [int , int ]] = []
305+ depth , start , position = 0 , - 1 , 0
306+ while position < len (text ):
307+ character = text [position ]
308+ if character in "\" '" :
309+ if depth == 1 and start < 0 :
310+ start = position
311+ position = _string_end (text , position )
312+ continue
313+ if character == "#" :
314+ newline = text .find ("\n " , position )
315+ position = len (text ) if newline < 0 else newline
316+ continue
317+ if character in "[{" :
318+ if depth == 1 and start < 0 :
319+ start = position
320+ depth += 1
321+ elif character in "]}" :
322+ depth -= 1
323+ if depth == 0 :
324+ if start >= 0 :
325+ elements .append ((start , position ))
326+ return elements
327+ elif character == "," and depth == 1 :
328+ if start >= 0 :
329+ elements .append ((start , position ))
330+ start = - 1
331+ elif depth == 1 and start < 0 and not character .isspace ():
332+ start = position
333+ position += 1
334+ return []
335+
336+
337+ def _string_end (text : str , start : int ) -> int :
338+ """The offset just past the TOML string that starts at ``start``."""
339+
340+ quote = text [start ]
341+ if text .startswith (quote * 3 , start ):
342+ position = start + 3
343+ while position < len (text ):
344+ if quote == '"' and text [position ] == "\\ " :
345+ position += 2
346+ elif text .startswith (quote * 3 , position ):
347+ run = len (text [position :]) - len (text [position :].lstrip (quote ))
348+ return position + min (run , 5 )
349+ else :
350+ position += 1
351+ return len (text )
352+ position = start + 1
353+ while position < len (text ) and text [position ] not in (quote , "\n " ):
354+ position += 2 if quote == '"' and text [position ] == "\\ " else 1
355+ return position + 1
356+
357+
269358def _resolve (keys : tuple [str , ...], arrays : dict [Pointer , int ]) -> Pointer :
270359 """The structural path of a table named by ``keys``: a key that names an array of
271360 tables stands for its last element."""
0 commit comments