parser.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var utils_1 = require("./utils");
  4. var Parser = /** @class */ (function () {
  5. function Parser(source, fileName) {
  6. this.source = source;
  7. this.fileName = fileName;
  8. this.pos = 0;
  9. this.line = 1;
  10. this.column = 1;
  11. }
  12. Parser.prototype.consumeChar = function () {
  13. var ch = this.source.charCodeAt(this.pos);
  14. this.advance();
  15. return ch;
  16. };
  17. Parser.prototype.consumeQuoteString = function (resultRef) {
  18. if (resultRef === void 0) { resultRef = {}; }
  19. var start = this.pos;
  20. if (this.match(39 /* SINGLE_QUOTE */) || this.match(34 /* DOUBLE_QUOTE */) || this.match(96 /* BACK_QUOTE */)) {
  21. var quoteType = this.consumeChar();
  22. while (!this.eof() && !this.match(quoteType)) {
  23. if (this.match(92 /* BACK_SLASH */) && this.match(quoteType, this.pos + 1)) {
  24. this.advance(2);
  25. }
  26. else
  27. this.advance();
  28. }
  29. if (this.match(quoteType))
  30. this.advance();
  31. resultRef.result = this.source.substring(start, this.pos);
  32. return true;
  33. }
  34. return false;
  35. };
  36. Parser.prototype.advance = function (step) {
  37. var _this = this;
  38. var _advanceOnce = function () {
  39. if (_this.source[_this.pos] === '\n') {
  40. _this.column = 1;
  41. _this.line++;
  42. }
  43. else {
  44. _this.column++;
  45. }
  46. _this.pos++;
  47. };
  48. if (!step) {
  49. _advanceOnce();
  50. }
  51. else {
  52. while (step-- > 0)
  53. _advanceOnce();
  54. }
  55. };
  56. Parser.prototype.eof = function () {
  57. return this.pos >= this.source.length;
  58. };
  59. Parser.prototype.match = function (code, pos) {
  60. return this.source.charCodeAt(pos && pos !== -1 ? pos : this.pos) === code;
  61. };
  62. Parser.prototype.currentChar = function () {
  63. return this.source[this.pos];
  64. };
  65. Parser.prototype.peekCharCode = function () {
  66. return this.source.charCodeAt(this.pos);
  67. };
  68. Parser.prototype.consumeWhitespace = function () {
  69. this.consumeWhile(utils_1.isWhitespace);
  70. };
  71. Parser.prototype.consumeWhile = function (checkFunc) {
  72. var result = [];
  73. while (!this.eof() && checkFunc(this.source.charCodeAt(this.pos))) {
  74. var ch = this.source[this.pos];
  75. this.advance();
  76. result.push(ch);
  77. }
  78. return result.join('');
  79. };
  80. Parser.prototype.currentContext = function () {
  81. var MAX_NEXT_LINE = 2;
  82. var answer = '';
  83. var _pos = this.pos - 1, accumulateNextLine = 0;
  84. while (_pos >= 0 && accumulateNextLine < MAX_NEXT_LINE + 2) {
  85. if (this.source[_pos] === '\n') {
  86. accumulateNextLine++;
  87. }
  88. answer = this.source[_pos] + answer;
  89. _pos--;
  90. }
  91. if (this.pos < this.source.length) {
  92. answer += this.source[this.pos];
  93. }
  94. _pos = this.pos + 1, accumulateNextLine = 0;
  95. while (_pos < this.source.length && accumulateNextLine < MAX_NEXT_LINE) {
  96. if (this.source[_pos] === '\n') {
  97. accumulateNextLine++;
  98. }
  99. answer += this.source[_pos];
  100. _pos++;
  101. }
  102. return answer;
  103. };
  104. return Parser;
  105. }());
  106. exports.default = Parser;