3 ��X � @ sl d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ej d�Z ed�Zdd � Zd d� Z dd d�ZdS )a pygments.regexopt ~~~~~~~~~~~~~~~~~ An algorithm that generates optimized regexes for matching long lists of literal strings. :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. � N)�escape)�commonprefix)�groupby)� itemgetterz [\^\\\-\]]c C s dt jdd� dj| �� d S )N�[c S s d| j � S )N�\)�group)�m� r �/usr/lib/python3.6/regexopt.py�<lambda> s zmake_charset.<locals>.<lambda>� �])� CS_ESCAPE�sub�join)Zlettersr r r �make_charset s r c s� |rdp d}| sdS | d � t | �dkr8|t� � | S � sZ|t| dd� d� d | S t � �dkr�g }g }x.| D ]&}t |�dkr�|j|� qt|j|� qtW t |�dkr�|r�|t|d� d t|� | S |t|� | S t| �}|�rt |��|t|� t�fd d �| D �d� | S dd � | D �}t|�}|�rtt |��|tt�fdd �| D ��d� t|ddd� � | S |djdd � t| � fdd��D �� | S )zEReturn a regex that matches any string in the sorted list of strings.�)r r � Nz(?:�?�|c s g | ]}|� d � �qS )Nr )�.0�s)�plenr r � <listcomp>@ s z#regex_opt_inner.<locals>.<listcomp>c S s g | ]}|d d d� �qS )Nr ���r )r r r r r r C s c 3 s | ]}|d � � V qd S )Nr )r r )�slenr r � <genexpr>I s z"regex_opt_inner.<locals>.<genexpr>c s s | ]}t t|d �d�V qdS )r r N)�regex_opt_inner�list)r r r r r r N s c s | d � d kS )Nr r )r )�firstr r r O s z!regex_opt_inner.<locals>.<lambda>r ) �lenr r �appendr r �sortedr r ) �stringsZ open_parenZclose_parenZ oneletter�restr �prefixZstrings_rev�suffixr )r r r r r s: (6r r c C s t | �} |t| d� | S )z�Return a compiled regex that matches any string in the given list. The strings to match must be literal strings, not regexes. They will be regex-escaped. *prefix* and *suffix* are pre- and appended to the final regex. �()r# r )r$ r&