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

unable to use phpgrep as library #70

Open
ervishal opened this issue Feb 23, 2021 · 5 comments
Open

unable to use phpgrep as library #70

ervishal opened this issue Feb 23, 2021 · 5 comments

Comments

@ervishal
Copy link

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
Copy link
Owner

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
Copy link
Author

ervishal commented Feb 24, 2021 via email

@quasilyte
Copy link
Owner

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

Then you import it:

"github.com/VKCOM/noverify/src/phpgrep"

And then you use it:

var c phpgrep.Compiler
c.CaseSensitive = p.args.caseSensitive
m, err := c.Compile([]byte(p.args.pattern))

phpgrep operates on IR rather than raw AST, so the conversion step is needed as well when matching:

func (w *worker) parseFile(data []byte) (*ir.Root, error) {
root, err := parseutil.ParseFile(data)
if err != nil {
return nil, err
}
return w.irconv.ConvertRoot(root), nil
}

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

@ervishal
Copy link
Author

ervishal commented Feb 25, 2021

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
Copy link
Author

ervishal commented Mar 2, 2021

@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.

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