Created
December 19, 2014 16:17
-
-
Save lydell/be49dbf80c382c473004 to your computer and use it in GitHub Desktop.
Simple tokenizer function (for js-tokens and css-tokens)
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
### | |
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 |
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
// 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