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

Expose helper functions and allow AST manipulation #45

Open
erikgeiser opened this issue Nov 17, 2020 · 0 comments
Open

Expose helper functions and allow AST manipulation #45

erikgeiser opened this issue Nov 17, 2020 · 0 comments

Comments

@erikgeiser
Copy link

Hi, great library you have here! We are building a file globbing library (https://github.com/goreleaser/fileglob) on top of glob which required us to write some helper functions that operate on glob/syntax/ast. Maybe some of these functions are useful to your users and could be exposed directly in the glob library.

Consider the following example (source code can be found here: https://github.com/goreleaser/fileglob/blob/main/prefix.go):

  • ContainsMatchers: Returns true if the pattern contains matchers and false otherwise. This is useful to determine whether the pattern directly references a file or multiple files via matchers.

  • UnquoteMeta: For patterns without matchers, return the static text represented by the pattern. This is useful to determine which file is directly referenced by a pattern that may contain escaped matchers.

Currently, using these functions in our package results in the pattern being parsed multiple times: Once for every function and then finally for the actual compilation.

Also, it would be useful to allow for AST manipulation. This could be used to inject globbing options, perform queries on the AST (like the functions above) or to extend the functionality of this library. https://github.com/antonmedv/expr uses a great pattern for this, the Patcher: https://github.com/antonmedv/expr/blob/master/docs/Visitor-and-Patch.md. In glob this could be done by passing a variadic list of function operating on the AST to the Compile function:

var containsMatchers bool

# MustCompile(string, func(rootNode ...*ast.Node))
glob.MustCompile("/foo/*", ContainsMatchers(&containsMatchers))

What do you think? If you think these ideas are not a good fit for glob you can close this issue.

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

1 participant