DSL / Python / New Language: How to build a CSS pre-processor (like SASS) from scratch (DotDot)

DSL / Python / New Language: How to build a CSS pre-processor (like SASS) from scratch (DotDot)

If you are in web development, maybe you’ve heard of Sass, Less, Pug, Stylus etc. All these are pre-processors. In this tutorial we’re going to build nothing less than a functional css pre-processor from scratch with variables and functions. This type of new language is called source-to-source compiled. If you are thrilled, i hope not to disappoint you.

Mesh drilling

Charting The Routes

First let’s see what we’ll be doing. We’ll call the language DotDot with extention .dot

When designing a new language, it’s good to spend sometimes on design. If you have something that really flows, you can start coding.

Here is a DotDot snippet:

which compiles to this:

Dissecting Design

Let’s see what features we included

Variables

We see that

  • we don’t need to specify a specifier like $ or var in Js.
  • we can add – in the variable name

We can call variables in function values

And in attribute values

Where .. denotes a variable call in attribute directly.

Functions

Functions are denoted by an @ sign. These are called as in the following case where it expands into the properties it contains

That is enough complexity to deal with. Let’s start!

We also add our source as a variable

Defining Constants

We’ve bundled our constants in a class.

We then define our keywords

Building the Lexer

The code for the lexer is from our lexer tutorial. Please go over it if you feel the need to. Here we converted the code into a class with a get_lexeme method. The metho gives us a list. The dotdot snippet above gets converted to the list held in lexemes variable below.

Here is our lexer class:

Then we declare our basic variables:

lexemes is now equal to

With such separated data, it’s much easier to proceed!

The Concept of Memory

Next we define a dictionary to hold all our variables.

where

will become this later on:

to retrieve it we’ll just do

The Concept of Tree

We’ll have a dictionary called tree

which will hold the converted code as

Our next step will exactly be this: converting the lexemes list into this dictionary

Generating The Tree

To keep track of where we are, we’ll have a series of variables taking True/False values (on/off)

Setting up holders

id string will hold like #add, .x a div etc

the last_ variables just hold variables that are not emptied upon leaving the sub section

Setting up flags

We’ll have 3 flags.

One when we are starting a block, which will become true when encountering a { and false when encountering a }

An attribute is color in color:black;

The attribute ongoing will become true when passing over { and ; and will become false when passing over :

value_ongoing will become true when going over : and false when going over ;

We’ll start looping over the list and implement what we described of the flags above

Continue is needed as once we have activated a flag, we have no work to do, we move on.

Dealing with variables

To assign variables, we just wait for the = sign then continue.

Then when going over a variable name or a value we just prevent the loop from continuing by using continue

Where The Tree Builds

Now we’ll make use of the flags

Here we are dealing with the block id, example #add in #add {}. We did

here is an example tree at this point

Using the same principle, we’ll do so for the attribute

Here is an example tree at this point

and value

Here is an example tree at this point

That snippet ends our tree-building block

Replacing Values

Now let us see how to replace variable names in functions like rgb() and rgba()

Here we replace rgb(x, 4, 5) to rgb(1, 4, 5) by replacing the x by 1 in the memory dictionary. To build a CSS pre-processor from scratch, we have to take all cases, which we’ll do after.

Transforming The Tree Into It’s Final Form

Now we need to pass over the tree once again and expand functions, replace values, including the use of our defined function above.

Technically we can’t change a dictionary while iterating over it. We have to copy it to break references to it. We have to deepcopy it.

Now, if we get the first symbol of the id as a @, we just skip it as we don’t need to expand the definition.

Next we say if there is a ( in the value, it denotes a function like rgb(). In that case we use our function parseFunc.

Next we see a . in the value, we go see in the memory dictionary and replace it

Next we we see the @ symbol in as key in a block, we just add the dictionaries in it to the dictionaries in that block (Done by update in Python) .

That also shows the advantage of using a data structure over plain strings.

Compiling To Normal Css

Now we can compile it to normal CSS, we’ll just print it for now (you can file= in print btw)

which produces

The Future of It

This was done without a safety net and lacking lot of cool features but it is done to demonstrate that with some normal programming logic, you can set up something cool. It was my usual linkedIn Project of The Week.

If you want to build a CSS pre-processor from scratch based on indentation (like stylus), use this tutorial.

Github Link: https://github.com/Abdur-rahmaanJ/dotdot

Pictures from unsplash.

— Abdur-Rahmaan Janhangeer

Lives in Mauritius, cruising python waters for now.