dex.cf.parser
CF Parser - Recursive-Descent Parser for CF Configuration Files
This module provides a parser that consumes tokens from the lexer and produces a CfDocument representing the concrete syntax tree (CST) of the CF source.
The parser implements a recursive-descent strategy and supports:
- Implicit top-level objects (no braces required at root)
- Comment preservation and attachment to nodes
- Multiple separator styles (comma, semicolon, newline)
- Nesting depth limiting for security (max 64 levels)
- Detailed error reporting with source locations
- Include directive with security mitigations
Copyright
© 2025 Dejan LekićBSD-3-Clause
Types 2
Configuration for the CF parser.
Controls parser behavior including include directive handling and security settings.
bool enableIncludesWhether to enable include directive processing (Extended feature level)string includeBaseDirBase directory for resolving relative include paths If empty, the current file's directory is usedbool allowAbsolutePathsWhether to allow absolute paths in includes (security risk)bool allowParentTraversalWhether to allow parent directory traversal (..) in includesstring delegate(string path) @safe fileReaderFile reader delegate for include resolution If null, includes are disabled regardless of enableIncludesbool enableEnvSubstitutionWhether to enable environment variable substitution (Extended feature level)string delegate(string varName) @safe envReaderEnvironment variable reader delegate If null, uses a default implementation that returns empty string Signature: (varName) => value or null if not setRecursive-descent parser for CF source text.
Consumes tokens from a Lexer and produces a CfDocument that preserves all metadata needed for roundtrip fidelity including comments, locations, and original formatting.
Example:
auto parser = CfParser(`key = "value"`);
auto doc = parser.parseDocument();
assert(doc.root.members.length == 1);Lexer lexerToken currentTokenToken peekedTokenbool hasPeekedbool initializedsize_t nestingDepthstring sourceFilenameCfComment[] pendingCommentsCfParserConfig configstring[] includeStacksize_t includeDepthbool matchAny(TokenType[] types...) @safe pureChecks if the current token matches any of the expected types.SeparatorStyle skipSeparators() @safe pureSkips separator tokens (newlines, commas, semicolons) between members/elements.void parseObjectMembers(CfNode * node, bool explicit) @safeParses object members into the given node.void validateIncludePath(string path, Location loc) @safe pureValidates an include path for security issues.bool containsParentTraversal(string path) @safe pure nothrowChecks if a path contains parent directory traversal.string resolveIncludePath(string includePath) @safe pureResolves an include path relative to the current file.CfNode * parseEnvVarNode() @safeParses a standalone environment variable reference as a string node.string substituteEnvVarsInString(string input, Location loc) @safeSubstitutes environment variables in a string.string substituteEnvVar(string raw, Location loc) @safeSubstitutes a single environment variable reference.string getEnvValue(string varName) @safeGets an environment variable value using the configured reader.bool isVarChar(char c) @safe pure nothrowChecks if a character is valid in an environment variable name.long parseIntegerValue(string raw) @safe pureParses an integer value string, handling various bases and underscores.string stripUnderscores(string s) @safe pure nothrowStrips underscore separators from a numeric string.QuoteStyle detectQuoteStyle(string raw) @safe pure nothrowDetects the quote style from a raw string token.string parseStringValue(string raw) @safe pureParses the content of a string token to extract the decoded value.string stripTripleQuotedIndent(string content) @safe pureStrips indentation from a triple-quoted string.void encodeUTF8(Appender)(ref Appender result, dchar c) @safe pureEncode a Unicode codepoint as UTF-8string decodeEscapedDollar(string s) @safe pureDecodes escaped dollar signs (\$) to literal $. Called after env var substitution or when substitution is disabled.string tokenTypeName(TokenType type) @safe pure nothrowReturns a human-readable name for a token type.this(string input, string filename = "", CfParserConfig cfg = CfParserConfig.init)Constructs a parser for the given CF source text.this(string input, string filename, CfParserConfig cfg, string[] existingStack, size_t depth)Constructs a parser with an existing include stack (for nested includes).Variables 2
MAX_NESTING_DEPTH = 64Maximum nesting depth for objects and arrays.
This limit is enforced to prevent stack overflow attacks from deeply nested structures as recommended in CF-SPEC.md section 10.3.
MAX_INCLUDE_DEPTH = 32Maximum include depth.
This limit is enforced to prevent excessive include chains as recommended in CF-SPEC.md section 10.3.