Writing my first code formatter

in #programming3 years ago

Now that we've entered 2019, I'm looking back at the past year, realizing that I have a lot of programming projects which I have yet to write about here on Steemit. On that note, I would like to share one of my latest projects, one i just recently finished.

OyvindSabo 1036 commits.png

I've been writing a lot of Gremlin code over the past few months, and despite the extensive Apache Tinkerpop documentation, there seems to be some mixed opinions about how to format the queries. In hopes of arriving at something which could be considered somewhat of a standard, I decided to spend a few days of this Christmas to write a Gremlin code formater.

Usually, when developing small widgets, I like to stick to a single HTML document containing all the JavaScript, HTML and CSS in a single file. I know this might not be the cleanest approach, but when it comes to open source software, I know that the chance of the code ever being used (or even tested) by someone else, quickly goes to zero if they are not able to easily run the code. Sticking to a single HTML file enables people to simply paste the code onto their website, or easily run the code locally opening it in a browser.

Gremlint GitHub.png

Having little experience with parsers (except from that time when we were encouraged to program the Oz programming language inside the Oz programming language...), I was a bit unsure about what approach to take. I considered taking a very functional approach, using multiple maps and filters, but in the end I realized that the formatting of each line depended very much on the previously formatted lines, so I decided to just parse the queries one character at a time, while simultaneously keeping a set of counters for unclosed parentesis, unclosed comments, unclosed strings, and so on.

The formatter would make sure to vertically align lines, properly indent nested method calls, add consistent quotes, and color code strings, parameters and comments.

Gremlint Internet Explorer.png

A complicating factor I initially underestimated was the fact that Gremlin queries can contain Groovy code. Groovy is very different from Gremlin when it comes to parsing. The structure of a Gremlin query is very simple, so for instance, all whitespaces which are not inside a String can be removed, and the query will still work as before. Realizing that writing a separate formatter for Groovy would be a huge endeavor, I decided to simply retain the formatting of the parts of the queries which consist of Groovy. After all, I was writing a Gremlin formatter.

Gremlint Code.png

My iterative parsing approach scaled very quickly, and I suddenly found myself with a single function spanning 250 lines. Woops. But it works. For now.

Well, anyways, since I had made a linter/formatter for Gremlin, I decided to name it Gremlint. It's a pun.

Gremlint logo 1920x360.png

It turns out that gremlint.com was available, so I bought it and put my freshly built formater online.

Gremlint website.png

I had a lot of fun drawing small gremlin cartoons to fill the webpage.

Gremlint Gremlin Cartoons.png

Now that I had a website, I might as well write a Gremlin style guide to give the impression that I have some kind of authority in the Gremlin community.

Gremlin Style Guide.png

The style guide is still very much a work in progress, but it would be fun to be the author of the world's best Gremlin style guide. It's such a niche market that I think it is very much possible.

If you want to checkout the Gremlint GitHub repo, you can do so here: https://github.com/OyvindSabo/gremlint

If you want to checkout the Gremlin Style Guide Github repo, you can do so here: https://github.com/OyvindSabo/gremlin-style-guide


Honor to you brother. It is not easy to find motivation for programming projects. Good for you for being able to smack em around a bit.

Congratulations @oyvindsabo! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

SteemFest Meet The Stemians Contest - The mysterious rule revealed
SteemFest⁴ - Meet the Steemians Contest
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.50
TRX 0.09
JST 0.065
BTC 50700.08
ETH 4315.06
BNB 579.77
SBD 6.33