2003-06-30 16:44:03 +04:00
|
|
|
/*
|
|
|
|
* This file is part of NetSurf, http://netsurf.sourceforge.net/
|
|
|
|
* Licensed under the GNU General Public License,
|
|
|
|
* http://www.opensource.org/licenses/gpl-license
|
|
|
|
* Copyright 2003 James Bursa <bursa@users.sourceforge.net>
|
2003-03-26 00:03:14 +03:00
|
|
|
*/
|
|
|
|
|
2003-04-02 00:18:18 +04:00
|
|
|
/*
|
2003-03-26 00:03:14 +03:00
|
|
|
|
|
|
|
CSS parser using the lemon parser generator
|
|
|
|
|
|
|
|
see CSS2 Specification, chapter 4
|
|
|
|
http://www.w3.org/TR/REC-CSS2/syndata.html,
|
|
|
|
and errata
|
2003-04-02 00:18:18 +04:00
|
|
|
http://www.w3.org/Style/css2-updates/REC-CSS2-19980512-errata
|
2003-03-26 00:03:14 +03:00
|
|
|
|
|
|
|
stylesheet : [ CDO | CDC | S | statement ]*;
|
|
|
|
statement : ruleset | at-rule;
|
|
|
|
at-rule : ATKEYWORD S* any* [ block | ';' S* ];
|
|
|
|
block : '{' S* [ any | block | ATKEYWORD S* | ';' ]* '}' S*;
|
|
|
|
ruleset : selector? '{' S* declaration? [ ';' S* declaration? ]* '}' S*;
|
|
|
|
selector : any+;
|
|
|
|
declaration : property ':' S* value;
|
|
|
|
property : IDENT S*;
|
|
|
|
value : [ any | block | ATKEYWORD S* ]+;
|
|
|
|
any : [ IDENT | NUMBER | PERCENTAGE | DIMENSION | STRING
|
|
|
|
| DELIM | URI | HASH | UNICODE-RANGE | INCLUDES
|
|
|
|
| FUNCTION | DASHMATCH | '(' any* ')' | '[' any* ']' ] S*;
|
|
|
|
|
|
|
|
Note: S, CDO, CDC will be stripped out by the scanner
|
|
|
|
*/
|
|
|
|
|
2003-04-02 00:18:18 +04:00
|
|
|
stylesheet ::= statement_list.
|
2003-03-26 00:03:14 +03:00
|
|
|
|
2003-04-02 00:18:18 +04:00
|
|
|
statement_list ::= .
|
|
|
|
statement_list ::= statement_list statement.
|
2003-03-26 00:03:14 +03:00
|
|
|
|
2003-04-02 00:18:18 +04:00
|
|
|
statement ::= ruleset.
|
|
|
|
statement ::= at_rule.
|
2003-03-26 00:03:14 +03:00
|
|
|
|
2003-04-02 00:18:18 +04:00
|
|
|
at_rule ::= ATKEYWORD any_list block.
|
2003-04-06 22:09:34 +04:00
|
|
|
at_rule ::= ATKEYWORD(A) any_list(B) SEMI.
|
|
|
|
{ if (strcasecmp(A, "@import") == 0)
|
|
|
|
css_atimport(param->stylesheet, B);
|
|
|
|
free(A); css_free_node(B); }
|
2003-03-26 00:03:14 +03:00
|
|
|
|
|
|
|
block ::= LBRACE block_body RBRACE.
|
|
|
|
block_body ::= .
|
|
|
|
block_body ::= block_body any.
|
|
|
|
block_body ::= block_body block.
|
|
|
|
block_body ::= block_body ATKEYWORD.
|
|
|
|
block_body ::= block_body SEMI.
|
|
|
|
|
2003-04-02 01:33:08 +04:00
|
|
|
ruleset ::= selector_list(A) LBRACE declaration_list(B) RBRACE.
|
2003-04-05 20:24:43 +04:00
|
|
|
{ css_add_ruleset(param->stylesheet, A, B);
|
2003-04-04 19:19:32 +04:00
|
|
|
css_free_node(B); }
|
2003-04-05 20:24:43 +04:00
|
|
|
ruleset ::= LBRACE declaration_list(A) RBRACE.
|
|
|
|
/* this form of ruleset not used in CSS2
|
|
|
|
used to parse style attributes (ruleset_only = 1) */
|
|
|
|
{ if (param->ruleset_only) param->declaration = A;
|
|
|
|
else css_free_node(A); }
|
2003-10-17 00:48:22 +04:00
|
|
|
ruleset ::= any_list_1(A) LBRACE declaration_list(B) RBRACE.
|
|
|
|
{ css_free_node(A); css_free_node(B); } /* not CSS2 */
|
2003-03-26 00:03:14 +03:00
|
|
|
|
2003-04-02 01:33:08 +04:00
|
|
|
selector_list(A) ::= selector(B).
|
2003-03-26 00:03:14 +03:00
|
|
|
{ A = B; }
|
2003-04-02 01:33:08 +04:00
|
|
|
selector_list(A) ::= selector_list(B) COMMA selector(C).
|
|
|
|
{ C->next = B; A = C; }
|
|
|
|
|
|
|
|
selector(A) ::= simple_selector(B).
|
|
|
|
{ A = B; }
|
2003-10-08 01:34:27 +04:00
|
|
|
selector(A) ::= selector(B) css_combinator(C) simple_selector(D).
|
2003-09-28 21:37:43 +04:00
|
|
|
{ D->right = B; D->comb = C; A = D;
|
|
|
|
A->specificity += B->specificity; }
|
2003-04-02 01:33:08 +04:00
|
|
|
|
2003-10-08 01:34:27 +04:00
|
|
|
css_combinator(A) ::= .
|
|
|
|
{ A = CSS_COMB_ANCESTOR; }
|
|
|
|
css_combinator(A) ::= PLUS.
|
|
|
|
{ A = CSS_COMB_PRECEDED; }
|
|
|
|
css_combinator(A) ::= GT.
|
|
|
|
{ A = CSS_COMB_PARENT; }
|
2003-04-02 01:33:08 +04:00
|
|
|
|
|
|
|
simple_selector(A) ::= element_name(B) detail_list(C).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_SELECTOR, B, C, 0);
|
2003-11-19 22:57:11 +03:00
|
|
|
A->specificity = 1 + C->specificity; }
|
|
|
|
simple_selector(A) ::= element_name(B).
|
|
|
|
{ A = css_new_node(CSS_NODE_SELECTOR, B, 0, 0);
|
|
|
|
A->specificity = 1; }
|
|
|
|
simple_selector(A) ::= detail_list(C).
|
|
|
|
{ A = css_new_node(CSS_NODE_SELECTOR, 0, C, 0);
|
|
|
|
A->specificity = C->specificity; }
|
2003-04-02 01:33:08 +04:00
|
|
|
|
|
|
|
element_name(A) ::= IDENT(B).
|
|
|
|
{ A = B; }
|
|
|
|
|
2003-11-19 22:57:11 +03:00
|
|
|
detail_list(A) ::= detail(B).
|
|
|
|
{ A = B; }
|
|
|
|
detail_list(A) ::= detail(B) detail_list(C).
|
|
|
|
{ A = B; A->specificity += C->specificity; A->next = C; }
|
|
|
|
|
|
|
|
detail(A) ::= HASH(B).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_ID, B, 0, 0);
|
2003-11-19 22:57:11 +03:00
|
|
|
A->specificity = 0x10000; }
|
|
|
|
detail(A) ::= DOT IDENT(B).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_CLASS, B, 0, 0);
|
2003-11-19 22:57:11 +03:00
|
|
|
A->specificity = 0x100; }
|
|
|
|
detail(A) ::= LBRAC IDENT(B) RBRAC.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_ATTRIB, B, 0, 0);
|
2003-11-19 22:57:11 +03:00
|
|
|
A->specificity = 0x100; }
|
|
|
|
detail(A) ::= LBRAC IDENT(B) EQUALS IDENT(C) RBRAC.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_ATTRIB_EQ, B, 0, 0); A->data2 = C;
|
2003-11-19 22:57:11 +03:00
|
|
|
A->specificity = 0x100; }
|
|
|
|
detail(A) ::= LBRAC IDENT(B) EQUALS STRING(C) RBRAC.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_ATTRIB_EQ, B, 0, 0); A->data2 = css_unquote(C);
|
2003-11-19 22:57:11 +03:00
|
|
|
A->specificity = 0x100; }
|
|
|
|
detail(A) ::= LBRAC IDENT(B) INCLUDES IDENT(C) RBRAC.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_ATTRIB_INC, B, 0, 0); A->data2 = C;
|
2003-11-19 22:57:11 +03:00
|
|
|
A->specificity = 0x100; }
|
|
|
|
detail(A) ::= LBRAC IDENT(B) INCLUDES STRING(C) RBRAC.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_ATTRIB_INC, B, 0, 0); A->data2 = css_unquote(C);
|
2003-11-19 22:57:11 +03:00
|
|
|
A->specificity = 0x100; }
|
|
|
|
detail(A) ::= LBRAC IDENT(B) DASHMATCH IDENT(C) RBRAC.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_ATTRIB_DM, B, 0, 0); A->data2 = C;
|
2003-11-19 22:57:11 +03:00
|
|
|
A->specificity = 0x100; }
|
|
|
|
detail(A) ::= LBRAC IDENT(B) DASHMATCH STRING(C) RBRAC.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_ATTRIB_DM, B, 0, 0); A->data2 = css_unquote(C);
|
2003-11-19 22:57:11 +03:00
|
|
|
A->specificity = 0x100; }
|
|
|
|
detail(A) ::= COLON IDENT(B).
|
2003-10-17 21:39:29 +04:00
|
|
|
{ if (strcasecmp(B, "link") == 0) {
|
|
|
|
A = css_new_node(CSS_NODE_ATTRIB, xstrdup("href"), 0, 0);
|
2003-11-19 22:57:11 +03:00
|
|
|
A->specificity = 0x100;
|
2003-10-17 21:39:29 +04:00
|
|
|
free(B);
|
|
|
|
} else {
|
|
|
|
A = css_new_node(CSS_NODE_PSEUDO, B, 0, 0);
|
2003-11-19 22:57:11 +03:00
|
|
|
A->specificity = 0x100;
|
2003-10-17 21:39:29 +04:00
|
|
|
} }
|
2003-11-19 22:57:11 +03:00
|
|
|
detail(A) ::= COLON FUNCTION(B) IDENT RPAREN.
|
2003-10-17 21:39:29 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_PSEUDO, B, 0, 0);
|
2003-11-19 22:57:11 +03:00
|
|
|
A->specificity = 0x100; }
|
2003-03-26 00:03:14 +03:00
|
|
|
|
|
|
|
declaration_list(A) ::= .
|
|
|
|
{ A = 0; }
|
|
|
|
declaration_list(A) ::= declaration(B).
|
|
|
|
{ A = B; }
|
|
|
|
declaration_list(A) ::= declaration_list(B) SEMI.
|
|
|
|
{ A = B; }
|
|
|
|
declaration_list(A) ::= declaration(B) SEMI declaration_list(C).
|
2003-11-19 22:57:11 +03:00
|
|
|
{ if (B) { B->next = C; A = B; } else { A = C; } }
|
2003-03-26 00:03:14 +03:00
|
|
|
|
|
|
|
declaration(A) ::= property(B) COLON value(C).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_DECLARATION, B, C, 0); }
|
2003-11-19 22:57:11 +03:00
|
|
|
declaration(A) ::= any_list_1(B). /* malformed declaration: ignore */
|
|
|
|
{ A = 0; css_free_node(B); }
|
2003-03-26 00:03:14 +03:00
|
|
|
|
|
|
|
property(A) ::= IDENT(B).
|
|
|
|
{ A = B; }
|
|
|
|
|
|
|
|
value(A) ::= any(B).
|
|
|
|
{ A = B; }
|
|
|
|
value(A) ::= any(B) value(C).
|
|
|
|
{ B->next = C; A = B; }
|
|
|
|
value(A) ::= value(B) block.
|
|
|
|
{ A = B; }
|
|
|
|
value(A) ::= value(B) ATKEYWORD.
|
|
|
|
{ A = B; }
|
|
|
|
|
2003-11-19 22:57:11 +03:00
|
|
|
|
2003-03-26 00:03:14 +03:00
|
|
|
any_list(A) ::= .
|
|
|
|
{ A = 0; }
|
|
|
|
any_list(A) ::= any(B) any_list(C).
|
|
|
|
{ B->next = C; A = B; }
|
2003-10-17 00:48:22 +04:00
|
|
|
any_list_1(A) ::= any(B) any_list(C).
|
|
|
|
{ B->next = C; A = B; }
|
2003-03-26 00:03:14 +03:00
|
|
|
any(A) ::= IDENT(B).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_IDENT, B, 0, 0); }
|
2003-03-26 00:03:14 +03:00
|
|
|
any(A) ::= NUMBER(B).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_NUMBER, B, 0, 0); }
|
2003-03-26 00:03:14 +03:00
|
|
|
any(A) ::= PERCENTAGE(B).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_PERCENTAGE, B, 0, 0); }
|
2003-03-26 00:03:14 +03:00
|
|
|
any(A) ::= DIMENSION(B).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_DIMENSION, B, 0, 0); }
|
2003-03-26 00:03:14 +03:00
|
|
|
any(A) ::= STRING(B).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_STRING, css_unquote(B), 0, 0); }
|
2003-03-26 00:03:14 +03:00
|
|
|
any(A) ::= DELIM(B).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_DELIM, B, 0, 0); }
|
2003-03-26 00:03:14 +03:00
|
|
|
any(A) ::= URI(B).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_URI, B, 0, 0); }
|
2003-03-26 00:03:14 +03:00
|
|
|
any(A) ::= HASH(B).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_HASH, B, 0, 0); }
|
2003-03-26 00:03:14 +03:00
|
|
|
any(A) ::= UNICODE_RANGE(B).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_UNICODE_RANGE, B, 0, 0); }
|
2003-04-02 01:33:08 +04:00
|
|
|
any(A) ::= INCLUDES.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_INCLUDES, 0, 0, 0); }
|
2003-03-26 00:03:14 +03:00
|
|
|
any(A) ::= FUNCTION(B).
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_FUNCTION, B, 0, 0); }
|
2003-04-02 01:33:08 +04:00
|
|
|
any(A) ::= DASHMATCH.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_DASHMATCH, 0, 0, 0); }
|
2003-04-02 01:33:08 +04:00
|
|
|
any(A) ::= COLON.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_COLON, 0, 0, 0); }
|
2003-04-02 01:33:08 +04:00
|
|
|
any(A) ::= COMMA.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_COMMA, 0, 0, 0); }
|
2003-10-17 21:39:29 +04:00
|
|
|
any(A) ::= DOT.
|
|
|
|
{ A = css_new_node(CSS_NODE_DOT, 0, 0, 0); }
|
2003-04-02 01:33:08 +04:00
|
|
|
any(A) ::= PLUS.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_PLUS, 0, 0, 0); }
|
2003-04-02 01:33:08 +04:00
|
|
|
any(A) ::= GT.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_GT, 0, 0, 0); }
|
2003-03-26 00:03:14 +03:00
|
|
|
any(A) ::= LPAREN any_list(B) RPAREN.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_PAREN, 0, B, 0); }
|
2003-03-26 00:03:14 +03:00
|
|
|
any(A) ::= LBRAC any_list(B) RBRAC.
|
2003-10-08 01:34:27 +04:00
|
|
|
{ A = css_new_node(CSS_NODE_BRAC, 0, B, 0); }
|
2003-03-26 00:03:14 +03:00
|
|
|
|
|
|
|
|
|
|
|
/* lemon directives */
|
|
|
|
|
2003-04-05 20:24:43 +04:00
|
|
|
%extra_argument { struct parse_params *param }
|
2003-04-02 00:18:18 +04:00
|
|
|
%include {
|
|
|
|
#define CSS_INTERNALS
|
2003-04-04 19:19:32 +04:00
|
|
|
#include "netsurf/css/scanner.h"
|
2003-04-02 00:18:18 +04:00
|
|
|
#include "netsurf/css/css.h"
|
2003-03-26 00:03:14 +03:00
|
|
|
#include "netsurf/utils/utils.h" }
|
|
|
|
%name css_parser_
|
|
|
|
|
|
|
|
%token_type { char* }
|
|
|
|
%token_destructor { xfree($$); }
|
|
|
|
|
2003-10-08 01:34:27 +04:00
|
|
|
%type selector_list { struct css_node * }
|
|
|
|
%type selector { struct css_node * }
|
|
|
|
%type css_combinator { css_combinator }
|
|
|
|
%type simple_selector { struct css_node * }
|
|
|
|
%type detail_list { struct css_node * }
|
2003-11-19 22:57:11 +03:00
|
|
|
%type detail { struct css_node * }
|
2003-10-08 01:34:27 +04:00
|
|
|
%type declaration_list { struct css_node * }
|
|
|
|
%type declaration { struct css_node * }
|
|
|
|
%type value { struct css_node * }
|
|
|
|
%type any_list { struct css_node * }
|
|
|
|
%type any_list_1 { struct css_node * }
|
|
|
|
%type any { struct css_node * }
|
2003-04-02 00:18:18 +04:00
|
|
|
|
2003-04-04 19:19:32 +04:00
|
|
|
%destructor selector_list { css_free_node($$); }
|
2003-03-26 00:03:14 +03:00
|
|
|
%destructor selector { css_free_node($$); }
|
2003-04-04 19:19:32 +04:00
|
|
|
%destructor simple_selector { css_free_node($$); }
|
2003-09-28 03:36:34 +04:00
|
|
|
%destructor detail_list { css_free_node($$); }
|
2003-03-26 00:03:14 +03:00
|
|
|
%destructor declaration_list { css_free_node($$); }
|
|
|
|
%destructor declaration { css_free_node($$); }
|
|
|
|
%destructor value { css_free_node($$); }
|
|
|
|
%destructor any_list { css_free_node($$); }
|
|
|
|
%destructor any_list_1 { css_free_node($$); }
|
|
|
|
%destructor any { css_free_node($$); }
|
|
|
|
|
2003-10-17 21:39:29 +04:00
|
|
|
%left COLON COMMA GT HASH LBRAC PLUS.
|
|
|
|
%left DOT.
|
2003-10-17 00:48:22 +04:00
|
|
|
%left IDENT.
|
|
|
|
%left LBRACE.
|
2003-10-17 21:39:29 +04:00
|
|
|
|
|
|
|
%syntax_error { param->syntax_error = true; }
|