classPreprocessor
final class Preprocessor
@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.
$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): stringReplaces excluded regions and directive lines with spaces.
Parameters
$source | string | The grammar file |
$defines | list<string> | The names defined on the command line |
Returns
string The file with the same line breaks and directives settledThrows
SyntaxException When a region is not closed or an expression is not well formedTest cases 6
PreprocessorTest::testPreprocesscallsPreprocessorTest::testPreprocessCountsDirectivesOnlyAtTheStartOfALinecallsPreprocessorTest::testPreprocessNestsExcludedRegionscallsPreprocessorTest::testPreprocessRejectsABadExpressioncallsPreprocessorTest::testPreprocessRejectsAnUnterminatedRegioncallsPreprocessorTest::testPreprocessSettlesADirectiveOnTheFirstLineOfAFileWithoutAFinalNewlinecalls
Called from 1
Calls 11
- new Condition line 39
- new Exclusion line 40
- function-call
strlenline 42 - method-call Preprocessor::directive() line 50
- method-call Preprocessor::settle() line 54
- new Location line 54
- method-call Preprocessor::blank() line 55
- method-call Preprocessor::lineEnd() line 55
- method-call Exclusion::depth() line 57
- new SyntaxException line 58
- method-call Exclusion::startLine() line 58
public function settle(
string $source,
int $index,
string $directive,
Exclusion $exclusion,
Condition $condition,
Location $location,
): stringApplies one directive to the excluded region and blanks the region it closes.
Parameters
Returns
string The file with a closed region blankedTest cases 2
Called from 1
Calls 7
- method-call Exclusion::depth() line 79
- method-call Exclusion::leave() line 79
- method-call Preprocessor::blank() line 79
- method-call Exclusion::start() line 79
- method-call Exclusion::enter() line 88
- method-call Exclusion::nest() line 94
- method-call Preprocessor::opens() line 95
public function directive(string $source, int $index): string|nullRecognises the directive at a % as Lemon does.
Parameters
$source | string | The file |
$index | int | Where the `%` is |
Returns
string|null `endif`, `else`, `ifdef`, `if`, `ifndef`, or null for anything elseTest cases 1
Called from 1
public function opens(
Condition $condition,
string $source,
int $index,
string $directive,
Location $location,
): boolEvaluates an opening directive and says whether it excludes what follows.
Parameters
Returns
bool True when the region is excludedTest cases 1
Called from 1
Calls 4
- method-call Preprocessor::lineEnd() line 139
- function-call
strcspnline 140 - method-call Condition::evaluate() line 141
- function-call
substrline 141
public function lineEnd(string $source, int $index): intFinds where the line holding an index ends.
Parameters
$source | string | The file |
$index | int | An index in the line |
Returns
int The index of the newline, or the length of the fileTest cases 1
Called from 2
public function blank(string $source, int $from, int $to): stringReplaces every byte but newlines with a space.
Parameters
$source | string | The file |
$from | int | The first index to blank |
$to | int | The index after the last one |
Returns
string The file with the region blankedTest cases 1
Called from 3
- method-call LemonParser\Preprocessor\Preprocessor::preprocess() packages/lemon-parser/src/Preprocessor/Preprocessor.php:55
- method-call LemonParser\Preprocessor\Preprocessor::settle() packages/lemon-parser/src/Preprocessor/Preprocessor.php:79
- method-call LemonParser\Preprocessor\Preprocessor::settle() packages/lemon-parser/src/Preprocessor/Preprocessor.php:85
Test cases 15§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 12
PreprocessorTest::testBlankcallsPreprocessorTest::testDirectivecallsPreprocessorTest::testLineEndcallsPreprocessorTest::testOpenscallsPreprocessorTest::testPreprocesscallsPreprocessorTest::testPreprocessCountsDirectivesOnlyAtTheStartOfALinecallsPreprocessorTest::testPreprocessNestsExcludedRegionscallsPreprocessorTest::testPreprocessRejectsABadExpressioncallsPreprocessorTest::testPreprocessRejectsAnUnterminatedRegioncallsPreprocessorTest::testPreprocessSettlesADirectiveOnTheFirstLineOfAFileWithoutAFinalNewlinecallsPreprocessorTest::testSettlecallsPreprocessorTest::testSettleNestsInsideAnExcludedRegioncalls
Other tests reaching this symbol 3
LemonGrammarCompilerTestcallsLemonGrammarReaderTestcallsParserTestcalls