Melody – a language that compiles to regular expressions
github.com
github.com
Here's the parse rule for Batman:
[
16 "na"
2 [space "batman"]
]
And complete example for the Semantic version: digit: charset "1234567890"
if parse "0.2.5" [
opt "v"
copy major [some digit]
"."
copy minor [some digit]
"."
copy patch [some digit]
][
print [major minor patch]
]https://github.com/oilshell/oil/wiki/Alternative-Regex-Synta...
(including my own https://www.oilshell.org/release/latest/doc/eggex.html which is built into Oil)
Perl 6 (which is now actually called Raku, for what it's worth) has BNF-style Grammars as a first class citizen of the language (a virtually unheard of thing AFAIK), and programmers are encouraged to use it for complex parsing tasks instead of regex. I don't know whether that falls under "alternative syntax for regex", they are very closely related to regexes and actually much more powerful and readable, including regexes as a subset. But adding them might drag you into adding every Context Free\Parsing Expression Grammar tool out there, things like Bison and YACC and Antlr.
The language SNOBOL[2][3][4] is one of the earliest languages with text matching and processing as a first class citizen (in fact, the only citizen). Being designed (1962) before the first software implementation of regular expressions* (1968), the pattern language it uses is not based on regular expression, and in some cases actually exceeds it (e.g. matching balanced parenthesis, which mathmatical regex can't do, but some variants of practical regexes can with special non-regular constructs).
This thread[5] in Retrocomputing stack exchange discusses what is the earliest language with string pattern matching capabilities, and finds hidden gems in the process.
[1] https://reference.wolfram.com/language/guide/StringPatterns....
[2] https://en.wikipedia.org/wiki/SNOBOL
[3] https://dl.acm.org/doi/10.1145/800025.1198417
[5] https://retrocomputing.stackexchange.com/questions/658/what-...
* : As opposed to the mathematical definition of regular expression, which dates back to 1956 and perhaps even before.
I think SNOBOL might count, but it's a bit different in that I think it's a "dead" language now? e.g. it doesn't appear to have a user base or implementations. But feel free to add it or anything else if there are good links.
There's definitely a fuzzy line between things like LPeg or Rosie and YACC/ANTLR ... it's less about the power of the language and what kind of tasks people use it for, I suppose. If it's "scripting friendly".
ie
group $first_name { some of <letter> }
1 of <space>
group $last_name { some of <letter> }Have you considered borrowing the :emoji: convention that slack/discord/github use? :space: :feed: etc..
(Oddly enough, I don't bother doing this with ctrl/cmd + a/s/f/z/x/c/v, but I think that is mostly because keys to the right of the space bar vary so much between laptops and keyboards that I never bothered trying to stick with it).
In fact, you might want to go a step further and consider using [] for match and {} for capture (thus eliminating the capture keyword as well). Using [] for match would be natural for Javascript programmers.
A library which takes a regex and shows some examples that pass and some that fail. I would find that the easiest way of understanding a regex, rather than changing the language itself. (Though Melody looks v promising and I'm keen to see it develop).
It wouldn't be trivial to build — particularly for the "fail" examples, you'd want them fairly close to passing. For example, with `(/*\.csv\.gz)` you'd want `foo.csv.gz` rather than `aoseutn` as an example of a failure.
Does it (or are the plans to) reverse compile? If I could input my regex and output melody script one could create an excellent interactive learning tool, and also more selfishly help with adoption in teams with crusty old devs like me who like our magic rituals and prefer typing our regex by hand.
Also are there plans to support runtime compiling in JS? Something like...
someMelodyObject = <initialise and configure melody> String.replace(someMelodyObject.toRegexp(), someString)
This I think would make it a compelling library for inclusion into projects assuming it were fairly efficient and lightweight. Not sure how or if you'd have to deal with performance and caching but it would probably go a ways to improving adoption among web developers at least.
Anyways good luck with the project. Regex is often considered a dark art when it's actually fairly concise and expressive, opening it up to more people at a higher level could lead to greater understanding of regex in general. Also what an interesting and challenging project to undertake, definitely a nontrivial challenge all told.
1. A reverse compiler is one of the 'maybe' features (see the table in the README), it's something I'd like but would essentially be an entire compiler so it's non trivial
2. The plan is to make Melody available as a compile step (like e.g. SASS) with no runtime overhead or as a Rust crate. You could do the compilation at runtime but other than including variables in the pattern I'm not sure if it'd have a benefit over compile time transforming, + it'd have a performance impact.
Thank you!
Re 2, shouldn't be a problem as we already have build processes in place. Most projects I work on have npm build steps, I'm not sure how that figures in with rust (I really need to get off my butt and check it out sometime), but if it could be pulled in as an npm dependency that would work. If it could be done inline even better (e.g. inline melody within a JS file, compiles to the expression inline...)
Anyway good job again so far, have followed the repo, all the best once more!
>If it could be done inline even better
I've been thinking of using a tagged template based mechanism (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...) so no special editor support is needed and then transpile to a normal regex (similar to how htm handles it's syntax with no runtime, https://www.npmjs.com/package/babel-plugin-htm) but I'm not working on that part of the process yet so it may change.
If Melody does end up using the tagged template mechanism it may end up looking something like this:
import melody from "melody";
const batmanRegex = melody`
16 of "na";
2 of match {
<space>;
"batman";
}
`;
That would transpile to: const batmanRegex = /(?:na){16}(?:\sbatman){2}/;
Backticks would need escaping but otherwise it'd be normal Melody syntaxhttps://stackoverflow.com/questions/15463257/commenting-regu...
Some interesting workarounds mentioned here that might pair well with melody type languages.
A way to specify example strings that match or don't as a mini unit test would be cool too.
manual: https://regexr.com/
Please don't submit to HN until I release the vscode plugin :-)
Sorry I had to make the joke! This is fine. Regex are a pain.