Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for custom Token definiton entry #2

Open
markopaden opened this issue May 29, 2019 · 2 comments
Open

Allow for custom Token definiton entry #2

markopaden opened this issue May 29, 2019 · 2 comments

Comments

@markopaden
Copy link

Currently, this module only allows adding definitions by adding TokenDefn objects. TokenDefn constructor has this line:
$this->regex = sprintf('%s^%s%s%s', $delimiter, $regex, $delimiter, $modifiers);
Sometimes, you don't want to add ^ into your expression. There should be a way to insert regex token only by giving it regex(string) and token name.

@tmilos
Copy link
Owner

tmilos commented May 29, 2019

Any suggestion how to define such regex in array config like below?

$config = new LexerArrayConfig([
            '\\s' => '',
            '\\d+' => 'number',
            '\\+' => 'plus',
            '-' => 'minus',
            '\\*' => 'mul',
            '/' => 'div',
        ]);

@markopaden
Copy link
Author

markopaden commented Jun 13, 2019

None really. I made CustomTokenDefn class that extends TokenDefn:

class CustomTokenDefn extends TokenDefn
{
    /**
     * CustomTokenDefn constructor.
     *
     * @param string $name
     * @param string $regex
     */
    public function __construct(string $name, string $regex)
    {
        parent::__construct($name, $regex);
        $this->name = $name;
        $this->regex = $regex;
    }
}

and I can add that to the config with addTokenDefinition() function.
Still, the problem remains since order of tokens is important.
Also, your php doc here:

/**
     * @param TokenDefn[] $tokenDefinitions
     */
    public function __construct(array $tokenDefinitions)
    {
        foreach ($tokenDefinitions as $k => $v) {
            if ($v instanceof TokenDefn) {
                $this->addTokenDefinition($v);
            } elseif (is_string($k) && is_string($v)) {
                $this->addTokenDefinition(new TokenDefn($v, $k));
            }
        }
    } 

says that the constructor accepts only an array of TokenDefn object, while it also accepts normal assoc array (even in your example). That makes PHPstan throw an error. Correct phpdoc would be:

/**
     * @param TokenDefn[]|array $tokenDefinitions
     */

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

No branches or pull requests

2 participants