mbasso / natural-regex

Create regex from natural language
MIT License
152 stars 10 forks source link

Conversion from regular regex to natural regex #1

Open vaidsu opened 7 years ago

vaidsu commented 7 years ago

General Information

Description

Most of the users are used to writing regex and due to its legacy there is lot of code already written in regex syntax (be it any language of choice). With that said, it will be great if we can have a converter that can convert from regex expression to natural regex, that way a library of regex can be used and an index can be generated which can then be used across the code. Kind of similar to webpack's index within the final bundled file (if I am clear with the comparison).

That way the existing code can be converted into Natural regex improving its readability (which I believe is one of the important motivations of this project). While that is done, users might tend to move towards natural regex.

I will try to experiment a converter.

If there is already one, please comment.

Hoping if I can contribute.

mbasso commented 7 years ago

Hi @vaidsu, I think that this is a fantastic idea, a converter that translates normal regex into natural regex might be useful for a lot of users and, unfortunately, at the moment there isn't one. This means improving readability, as you said, and use a common language, avoiding integrations like this. Do you have any idea about the implementation? Please feel free to experiment and contribute! 😄

vaidsu commented 7 years ago

@mbasso thanks for motivation :) I am new to open source and have been prepping to do some good community service. Was watching for new innovative ideas and repos and found this will match my one of the first contributions.

Here is what I thought:

My main motive is to enforce people to see how readability increases when the code gets migrated.

Or in simple case, we can just offer a CLI or a service that converts a given expression into natural expression. May be that will be a good start? :)

I will come up with some fiddle and contact you.

mbasso commented 7 years ago

Thank you for choosing this project 😄 I'm really happy that you like it.

Yeah, I think that this is a good starting point, I had the same idea. We can use jison in the same way as I do for the conversion from natural to native regex, implementing the lexer and parser. For example, we can add a NaturalRegex.reverse(code: string, flags: string or object) API to this library with a conditional import of the new parser (alternatively we can just create a new package) and then a --reverse param to natural-regex-cli.

The idea of parsing the whole file and convert all the regular expressions to natural-regex is awesome. Basically I think that, given a directory, we have to return all the given files with the natural-regex instead of the native ones, using the API that I wrote before. We can see if we can do this with babel for example. Do you know if it is possible in this way?

Regarding the plugin for the bundler, I think that this is a great idea if we want to do the opposite, convert natural-regex to native regex. This means no parsing at runtime, so, max performance. The bundle contains directly the native regexps while the original files contain our natural syntax. I think that this can be done with babel or with a plugin for webpack.

What do you think about that?

vaidsu commented 7 years ago

@mbasso I apologize for delay. I run a regular work and that drains me sometimes, but community service is what I do internally in my company. So wanted to raise hands to several other open source community.

I have pulled the code and already mocking up something. Will come up with something asap and send out a review.

Starting with the CLI is a good way to start that way we can probably help people learn the context and in the future you make it like real english and add more grammar we don't have to worry about adaptation. Hope I am close to what you have been thinking.