How Firefox builds its CSS tokenizer

Here's a little excerpt taken from the nsCSSScanner.cpp library of the Firefox source code. Notice of the CSS lexical table is build token by token.

void
 nsCSSScanner::BuildLexTable()
 {
   gLexTableSetup = PR_TRUE;
 
   PRUint8* lt = gLexTable;
   int i;
   lt[CSS_ESCAPE] = START_IDENT;
   lt['-'] |= IS_IDENT;
   lt['_'] |= IS_IDENT | START_IDENT;
   lt[' '] |= IS_WHITESPACE;   // space
   lt['\t'] |= IS_WHITESPACE;  // horizontal tab
   lt['\r'] |= IS_WHITESPACE;  // carriage return
   lt['\n'] |= IS_WHITESPACE;  // line feed
   lt['\f'] |= IS_WHITESPACE;  // form feed
   for (i = 161; i <= 255; i++) {
     lt[i] |= IS_IDENT | START_IDENT;
   }
   for (i = '0'; i <= '9'; i++) {
     lt[i] |= IS_DIGIT | IS_HEX_DIGIT | IS_IDENT;
   }
   for (i = 'A'; i <= 'Z'; i++) {
     if ((i >= 'A') && (i <= 'F')) {
       lt[i] |= IS_HEX_DIGIT;
       lt[i+32] |= IS_HEX_DIGIT;
     }
     lt[i] |= IS_IDENT | START_IDENT;
     lt[i+32] |= IS_IDENT | START_IDENT;
  }
}

Leave a Reply

Note: Only a member of this blog may post a comment.