forsyth20121205-174063a80cc13 years agoHistory
349 lines · 8.1 KB · Text
RawDownload
.TH W3C-CSS 2.SH NAMEw3c-css \- cascading style sheet parser.SH SYNOPSIS.EXinclude "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);.EE.SH DESCRIPTION.B Cssimplements a parser for the World-Wide Web Consortium's CascadingStyle Sheet, specification 2.1..PP.B Initmust be called before any other operation in the module.If.I diagis non-zero, the module will print diagnostics on standard output formalformed or unrecognised items that are ignored during parsing (asrequired by the specification)..PP.B Parsetakes a complete stylesheet in string.IR s ,parses it, and returns a tuple.BI ( sheet,\ err )where.I sheetrefers to a.B Stylesheetvalue containing the logical content of.IR s ,as described below.On a fatal error,.I sheetis nil and.I erris a diagnostic.Most syntactic errors are ignored, as the specification requires..PPIn some applications there can be auxiliary declarations outside a stylesheet..B Parsedecltakes a string.I scontaining a sequence of declarations, and returns a tuple.BI ( decls,\ err )where.I declsis a list of references to.B Declvalues, each representing a single.I declarationin.IR s .On a fatal error,.I declsis nil, and.I erris a diagnostic..PPThe 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 thespecification, it abstracts away from the complex rules about where whitespace can appear.).TP.B Stylesheet.EX.ft R\f2stylesheet\fP ::= [ '\f5@charset\fP' STRING '\f5;\fP' ] \f2import\fP* \f2statement\fP*.EE.IPLimbo lists represent lists of items in the grammar.Nil values denote optional components that are missing.Upper-case names such asIDENT,STRINGandNUMBERare terminals; see the CSS specification for theiroften subtle definitions.They are usually representedby Limbo string values in the adts..TP.B Import.EX.ft R\f2import\fP ::= '\f5@import\fP' (\f2STRING\fP|\f2uri\fP) [\f2medium\fP ('\f5,\fP' \f2medium\fP)*] '\f5;\fP'\f2uri\fP ::= '\f5url(\fP' STRING '\f5)\fP'.EE.IP.B Import.nameholds the text of theSTRINGor.IR uri ..TP.B Statement.EX.ft R\f2statement\fP ::= \f2ruleset\fP | \f2media\fP | \f2page\fP\f2media\fP ::= '\f5@media\fP' \f2medium\fP ('\f5,\fP' \f2medium\fP)* '\f5{\fP' \f2ruleset\fP* '\f5}\fP'\f2medium\fP ::= IDENT\f2page\fP ::= '\f5@page\fP' [\f2pseudo_page\fP] '\f5{\fP' \f2declaration\fP ('\f5;\fP' \f2declaration\fP)* '\f5}\fP'\f2pseudo_page\fP ::= '\f5:\fP' IDENT\f2ruleset\fP ::= \f2selector\fP ('\f5,\fP' \f2selector\fP)* '\f5{\fP' \f2declaration\fP ('\f5;\fP' \f2declaration\fP)* '\f5}\fP'.EE.IP.B Statementis not in the reference grammar, but is introduced here to give a name correspondingto the pick adt..TP.B Decl.EX.ft R\f2declaration\fP ::= \f2property\fP '\f5:\fP' \f2expr\fP ['\f5!\fP' '\f5important\fP'] | /* \f2empty\fP */\f2property\fP ::= IDENT.EE.B Decl.valuesis a list representing the terms of the.I expr(see below for details)..BR Decl 'sfield.B importantis non-zero if the optional `important' priority is given..TP.B "list of ref Value".EX.ft R\f2expr\fP ::= \f2term\fP (\f2operator\fP \f2term\fP)*\f2operator\fP ::= '\f5/\fP' | '\f5,\fP' | /* \f2empty\fP */.EE.IPAn.I expris always represented as a list of references to.B Valuein some containing structure(where.B Valuerepresents a.IR term ,see below).The.I operatorpreceding each.I termappears as the field.B sepof the corresponding.BR Value ,where a space character represents `empty' (concatenation)..TP.BR Selector.EX.ft R\f2selector\fP ::= \f2simple_selector\fP (\f2combinator\fP \f2simple_selector\fP)*\f2combinator\fP ::= '\f5+\fP' | '\f5>\fP' | /* \f2empty\fP */.EE.IP.B Selectoris just a type synonym for a list of tuples, say.BI ( com,\ simplesel )where the.I simpleselvalue represents.I simple_selector(see below), and the integer.I comis one of the characters space (representing `empty'),.RB ` > 'or.RB ` + ',giving the combinator that preceded the simple selector.(The first in the list is always space.).TP.BR Simplesel ", " Select.EX.ft R\f2simple_selector\fP ::= \f2element_name\fP (\f2hash\fP | \f2class\fP | \f2attrib\fP | \f2pseudo\fP)*	| (\f2hash\fP | \f2class\fP | \f2attrib\fP | \f2pseudo\fP)+\f2hash\fP ::= '\f5#\fP' NAME\f2class\fP ::= '\f5.\fP' IDENT\f2element_name\fP ::= IDENT | '\f5*\fP'\f2attrib\fP ::= '\f5[\fP' IDENT [('\f5=\fP' | '\f5|=\fP' | '\f5~=\fP') (IDENT | STRING)] '\f5]\fP'\f2pseudo\fP ::= '\f5:\fP' ( IDENT | IDENT '\f5(\fP' [IDENT] '\f5)\fP' ).EE.IPA.I simple_selectoris represented by.BR Simplesel ,a list of references to.B Selectvalues, each representing one.I element_nameor qualifier.An.I element_nameis represented by.B Select.Elementfor anIDENT,or.B Select.Anyfor.RB ` * '.The qualifiers are.I hash.RB ( Select.ID ),.I class.RB ( Select.Class ),.I attrib.RB ( Select.Attrib ,where  the comparison operator is the string.BR op ),.I pseudo(either.B Select.Pseudoif a plain identifier, or.B Select.Pseudofnfor a function with optional parameter)..TP.B Value.EX.ft R\f2term\fP ::= ['\f5+\fP' | '\f5-\fP'] (NUMBER | \f2percent\fP | \f2unit\fP) | STRING | IDENT | \f2uri\fP | \f2function\fP | \f2hexcolour\fP | \f2rgb\fP\f2function\fP ::= IDENT '\f5(\fP' \f2expr\fP '\f5)\fP'\f2hash\fP ::= '\f5#\fP' NAME\f2hexcolour\fP ::= '\f5#\fP' HEXDIGIT+\f2percent\fP ::= NUMBER '\f5%\fP'\f2unit\fP ::= NUMBER STRING\f2rgb\fP ::= '\f5rgb(\fP' \f2term\fP '\f5,\fP' \f2term\fP '\f5,\fP' \f2term\fP '\f5)\fP'\f2uri\fP ::= '\f5url(\fP' STRING '\f5)\fP'.EE.IPAny sign before a.BR Number ,.B Percentageor.B Unitappears as the first character of.BR value .All the dimensional units (LENGTH, EMS, EXS, ANGLE, TIME, FREQ and others)in the reference grammar are mapped to.BR Value.Unit ,with the field.B unitscontaining the name of the relevant unit (eg,.LR cm ,.LR in ,etc.) in lower case.Values and names appear shorn of the surrounding punctuation..B Value.Hexcolourincludes the original sequence of hex digits as a string,and a decoding of it as an.B rgbtriple.The arguments to the CSS.B rgbfunction are similarly presented in original and decoded forms, in.BR Value.RGB .Other function references are returned uninterpreted in.BR Value.Function ..SH SOURCE.B /appl/lib/w3c/css.b.SH SEE ALSO``Cascading Style Sheets, level 2 revision 1'',.B http://www.w3.org/TR/CSS21