rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
80 lines • 2.45 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseTokenReader = void 0;
const Lexeme_1 = require("../models/Lexeme");
const stringUtils_1 = require("../utils/stringUtils");
/**
* Base class for token readers
*/
class BaseTokenReader {
constructor(input, position = 0) {
this.input = input;
this.position = position;
}
/**
* Get the current position in the input
*/
getPosition() {
return this.position;
}
/**
* Set the position in the input
*/
setPosition(position) {
this.position = position;
}
/**
* Check if we've reached the end of input
*/
isEndOfInput(shift = 0) {
return this.position + shift >= this.input.length;
}
/**
* Check if we can read more characters
*/
canRead(shift = 0) {
return !this.isEndOfInput(shift);
}
/**
* Read an expected character
*/
read(expectChar) {
if (this.isEndOfInput()) {
throw new Error(`Unexpected character. expect: ${expectChar}, actual: EndOfInput, position: ${this.position}`);
}
const char = this.input[this.position];
if (char !== expectChar) {
throw new Error(`Unexpected character. expect: ${expectChar}, actual: ${char}, position: ${this.position}`);
}
this.position++;
return char;
}
/**
* Create a lexeme with the specified type and value
*/
createLexeme(type, value, comments = null) {
if (type === Lexeme_1.TokenType.Command || type === Lexeme_1.TokenType.Operator || type === Lexeme_1.TokenType.Function) {
// Benchmark tests showed that directly calling toLowerCase() is ~5x faster
// than first checking if the string is already lowercase.
// See benchmarks/lowercase-benchmark.js for detailed performance analysis.
return {
type,
value: value.toLowerCase(),
comments: comments,
};
}
return {
type,
value,
comments: comments,
};
}
/**
* Get debug info for error reporting
*/
getDebugPositionInfo(errPosition) {
return stringUtils_1.StringUtils.getDebugPositionInfo(this.input, errPosition);
}
}
exports.BaseTokenReader = BaseTokenReader;
//# sourceMappingURL=BaseTokenReader.js.map
;