Skip to content

Instantly share code, notes, and snippets.

@lydell
Created December 19, 2014 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lydell/be49dbf80c382c473004 to your computer and use it in GitHub Desktop.
Save lydell/be49dbf80c382c473004 to your computer and use it in GitHub Desktop.
Simple tokenizer function (for js-tokens and css-tokens)
###
Tokenizes `string` into an array of tokens, using `regex` such as [js-tokens]
or [css-tokens].
[js-tokens]: https://github.com/lydell/js-tokens
[css-tokens]: https://github.com/lydell/css-tokens
###
tokenize = (string, regex) ->
regex.lastIndex = 0
if string is ""
[]
else
regex.matchToToken match while match = regex.exec string
// Generated by CoffeeScript 1.8.0
/*
Tokenizes `string` into an array of tokens, using `regex` such as [js-tokens]
or [css-tokens].
[js-tokens]: https://github.com/lydell/js-tokens
[css-tokens]: https://github.com/lydell/css-tokens
*/
var tokenize;
tokenize = function(string, regex) {
var match, _results;
regex.lastIndex = 0;
if (string === "") {
return [];
} else {
_results = [];
while (match = regex.exec(string)) {
_results.push(regex.matchToToken(match));
}
return _results;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment