include "css.m"; css := load CSS CSS->PATH; Stylesheet: adt { charset: string; imports: list of ref Import; statements: list of ref Statement; }; Import: adt { name: string; media: list of string; }; Statement: adt { pick{ Media => media: list of string; rules: list of ref Statement.Ruleset; Page => pseudo: string; decls: list of ref Decl; Ruleset => selectors: list of Selector; decls: list of ref Decl; } }; Decl: adt { property: string; values: list of ref Value; important: int; }; Selector: type list of (int, Simplesel); # (combinator, simplesel) Simplesel: type list of ref Select; Select: adt { name: string; pick{ Element or ID or Any or Class or Pseudo => # empty Attrib => op: string; # "=" "~=" "|=" value: ref Value; # optional Ident or String Pseudofn => arg: string; } }; Value: adt { sep: int; # operator preceding this term pick{ String or Number or Percentage or Url or Unicoderange => value: string; Hexcolour => value: string; # as given rgb: (int, int, int); # converted RGB => args: cyclic list of ref Value; # as given rgb: (int, int, int); # converted Ident => name: string; Unit => value: string; # int or float units: string; # suffix giving units Function => name: string; args: cyclic list of ref Value; } }; init: fn(diag: int); parse: fn(s: string): (ref Stylesheet, string); parsedecl: fn(s: string): (list of ref Decl, string);
Init must be called before any other operation in the module. If diag is non-zero, the module will print diagnostics on standard output for malformed or unrecognised items that are ignored during parsing (as required by the specification).
Parse takes a complete stylesheet in string s, parses it, and returns a tuple (sheet, err) where sheet refers to a Stylesheet value containing the logical content of s, as described below. On a fatal error, sheet is nil and err is a diagnostic. Most syntactic errors are ignored, as the specification requires.
In some applications there can be auxiliary declarations outside a stylesheet. Parsedecl takes a string s containing a sequence of declarations, and returns a tuple (decls, err) where decls is a list of references to Decl values, each representing a single declaration in s. On a fatal error, decls is nil, and err is a diagnostic.
The adts represent an abstract syntax of the CSS grammar. The concrete syntax is presented below in an extended BNF, derived from the reference grammar, with each section labelled by the name of the corresponding adts. (Compared to the reference grammar in the specification, it abstracts away from the complex rules about where whitespace can appear.)
stylesheet ::= [ '@charset' STRING ';' ] import* statement*
import ::= '@import' (STRING|uri) [medium (',' medium)*] ';' uri ::= 'url(' STRING ')'
statement ::= ruleset | media | page media ::= '@media' medium (',' medium)* '{' ruleset* '}' medium ::= IDENT page ::= '@page' [pseudo_page] '{' declaration (';' declaration)* '}' pseudo_page ::= ':' IDENT ruleset ::= selector (',' selector)* '{' declaration (';' declaration)* '}'
declaration ::= property ':' expr ['!' 'important'] | /* empty */ property ::= IDENTDecl.values is a list representing the terms of the expr (see below for details). Decl's field important is non-zero if the optional `important' priority is given.
expr ::= term (operator term)* operator ::= '/' | ',' | /* empty */
selector ::= simple_selector (combinator simple_selector)* combinator ::= '+' | '>' | /* empty */
simple_selector ::= element_name (hash | class | attrib | pseudo)* | (hash | class | attrib | pseudo)+ hash ::= '#' NAME class ::= '.' IDENT element_name ::= IDENT | '*' attrib ::= '[' IDENT [('=' | '|=' | '~=') (IDENT | STRING)] ']' pseudo ::= ':' ( IDENT | IDENT '(' [IDENT] ')' )
term ::= ['+' | '-'] (NUMBER | percent | unit) | STRING | IDENT | uri | function | hexcolour | rgb function ::= IDENT '(' expr ')' hash ::= '#' NAME hexcolour ::= '#' HEXDIGIT+ percent ::= NUMBER '%' unit ::= NUMBER STRING rgb ::= 'rgb(' term ',' term ',' term ')' uri ::= 'url(' STRING ')'
W3C-CSS(2 ) | Rev: Tue Mar 31 02:42:39 GMT 2015 |