So basically I want to parsed structure CSS code in PHP, using a lexer/parser generated by the PEAR packages PHP_LexerGenerator and PHP_ParserGenerator. My goal is to parse files like this:
selector, selector2 {
prop: value;
prop2 /*comment */ :
value;
subselector {
prop: value;
subsub { prop: value; }
}
}
This is all fine as long as I don't have pseudo classes. Pseudoclasses allow it, to add :
and a CSS name ([a-z][a-z0-9]*
) to an element, like in a.menu:visited
. Being somewhat lazy, the parser has no list of valid pseudo classes and accepts everything for the class name.
My grammar (ignoring all the special cases and whitespace) looks like this:
document ::= (<rule>)*
rule ::= <selector> '{' (<content>)* '}'
content ::= <rule>
content ::= <definition>
definition ::= <name> ':' <name> ';'
// h1 .class.class2#id :visited
<selector> ::= <name> (('.'|'#') <name>)* (':' <name>)?
Now, when I try to parse the following
h1 {
test:visited {
simple: case;
}
}
The parser complains, that it expected a <name>
to follow the double colon. So it tries to read the simple:
as a <selector>
(just look at the syntax highlighting of SO).
Is it my error that the parser can not backtrace enough to try the <definition>
rule? Or is Lemon just not powerful enough to express this? If so, what can I do to get a parser working with this grammar?
select1, select2 { ... }
notation. There's no rule that handles the 'comma separated list of selectors'. – Jonathan Leffler