Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
varnish.py190 linesDownload Raw Back to lexers
1"""2    pygments.lexers.varnish3    ~~~~~~~~~~~~~~~~~~~~~~~4 5    Lexers for Varnish configuration6 7    :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.8    :license: BSD, see LICENSE for details.9"""10 11from pygments.lexer import RegexLexer, include, bygroups, using, this, \12    inherit, words13from pygments.token import Text, Comment, Operator, Keyword, Name, String, \14    Number, Punctuation, Literal, Whitespace15 16__all__ = ['VCLLexer', 'VCLSnippetLexer']17 18 19class VCLLexer(RegexLexer):20    """21    For Varnish Configuration Language (VCL).22    """23    name = 'VCL'24    aliases = ['vcl']25    filenames = ['*.vcl']26    mimetypes = ['text/x-vclsrc']27    url = 'https://www.varnish-software.com/developers/tutorials/varnish-configuration-language-vcl'28    version_added = '2.2'29 30    def analyse_text(text):31        # If the very first line is 'vcl 4.0;' it's pretty much guaranteed32        # that this is VCL33        if text.startswith('vcl 4.0;'):34            return 1.035        # Skip over comments and blank lines36        # This is accurate enough that returning 0.9 is reasonable.37        # Almost no VCL files start without some comments.38        elif '\nvcl 4.0;' in text[:1000]:39            return 0.940 41    tokens = {42        'probe': [43            include('whitespace'),44            include('comments'),45            (r'(\.\w+)(\s*=\s*)([^;]*)(;)',46             bygroups(Name.Attribute, Operator, using(this), Punctuation)),47            (r'\}', Punctuation, '#pop'),48        ],49        'acl': [50            include('whitespace'),51            include('comments'),52            (r'[!/]+', Operator),53            (r';', Punctuation),54            (r'\d+', Number),55            (r'\}', Punctuation, '#pop'),56        ],57        'backend': [58            include('whitespace'),59            (r'(\.probe)(\s*=\s*)(\w+)(;)',60             bygroups(Name.Attribute, Operator, Name.Variable.Global, Punctuation)),61            (r'(\.probe)(\s*=\s*)(\{)',62             bygroups(Name.Attribute, Operator, Punctuation), 'probe'),63            (r'(\.\w+\b)(\s*=\s*)([^;\s]*)(\s*;)',64             bygroups(Name.Attribute, Operator, using(this), Punctuation)),65            (r'\{', Punctuation, '#push'),66            (r'\}', Punctuation, '#pop'),67        ],68        'statements': [69            (r'(\d\.)?\d+[sdwhmy]', Literal.Date),70            (r'(\d\.)?\d+ms', Literal.Date),71            (r'(vcl_pass|vcl_hash|vcl_hit|vcl_init|vcl_backend_fetch|vcl_pipe|'72             r'vcl_backend_response|vcl_synth|vcl_deliver|vcl_backend_error|'73             r'vcl_fini|vcl_recv|vcl_purge|vcl_miss)\b', Name.Function),74            (r'(pipe|retry|hash|synth|deliver|purge|abandon|lookup|pass|fail|ok|'75             r'miss|fetch|restart)\b', Name.Constant),76            (r'(beresp|obj|resp|req|req_top|bereq)\.http\.[a-zA-Z_-]+\b', Name.Variable),77            (words((78                'obj.status', 'req.hash_always_miss', 'beresp.backend', 'req.esi_level',79                'req.can_gzip', 'beresp.ttl', 'obj.uncacheable', 'req.ttl', 'obj.hits',80                'client.identity', 'req.hash_ignore_busy', 'obj.reason', 'req.xid',81                'req_top.proto', 'beresp.age', 'obj.proto', 'obj.age', 'local.ip',82                'beresp.uncacheable', 'req.method', 'beresp.backend.ip', 'now',83                'obj.grace', 'req.restarts', 'beresp.keep', 'req.proto', 'resp.proto',84                'bereq.xid', 'bereq.between_bytes_timeout', 'req.esi',85                'bereq.first_byte_timeout', 'bereq.method', 'bereq.connect_timeout',86                'beresp.do_gzip',  'resp.status', 'beresp.do_gunzip',87                'beresp.storage_hint', 'resp.is_streaming', 'beresp.do_stream',88                'req_top.method', 'bereq.backend', 'beresp.backend.name', 'beresp.status',89                'req.url', 'obj.keep', 'obj.ttl', 'beresp.reason', 'bereq.retries',90                'resp.reason', 'bereq.url', 'beresp.do_esi', 'beresp.proto', 'client.ip',91                'bereq.proto', 'server.hostname', 'remote.ip', 'req.backend_hint',92                'server.identity', 'req_top.url', 'beresp.grace', 'beresp.was_304',93                'server.ip', 'bereq.uncacheable'), suffix=r'\b'),94             Name.Variable),95            (r'[!%&+*\-,/<.}{>=|~]+', Operator),96            (r'[();]', Punctuation),97 98            (r'[,]+', Punctuation),99            (words(('hash_data', 'regsub', 'regsuball', 'if', 'else',100                    'elsif', 'elif', 'synth', 'synthetic', 'ban',101                    'return', 'set', 'unset', 'import', 'include', 'new',102                    'rollback', 'call'), suffix=r'\b'),103             Keyword),104            (r'storage\.\w+\.\w+\b', Name.Variable),105            (words(('true', 'false')), Name.Builtin),106            (r'\d+\b', Number),107            (r'(backend)(\s+\w+)(\s*\{)',108             bygroups(Keyword, Name.Variable.Global, Punctuation), 'backend'),109            (r'(probe\s)(\s*\w+\s)(\{)',110             bygroups(Keyword, Name.Variable.Global, Punctuation), 'probe'),111            (r'(acl\s)(\s*\w+\s)(\{)',112             bygroups(Keyword, Name.Variable.Global, Punctuation), 'acl'),113            (r'(vcl )(4.0)(;)$',114             bygroups(Keyword.Reserved, Name.Constant, Punctuation)),115            (r'(sub\s+)([a-zA-Z]\w*)(\s*\{)',116                bygroups(Keyword, Name.Function, Punctuation)),117            (r'([a-zA-Z_]\w*)'118             r'(\.)'119             r'([a-zA-Z_]\w*)'120             r'(\s*\(.*\))',121             bygroups(Name.Function, Punctuation, Name.Function, using(this))),122            (r'[a-zA-Z_]\w*', Name),123        ],124        'comment': [125            (r'[^*/]+', Comment.Multiline),126            (r'/\*', Comment.Multiline, '#push'),127            (r'\*/', Comment.Multiline, '#pop'),128            (r'[*/]', Comment.Multiline),129        ],130        'comments': [131            (r'#.*$', Comment),132            (r'/\*', Comment.Multiline, 'comment'),133            (r'//.*$', Comment),134        ],135        'string': [136            (r'"', String, '#pop'),137            (r'[^"\n]+', String),  # all other characters138        ],139        'multistring': [140            (r'[^"}]', String),141            (r'"\}', String, '#pop'),142            (r'["}]', String),143        ],144        'whitespace': [145            (r'L?"', String, 'string'),146            (r'\{"', String, 'multistring'),147            (r'\n', Whitespace),148            (r'\s+', Whitespace),149            (r'\\\n', Text),  # line continuation150        ],151        'root': [152            include('whitespace'),153            include('comments'),154            include('statements'),155            (r'\s+', Whitespace),156        ],157    }158 159 160class VCLSnippetLexer(VCLLexer):161    """162    For Varnish Configuration Language snippets.163    """164    name = 'VCLSnippets'165    aliases = ['vclsnippets', 'vclsnippet']166    mimetypes = ['text/x-vclsnippet']167    filenames = []168    url = 'https://www.varnish-software.com/developers/tutorials/varnish-configuration-language-vcl'169    version_added = '2.2'170 171    def analyse_text(text):172        # override method inherited from VCLLexer173        return 0174 175    tokens = {176        'snippetspre': [177            (r'\.\.\.+', Comment),178            (r'(bereq|req|req_top|resp|beresp|obj|client|server|local|remote|'179             r'storage)($|\.\*)', Name.Variable),180        ],181        'snippetspost': [182            (r'(backend)\b', Keyword.Reserved),183        ],184        'root': [185            include('snippetspre'),186            inherit,187            include('snippetspost'),188        ],189    }190 
codekingpro/portable-devtools · Team Ai