I was having a look at the CSS syntax here and here and I was amazed to see both the token productions and the grammar littered with whitespace declarations. Normally whitespace is defined once in the lexer and skipped, never to be seen again. Ditto comments.
I imagine the orientation towards user-agents rather than true compilers is part of the motivation here, and also the requirement to proceed in the face of errors, but it still seems pretty odd.
Are real-life UAs that parse CSS really implemented according to this (these) grammars?
EDIT: reason for the question is actually the various LESS implementations. less.js
doesn't understand consecutive comments, and lessc.exe
doesn't understand comments inside selectors. In this respect they are not even able to parse CSS correctly, however that is defined. So I went to see what the actual grammar of CSS was and ...
CSS, while similar to many programming languages, does have some rare instances where whitespace can be important.
Say we have the following base markup:
There's nothing special here, except a div inside of a div, with some styles on it so that you can see the difference between them.
Now lets add another class and an ID to the outer div:
If I want to give a background to the outer div in the following manner:
...then the whitespace becomes important. The rule above does style the outer div, as it is parsed differently than the following:
...which does NOT style the outer div.
To see how these are parsed differently, you can look at how the selectors are tokenized:
Example1:
Example2:
If we were to blindly ignore whitespace (as compilers usually do), we would miss this difference.
With that being said, I am not implying that this grammer is good. I also have a fair amount of experience in writing grammars, and I cringe when looking at this grammar. The easiest solution would have been to add the
#
and.
symbols into the IDENT token and then everything else becomes much easier.However they did not chose to do this, and the need for whitespace is an artifact of that decision.