Skip to main content

Crate nginx_lint_parser

Crate nginx_lint_parser 

Source
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

§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.