A VCL (Varnish Configuration Language) parser implemented in Go that parses VCL files into Abstract Syntax Trees (AST).
- Complete lexical analysis of VCL syntax
- Recursive descent parser with error recovery
- Type-safe AST representation
- Post-parse resolution of include statements
- Symbol table and semantic analysis
- Visitor pattern for AST traversal
- VMOD and variable semantics loaded from varnishd build
Semantics like what variables are available in a given context are defined in the metadata package, in a JSON file that
is generated by the generate.py
script inside varnishd. This file is embedded into the library at compile time.
VMOD semantics are loaded from a collection of VCC files in vcclib
. These are embedded into the library at compile
time.
package main
import (
"fmt"
"log"
"github.com/varnish/vclparser/pkg/parser"
)
func main() {
vclCode := `
vcl 4.0;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
if (req.method == "GET") {
return (hash);
}
}
`
ast, err := parser.Parse(vclCode)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Parsed VCL with %d declarations\n", len(ast.Declarations))
}
pkg/lexer/
- Lexical analysis and tokenizationpkg/ast/
- AST node definitions and visitor patternpkg/parser/
- Recursive descent parser implementationpkg/types/
- Type system and symbol tableexamples/
- Usage examplestests/testdata/
- Test VCL files
This parser supports the full VCL language including:
- Version declarations (
vcl 4.0;
) - Backend definitions with properties
- Access Control Lists (ACLs)
- Probe definitions
- Subroutine definitions
- All VCL statements (if/else, set, unset, call, return, etc.)
- Expression parsing with proper operator precedence
- Built-in variables and functions
- C-code blocks (C{ }C)
go test ./...