a
    ÷£Be  ã                   @   sF   d dl Z d dlZd dlmZmZmZ e dej¡ZG dd„ de	ƒZ
dS )é    N)ÚDictÚListÚTupleú^c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚLineNumbersaJ  
  Class to convert between character offsets in a text string, and pairs (line, column) of 1-based
  line and 0-based column numbers, as used by tokens and AST nodes.

  This class expects unicode for input and stores positions in unicode. But it supports
  translating to and from utf8 offsets, which are used by ast parsing.
  c                 C   s0   dd„ t  |¡D ƒ| _|| _t|ƒ| _i | _d S )Nc                 S   s   g | ]}|  d ¡‘qS )r   )Ústart)Ú.0Úm© r
   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/asttokens/line_numbers.pyÚ
<listcomp>    ó    z(LineNumbers.__init__.<locals>.<listcomp>)Ú_line_start_reÚfinditerÚ_line_offsetsÚ_textÚlenÚ	_text_lenÚ_utf8_offset_cache)ÚselfÚtextr
   r
   r   Ú__init__   s    
zLineNumbers.__init__c                 C   s�   | j  |¡}|du rt|t| jƒk r,| j| n| j}| j| j|d  |… }dd„ t|ƒD ƒ}| t|ƒ¡ || j |< |tdt	t|ƒd |ƒƒ S )z`
    Given a 1-based line number and 0-based utf8 column, returns a 0-based unicode column.
    Né   c                 S   s$   g | ]\}}|  d ¡D ]}|‘qqS )Úutf8)Úencode)r   ÚiÚcÚbyter
   r
   r   r   /   r   z-LineNumbers.from_utf8_col.<locals>.<listcomp>r   )
r   Úgetr   r   r   r   Ú	enumerateÚappendÚmaxÚmin)r   ÚlineZutf8_columnÚoffsetsZ
end_offsetZ	line_textr
   r
   r   Úfrom_utf8_col%   s    
zLineNumbers.from_utf8_colc                 C   sH   |d8 }|t | jƒkr| jS |dk r(dS t| j| td|ƒ | jƒS dS )z`
    Converts 1-based line number and 0-based column to 0-based character offset into text.
    r   r   N)r   r   r   r"   r!   )r   r#   Úcolumnr
   r
   r   Úline_to_offset5   s    zLineNumbers.line_to_offsetc                 C   s:   t dt| j|ƒƒ}t | j|¡d }|d || j|  fS )zo
    Converts 0-based character offset to pair (line, col) of 1-based line and 0-based column
    numbers.
    r   r   )r!   r"   r   ÚbisectÚbisect_rightr   )r   ÚoffsetÚ
line_indexr
   r
   r   Úoffset_to_lineB   s    zLineNumbers.offset_to_lineN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   r'   r,   r
   r
   r
   r   r      s
   r   )r(   ÚreÚtypingr   r   r   ÚcompileÚMr   Úobjectr   r
   r
   r
   r   Ú<module>   s   