Class CssParser

java.lang.Object
org.jhotdraw8.css.parser.CssParser

public class CssParser extends Object
The CssParser processes a stream of characters into a Stylesheet object.

The CSS Syntax Module Level 3 defines a grammar which is equivalent to the following EBNF ISO/IEC 14977 productions:

 stylesheet_core = { S | CDO | CDC | qualified_rule | at_rule } ;

 rule_list    = { S | qualified_rule | at_rule} ;

 at_rule      = AT_KEYWORD , { component_value } , ( curly_block | ';' ) ;

 qualified_rule
              = { component_value } , curly_block ;

 declaration_list_core
              = { S } , ( [ declaration_core ] , [ ';' , declaration_list_core ]
                        | at_rule , declaration_list_core ,
                        ) ;

 declaration_core  = IDENT , { S } ,  ":", { component_value } , [ !important ] ;

 !important   = '!' , { S } , "important" , { S } ;

 component_value
              = ( preserved_token | curly_block | round_block | square_block
                | function_block ) ;

 curly_block  = '{' , { component_value } , '}' ;
 round_block  = '(' , { component_value } , ')' ;
 square_block = '[' , { component_value } , ']' ;
 function_block
              = ROUND_BLOCK , { component_value } , ')' ;

 
This parser parses the following syntax:
 stylesheet   = { S | CDO | CDC | qualified_rule | style_rule } ;

 operator     = ( '/' | ',' ) , { S } ;

 combinator   = ( '+' | '>' | '~' ) , { S } ;

 unary_operator
              = ( '-' | '+' ) ;

 property     = IDENT , { S } ;

 style_rule   = [ selector_group ] , "{" , declaration_list , "}" ;

 selector_group
              = selector , { "," , { S }, selector } ;

 selector     = simple_selector ,
                { ( combinator , selector
                  | { S }, [ [ combinator ] , selector ]
                  )
                } ;

 simple_selector
              = universal_selector | type_selector | id_selector
                | class_selector | pseudoclass_selector | attribute_selector ;
 universal_selector   = '*' ;
 type_selector        = ns_aware_ident ;
 id_selector          = HASH ;
 class_selector       = "." , IDENT ;
 pseudoclass_selector = ":" , IDENT ;
 attribute_selector   = "[" , ns_aware_ident
                            , [ ( "=" | "~=" | "|=" ) , ( IDENT | STRING ) ],
                        "]" ;
 ns_aware_ident      = IDENT
                      | '*' , '|', IDENT
                      | IDENT , '|', IDENT
                      ;

 declaration_list
              = { S } , [ declaration ] , [ ';' , declaration_list ] ;

 declaration  = IDENT , { S } ,  ":", { terms } ;

 terms        = { { S } , ( term | bracketedTerms ) } ;

 term         = any token - ( "]" | "}" | ";" | S ) ;

 bracketedTerms = "{", { { S } , term } , { S } , "}"
                | "[", { { S } , term } , { S } , "]";


 function     = ROUND_BLOCK , { S } , expr , ')' , { S } ;
 expr         = term , { [ operator ] , term } ;
 

References:

CSS Syntax Module Level 3, Chapter 5. Parsing
w3.org
W3C CSS2.2, Appendix G.1 Grammar of CSS 2.2
w3.org
FIXME The parser does not support the !important declaration.
Author:
Werner Randelshofer