Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Feature: Added support to inject a Lexer builder so custom SubLexers can be added. #11

Open
wants to merge 1 commit into
base: 0.2
Choose a base branch
from

Conversation

taccommandeur
Copy link

  • Added the Lexer factory for creating Lexer objects.
  • Added the Lexer builder interface for overriding the way Lexer objects are built.
  • Added unit tests for the Lexer factory.

- Added the Lexer builder interface for overriding the way Lexer objects are built.
- Added unit tests for the Lexer factory.
@taccommandeur
Copy link
Author

Usage of the added feature:

class LexerWithCustomChainBuilder implements LexerBuilderInterface
{
    /**
     * {@inheritdoc}
     */
    public function build()
    {
        // Return a lexer with a custom chain.
        return new Lexer($this->createSubLexerChain());
    }

    public function createSubLexerChain(): SubLexerChain
    {
        return (new SubLexerChain())
            ->addSubLexer(new SubLexer\ConstantSubLexer())
            ->addSubLexer(new SubLexer\PunctuationSubLexer())
            ->addSubLexer(new SubLexer\FiqlOperatorSubLexer())
            ->addSubLexer(new SubLexer\RqlOperatorSubLexer())
            ->addSubLexer(new SubLexer\TypeSubLexer())

            ->addSubLexer(new SubLexer\GlobSubLexer())
            ->addSubLexer(new SomeCustomSubLexer()) // We want to add a custom lexer in the lexer chain.
            ->addSubLexer(new SubLexer\StringSubLexer())
            ->addSubLexer(new SubLexer\DatetimeSubLexer())
            ->addSubLexer(new SubLexer\NumberSubLexer())

            ->addSubLexer(new SubLexer\SortSubLexer());
    }
}
// This will make the parser use lexers with our custom chain.
LexerFactory::setLexerBuilder(new LexerWithCustomChainBuilder());

When not overriding the builder the lexer will simply use the default builder.

@andreas-glaser
Copy link
Owner

Sweet thanks! Could you please base this PR on the new 0.3 branch and add unit tests to your code?

@taccommandeur
Copy link
Author

Yes I will rebase it shortly. There are already two unit tests in the LexerFactoryTest class which check if both the default behaviour and the override work. These are based on the 0.2 branch so I will check if these can be left unchanged for 0.3.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants