Tim van der Lippe | 706ec96 | 2021-06-04 13:24:42 +0100 | [diff] [blame] | 1 | var SyntaxError = require('./SyntaxError'); |
| 2 | |
| 3 | var TAB = 9; |
| 4 | var N = 10; |
| 5 | var F = 12; |
| 6 | var R = 13; |
| 7 | var SPACE = 32; |
| 8 | |
| 9 | var Tokenizer = function(str) { |
| 10 | this.str = str; |
| 11 | this.pos = 0; |
| 12 | }; |
| 13 | |
| 14 | Tokenizer.prototype = { |
| 15 | charCodeAt: function(pos) { |
| 16 | return pos < this.str.length ? this.str.charCodeAt(pos) : 0; |
| 17 | }, |
| 18 | charCode: function() { |
| 19 | return this.charCodeAt(this.pos); |
| 20 | }, |
| 21 | nextCharCode: function() { |
| 22 | return this.charCodeAt(this.pos + 1); |
| 23 | }, |
| 24 | nextNonWsCode: function(pos) { |
| 25 | return this.charCodeAt(this.findWsEnd(pos)); |
| 26 | }, |
| 27 | findWsEnd: function(pos) { |
| 28 | for (; pos < this.str.length; pos++) { |
| 29 | var code = this.str.charCodeAt(pos); |
| 30 | if (code !== R && code !== N && code !== F && code !== SPACE && code !== TAB) { |
| 31 | break; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | return pos; |
| 36 | }, |
| 37 | substringToPos: function(end) { |
| 38 | return this.str.substring(this.pos, this.pos = end); |
| 39 | }, |
| 40 | eat: function(code) { |
| 41 | if (this.charCode() !== code) { |
| 42 | this.error('Expect `' + String.fromCharCode(code) + '`'); |
| 43 | } |
| 44 | |
| 45 | this.pos++; |
| 46 | }, |
| 47 | peek: function() { |
| 48 | return this.pos < this.str.length ? this.str.charAt(this.pos++) : ''; |
| 49 | }, |
| 50 | error: function(message) { |
| 51 | throw new SyntaxError(message, this.str, this.pos); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | module.exports = Tokenizer; |