final class Preprocessor
Public API: explicitly declared with @visibility public.

Settles %if, %ifdef, %ifndef, %else and %endif before the file is scanned.

As in Lemon's preprocess_input, a directive counts only at the start of a line, the directive lines and the excluded lines are blanked rather than removed, so every position in the result is a position in the original, and an excluded region may nest further directives.

Blanking a region that is not defineddoctest
$preprocessor = new \LemonParser\Preprocessor\Preprocessor();
    $preprocessor->preprocess("a ::= B.\n%ifdef X\na ::= C.\n%endif\na ::= D.\n", []) // => "a ::= B.\n        \n        \n      \na ::= D.\n"
    $preprocessor->preprocess("%ifndef X || Y\na ::= C.\n%endif\n", ['Y']) // => "              \n        \n      \n"

Methods§

public function preprocess(string $source, list<string> $defines): string

Replaces excluded regions and directive lines with spaces.

Parameters

$sourcestringThe grammar file
$defineslist<string>The names defined on the command line

Returns

string The file with the same line breaks and directives settled

Throws

SyntaxException When a region is not closed or an expression is not well formed
Test cases 6
Called from 1
Calls 11
public function settle(
    string $source,
    int $index,
    string $directive,
    Exclusion $exclusion,
    Condition $condition,
    Location $location,
): string

Applies one directive to the excluded region and blanks the region it closes.

Parameters

$sourcestringThe file
$indexintWhere the `%` is
$directivestring`endif`, `else`, `if`, `ifdef` or `ifndef`
$exclusionExclusionThe region being excluded, if any
$conditionConditionEvaluates expressions
$locationLocationWhere the directive is

Returns

string The file with a closed region blanked
Test cases 2
Called from 1
Calls 7
public function directive(string $source, int $index): string|null

Recognises the directive at a % as Lemon does.

Parameters

$sourcestringThe file
$indexintWhere the `%` is

Returns

string|null `endif`, `else`, `ifdef`, `if`, `ifndef`, or null for anything else
Test cases 1
Called from 1
Calls 3
public function opens(
    Condition $condition,
    string $source,
    int $index,
    string $directive,
    Location $location,
): bool

Evaluates an opening directive and says whether it excludes what follows.

Parameters

$conditionConditionEvaluates expressions
$sourcestringThe file
$indexintWhere the `%` is
$directivestring`if`, `ifdef` or `ifndef`
$locationLocationWhere the directive is

Returns

bool True when the region is excluded
Test cases 1
Called from 1
Calls 4
public function lineEnd(string $source, int $index): int

Finds where the line holding an index ends.

Parameters

$sourcestringThe file
$indexintAn index in the line

Returns

int The index of the newline, or the length of the file
Test cases 1
Called from 2
Calls 2
public function blank(string $source, int $from, int $to): string

Replaces every byte but newlines with a space.

Parameters

$sourcestringThe file
$fromintThe first index to blank
$tointThe index after the last one

Returns

string The file with the region blanked
Test cases 1
Called from 3
Calls 2

Test cases 15§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 12
Other tests reaching this symbol 3

Relations§

Instantiated in 1
Method calls 1
Type declarations 1