• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

test/25-Sep-2023-4742

.npmignoreD24-Sep-2023104 1514

.travis.ymlD24-Sep-202338 43

LICENSED24-Sep-20231 KiB1916

README.mdD24-Sep-20231.7 KiB6947

index.jsD24-Sep-2023841 3129

package.jsonD24-Sep-2023469 1919

README.md

1# token-stream
2
3Take an array of token and produce a more useful API to give to a parser.
4
5[![Build Status](https://img.shields.io/travis/jadejs/token-stream/master.svg)](https://travis-ci.org/jadejs/token-stream)
6[![Dependency Status](https://img.shields.io/gemnasium/jadejs/token-stream.svg)](https://gemnasium.com/jadejs/token-stream)
7[![NPM version](https://img.shields.io/npm/v/token-stream.svg)](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