quasilyte / phpgrep

Syntax-aware grep for PHP code.
MIT License
236 stars 10 forks source link

unable to use phpgrep as library #70

Open ervishal opened 3 years ago

ervishal commented 3 years ago

getting error "use of internal package github.com/quasilyte/phpgrep/internal/phpgrep not allowed"

Readme says phpgrep can be used as command as well as library but its not, please help us getting the right way or there is any fix required ?

quasilyte commented 3 years ago

Right now it's not easy to use phpgrep as a library since the matching code is now implemented in noverify/phpgrep.

You can try using github.com/VKCOM/noverify as a dependency.

ervishal commented 3 years ago

I checked it but did not see way to use phpgrep as package and use exported function. Do you have any references?

Do I need to use phpgrep as cmd using os/exec.Command function only? Then it should take care of phpgrep installation as binary.

On Wed, 24 Feb 2021, 06:55 Iskander (Alex) Sharipov, < notifications@github.com> wrote:

Right now it's not easy to use phpgrep as a library since the matching code is now implemented in noverify/phpgrep https://github.com/VKCOM/noverify/tree/master/src/phpgrep.

You can try using github.com/VKCOM/noverify as a dependency.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/quasilyte/phpgrep/issues/70#issuecomment-784663856, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACXN3JCWFS7I6UYKCVB4SYTTARIRLANCNFSM4YC63KTQ .

quasilyte commented 3 years ago

Here is how you add it as a library: https://github.com/quasilyte/phpgrep/blob/master/go.mod#L6

Then you import it: https://github.com/quasilyte/phpgrep/blob/3a5ded728b2a604df42007acd4da2f8ff5e3ca62/internal/phpgrep/program.go#L23

And then you use it: https://github.com/quasilyte/phpgrep/blob/3a5ded728b2a604df42007acd4da2f8ff5e3ca62/internal/phpgrep/program.go#L122-L125

phpgrep operates on IR rather than raw AST, so the conversion step is needed as well when matching: https://github.com/quasilyte/phpgrep/blob/971627f24a6974ec8008694f954b20840259bdcc/internal/phpgrep/worker.go#L46-L52

If you can explain how would you like to use it, maybe we can make that use case less inconvenient.

ervishal commented 3 years ago

thanks @quasilyte. basically I do want to match particular exception statement from php Class method and similar method with different name can exists in different files(php classes) in a directory.

protected function doDelete()
    {
        throw ErrorUtility::exceptionFromString("SYNTAX::DELETE_UNSUPPORTED::Unsupported method", 405);
    }

using phpgrep as command line and with pattern '${"x:call"}' 'x~exceptionFromString' I am able to see get matches but there are more patterns I want to add to this like it should contain 405 too.

same thing as a library (as instructed by you above) I am getting no matches or sometimes syntax error at line 1

                var c phpgrep.Compiler
        c.CaseSensitive = false
        pattern := `'${"x:call"}' 'x~exceptionFromString'`
        match, err := c.Compile([]byte(pattern))
        checkErr(err)
        result, ok := match.Match(mustParse(string(src)))

Appreciating your help.

ervishal commented 3 years ago

@quasilyte please check my comment above with example, this isnot giving any matches. getting nil [] matches. whichever pattern I use '${"x:call"}' or 'x~exceptionFromString' this is not working using library.