index.js 972 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. var assert = require('assert');
  3. var TokenStream = require('../');
  4. assert.throws(function () {
  5. new TokenStream('foo,bar');
  6. });
  7. var stream = new TokenStream([
  8. 'a',
  9. 'b',
  10. 'c',
  11. 'd'
  12. ]);
  13. assert.throws(function () {
  14. stream.lookahead(9);
  15. });
  16. assert(stream.peek() === 'a');
  17. assert(stream.lookahead(0) == 'a');
  18. assert(stream.lookahead(1) == 'b');
  19. assert(stream.advance() === 'a');
  20. assert(stream.peek() === 'b');
  21. assert(stream.lookahead(0) == 'b');
  22. assert(stream.lookahead(1) == 'c');
  23. stream.defer('z');
  24. assert(stream.peek() === 'z');
  25. assert(stream.lookahead(0) == 'z');
  26. assert(stream.lookahead(1) == 'b');
  27. assert(stream.advance() === 'z');
  28. assert(stream.advance() === 'b');
  29. assert(stream.advance() === 'c');
  30. assert(stream.advance() === 'd');
  31. assert.throws(function () {
  32. stream.peek();
  33. });
  34. assert.throws(function () {
  35. stream.lookahead(0);
  36. });
  37. assert.throws(function () {
  38. stream.lookahead(1);
  39. });
  40. assert.throws(function () {
  41. stream.advance();
  42. });