mini\Parsing\GenericParser
final
class
Documentation
GenericParser
A small, general-purpose, lossless structural parser for arbitrary strings.
The parser walks an input string and builds a lightweight syntax tree:
- Text spans are represented as TextNode instances.
- Delimited regions (quotes and bracket pairs) are represented as DelimitedNode instances.
- The root of the tree is a NodeList, which:
- implements ArrayAccess, IteratorAggregate and Countable,
- can be cast to string to reconstruct the original input,
- allows
$tree[1]to access the second root-level node.
Configuration is provided to the constructor:
$quotes: list of characters that start/end quoted regions (e.g. ['"', "'", '`']).$escapeStyle: how strings are escaped:- GenericParser::ESCAPE_NONE : no escape handling.
- GenericParser::ESCAPE_C : C-style backslash escapes (" \ \n \r \t ...).
- GenericParser::ESCAPE_JSON : JSON-style backslash escapes (subset of C-style).
- GenericParser::ESCAPE_QUOTE_DOUBLING : quote-doubling ("" or '' inside strings).
$pairs: associative array of opening => closing delimiters (e.g. ['(' => ')', '[' => ']', '{' => '}']).
Example:
$parser = new GenericParser( quotes: ['"', "'", '`'], escapeStyle: GenericParser::ESCAPE_C, pairs: ['(' => ')', '[' => ']', '{' => '}'] );
$tree = $parser->parse($input);
echo $tree; // outputs the exact string that was parsed echo $tree[1]; // second root-level node
This parser is intentionally minimal: it only understands quotes and bracket pairs. Everything else is left as raw text. Quoted regions are treated as opaque: their contents are not further structured into DelimitedNode instances; they simply contain a single TextNode child.
Constants (4)
| Name | Value |
|---|---|
ESCAPE_NONE |
'none' |
ESCAPE_C |
'c' |
ESCAPE_JSON |
'json' |
ESCAPE_QUOTE_DOUBLING |
'double' |
Properties (6)
array $quotes
array $pairs
string $escapeStyle
string $input
int $length
int $pos
Methods (6)
Use same char for symmetric: ['"' => '"', "'" => "'"]
Create a parser configured for SQL
Parse the given string into a NodeList tree.
Parse until the end of the string or until one of the given delimiters is encountered.
Parse a delimited region starting at the current position.
Parse a quoted region starting at the current position.