@vlocode/apex
Version:
Salesforce APEX Parser and Grammar
134 lines • 4.09 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CaseInsensitiveCharStream = exports.BufferStream = void 0;
const antlr4ng_1 = require("antlr4ng");
/**
* Implementation of the {@link CharStream} interface that uses a {@link Buffer} as the underlying data source.
*/
class BufferStream {
data;
name;
size;
get index() { return this.#index; }
#strData;
#index = 0;
constructor(data, name = '<unknown>') {
this.data = data;
this.name = name;
this.size = this.data.length;
}
getSourceName() {
return this.name;
}
/**
* Reset the stream so that it's in the same state it was
* when the object was created *except* the data array is not
* touched.
*/
reset() {
this.#index = 0;
}
consume() {
if (this.#index >= this.size) {
// assert this.LA(1) == Token.EOF
throw ("cannot consume EOF");
}
this.#index += 1;
}
LA(offset) {
if (offset === 0) {
return 0; // undefined
}
if (offset < 0) {
offset += 1; // e.g., translate LA(-1) to use offset=0
}
const pos = this.index + offset - 1;
if (pos < 0 || pos >= this.size) { // invalid
return antlr4ng_1.Token.EOF;
}
return this.data[pos];
}
LT(offset) {
return this.LA(offset);
}
mark() {
return -1;
}
release() {
// no resources to release
}
/**
* consume() ahead until p==index; can't just set p=index as we must
* update line and column. If we seek backwards, just set p
*/
seek(index) {
if (index <= this.index) {
this.#index = index; // just jump; don't update stream state (line, ...)
return;
}
// seek forward
this.#index = Math.min(index, this.size);
}
getTextFromRange(start, stop) {
return this.getText(antlr4ng_1.Interval.of(start, stop));
}
getTextFromInterval(interval) {
return this.getText(interval);
}
getText(interval, stop) {
const start = typeof interval === 'number' ? interval : interval.start;
stop = typeof interval === 'number' ? stop : interval.stop;
if (stop >= this.size) {
stop = this.size - 1;
}
if (start >= this.size) {
return '';
}
return this.data.subarray(start, stop + 1).toString();
}
toString() {
if (!this.#strData) {
this.#strData = this.data.toString();
}
return this.#strData;
}
}
exports.BufferStream = BufferStream;
/**
* Case insensitive Decorator for a `CharStream` implementation, this will make the `LA` method return the lower case
* character code for any character that is not `Token.EOF`.
*/
class CaseInsensitiveCharStream {
stream;
get index() { return this.stream.index; }
get size() { return this.stream.size; }
/**
* Implementation of the `CharStream` interface that is case insensitive.
* @param input Original input stream to decorate as case insensitive
*/
constructor(stream) {
this.stream = stream;
}
LA(offset) {
const c = this.stream.LA(offset);
return c === antlr4ng_1.Token.EOF ? c : this.toLower(c);
}
toLower(c) {
return (c >= 65 && c <= 90) ? c + 32 : c;
}
getTextFromRange(start, stop) {
return this.stream.getTextFromRange(start, stop);
}
getTextFromInterval(interval) {
return this.stream.getTextFromInterval(interval);
}
reset() { this.stream.reset(); }
consume() { this.stream.consume(); }
mark() { return this.stream.mark(); }
release(marker) { this.stream.release(marker); }
seek(index) { this.stream.seek(index); }
toString() { return this.stream.toString(); }
getSourceName() { return this.stream.getSourceName(); }
}
exports.CaseInsensitiveCharStream = CaseInsensitiveCharStream;
//# sourceMappingURL=streams.js.map