Friday, June 1st, 2007
The Cruiser Parser Library
<>p>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:
-
-
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(/x2Fx2F[^n]n/);
-
g.multilineComment = token(/x2Fx2A.*?x2Ax2F/);
-
g.comments = ignore(
-
any(g.inlineComment,g.multilineComment));
-
// parser
-
Behaviors.Stylesheet._parse = process(
-
many(any(g.comments,g.rules)),t.parse);
-
}
-
Parse away.
Related Content:











now for those who have no clue what you’re talking about… what would I use this for? :-)
That would be 2.5k, plus cruiser.js (4.5k) plus Prototype 1.5.1 (21.36k) equals 28.36k.
I don’t understand why devs fail to understand how to truley use JavaScript and their Frameworks.
The code is around 17kb if you use it right.
http://www.thinkvitamin.com/features/webapps/serving-javascript-fast
http://code.google.com/p/minify/
http://www.devpro.it/php5_id_145.html
http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files
This is a niche kind of library that I am personally *very* interested in. Thanks much for the lead, Dion.
On a related note, has anyone heard of a javascript parser that can read in an XML dtd and turn it into some kind of useable properties?
@Mike Ritchie,
Here’s a lead: http://xmljs.sourceforge.net/
Or you could try to pull it off yourself using Cruiser.