o
    N=&bO                     @   s   d Z ddlmZ zddlZddlmZ ddlmZ dZW n e	y'   dZY nw g dZ
G d	d
 d
eZG dd dZG dd dZdS )z@Lexical analysis of formal languages (i.e. code) using Pygments.    )ApplicationErrorN)get_lexer_by_name)_get_ttype_classTF)tokentext c                   @   s   e Zd ZdS )
LexerErrorN)__name__
__module____qualname__ r   r   @usr/lib/python3.10/site-packages/docutils/utils/code_analyzer.pyr      s    r   c                   @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )Lexera  Parse `code` lines and yield "classified" tokens.

    Arguments

      code       -- string of source code to parse,
      language   -- formal language the code is written in,
      tokennames -- either 'long', 'short', or 'none' (see below).

    Merge subsequent tokens of the same token-type.

    Iterating over an instance yields the tokens as ``(tokentype, value)``
    tuples. The value of `tokennames` configures the naming of the tokentype:

      'long':  downcased full token type name,
      'short': short name defined by pygments.token.STANDARD_TYPES
               (= class argument used in pygments html output),
      'none':  skip lexical analysis.
    shortc                 C   sj   || _ || _|| _d| _|dv s|dkrdS tstdz	t| j| _W dS  tjj	y4   td| w )zE
        Set up a lexical analyzer for `code` in `language`.
        N)r   r   nonez0Cannot analyze code. Pygments package not found.z6Cannot analyze code. No Pygments lexer found for "%s".)
codelanguage
tokennameslexerwith_pygmentsr   r   pygmentsutilZClassNotFound)selfr   r   r   r   r   r   __init__1   s   zLexer.__init__c                 c   sv    t |}t|\}}|D ]\}}||u r||7 }q||fV  ||}}q|dr0|dd }|r9||fV  dS dS )zrMerge subsequent tokens of same token-type.

           Also strip the final newline (added by pygments).
        
N)iternextendswith)r   tokensZlasttypeZlastvalttypevaluer   r   r   mergeL   s   


zLexer.mergec                 c   s    | j du rg | jfV  dS t| j| j }| |D ]$\}}| jdkr.t| d}nt	|g}dd |D }||fV  qdS )z7Parse self.code and yield "classified" tokens.
        Nlong.c                 S   s   g | ]}|t vr|qS r   )unstyled_tokens).0clsr   r   r   
<listcomp>j   s    z"Lexer.__iter__.<locals>.<listcomp>)
r   r   r   Zlexr"   r   strlowersplitr   )r   r   Z	tokentyper!   classesr   r   r   __iter__^   s   


zLexer.__iter__N)r   )r	   r
   r   __doc__r   r"   r-   r   r   r   r   r      s
    
r   c                   @   s    e Zd ZdZdd Zdd ZdS )NumberLinesaq  Insert linenumber-tokens at the start of every code line.

    Arguments

       tokens    -- iterable of ``(classes, value)`` tuples
       startline -- first line number
       endline   -- last line number

    Iterating over an instance yields the tokens with a
    ``(['ln'], '<the line number>')`` token added for every code line.
    Multi-line tokens are split.c                 C   s"   || _ || _dtt| | _d S )Nz%%%dd )r   	startlinelenr)   fmt_str)r   r   r0   Zendliner   r   r   r   {   s   zNumberLines.__init__c                 c   s    | j }dg| j| fV  | jD ]-\}}|d}|d d D ]}||d fV  |d7 }dg| j| fV  q||d fV  qd S )Nlnr   r      )r0   r2   r   r+   )r   linenor    r!   linesliner   r   r   r-      s   
zNumberLines.__iter__N)r	   r
   r   r.   r   r-   r   r   r   r   r/   n   s    r/   )r.   docutilsr   r   Zpygments.lexersr   Zpygments.formatters.htmlr   r   ImportErrorr%   r   r   r/   r   r   r   r   <module>   s   Q