Made tokenize() compliant with ES6 iterables for easier processing.

This commit is contained in:
Ingvar Stepanyan 2015-01-14 12:27:58 +02:00
parent 0f55a53a7d
commit ad9411d2ae
2 changed files with 24 additions and 0 deletions

View File

@ -162,6 +162,17 @@ token, and returns a `{start, end, type, value}` object (with added
`loc` property when the `locations` option is enabled and `range`
property when the `ranges` option is enabled).
In ES6 environment, returned result can be used as any other protocol-compliant iterable:
```javascript
for (let token of acorn.tokenize(str)) {
// iterate over the tokens
}
// transform code to array of tokens:
var tokens = [...acorn.tokenize(str)];
```
**tokTypes** holds an object mapping names to the token type objects
that end up in the `type` properties of tokens.

View File

@ -243,6 +243,19 @@
skipSpace();
};
getToken.current = function() { return new Token(); };
if (typeof Symbol !== 'undefined') {
getToken[Symbol.iterator] = function () {
return {
next: function () {
var token = getToken();
return {
done: token.type === _eof,
value: token
};
}
};
};
}
getToken.options = options;
return getToken;
};