classLemonGrammarReader
final class LemonGrammarReader
@visibility public.Turns a Lemon grammar file into the grammar the automaton is built from.
The file is read with lemon-parser after its %ifdef regions are settled with the names given; this class keeps what the automaton needs. %token names terminals, %left, %right and %nonassoc rank them, %token_class declarations and A|B positions become token classes, %fallback, %wildcard and %start_symbol are recorded for the parser, a [PREC] mark names a rule's precedence, and any upper-case name on a right-hand side is a terminal.
$grammar = (new \SqlParser\Compiler\LemonGrammarReader())->read("%left PLUS.\nexpr ::= expr PLUS|MINUS expr.\nexpr ::= NUM.\n");
[count($grammar->rules), $grammar->symbols->name($grammar->startSymbol())] // => [3, "expr"]$source = "%ifndef OMIT\ncmd ::= EXTRA.\n%endif\ncmd ::= SELECT.\n";
[count((new \SqlParser\Compiler\LemonGrammarReader())->read($source)->rules), count((new \SqlParser\Compiler\LemonGrammarReader())->read($source, ['OMIT'])->rules)] // => [3, 2]Methods§
public function __construct(private Parser $parser = new \LemonParser\Parser())public function read(string $source, list<string> $defines = []): GrammarReads a grammar file.
Parameters
$source | string | The text of the `.y` file |
$defines | list<string> | Names defined for `%ifdef`, as Lemon's `-D` option defines them |
Returns
Grammar The grammar with Lemon's precedence policyThrows
SyntaxException When the file is not a grammar Lemon acceptsGrammarException When the grammar is inconsistentUnknownSymbolException When a declaration names an unknown symbolTest cases 3
Calls 7
- method-call Parser::parse() line 70
- new GrammarBuilder line 71
- method-call GrammarBuilder::policy() line 72
- class-const PrecedencePolicy::FirstRankedTerminal() line 72
- method-call LemonGrammarReader::rule() line 75
- method-call LemonGrammarReader::declaration() line 77
- method-call GrammarBuilder::build() line 81
public function declaration(Declaration $declaration, GrammarBuilder $builder): voidRecords what a declaration says about terminals, precedence, token classes, fallbacks, the wildcard or the start symbol.
Parameters
$declaration | Declaration | The declaration |
$builder | GrammarBuilder | Collects the grammar |
Test cases 1
Called from 1
Calls 11
- method-call GrammarBuilder::precedence() line 93
- method-call LemonGrammarReader::names() line 93
- method-call LemonGrammarReader::associativity() line 93
- method-call GrammarBuilder::terminal() line 96
- method-call Declaration::fallback() line 98
- method-call GrammarBuilder::fallback() line 99
- method-call Declaration::tokens() line 99
- method-call GrammarBuilder::wildcard() line 101
- method-call GrammarBuilder::tokenClass() line 103
- class-const DirectiveKeyword::StartSymbol() line 104
- method-call GrammarBuilder::start() line 105
public function associativity(Associativity $associativity): AssociativityMaps Lemon's associativity onto the grammar's.
Parameters
$associativity | Associativity | As declared |
Returns
Associativity As the automaton resolves conflictsTest cases 1
Called from 1
Calls 6
- class-const Associativity::Left() line 119
- class-const Associativity::Left() line 119
- class-const Associativity::Right() line 120
- class-const Associativity::Right() line 120
- class-const Associativity::NonAssoc() line 121
- class-const Associativity::NonAssoc() line 121
public function rule(Rule $rule, GrammarBuilder $builder): voidAdds a rule, turning a position shared by several terminals into a token class.
Parameters
$rule | Rule | The rule |
$builder | GrammarBuilder | Collects the grammar |
Test cases 1
Called from 1
Calls 6
- method-call LemonGrammarReader::names() line 135
- function-call
implodeline 136 - method-call GrammarBuilder::isTerminal() line 137
- method-call GrammarBuilder::tokenClass() line 138
- method-call GrammarBuilder::terminal() line 140
- method-call GrammarBuilder::rule() line 144
public function names(list<Symbol> $symbols): list<string>Lists the names of symbols.
Parameters
$symbols | list<Symbol> | The symbols |
Returns
list<string> Their names in orderTest cases 1
Called from 4
- method-call SqlParser\Compiler\LemonGrammarReader::declaration() packages/sql-parser/src/Compiler/LemonGrammarReader.php:93
- method-call SqlParser\Compiler\LemonGrammarReader::declaration() packages/sql-parser/src/Compiler/LemonGrammarReader.php:99
- method-call SqlParser\Compiler\LemonGrammarReader::declaration() packages/sql-parser/src/Compiler/LemonGrammarReader.php:103
- method-call SqlParser\Compiler\LemonGrammarReader::rule() packages/sql-parser/src/Compiler/LemonGrammarReader.php:135
Calls 1
- function-call
array_mapline 156
Private surface 1§
Implementation details, listed for orientation only.
private Parser $parser = new \LemonParser\Parser()Test cases 7§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 7
LemonGrammarReaderTest::testAssociativitycallsLemonGrammarReaderTest::testDeclarationcallsLemonGrammarReaderTest::testNamescallsLemonGrammarReaderTest::testReadcallsLemonGrammarReaderTest::testReadRejectsWhatLemonRejectscallsLemonGrammarReaderTest::testReadSettlesConditionalRegionscallsLemonGrammarReaderTest::testRulecalls