marcioAlmada / yay

Yay is a high level PHP preprocessor
https://github.com/marcioAlmada/yay
MIT License
572 stars 35 forks source link

Proposal: parser for function modifiers #52

Closed assertchris closed 6 years ago

assertchris commented 6 years ago

Do you think it would be a good idea to add a parser for something like this?

optional(
    repeat(
        either(
            token(T_PUBLIC),
            token(T_PROTECTED),
            token(T_PRIVATE),
            token(T_STATIC)
        )
    )
)

...and if so, how would you recommend I do this?

marcioAlmada commented 6 years ago

I think it's fine to add this as a new function at parsers.php:

function method_modifiers(): Parser {
    return optional(
        repeat(
            either(
                token(T_PUBLIC),
                token(T_PROTECTED),
                token(T_PRIVATE),
                token(T_STATIC)
            )
        )
    );
}

You may or may not add a check for duplicated modifiers as in:

public public static function bar() // [...]
marcioAlmada commented 6 years ago

Actually, nevermind about the extra checks. It seems PHP goes for a compilation error rather than a syntax error https://3v4l.org/ZZJpB. And the same goes for contradictory modifiers as in private public foo().

assertchris commented 6 years ago

Ok, I'll make a PR for this and some tests.

assertchris commented 6 years ago

Going to submit as PR if I get a solution that looks good.