I need a validator for a less file. I need to validate a single less file, at only syntax level.
I can't get the validator to follow the dependencies or detect if a mixin is declared or not. That is my real problem.
I have found a lot of less processors which fail because the file has dependencies that i can't provide in this point of the process.
npm package would be perfect.
Finally, I decided to use antlr4
Steps:
The configuration is:
This info is directly from the web page.
Now you can get the grammar from https://github.com/antlr/grammars-v4
At this point you can generate the javascript version of your grammar
In my case, I created a directory, I downloaded the files and coded all my tests inside:
This process generates a javascript file that you will use, but you need the antlr lib for use this files in your node program.
Ant now starts to code:
The important parts in this code are the function lessLexer, lessParser, and parser.stylesheet(); This are different per grammar. The last one is difficult to know, but it is the point in the grammar that you want to validate. In my case, I get the file LessParser.g4, and you have the different node in the grammar in the definition:
In this case, you can validate the string like a stylesheet, statement, variableName ...
The last interesting point is the Error Validation, I use it to stop the validation in the first error, my case is very simple, but you can improve this point