Expand description
nginx configuration file parser
This crate provides a parser for nginx configuration files, producing an AST suitable for lint rules and autofix. It accepts any directive name, so extension modules (ngx_headers_more, lua-nginx-module, etc.) are supported without special configuration.
§Quick Start
use nginx_lint_parser::parse_string;
let config = parse_string("http { server { listen 80; } }").unwrap();
for directive in config.all_directives() {
println!("{} at line {}", directive.name, directive.span.start.line);
}To parse from a file on disk:
use std::path::Path;
use nginx_lint_parser::parse_config;
let config = parse_config(Path::new("/etc/nginx/nginx.conf")).unwrap();§Modules
ast— AST types:ast::Config,ast::Directive,ast::Block,ast::Argument,ast::Span,ast::Positionerror— Error types:error::ParseError
§Common Patterns
§Iterating over directives
Config::directives() yields only top-level directives.
Config::all_directives() recurses into blocks:
let config = parse_string("http { gzip on; server { listen 80; } }").unwrap();
// Top-level only → ["http"]
let top: Vec<_> = config.directives().map(|d| &d.name).collect();
assert_eq!(top, vec!["http"]);
// Recursive → ["http", "gzip", "server", "listen"]
let all: Vec<_> = config.all_directives().map(|d| &d.name).collect();
assert_eq!(all, vec!["http", "gzip", "server", "listen"]);§Checking arguments
let config = parse_string("server_tokens off;").unwrap();
let dir = config.directives().next().unwrap();
assert!(dir.is("server_tokens"));
assert_eq!(dir.first_arg(), Some("off"));
assert!(dir.args[0].is_off());
assert!(dir.args[0].is_literal());§Inspecting blocks
let config = parse_string("upstream backend { server 127.0.0.1:8080; }").unwrap();
let upstream = config.directives().next().unwrap();
if let Some(block) = &upstream.block {
for inner in block.directives() {
println!("{}: {}", inner.name, inner.first_arg().unwrap_or(""));
}
}Modules§
- ast
- AST types for nginx configuration files.
- context
- Context-aware directive traversal.
- error
- Error types for the nginx configuration parser.
- lexer_
rowan - Rowan-compatible lexer for nginx configuration files.
- line_
index - Byte-offset → line/column conversion for rowan CST nodes.
- parser
- Rowan-based recursive-descent parser for nginx configuration files.
- rowan_
to_ ast - Convert a rowan lossless CST into the existing AST types.
- syntax_
kind - Syntax kind definitions for the rowan-based nginx config parser.
Functions§
- is_
block_ directive - Check if a directive is a known block directive that requires
{instead of; - is_
block_ directive_ with_ extras - Check if a directive is a block directive, including custom additions
- is_
raw_ block_ cst_ node - Check if a CST BLOCK node belongs to a raw block directive (e.g.
*_by_lua_block). - is_
raw_ block_ directive - Check if a directive name indicates a raw block (Lua code, etc.)
- parse_
config - Parse a nginx configuration file from disk
- parse_
string - Parse nginx configuration from a string
- parse_
string_ rowan - Parse a source string into a rowan lossless concrete syntax tree.
- parse_
string_ with_ errors - Parse nginx configuration from a string, returning AST even when syntax errors exist.