Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | - | - | ||||
test/ | 25-Sep-2023 | - | 47 | 42 | ||
.npmignore | D | 24-Sep-2023 | 104 | 15 | 14 | |
.travis.yml | D | 24-Sep-2023 | 38 | 4 | 3 | |
LICENSE | D | 24-Sep-2023 | 1 KiB | 19 | 16 | |
README.md | D | 24-Sep-2023 | 1.7 KiB | 69 | 47 | |
index.js | D | 24-Sep-2023 | 841 | 31 | 29 | |
package.json | D | 24-Sep-2023 | 469 | 19 | 19 |
README.md
1# token-stream 2 3Take an array of token and produce a more useful API to give to a parser. 4 5[](https://travis-ci.org/jadejs/token-stream) 6[](https://gemnasium.com/jadejs/token-stream) 7[](https://www.npmjs.org/package/token-stream) 8 9## Installation 10 11 npm install token-stream 12 13## Usage 14 15```js 16var TokenStream = require('token-stream'); 17 18var stream = new TokenStream([ 19 'a', 20 'b', 21 'c', 22 'd' 23]); 24assert(stream.peek() === 'a'); 25assert(stream.lookahead(0) == 'a'); 26assert(stream.lookahead(1) == 'b'); 27 28assert(stream.advance() === 'a'); 29assert(stream.peek() === 'b'); 30assert(stream.lookahead(0) == 'b'); 31assert(stream.lookahead(1) == 'c'); 32 33stream.defer('z'); 34assert(stream.peek() === 'z'); 35assert(stream.lookahead(0) == 'z'); 36assert(stream.lookahead(1) == 'b'); 37assert(stream.advance() === 'z'); 38assert(stream.advance() === 'b'); 39assert(stream.advance() === 'c'); 40assert(stream.advance() === 'd'); 41 42// an error is thrown if you try and advance beyond the end of the stream 43assert.throws(function () { 44 stream.adavance(); 45}); 46``` 47 48## API 49 50### stream.peek() 51 52Gets and returns the next item in the stream without advancing the stream's position. 53 54### stream.advance() 55 56Returns the next item in the stream and advances the stream by one item. 57 58### stream.defer(token) 59 60Put a token on the start of the stream (useful if you need to back track after calling advance. 61 62### stream.lookahead(index) 63 64Return the item at `index` position from the start of the stream, but don't advance the stream. `stream.lookahead(0)` is equivalent to `stream.peek()`. 65 66## License 67 68 MIT 69