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

Types 2

Configuration for the CF parser.

Controls parser behavior including include directive handling and security settings.

Fields
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 used
bool allowAbsolutePathsWhether to allow absolute paths in includes (security risk)
bool allowParentTraversalWhether to allow parent directory traversal (..) in includes
string delegate(string path) @safe fileReaderFile reader delegate for include resolution If null, includes are disabled regardless of enableIncludes
bool 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 set
structCfParser

Recursive-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);

Fields
Lexer lexer
Token currentToken
Token peekedToken
bool hasPeeked
bool initialized
size_t nestingDepth
string sourceFilename
CfComment[] pendingComments
string[] includeStack
size_t includeDepth
Methods
Token current() @safe pureReturns the current token without consuming it.
Token peek() @safe purePeeks at the next token without consuming it.
void advance() @safe pureAdvances to the next token.
bool match(TokenType type) @safe pureChecks if the current token matches the expected type.
bool matchAny(TokenType[] types...) @safe pureChecks if the current token matches any of the expected types.
Token expect(TokenType type) @safe pureConsumes the current token if it matches the expected type.
SeparatorStyle skipSeparators() @safe pureSkips separator tokens (newlines, commas, semicolons) between members/elements.
CfComment[] collectComments() @safe pureCollects pending comment tokens into an array.
void enterNesting() @safe pureIncrements nesting depth, checking against the maximum limit.
void exitNesting() @safe pureDecrements nesting depth.
CfDocument parseDocument() @safeParses a complete CF document.
void parseObjectMembers(CfNode * node, bool explicit) @safeParses object members into the given node.
CfMember parseMember() @safeParses a single key-value member.
CfNode * parseValue() @safeParses a value of any type.
CfNode * parseObject() @safeParses an object literal.
void parseInclude(CfNode * targetNode) @safeParses an include directive.
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 * parseArray() @safeParses an array literal.
CfNode * parseStringNode() @safeParses a string literal node.
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.
CfNode * parseIntegerNode() @safe pureParses an integer literal node.
CfNode * parseFloatNode() @safe pureParses a float literal node.
CfNode * parseBooleanNode() @safe pureParses a boolean literal node.
CfNode * parseNullNode() @safe pureParses a null literal node.
CfNode * parseInfinityNode(bool negative) @safe pureParses an infinity literal node.
CfNode * parseNanNode() @safe pureParses a NaN literal node.
CfNode * parseDateNode() @safe pureParses a date literal node.
CfNode * parseTimeNode() @safe pureParses a time literal node.
CfNode * parseDateTimeNode() @safe pureParses a datetime literal node.
long parseIntegerValue(string raw) @safe pureParses an integer value string, handling various bases and underscores.
long parseDecimal(string s) @safe pure nothrowParses a decimal integer string.
long parseHex(string s) @safe pure nothrowParses a hexadecimal integer string.
long parseOctal(string s) @safe pure nothrowParses an octal integer string.
long parseBinary(string s) @safe pure nothrowParses a binary integer string.
double parseFloatValue(string raw) @safe pureParses a float value string, stripping underscores.
double parseFloatManual(string s) @safe pure nothrowManually parses a float string.
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.
private string stripTripleQuotedIndent(string content) @safe pureStrips indentation from a triple-quoted string.
private string decodeEscapes(string s) @safe pureDecodes escape sequences in a string.
private bool isValidHex(string s) @safe pure nothrowCheck if string contains only valid hex digits
private dchar parseHexCodepoint(string hex) @safe pureParse hex string to codepoint
private void encodeUTF8(Appender)(ref Appender result, dchar c) @safe pureEncode a Unicode codepoint as UTF-8
private string decodeEscapedDollar(string s) @safe pureDecodes escaped dollar signs (\$) to literal $. Called after env var substitution or when substitution is disabled.
CfComment makeComment(Token tok) @safe pureCreates a CfComment from a comment token.
string tokenTypeName(TokenType type) @safe pure nothrowReturns a human-readable name for a token type.
string intToStr(size_t n) @safe pure nothrowConverts an integer to a string without using std.conv.
Constructors
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

enumvarMAX_NESTING_DEPTH = 64

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

enumvarMAX_INCLUDE_DEPTH = 32

Maximum include depth.

This limit is enforced to prevent excessive include chains as recommended in CF-SPEC.md section 10.3.