| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- var utils_1 = require("./utils");
- var Parser = /** @class */ (function () {
- function Parser(source, fileName) {
- this.source = source;
- this.fileName = fileName;
- this.pos = 0;
- this.line = 1;
- this.column = 1;
- }
- Parser.prototype.consumeChar = function () {
- var ch = this.source.charCodeAt(this.pos);
- this.advance();
- return ch;
- };
- Parser.prototype.consumeQuoteString = function (resultRef) {
- if (resultRef === void 0) { resultRef = {}; }
- var start = this.pos;
- if (this.match(39 /* SINGLE_QUOTE */) || this.match(34 /* DOUBLE_QUOTE */) || this.match(96 /* BACK_QUOTE */)) {
- var quoteType = this.consumeChar();
- while (!this.eof() && !this.match(quoteType)) {
- if (this.match(92 /* BACK_SLASH */) && this.match(quoteType, this.pos + 1)) {
- this.advance(2);
- }
- else
- this.advance();
- }
- if (this.match(quoteType))
- this.advance();
- resultRef.result = this.source.substring(start, this.pos);
- return true;
- }
- return false;
- };
- Parser.prototype.advance = function (step) {
- var _this = this;
- var _advanceOnce = function () {
- if (_this.source[_this.pos] === '\n') {
- _this.column = 1;
- _this.line++;
- }
- else {
- _this.column++;
- }
- _this.pos++;
- };
- if (!step) {
- _advanceOnce();
- }
- else {
- while (step-- > 0)
- _advanceOnce();
- }
- };
- Parser.prototype.eof = function () {
- return this.pos >= this.source.length;
- };
- Parser.prototype.match = function (code, pos) {
- return this.source.charCodeAt(pos && pos !== -1 ? pos : this.pos) === code;
- };
- Parser.prototype.currentChar = function () {
- return this.source[this.pos];
- };
- Parser.prototype.peekCharCode = function () {
- return this.source.charCodeAt(this.pos);
- };
- Parser.prototype.consumeWhitespace = function () {
- this.consumeWhile(utils_1.isWhitespace);
- };
- Parser.prototype.consumeWhile = function (checkFunc) {
- var result = [];
- while (!this.eof() && checkFunc(this.source.charCodeAt(this.pos))) {
- var ch = this.source[this.pos];
- this.advance();
- result.push(ch);
- }
- return result.join('');
- };
- Parser.prototype.currentContext = function () {
- var MAX_NEXT_LINE = 2;
- var answer = '';
- var _pos = this.pos - 1, accumulateNextLine = 0;
- while (_pos >= 0 && accumulateNextLine < MAX_NEXT_LINE + 2) {
- if (this.source[_pos] === '\n') {
- accumulateNextLine++;
- }
- answer = this.source[_pos] + answer;
- _pos--;
- }
- if (this.pos < this.source.length) {
- answer += this.source[this.pos];
- }
- _pos = this.pos + 1, accumulateNextLine = 0;
- while (_pos < this.source.length && accumulateNextLine < MAX_NEXT_LINE) {
- if (this.source[_pos] === '\n') {
- accumulateNextLine++;
- }
- answer += this.source[_pos];
- _pos++;
- }
- return answer;
- };
- return Parser;
- }());
- exports.default = Parser;
|