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
Idea: Node Pattern sample-based generator #81
Comments
Yes, I want to have an easy way to check a node pattern. I also was thinking of showing the compiled code (maybe side by side with the node pattern) and have it show in color for executed / not executed, to make it easier to understand non matches. I was even thinking of putting an executable in the gem (maybe |
I have worked in something like this on fast: https://jonatas.github.io/fast/similarity_tutorial/ I made expression_from part of the gem and you can also use Maybe instead of generating a very generic expression we can generate several about the same code. I also implemented one very smart shortcut to find "any reference" about some wording: # Search all references about some word
Fast.shortcut(:ref) do
Kernel.class_eval do
def matches_args? identifier
search = ARGV.last
regex = Regexp.new(search, Regexp::IGNORECASE)
case identifier
when Symbol, String
regex.match?(identifier) || identifier.to_s.include?(search)
when Parser::AST::Node
regex.match?(identifier.to_sexp)
end
end
end
pattern = <<~FAST
{
({class def sym str} #matches_args? ...)
({const send} {nil? _} #matches_args? ...)
}
FAST
Fast::Cli.run!([pattern, '.', '--parallel'])
end |
That's already something to start with. |
@pirj you mean the example of the shortcut I shared "# Search all references about some word" or from the I'd love to contribute to the project, especially to introduce a basic CLI application to let more people know about the node pattern facilities :) |
I meant RSpec examples of how a sample-based node pattern generator would work: describe 'NP::G' do
it 'generates' do
expect(
generate('hello.hello(1) { }', 'foo.foo(2) { }')
).to eq '(block (send (send nil? _) int) _ nil?)'
end
end or how a visual matcher will highlight matched and unmatched branches of the NP given a code sample: describe 'NP::V' do
it 'highlights' do
expect(
highlight_unmatched('(block (send (send nil? _) int) nil?)', 'hello.hello(1) { 2 }')
).to eq '(block (send (send nil? _) int) _ *nil?*)'
end
end what you like most. The latter is a slightly different feature, it just happened to have born in this ticket. |
I love the idea of |
It looks amazing! |
Wow! that is very cool @marcandre! Looking ahead to test it locally :) I was thinking about we have like a web interface like rubular.com that we could test the patterns against some code. I know that @baweaver started something on https://github.com/baweaver/ast_explorer maybe we could join the ideas to create a good playground for it :) |
@marcandre that is a good point I was watching a live coding about opal right now on Ruby Kaigi with @youchan. Looks like it transpile to create the code. I think if opal compiles over opal it's possible :)´ The presentation was great and I learned a lot about how to create an AST rewriter in opal. I think we can start with the server version and see if we can evolve to use Opal in the FE as a second version due to the challenge of transpile all the dependencies to the FE. I'm not sure what is the state of opal actually. |
I put an initial version of a live debugger at https://nodepattern.herokuapp.com/ |
@marcandre is a coding machine 😮 👏 ❤️ I love to use it! very cool! |
Great job on the UI there. I'd always considered doing it through Lambda or just a Rails server because |
Thanks!
Right, but my app does match a Ruby AST against a node pattern, and My plan is to have a local version that can also do local searches. |
If you ever want to collab on any of that feel free to shoot me a message later. Interesting note on |
Over time I'd love to evolve it into something like Regexr but that's a beast of a task: |
The idea may be interesting, but probably not worth the effort. I'll close for now. |
Again, not a feature request, more like to start the discussion. Can't think of a better place for this discussion again.
As a prolific cop author, I personally find Node Pattern hard to write.
Please speak up if you use some tools, that visually indicate that a node pattern you're trying to craft matches a given set of source excerpts. I can think of this as:
§ hello 'Dear'
§ Greeter.hello ''
§ hello 'world'
§ hello 1
§ hello
Think visual regexp matcher we got used to already.
I recall there are Regexp generators that build a minimal Regexp to match a given set of strings (and always returns
.*
haha).Wondering if building a similar tool to automatically build node pattern given some code samples it should match, and some that it should not.
Unfortunately, I'm not aware of the principles of building such a tool. Do you have an idea of where to start?
The text was updated successfully, but these errors were encountered: