scanner.js 2.6 KB

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