📜 ⬆️ ⬇️

A story about how to write your own CSS preprocessor for 9 months

I want to talk about my brainchild - the preprocessor and the CSS parser, which I started working on in April last year. Why did I start to study them? Recognizing myself honestly now, I can say: I wanted to invent my own bicycle. What did I follow then? Hard to say. Perhaps the same. Or maybe because I didn’t really find anything that satisfies my CSS preprocessor requirements for my favorite development platform.

The requirements for CSS preprocessor have been formed after reading one of the articles here. This was an article about the Stylus for Node.js preprocessor. Actually, then I learned about these "preprocessors". I was struck by the simplicity of the syntax of this preprocessor. After two days (or maybe less) viewing the results from Google, I did not find anything interesting for myself. It was at this moment that a crazy thought came to my head: why not?
I had the following requirements:

Actually, probably this is all.

What did I do?


All this time elapsed from the starting point of reference, I spent on developing my product, and I got the following - the open source MySheet library, which is both a parser and CSS style preprocessor.

Well, of course, I had to write a small site for the presentation of my product (did you already guess which programming language was chosen by me for this purpose?). A few interesting moments of the development process - about how I created my library, what pitfalls I encountered and what I learned in the process of its creation - I will tell you a little later. And now, about the capabilities of my CSS preprocessor.

How it was…


With a description of what I have achieved to date, I seem to be finished. And now, I want to tell you how I led the development process - as in the intervals between work, study and my favorite girl, I wrote my library.


Fig. 1 - My favorite girl

From the very moment I started my mini-project, a long time has passed. There were many different questionable situations and questions. And it is not surprising - I have never done anything like this before. I did not read any special literature on compilers and at first I did everything exclusively at my discretion. I do not brag, but on the contrary I say that I did not do it in vain. Perhaps, so I would avoid some of the problems that arose.

For some reason, I immediately decided to create a library in the form of an open-source project and immediately told myself that one githab page would not be enough. After a part of the library was already written, I began to make a website for it, so that I could find defects in “online mode” and correct them, and also immediately realize in practice that it would be nice to see the end user.

I thought up the name for my library, acting on the contrary.I decided not to deviate from the example of the most famous today preprocessors SASS and LESS, and I thought that MSS is a good combination of letters, after all. And to make the name memorable, I decided to name my project MySheet. And the abbreviation fits (MySheet Styles), and there is a light zest in the title.

The name is invented, it's time to start designing the ship. The first thing I started doing was a source code parser. It was here that I miscalculated for the first time. I started to do it without breaking the source code into tokens and keywords, and was guided by the fact that the library will mainly include work with strings. Of course, I saw the drawbacks of this approach already at the stage of its selection, but unfortunately I couldn’t think of anything better. Later, while at the university, we had a course on compilers, I realized that it would be better to introduce a stage of preliminary lexical analysis. At least, because now I am faced with the problem of recognizing and memorizing comments for later output to compiled CSS code. Or, for example,Now I would like to make the parser extensions insensitive (which are connected to the library as additional modules) to the presence of unexpected line breaks and indents (the ones that are often added for readability of the code). In the near future, I want to include in the parser the stage of breaking into tokens, which, in my opinion, should solve these problems.

The idea of ​​color arithmetic came to me when I was implementing support for mathematical expressions. I thought that it would be nice to be able to lighten and darken the colors in the design of the site to match the matching color without having to open the color picker. Now the library is working with HSLA, RGBA, HEX and HTML color formats. To each of the colors specified in these formats, you can add a delta of any channel from any other color format. For example, you can add 255 points of the blue channel and 40 points of green to the color recorded as # 000 and get the color # 0028ff. The arithmetic expression will look like this: # 000 + 255b + 40g.

, MrColor ( «» ).

, . Webgrind. , , .

I want to tell one interesting thing. When the parser in the preprocessor starts parsing a rule, it passes through all registered modules and, roughly speaking, calls the parse method in each of them. And since an arithmetic expression and, for example, a function are two different entities, the library had to parse the same thing two times. I really didn't like it, and one day I came up with a solution. When the parser of an arithmetic expression finds that it is not an expression, but a simple function, it does not return false, but returns this very object of the function. Thus, I got rid of this flaw and increased the performance of the source code parsing process.

, . , , : « ! !». , , :


Fig. 2 –


, . , , . , .

, MSS . , IE ( , , IE , , CSS-).

If someone has any other ideas for improving my project, I will always be happy to hear them.

Instead of conclusion


Well, that's probably all that I would like to tell in my first article about my library. I think I was able to advertise my product a little. I also wanted, so to speak, to fix the point in the development and get feedback on the work already done.

I would be grateful for any advice and recommendations, as well as all kinds of support and just kind words.

If you like my article, I will definitely write more.

Links


GitHub: https://github.com/Dobby007/mysheet
Official website: http://mss.flydigo.com/
Documentation: http://mss.flydigo.com/docs

PS I'm sorry, but as someone noticed in comments - my hosting could not stand the pressure. About this, I even could not think. Now we will try to do something with this ...

PPS Unfortunately, my hosting, it turns out, is draconian restrictions on sites (and not only free ones). And they respond to requests exactly once in the Chinese Easter, so I will have to look towards other possible solutions for the content of my site.

PPPS Access to the site will be fully restored after updating the entire DNS chain. Now the site is available at: dobby007_h5a5nu.radius-host.net

Source: https://habr.com/ru/post/256589/


All Articles