scanner.cjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. const SyntaxError = require('./SyntaxError.cjs');
  3. const TAB = 9;
  4. const N = 10;
  5. const F = 12;
  6. const R = 13;
  7. const SPACE = 32;
  8. const NAME_CHAR = new Uint8Array(128).map((_, idx) =>
  9. /[a-zA-Z0-9\-]/.test(String.fromCharCode(idx)) ? 1 : 0
  10. );
  11. class Scanner {
  12. constructor(str) {
  13. this.str = str;
  14. this.pos = 0;
  15. }
  16. charCodeAt(pos) {
  17. return pos < this.str.length ? this.str.charCodeAt(pos) : 0;
  18. }
  19. charCode() {
  20. return this.charCodeAt(this.pos);
  21. }
  22. isNameCharCode(code = this.charCode()) {
  23. return code < 128 && NAME_CHAR[code] === 1;
  24. }
  25. nextCharCode() {
  26. return this.charCodeAt(this.pos + 1);
  27. }
  28. nextNonWsCode(pos) {
  29. return this.charCodeAt(this.findWsEnd(pos));
  30. }
  31. skipWs() {
  32. this.pos = this.findWsEnd(this.pos);
  33. }
  34. findWsEnd(pos) {
  35. for (; pos < this.str.length; pos++) {
  36. const code = this.str.charCodeAt(pos);
  37. if (code !== R && code !== N && code !== F && code !== SPACE && code !== TAB) {
  38. break;
  39. }
  40. }
  41. return pos;
  42. }
  43. substringToPos(end) {
  44. return this.str.substring(this.pos, this.pos = end);
  45. }
  46. eat(code) {
  47. if (this.charCode() !== code) {
  48. this.error('Expect `' + String.fromCharCode(code) + '`');
  49. }
  50. this.pos++;
  51. }
  52. peek() {
  53. return this.pos < this.str.length ? this.str.charAt(this.pos++) : '';
  54. }
  55. error(message) {
  56. throw new SyntaxError.SyntaxError(message, this.str, this.pos);
  57. }
  58. scanSpaces() {
  59. return this.substringToPos(this.findWsEnd(this.pos));
  60. }
  61. scanWord() {
  62. let end = this.pos;
  63. for (; end < this.str.length; end++) {
  64. const code = this.str.charCodeAt(end);
  65. if (code >= 128 || NAME_CHAR[code] === 0) {
  66. break;
  67. }
  68. }
  69. if (this.pos === end) {
  70. this.error('Expect a keyword');
  71. }
  72. return this.substringToPos(end);
  73. }
  74. scanNumber() {
  75. let end = this.pos;
  76. for (; end < this.str.length; end++) {
  77. const code = this.str.charCodeAt(end);
  78. if (code < 48 || code > 57) {
  79. break;
  80. }
  81. }
  82. if (this.pos === end) {
  83. this.error('Expect a number');
  84. }
  85. return this.substringToPos(end);
  86. }
  87. scanString() {
  88. const end = this.str.indexOf('\'', this.pos + 1);
  89. if (end === -1) {
  90. this.pos = this.str.length;
  91. this.error('Expect an apostrophe');
  92. }
  93. return this.substringToPos(end + 1);
  94. }
  95. }
  96. exports.Scanner = Scanner;