The Cruiser Parser Library
Written by on June 1st, 2007 in Ajax News.
Dan Yoder has recently developed a small (2.5k) JavaScript library for creating top-down recursive descent LLk parsers, Cruiser.
Dan uses it himself to parse stylesheets, to support CSS3 selectors.
Here is the parser:
JAVASCRIPT:
-
-
with ( Parser.Operators ) {
-
var g = Behaviors.Stylesheet.Grammar;
-
var t = Behaviors.Stylesheet.Translator;
-
// basic tokens
-
g.lbrace = token(’{'); g.rbrace = token(’}');
-
g.lparen = token(/\(/); g.rparen = token(/\)/);
-
g.colon = token(’:'); g.semicolon = token(’;');
-
// attributes
-
g.attrName = token(/[\w\-\d]+/);
-
g.attrValue = token(/[^;\}]+/);
-
g.attr = pair(g.attrName,g.attrValue,g.colon);
-
g.attrList = list(g.attr,g.semicolon,true);
-
g.style = process(
-
between(g.lbrace,g.attrList,g.rbrace),t.style);
-
// style rules
-
g.selector = token(/[^\{]+/);
-
g.rule = each(g.selector,g.style);
-
g.rules = process(many(g.rule),t.rules);
-
// comments
-
g.inlineComment = token(/\x2F\x2F[^\n]\n/);
-
g.multilineComment = token(/\x2F\x2A.*?\x2A\x2F/);
-
g.comments = ignore(
-
any(g.inlineComment,g.multilineComment));
-
// parser
-
Behaviors.Stylesheet._parse = process(
-
many(any(g.comments,g.rules)),t.parse);
-
}
-
Parse away.
Source: Ajaxian
Original Article: http://ajaxian.com/archives/the-cruiser-parser-library