summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKristian Lyngstol <kly@kly.no>2016-02-08 13:01:31 +0100
committerKristian Lyngstol <kly@kly.no>2016-02-08 13:01:31 +0100
commita1e6b33a5cb8f74f005d17f61b6bb1051259e8af (patch)
tree1b1e9287005c0411e9214e1b4b6e6f21cefcb5c3
parent0c812483363f57bc812f979d70d6b6773da6135f (diff)
downloadpygments-a1e6b33a5cb8f74f005d17f61b6bb1051259e8af.tar.gz
Add Varnish lexers, VCL and snippets
VCLLexer is meant for pure VCL files that are syntactically correct. VCLSnippet is used for example-code and pseudo-code, where shorthands like req.* is sensible. Think of it like VCLLexer being for .. code:: blocks, while VCLSnippetLexer is for in-line.
-rw-r--r--pygments/lexers/_mapping.py2
-rw-r--r--pygments/lexers/varnish.py173
2 files changed, 175 insertions, 0 deletions
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py
index 0a6b4965..b34fcc18 100644
--- a/pygments/lexers/_mapping.py
+++ b/pygments/lexers/_mapping.py
@@ -396,6 +396,8 @@ LEXERS = {
'TypoScriptHtmlDataLexer': ('pygments.lexers.typoscript', 'TypoScriptHtmlData', ('typoscripthtmldata',), (), ()),
'TypoScriptLexer': ('pygments.lexers.typoscript', 'TypoScript', ('typoscript',), ('*.ts', '*.txt'), ('text/x-typoscript',)),
'UrbiscriptLexer': ('pygments.lexers.urbi', 'UrbiScript', ('urbiscript',), ('*.u',), ('application/x-urbiscript',)),
+ 'VCLLexer': ('pygments.lexers.varnish', 'VCL', ('vcl',), ('*.vcl',), ('text/x-vclsrc',)),
+ 'VCLSnippetLexer': ('pygments.lexers.varnish', 'VCLSnippets', ('vclsnippets', 'vclsnippet'), ('*.vcl',), ('text/x-vclsnippet',)),
'VCTreeStatusLexer': ('pygments.lexers.console', 'VCTreeStatus', ('vctreestatus',), (), ()),
'VGLLexer': ('pygments.lexers.dsls', 'VGL', ('vgl',), ('*.rpf',), ()),
'ValaLexer': ('pygments.lexers.c_like', 'Vala', ('vala', 'vapi'), ('*.vala', '*.vapi'), ('text/x-vala',)),
diff --git a/pygments/lexers/varnish.py b/pygments/lexers/varnish.py
new file mode 100644
index 00000000..53f3f41f
--- /dev/null
+++ b/pygments/lexers/varnish.py
@@ -0,0 +1,173 @@
+# -*- coding: utf-8 -*-
+"""
+ pygments.lexers.varnish
+ ~~~~~~~~~~~~~~~~~~~~~~
+
+ Lexers for Varnish configuration
+
+ :copyright: Copyright 2016 by the Pygments team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+import re
+
+from pygments.lexer import RegexLexer, include, bygroups, using, this, inherit, words, \
+ default
+from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
+ Number, Punctuation
+
+from pygments.lexers.c_cpp import CLexer, CppLexer
+from pygments.lexers import _mql_builtins
+
+__all__ = ['VCLLexer', 'VCLSnippetLexer']
+
+class VCLLexer(CLexer):
+ """
+ For Varnish Configuration Language (VCL).
+
+ """
+ name = 'VCL'
+ aliases = ['vcl']
+ filenames = [ '*.vcl' ]
+ mimetypes = ['text/x-vclsrc']
+
+ tokens = {
+ 'time': [
+ (r'\d+(s|d|w|h|m)',Number)
+ ],
+ 'probe': [
+ (r'(\s*\.\w+)(\s*=\s*)([^;]*)(;)',
+ bygroups(Name.Attribute, Operator, using(this), Punctuation)),
+ (r'\s*}', Punctuation, '#pop')
+ ],
+ 'backend': [
+ include('whitespace'),
+ (r'(\s*)(\.host|\.port)(\s*=\s*)([^;]*)(\s*;)',
+ bygroups(Punctuation, Name.Attribute, Operator, using(this), Punctuation)),
+ (r'(\s*\.probe)(\s*=\s*)(\w+)(;)',
+ bygroups(Name.Attribute,Operator,Name.Variable.Global,Punctuation)),
+ (r'(\s*\.probe)(\s*=\s*)({)',
+ bygroups(Name.Attribute,Operator,Punctuation),'probe'),
+ (r'{',Punctuation,'#push'),
+ (r'}',Punctuation,'#pop')
+ ],
+ 'statements': [
+ include('time'),
+ (r'[~!%^&*+=|?:<>/-]', Operator),
+ (r'\s*(hash_data)(\()(.+)(\)\s*;\s*$)',
+ bygroups(Keyword, Punctuation, using(this), Punctuation)),
+ (r'(\s*set\s)([^\s]+)(\s*=\s*)(.+)(\s*;\s*)($|#.*$|//.*$|/\*.*$)',
+ bygroups(Keyword, Name.Variable, Punctuation, using(this), Punctuation, using(this))),
+ (r'(\s*unset\s)(\s*[^\s]+)(\s*;)',
+ bygroups(Keyword, Name.Variable, Punctuation)),
+ (r'(\s*regsub\s*)(\()(.*)(,)(.*)(,)(.*)(\))',
+ bygroups(Keyword, Punctuation, using(this), Punctuation,
+ using(this), Punctuation, using(this), Punctuation)),
+ (r'(\s*regsuball\s*)(\()(.*)(,)(.*)(,)(.*)(\))',
+ bygroups(Keyword, Punctuation, using(this), Punctuation,
+ using(this), Punctuation, using(this), Punctuation)),
+ (r'(import\s)(\w+)(;\s*)$',
+ bygroups(Keyword, Name.Variable.Global, Punctuation)),
+ (words(('vcl_recv','vcl_pipe','vcl_pass','vcl_hash','vcl_purge',
+ 'vcl_hit','vcl_miss','vcl_deliver','vcl_synth','vcl_backend_fetch',
+ 'vcl_backend_response','vcl_backend_error','vcl_init','vcl_fini'),
+ suffix=r'\b'),Name.Function),
+ (words(('if','else','elsif','synth',
+ 'synthetic'), suffix=r'\b'),Keyword),
+ (words(('true','false')),Name.Builtin),
+ (r'(\s*call \s*)([^\s;]+)(;)',
+ bygroups(Keyword,Name.Variable.Global,Punctuation)),
+ (r'obj.ttl',Name.Variable),
+ (r'(req|bereq|obj|resp|beresp)\.http\.[^\s]+',Name.Variable),
+ (r'(req|bereq)\.(url|method|xid)',Name.Variable),
+ (r'(resp|beresp|obj)\.(status|reason)',Name.Variable),
+ (r'(beresp|obj)\.(ttl|grace)',Name.Variable),
+ (r'(backend )(\w*)(\s*{)',
+ bygroups(Keyword, Name.Variable.Global, Punctuation), 'backend'),
+ (r'(\s*probe )(\s*\w+\s)({)',
+ bygroups(Keyword,Name.Variable.Global,Punctuation),'probe'),
+ (r'[();]', Punctuation),
+ (r'(client|server)\.(ip|identity)',Name.Variable),
+ (r'^(vcl )(4.0)(;)$',
+ bygroups(Keyword.Reserved,Name.Constant,Punctuation)),
+ ],
+ 'sub': [
+ include('whitespace'),
+ include('comments'),
+ include('returns'),
+ include('statements'),
+ (r'\s*\{\s*',Punctuation,'#push'),
+ (r'\s*\}\s*',Punctuation,'#pop')
+ ],
+ 'comment': [
+ (r'[^*/]', Comment.Multiline),
+ (r'/\*', Comment.Multiline, '#push'),
+ (r'\*/', Comment.Multiline, '#pop'),
+ (r'[*/]', Comment.Multiline)
+ ],
+ 'comments': [
+ (r'#.*$', Comment),
+ (r'/\*', Comment.Multiline, 'comment'),
+ (r'//.*$', Comment)
+ ],
+ 'string': [
+ (r'"', String, '#pop'),
+ (r'[^\\"\n]+', String), # all other characters
+
+ ],
+ 'multistring': [
+ (r'[^"}]', String),
+ (r'{"', String, '#push'),
+ (r'"}', String, '#pop'),
+ (r'["}]', String)
+ ],
+ 'whitespace': [
+ (r'L?"', String, 'string'),
+ (r'{"', String, 'multistring'),
+ (r'\n', Text),
+ (r'\s+', Text),
+ (r'\\\n', Text) # line continuation
+ ],
+ 'returns': [
+ (r'(\s*return )(\()(hash|lookup|ok|deliver|miss|fetch|pass|pipe)(\)\s*;$)',
+ bygroups(Keyword, Punctuation, Name.Constant, Punctuation)),
+ (r'(\s*return )(\()(\s*synth\s*)(\()(\s*\d+\s*)(,)([^)]+)(\)\s*\)\s*;)',
+ bygroups(Keyword, Punctuation, Keyword, Punctuation,Number,Punctuation,using(this),Punctuation)),
+ (r'(\s*return )(\()(\s*synth\s*)(\()(\s*\d+\s*)(\)\s*\)\s*;)',
+ bygroups(Keyword, Punctuation, Keyword, Punctuation,Number,Punctuation))
+ ],
+ 'root': [
+ include('whitespace'),
+ include('comments'),
+ include('returns'),
+ (r'(sub )(\w+)(\s*{)',
+ bygroups(Keyword, Name.Function, Punctuation),'sub'),
+ include('statements'),
+ (r'\s+', Text)
+ ],
+ }
+
+class VCLSnippetLexer(VCLLexer):
+ """
+ For Varnish Configuration Language snippets.
+ """
+
+ name = 'VCLSnippets'
+ aliases = ['vclsnippets', 'vclsnippet']
+ mimetypes = ['text/x-vclsnippet']
+ tokens = {
+ 'snippetspre': [
+ (r'\<variable\>', Name.Variable),
+ (r'\<value\>', Name.Variable)
+ ],
+ 'snippetspost': [
+ (r'(req|bereq|obj|resp|beresp|client|server)(\.http)?\.\*',Name.Variable),
+ (r'(req|bereq|obj|resp|beresp|client|server)',Name.Variable),
+ (r'(backend)', Keyword.Reserved)
+ ],
+ 'root': [
+ include('snippetspre'),
+ inherit,
+ include('snippetspost')
+ ]
+ }