Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

varnish: support Fastly's dialect #1454

Merged
merged 9 commits into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
File renamed without changes.
47 changes: 33 additions & 14 deletions lib/rouge/lexers/varnish.rb → lib/rouge/lexers/vcl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@

module Rouge
module Lexers
class Varnish < RegexLexer
title 'Varnish'
desc 'The Varnish (high-performance web accelerator) configuration language'
class VCL < RegexLexer
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
title 'VCL: Varnish Configuration Language'
desc 'The Configuration Language of Varnish HTTP Cache (https://varnish-cache.org/)'

tag 'varnish'
aliases 'varnishconf', 'VCL'
tag 'vcl'
aliases 'varnishconf', 'varnish'
filenames '*.vcl'
mimetypes 'text/x-varnish'
mimetypes 'text/x-vcl', 'text/x-varnish'

INUM = %r/[0-9]+/
FNUM = %r/#{INUM}+(?:\.#{INUM}(?:e[\+\-]?#{INUM})?|e[\+\-]?#{INUM})/i
HNUM = %r/[0-9a-f]+/i
HINUM = %r/0x#{HNUM}/i
HFNUM = %r/0x#{HNUM}(?:\.#{HNUM}(?:p[\+\-]?#{HNUM})?|(?:p[\+\-]?#{HNUM})?)/i

LNUM = '[0-9]+'
DNUM = '([0-9]*"."[0-9]+)|([0-9]+"."[0-9]*)'
SPACE = '[ \f\n\r\t\v]+'

# backend acl
def self.keywords
@keywords ||= Set.new %w[
vcl set unset include import if else elseif elif elsif director probe
backend acl

declare local
BOOL FLOAT INTEGER IP RTIME STRING TIME
]
end

Expand Down Expand Up @@ -65,14 +72,23 @@ def self.variables
# long strings ({" ... "})
rule %r/\{".*?"}/m, Str::Single

# heredoc style long strings ({xyz"..."xyz})
rule %r/\{(\w+)".*?"(\1)}/m, Str::Single

# comments
rule %r'/\*.*?\*/'m, Comment::Multiline
rule %r'(?://|#).*', Comment::Single

rule %r/true|false/, Keyword::Constant

# "wildcard variables"
rule %r/(?:(?:be)?re(?:sp|q)|obj)\.http\.[\w.-]+/ do
var_prefix = Regexp.union(%w(beresp bereq resp req obj))
rule %r/(?:#{var_prefix})\.http\.[\w.-]+/ do
token Name::Variable
end

# local variables (var.*)
rule %r/(?:var)\.[\w.-]+/ do
token Name::Variable
end

Expand All @@ -86,20 +102,23 @@ def self.variables
push :inline_c
end

rule %r/[a-z_.-]+/i do |m|
rule %r/\.?[a-z_][a-z_0-9\.\-]*/i do |m|
next token Keyword if self.class.keywords.include? m[0]
next token Name::Function if self.class.functions.include? m[0]
next token Name::Variable if self.class.variables.include? m[0]
token Text
end

# duration
rule %r/(?:#{LNUM}|#{DNUM})(?:ms|[smhdwy])/, Num::Other
duration_suffix = Regexp.union(%w(ms s m h d w y))
rule %r/(?:#{INUM}|#{FNUM}|#{HINUM}|#{HFNUM})(?:#{duration_suffix})/, Num::Other
# size in bytes
rule %r/#{LNUM}[KMGT]?B/, Num::Other
rule %r/#{INUM}[KMGT]?B/, Num::Other
# literal numeric values (integer/float)
rule %r/#{LNUM}/, Num::Integer
rule %r/#{DNUM}/, Num::Float
rule %r/#{FNUM}/, Num::Float
rule %r/#{HFNUM}/, Num::Float
rule %r/#{INUM}/, Num::Integer
rule %r/#{HINUM}/, Num::Integer

# standard strings
rule %r/"/, Str::Double, :string
Expand Down
9 changes: 5 additions & 4 deletions spec/lexers/varnish_spec.rb → spec/lexers/vcl_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
describe Rouge::Lexers::Varnish do
let(:subject) { Rouge::Lexers::Varnish.new }
describe Rouge::Lexers::VCL do
let(:subject) { Rouge::Lexers::VCL.new }

describe 'guessing' do
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => 'varnish.vcl'
assert_guess :filename => 'config.vcl'
end

it 'guesses by mimetype' do
assert_guess :mimetype => 'text/x-vcl'
assert_guess :mimetype => 'text/x-varnish'
end
end
Expand All @@ -17,7 +18,7 @@
include Support::Lexing

it 'forwards C blocks to C lexer' do
assert_tokens_equal 'foo C{int}C bar', ['Text', 'foo '], [ 'Comment.Preproc', 'C{' ], [ 'Keyword.Type', 'int' ], [ 'Comment.Preproc', '}C' ], [ 'Text', ' bar' ]
assert_tokens_equal 'foo C{int}C bar', ['Text', 'foo '], [ 'Comment.Preproc', 'C{' ], [ 'Keyword.Type', 'int' ], [ 'Comment.Preproc', '}C' ], [ 'Text', ' bar' ]
end
end
end
23 changes: 23 additions & 0 deletions spec/visual/samples/varnish → spec/visual/samples/vcl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# VCL: https://varnish-cache.org/docs/trunk/users-guide/vcl.html

# Define a function that converts a string to lower-case in-place.
C{
#include <ctype.h>
Expand Down Expand Up @@ -38,3 +40,24 @@ sub vcl_synth {

# Fall-through to default
}

#### Fastly's dialect ####

# reference: https://docs.fastly.com/vcl/

# local variables
declare local var.ip1 IP;
set var.ip1 = "192.0.2.0";

# string literals
set var.s = {xyz"Hello, world!"xyz}; # heredoc-style

# more literals
set resp.http.X-Str = {"Hello, world!"};
set resp.http.X-Rtime = 10ms;
set resp.http.X-RTime = 2.5h;
set resp.http.X-Int = 0xFF;
set resp.http.X-Float = 1.2e3;
set resp.http.X-Float = 1e2;
set resp.http.X-HexFloat = 0xA.Bp3;
set resp.http.X-HexFloat = 0xAp3;