rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
76 lines • 2.26 kB
JavaScript
import { TokenType } from '../models/Lexeme';
import { StringUtils } from '../utils/stringUtils';
/**
* Base class for token readers
*/
export 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 === TokenType.Command || type === TokenType.Operator || type === 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.getDebugPositionInfo(this.input, errPosition);
}
}
//# sourceMappingURL=BaseTokenReader.js.map