Created
August 28, 2011 23:05
-
-
Save tj/1177360 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Module dependencies. | |
*/ | |
var fs = require('fs'); | |
var css = fs.readFileSync('style.css', 'utf8'); | |
function scan(buf) { | |
var state = ['root']; | |
state.name = function(){ | |
return this[this.length - 1]; | |
}; | |
return next; | |
function o(pattern) { | |
var captures; | |
if ('string' == typeof pattern) { | |
if (0 == buf.indexOf(pattern)) { | |
buf = buf.slice(pattern.length); | |
return pattern; | |
} | |
} else if (pattern instanceof RegExp) { | |
if (captures = pattern.exec(buf)) { | |
buf = buf.slice(captures[0].length); | |
return captures[1]; | |
} | |
} | |
} | |
function next() { | |
return ident() | |
|| block() | |
|| colon() | |
|| eol() | |
|| string() | |
|| whitespace() | |
|| commentOpen() | |
|| commentClose() | |
|| comment(); | |
} | |
function string() { | |
var ret = o(/^"([^"]+)"/); | |
if (!ret) return; | |
return ['string', ret]; | |
} | |
function whitespace() { | |
if (o(/^(\s+)/)) return next(); | |
} | |
function colon() { | |
if (o(':')) return ['colon']; | |
} | |
function eol() { | |
if (o(';')) return ['eol']; | |
} | |
function block() { | |
if (o('{')) return ['block-open']; | |
if (o('}')) return ['block-close']; | |
} | |
function ident() { | |
var ret = o(/^([-\w]+)/); | |
if (!ret) return; | |
return ['ident', ret]; | |
} | |
function comment() { | |
if ('comment' != state.name()) return; | |
var i = buf.indexOf('*/') | |
, ret = buf.slice(0, i); | |
buf = buf.slice(i); | |
return ['comment', ret]; | |
} | |
function commentClose() { | |
var ret = o('*/'); | |
if (!ret) return; | |
state.pop(); | |
return ['comment-close']; | |
} | |
function commentOpen() { | |
var ret = o('/*'); | |
if (!ret) return; | |
state.push('comment'); | |
return ['comment-open']; | |
} | |
} | |
var next = scan(css); | |
var tok; | |
while (tok = next()) console.log(tok); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ 'ident', 'body' ] | |
[ 'block-open' ] | |
[ 'ident', 'padding' ] | |
[ 'colon' ] | |
[ 'ident', '50px' ] | |
[ 'eol' ] | |
[ 'ident', 'font-size' ] | |
[ 'colon' ] | |
[ 'ident', '12px' ] | |
[ 'eol' ] | |
[ 'block-close' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment