1""" 
    2    pygments.lexers.futhark 
    3    ~~~~~~~~~~~~~~~~~~~~~~~ 
    4 
    5    Lexer for the Futhark language 
    6 
    7    :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS. 
    8    :license: BSD, see LICENSE for details. 
    9""" 
    10 
    11from pygments.lexer import RegexLexer, bygroups 
    12from pygments.token import Comment, Operator, Keyword, Name, String, \ 
    13    Number, Punctuation, Whitespace 
    14from pygments import unistring as uni 
    15 
    16__all__ = ['FutharkLexer'] 
    17 
    18 
    19class FutharkLexer(RegexLexer): 
    20    """ 
    21    A Futhark lexer 
    22    """ 
    23    name = 'Futhark' 
    24    url = 'https://futhark-lang.org/' 
    25    aliases = ['futhark'] 
    26    filenames = ['*.fut'] 
    27    mimetypes = ['text/x-futhark'] 
    28    version_added = '2.8' 
    29 
    30    num_types = ('i8', 'i16', 'i32', 'i64', 'u8', 'u16', 'u32', 'u64', 'f32', 'f64') 
    31 
    32    other_types = ('bool', ) 
    33 
    34    reserved = ('if', 'then', 'else', 'def', 'let', 'loop', 'in', 'with', 
    35                'type', 'type~', 'type^', 
    36                'val', 'entry', 'for', 'while', 'do', 'case', 'match', 
    37                'include', 'import', 'module', 'open', 'local', 'assert', '_') 
    38 
    39    ascii = ('NUL', 'SOH', '[SE]TX', 'EOT', 'ENQ', 'ACK', 
    40             'BEL', 'BS', 'HT', 'LF', 'VT', 'FF', 'CR', 'S[OI]', 'DLE', 
    41             'DC[1-4]', 'NAK', 'SYN', 'ETB', 'CAN', 
    42             'EM', 'SUB', 'ESC', '[FGRU]S', 'SP', 'DEL') 
    43 
    44    num_postfix = r'({})?'.format('|'.join(num_types)) 
    45 
    46    identifier_re = '[a-zA-Z_][a-zA-Z_0-9\']*' 
    47 
    48    # opstart_re = '+\-\*/%=\!><\|&\^' 
    49 
    50    tokens = { 
    51        'root': [ 
    52            (r'--(.*?)$', Comment.Single), 
    53            (r'\s+', Whitespace), 
    54            (r'\(\)', Punctuation), 
    55            (r'\b({})(?!\')\b'.format('|'.join(reserved)), Keyword.Reserved), 
    56            (r'\b({})(?!\')\b'.format('|'.join(num_types + other_types)), Keyword.Type), 
    57 
    58            # Identifiers 
    59            (r'#\[([a-zA-Z_\(\) ]*)\]', Comment.Preproc), 
    60            (rf'[#!]?({identifier_re}\.)*{identifier_re}', Name), 
    61 
    62            (r'\\', Operator), 
    63            (r'[-+/%=!><|&*^][-+/%=!><|&*^.]*', Operator), 
    64            (r'[][(),:;`{}?.\'~^]', Punctuation), 
    65 
    66            #  Numbers 
    67            (r'0[xX]_*[\da-fA-F](_*[\da-fA-F])*_*[pP][+-]?\d(_*\d)*' + num_postfix, 
    68             Number.Float), 
    69            (r'0[xX]_*[\da-fA-F](_*[\da-fA-F])*\.[\da-fA-F](_*[\da-fA-F])*' 
    70             r'(_*[pP][+-]?\d(_*\d)*)?' + num_postfix, Number.Float), 
    71            (r'\d(_*\d)*_*[eE][+-]?\d(_*\d)*' + num_postfix, Number.Float), 
    72            (r'\d(_*\d)*\.\d(_*\d)*(_*[eE][+-]?\d(_*\d)*)?' + num_postfix, Number.Float), 
    73            (r'0[bB]_*[01](_*[01])*' + num_postfix, Number.Bin), 
    74            (r'0[xX]_*[\da-fA-F](_*[\da-fA-F])*' + num_postfix, Number.Hex), 
    75            (r'\d(_*\d)*' + num_postfix, Number.Integer), 
    76 
    77            #  Character/String Literals 
    78            (r"'", String.Char, 'character'), 
    79            (r'"', String, 'string'), 
    80            #  Special 
    81            (r'\[[a-zA-Z_\d]*\]', Keyword.Type), 
    82            (r'\(\)', Name.Builtin), 
    83        ], 
    84        'character': [ 
    85            # Allows multi-chars, incorrectly. 
    86            (r"[^\\']'", String.Char, '#pop'), 
    87            (r"\\", String.Escape, 'escape'), 
    88            ("'", String.Char, '#pop'), 
    89        ], 
    90        'string': [ 
    91            (r'[^\\"]+', String), 
    92            (r"\\", String.Escape, 'escape'), 
    93            ('"', String, '#pop'), 
    94        ], 
    95 
    96        'escape': [ 
    97            (r'[abfnrtv"\'&\\]', String.Escape, '#pop'), 
    98            (r'\^[][' + uni.Lu + r'@^_]', String.Escape, '#pop'), 
    99            ('|'.join(ascii), String.Escape, '#pop'), 
    100            (r'o[0-7]+', String.Escape, '#pop'), 
    101            (r'x[\da-fA-F]+', String.Escape, '#pop'), 
    102            (r'\d+', String.Escape, '#pop'), 
    103            (r'(\s+)(\\)', bygroups(Whitespace, String.Escape), '#pop'), 
    104        ], 
    105    }