1""" 
    2    pygments.lexers.rebol 
    3    ~~~~~~~~~~~~~~~~~~~~~ 
    4 
    5    Lexers for the REBOL and related languages. 
    6 
    7    :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS. 
    8    :license: BSD, see LICENSE for details. 
    9""" 
    10 
    11import re 
    12 
    13from pygments.lexer import RegexLexer, bygroups 
    14from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ 
    15    Number, Generic, Whitespace 
    16 
    17__all__ = ['RebolLexer', 'RedLexer'] 
    18 
    19 
    20class RebolLexer(RegexLexer): 
    21    """ 
    22    A REBOL lexer. 
    23    """ 
    24    name = 'REBOL' 
    25    aliases = ['rebol'] 
    26    filenames = ['*.r', '*.r3', '*.reb'] 
    27    mimetypes = ['text/x-rebol'] 
    28    url = 'http://www.rebol.com' 
    29    version_added = '1.1' 
    30 
    31    flags = re.IGNORECASE | re.MULTILINE 
    32 
    33    escape_re = r'(?:\^\([0-9a-f]{1,4}\)*)' 
    34 
    35    def word_callback(lexer, match): 
    36        word = match.group() 
    37 
    38        if re.match(".*:$", word): 
    39            yield match.start(), Generic.Subheading, word 
    40        elif re.match( 
    41            r'(native|alias|all|any|as-string|as-binary|bind|bound\?|case|' 
    42            r'catch|checksum|comment|debase|dehex|exclude|difference|disarm|' 
    43            r'either|else|enbase|foreach|remove-each|form|free|get|get-env|if|' 
    44            r'in|intersect|loop|minimum-of|maximum-of|mold|new-line|' 
    45            r'new-line\?|not|now|prin|print|reduce|compose|construct|repeat|' 
    46            r'reverse|save|script\?|set|shift|switch|throw|to-hex|trace|try|' 
    47            r'type\?|union|unique|unless|unprotect|unset|until|use|value\?|' 
    48            r'while|compress|decompress|secure|open|close|read|read-io|' 
    49            r'write-io|write|update|query|wait|input\?|exp|log-10|log-2|' 
    50            r'log-e|square-root|cosine|sine|tangent|arccosine|arcsine|' 
    51            r'arctangent|protect|lowercase|uppercase|entab|detab|connected\?|' 
    52            r'browse|launch|stats|get-modes|set-modes|to-local-file|' 
    53            r'to-rebol-file|encloak|decloak|create-link|do-browser|bind\?|' 
    54            r'hide|draw|show|size-text|textinfo|offset-to-caret|' 
    55            r'caret-to-offset|local-request-file|rgb-to-hsv|hsv-to-rgb|' 
    56            r'crypt-strength\?|dh-make-key|dh-generate-key|dh-compute-key|' 
    57            r'dsa-make-key|dsa-generate-key|dsa-make-signature|' 
    58            r'dsa-verify-signature|rsa-make-key|rsa-generate-key|' 
    59            r'rsa-encrypt)$', word): 
    60            yield match.start(), Name.Builtin, word 
    61        elif re.match( 
    62            r'(add|subtract|multiply|divide|remainder|power|and~|or~|xor~|' 
    63            r'minimum|maximum|negate|complement|absolute|random|head|tail|' 
    64            r'next|back|skip|at|pick|first|second|third|fourth|fifth|sixth|' 
    65            r'seventh|eighth|ninth|tenth|last|path|find|select|make|to|copy\*|' 
    66            r'insert|remove|change|poke|clear|trim|sort|min|max|abs|cp|' 
    67            r'copy)$', word): 
    68            yield match.start(), Name.Function, word 
    69        elif re.match( 
    70            r'(error|source|input|license|help|install|echo|Usage|with|func|' 
    71            r'throw-on-error|function|does|has|context|probe|\?\?|as-pair|' 
    72            r'mod|modulo|round|repend|about|set-net|append|join|rejoin|reform|' 
    73            r'remold|charset|array|replace|move|extract|forskip|forall|alter|' 
    74            r'first+|also|take|for|forever|dispatch|attempt|what-dir|' 
    75            r'change-dir|clean-path|list-dir|dirize|rename|split-path|delete|' 
    76            r'make-dir|delete-dir|in-dir|confirm|dump-obj|upgrade|what|' 
    77            r'build-tag|process-source|build-markup|decode-cgi|read-cgi|' 
    78            r'write-user|save-user|set-user-name|protect-system|parse-xml|' 
    79            r'cvs-date|cvs-version|do-boot|get-net-info|desktop|layout|' 
    80            r'scroll-para|get-face|alert|set-face|uninstall|unfocus|' 
    81            r'request-dir|center-face|do-events|net-error|decode-url|' 
    82            r'parse-header|parse-header-date|parse-email-addrs|import-email|' 
    83            r'send|build-attach-body|resend|show-popup|hide-popup|open-events|' 
    84            r'find-key-face|do-face|viewtop|confine|find-window|' 
    85            r'insert-event-func|remove-event-func|inform|dump-pane|dump-face|' 
    86            r'flag-face|deflag-face|clear-fields|read-net|vbug|path-thru|' 
    87            r'read-thru|load-thru|do-thru|launch-thru|load-image|' 
    88            r'request-download|do-face-alt|set-font|set-para|get-style|' 
    89            r'set-style|make-face|stylize|choose|hilight-text|hilight-all|' 
    90            r'unlight-text|focus|scroll-drag|clear-face|reset-face|scroll-face|' 
    91            r'resize-face|load-stock|load-stock-block|notify|request|flash|' 
    92            r'request-color|request-pass|request-text|request-list|' 
    93            r'request-date|request-file|dbug|editor|link-relative-path|' 
    94            r'emailer|parse-error)$', word): 
    95            yield match.start(), Keyword.Namespace, word 
    96        elif re.match( 
    97            r'(halt|quit|do|load|q|recycle|call|run|ask|parse|view|unview|' 
    98            r'return|exit|break)$', word): 
    99            yield match.start(), Name.Exception, word 
    100        elif re.match('REBOL$', word): 
    101            yield match.start(), Generic.Heading, word 
    102        elif re.match("to-.*", word): 
    103            yield match.start(), Keyword, word 
    104        elif re.match(r'(\+|-|\*|/|//|\*\*|and|or|xor|=\?|=|==|<>|<|>|<=|>=)$', 
    105                      word): 
    106            yield match.start(), Operator, word 
    107        elif re.match(r".*\?$", word): 
    108            yield match.start(), Keyword, word 
    109        elif re.match(r".*\!$", word): 
    110            yield match.start(), Keyword.Type, word 
    111        elif re.match("'.*", word): 
    112            yield match.start(), Name.Variable.Instance, word  # lit-word 
    113        elif re.match("#.*", word): 
    114            yield match.start(), Name.Label, word  # issue 
    115        elif re.match("%.*", word): 
    116            yield match.start(), Name.Decorator, word  # file 
    117        else: 
    118            yield match.start(), Name.Variable, word 
    119 
    120    tokens = { 
    121        'root': [ 
    122            (r'\s+', Text), 
    123            (r'#"', String.Char, 'char'), 
    124            (r'#\{[0-9a-f]*\}', Number.Hex), 
    125            (r'2#\{', Number.Hex, 'bin2'), 
    126            (r'64#\{[0-9a-z+/=\s]*\}', Number.Hex), 
    127            (r'"', String, 'string'), 
    128            (r'\{', String, 'string2'), 
    129            (r';#+.*\n', Comment.Special), 
    130            (r';\*+.*\n', Comment.Preproc), 
    131            (r';.*\n', Comment), 
    132            (r'%"', Name.Decorator, 'stringFile'), 
    133            (r'%[^(^{")\s\[\]]+', Name.Decorator), 
    134            (r'[+-]?([a-z]{1,3})?\$\d+(\.\d+)?', Number.Float),  # money 
    135            (r'[+-]?\d+\:\d+(\:\d+)?(\.\d+)?', String.Other),    # time 
    136            (r'\d+[\-/][0-9a-z]+[\-/]\d+(\/\d+\:\d+((\:\d+)?' 
    137             r'([.\d+]?([+-]?\d+:\d+)?)?)?)?', String.Other),   # date 
    138            (r'\d+(\.\d+)+\.\d+', Keyword.Constant),             # tuple 
    139            (r'\d+X\d+', Keyword.Constant),                   # pair 
    140            (r'[+-]?\d+(\'\d+)?([.,]\d*)?E[+-]?\d+', Number.Float), 
    141            (r'[+-]?\d+(\'\d+)?[.,]\d*', Number.Float), 
    142            (r'[+-]?\d+(\'\d+)?', Number), 
    143            (r'[\[\]()]', Generic.Strong), 
    144            (r'[a-z]+[^(^{"\s:)]*://[^(^{"\s)]*', Name.Decorator),  # url 
    145            (r'mailto:[^(^{"@\s)]+@[^(^{"@\s)]+', Name.Decorator),  # url 
    146            (r'[^(^{"@\s)]+@[^(^{"@\s)]+', Name.Decorator),         # email 
    147            (r'comment\s"', Comment, 'commentString1'), 
    148            (r'comment\s\{', Comment, 'commentString2'), 
    149            (r'comment\s\[', Comment, 'commentBlock'), 
    150            (r'comment\s[^(\s{"\[]+', Comment), 
    151            (r'/[^(^{")\s/[\]]*', Name.Attribute), 
    152            (r'([^(^{")\s/[\]]+)(?=[:({"\s/\[\]])', word_callback), 
    153            (r'<[\w:.-]*>', Name.Tag), 
    154            (r'<[^(<>\s")]+', Name.Tag, 'tag'), 
    155            (r'([^(^{")\s]+)', Text), 
    156        ], 
    157        'string': [ 
    158            (r'[^(^")]+', String), 
    159            (escape_re, String.Escape), 
    160            (r'[(|)]+', String), 
    161            (r'\^.', String.Escape), 
    162            (r'"', String, '#pop'), 
    163        ], 
    164        'string2': [ 
    165            (r'[^(^{})]+', String), 
    166            (escape_re, String.Escape), 
    167            (r'[(|)]+', String), 
    168            (r'\^.', String.Escape), 
    169            (r'\{', String, '#push'), 
    170            (r'\}', String, '#pop'), 
    171        ], 
    172        'stringFile': [ 
    173            (r'[^(^")]+', Name.Decorator), 
    174            (escape_re, Name.Decorator), 
    175            (r'\^.', Name.Decorator), 
    176            (r'"', Name.Decorator, '#pop'), 
    177        ], 
    178        'char': [ 
    179            (escape_re + '"', String.Char, '#pop'), 
    180            (r'\^."', String.Char, '#pop'), 
    181            (r'."', String.Char, '#pop'), 
    182        ], 
    183        'tag': [ 
    184            (escape_re, Name.Tag), 
    185            (r'"', Name.Tag, 'tagString'), 
    186            (r'[^(<>\r\n")]+', Name.Tag), 
    187            (r'>', Name.Tag, '#pop'), 
    188        ], 
    189        'tagString': [ 
    190            (r'[^(^")]+', Name.Tag), 
    191            (escape_re, Name.Tag), 
    192            (r'[(|)]+', Name.Tag), 
    193            (r'\^.', Name.Tag), 
    194            (r'"', Name.Tag, '#pop'), 
    195        ], 
    196        'tuple': [ 
    197            (r'(\d+\.)+', Keyword.Constant), 
    198            (r'\d+', Keyword.Constant, '#pop'), 
    199        ], 
    200        'bin2': [ 
    201            (r'\s+', Number.Hex), 
    202            (r'([01]\s*){8}', Number.Hex), 
    203            (r'\}', Number.Hex, '#pop'), 
    204        ], 
    205        'commentString1': [ 
    206            (r'[^(^")]+', Comment), 
    207            (escape_re, Comment), 
    208            (r'[(|)]+', Comment), 
    209            (r'\^.', Comment), 
    210            (r'"', Comment, '#pop'), 
    211        ], 
    212        'commentString2': [ 
    213            (r'[^(^{})]+', Comment), 
    214            (escape_re, Comment), 
    215            (r'[(|)]+', Comment), 
    216            (r'\^.', Comment), 
    217            (r'\{', Comment, '#push'), 
    218            (r'\}', Comment, '#pop'), 
    219        ], 
    220        'commentBlock': [ 
    221            (r'\[', Comment, '#push'), 
    222            (r'\]', Comment, '#pop'), 
    223            (r'"', Comment, "commentString1"), 
    224            (r'\{', Comment, "commentString2"), 
    225            (r'[^(\[\]"{)]+', Comment), 
    226        ], 
    227    } 
    228 
    229    def analyse_text(text): 
    230        """ 
    231        Check if code contains REBOL header and so it probably not R code 
    232        """ 
    233        if re.match(r'^\s*REBOL\s*\[', text, re.IGNORECASE): 
    234            # The code starts with REBOL header 
    235            return 1.0 
    236        elif re.search(r'\s*REBOL\s*\[', text, re.IGNORECASE): 
    237            # The code contains REBOL header but also some text before it 
    238            return 0.5 
    239 
    240 
    241class RedLexer(RegexLexer): 
    242    """ 
    243    A Red-language lexer. 
    244    """ 
    245    name = 'Red' 
    246    aliases = ['red', 'red/system'] 
    247    filenames = ['*.red', '*.reds'] 
    248    mimetypes = ['text/x-red', 'text/x-red-system'] 
    249    url = 'https://www.red-lang.org' 
    250    version_added = '2.0' 
    251 
    252    flags = re.IGNORECASE | re.MULTILINE 
    253 
    254    escape_re = r'(?:\^\([0-9a-f]{1,4}\)*)' 
    255 
    256    def word_callback(lexer, match): 
    257        word = match.group() 
    258 
    259        if re.match(".*:$", word): 
    260            yield match.start(), Generic.Subheading, word 
    261        elif re.match(r'(if|unless|either|any|all|while|until|loop|repeat|' 
    262                      r'foreach|forall|func|function|does|has|switch|' 
    263                      r'case|reduce|compose|get|set|print|prin|equal\?|' 
    264                      r'not-equal\?|strict-equal\?|lesser\?|greater\?|lesser-or-equal\?|' 
    265                      r'greater-or-equal\?|same\?|not|type\?|stats|' 
    266                      r'bind|union|replace|charset|routine)$', word): 
    267            yield match.start(), Name.Builtin, word 
    268        elif re.match(r'(make|random|reflect|to|form|mold|absolute|add|divide|multiply|negate|' 
    269                      r'power|remainder|round|subtract|even\?|odd\?|and~|complement|or~|xor~|' 
    270                      r'append|at|back|change|clear|copy|find|head|head\?|index\?|insert|' 
    271                      r'length\?|next|pick|poke|remove|reverse|select|sort|skip|swap|tail|tail\?|' 
    272                      r'take|trim|create|close|delete|modify|open|open\?|query|read|rename|' 
    273                      r'update|write)$', word): 
    274            yield match.start(), Name.Function, word 
    275        elif re.match(r'(yes|on|no|off|true|false|tab|cr|lf|newline|escape|slash|sp|space|null|' 
    276                      r'none|crlf|dot|null-byte)$', word): 
    277            yield match.start(), Name.Builtin.Pseudo, word 
    278        elif re.match(r'(#system-global|#include|#enum|#define|#either|#if|#import|#export|' 
    279                      r'#switch|#default|#get-definition)$', word): 
    280            yield match.start(), Keyword.Namespace, word 
    281        elif re.match(r'(system|halt|quit|quit-return|do|load|q|recycle|call|run|ask|parse|' 
    282                      r'raise-error|return|exit|break|alias|push|pop|probe|\?\?|spec-of|body-of|' 
    283                      r'quote|forever)$', word): 
    284            yield match.start(), Name.Exception, word 
    285        elif re.match(r'(action\?|block\?|char\?|datatype\?|file\?|function\?|get-path\?|zero\?|' 
    286                      r'get-word\?|integer\?|issue\?|lit-path\?|lit-word\?|logic\?|native\?|' 
    287                      r'op\?|paren\?|path\?|refinement\?|set-path\?|set-word\?|string\?|unset\?|' 
    288                      r'any-struct\?|none\?|word\?|any-series\?)$', word): 
    289            yield match.start(), Keyword, word 
    290        elif re.match(r'(JNICALL|stdcall|cdecl|infix)$', word): 
    291            yield match.start(), Keyword.Namespace, word 
    292        elif re.match("to-.*", word): 
    293            yield match.start(), Keyword, word 
    294        elif re.match(r'(\+|-\*\*|-|\*\*|//|/|\*|and|or|xor|=\?|===|==|=|<>|<=|>=|' 
    295                      r'<<<|>>>|<<|>>|<|>%)$', word): 
    296            yield match.start(), Operator, word 
    297        elif re.match(r".*\!$", word): 
    298            yield match.start(), Keyword.Type, word 
    299        elif re.match("'.*", word): 
    300            yield match.start(), Name.Variable.Instance, word  # lit-word 
    301        elif re.match("#.*", word): 
    302            yield match.start(), Name.Label, word  # issue 
    303        elif re.match("%.*", word): 
    304            yield match.start(), Name.Decorator, word  # file 
    305        elif re.match(":.*", word): 
    306            yield match.start(), Generic.Subheading, word  # get-word 
    307        else: 
    308            yield match.start(), Name.Variable, word 
    309 
    310    tokens = { 
    311        'root': [ 
    312            (r'\s+', Text), 
    313            (r'#"', String.Char, 'char'), 
    314            (r'#\{[0-9a-f\s]*\}', Number.Hex), 
    315            (r'2#\{', Number.Hex, 'bin2'), 
    316            (r'64#\{[0-9a-z+/=\s]*\}', Number.Hex), 
    317            (r'([0-9a-f]+)(h)((\s)|(?=[\[\]{}"()]))', 
    318             bygroups(Number.Hex, Name.Variable, Whitespace)), 
    319            (r'"', String, 'string'), 
    320            (r'\{', String, 'string2'), 
    321            (r';#+.*\n', Comment.Special), 
    322            (r';\*+.*\n', Comment.Preproc), 
    323            (r';.*\n', Comment), 
    324            (r'%"', Name.Decorator, 'stringFile'), 
    325            (r'%[^(^{")\s\[\]]+', Name.Decorator), 
    326            (r'[+-]?([a-z]{1,3})?\$\d+(\.\d+)?', Number.Float),  # money 
    327            (r'[+-]?\d+\:\d+(\:\d+)?(\.\d+)?', String.Other),    # time 
    328            (r'\d+[\-/][0-9a-z]+[\-/]\d+(/\d+:\d+((:\d+)?' 
    329             r'([\.\d+]?([+-]?\d+:\d+)?)?)?)?', String.Other),   # date 
    330            (r'\d+(\.\d+)+\.\d+', Keyword.Constant),             # tuple 
    331            (r'\d+X\d+', Keyword.Constant),                   # pair 
    332            (r'[+-]?\d+(\'\d+)?([.,]\d*)?E[+-]?\d+', Number.Float), 
    333            (r'[+-]?\d+(\'\d+)?[.,]\d*', Number.Float), 
    334            (r'[+-]?\d+(\'\d+)?', Number), 
    335            (r'[\[\]()]', Generic.Strong), 
    336            (r'[a-z]+[^(^{"\s:)]*://[^(^{"\s)]*', Name.Decorator),  # url 
    337            (r'mailto:[^(^{"@\s)]+@[^(^{"@\s)]+', Name.Decorator),  # url 
    338            (r'[^(^{"@\s)]+@[^(^{"@\s)]+', Name.Decorator),         # email 
    339            (r'comment\s"', Comment, 'commentString1'), 
    340            (r'comment\s\{', Comment, 'commentString2'), 
    341            (r'comment\s\[', Comment, 'commentBlock'), 
    342            (r'comment\s[^(\s{"\[]+', Comment), 
    343            (r'/[^(^{^")\s/[\]]*', Name.Attribute), 
    344            (r'([^(^{^")\s/[\]]+)(?=[:({"\s/\[\]])', word_callback), 
    345            (r'<[\w:.-]*>', Name.Tag), 
    346            (r'<[^(<>\s")]+', Name.Tag, 'tag'), 
    347            (r'([^(^{")\s]+)', Text), 
    348        ], 
    349        'string': [ 
    350            (r'[^(^")]+', String), 
    351            (escape_re, String.Escape), 
    352            (r'[(|)]+', String), 
    353            (r'\^.', String.Escape), 
    354            (r'"', String, '#pop'), 
    355        ], 
    356        'string2': [ 
    357            (r'[^(^{})]+', String), 
    358            (escape_re, String.Escape), 
    359            (r'[(|)]+', String), 
    360            (r'\^.', String.Escape), 
    361            (r'\{', String, '#push'), 
    362            (r'\}', String, '#pop'), 
    363        ], 
    364        'stringFile': [ 
    365            (r'[^(^")]+', Name.Decorator), 
    366            (escape_re, Name.Decorator), 
    367            (r'\^.', Name.Decorator), 
    368            (r'"', Name.Decorator, '#pop'), 
    369        ], 
    370        'char': [ 
    371            (escape_re + '"', String.Char, '#pop'), 
    372            (r'\^."', String.Char, '#pop'), 
    373            (r'."', String.Char, '#pop'), 
    374        ], 
    375        'tag': [ 
    376            (escape_re, Name.Tag), 
    377            (r'"', Name.Tag, 'tagString'), 
    378            (r'[^(<>\r\n")]+', Name.Tag), 
    379            (r'>', Name.Tag, '#pop'), 
    380        ], 
    381        'tagString': [ 
    382            (r'[^(^")]+', Name.Tag), 
    383            (escape_re, Name.Tag), 
    384            (r'[(|)]+', Name.Tag), 
    385            (r'\^.', Name.Tag), 
    386            (r'"', Name.Tag, '#pop'), 
    387        ], 
    388        'tuple': [ 
    389            (r'(\d+\.)+', Keyword.Constant), 
    390            (r'\d+', Keyword.Constant, '#pop'), 
    391        ], 
    392        'bin2': [ 
    393            (r'\s+', Number.Hex), 
    394            (r'([01]\s*){8}', Number.Hex), 
    395            (r'\}', Number.Hex, '#pop'), 
    396        ], 
    397        'commentString1': [ 
    398            (r'[^(^")]+', Comment), 
    399            (escape_re, Comment), 
    400            (r'[(|)]+', Comment), 
    401            (r'\^.', Comment), 
    402            (r'"', Comment, '#pop'), 
    403        ], 
    404        'commentString2': [ 
    405            (r'[^(^{})]+', Comment), 
    406            (escape_re, Comment), 
    407            (r'[(|)]+', Comment), 
    408            (r'\^.', Comment), 
    409            (r'\{', Comment, '#push'), 
    410            (r'\}', Comment, '#pop'), 
    411        ], 
    412        'commentBlock': [ 
    413            (r'\[', Comment, '#push'), 
    414            (r'\]', Comment, '#pop'), 
    415            (r'"', Comment, "commentString1"), 
    416            (r'\{', Comment, "commentString2"), 
    417            (r'[^(\[\]"{)]+', Comment), 
    418        ], 
    419    }