mocha-vscode/src/zq/parser/ZqParserContext.ts

123 lines
3.1 KiB
TypeScript

// Copyright (C) 2025 Michael Becker <alcexhim@gmail.com>
//
// This file is part of mocha-vscode.
//
// mocha-vscode is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mocha-vscode is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with mocha-vscode. If not, see <https://www.gnu.org/licenses/>.
import { StringExtensions } from "../../StringExtensions";
import { ZqTokenInfo } from "./ZqTokenInfo";
export class ZqParserContext {
private _value: string = "";
public get value(): string {
return this._value;
}
private _currentIndex: number = 0;
public get currentIndex(): number {
return this._currentIndex;
}
public get currentValue(): string {
return this.value.substring(this.currentIndex);
}
public get endOfStream(): boolean {
return this.currentIndex >= this.value.length;
}
private _lineIndex : number = 0;
public get lineIndex() : number {
return this._lineIndex;
}
public set lineIndex(v : number) {
this._lineIndex = v;
}
private _columnIndex : number = 0;
public get columnIndex() : number {
return this._columnIndex;
}
public set columnIndex(v : number) {
this._columnIndex = v;
}
constructor(value: string) {
this._value = value;
}
readLine() : string {
let idxNewline = this._value.indexOf("\n", this._currentIndex);
let retval = this._value.substring(this._currentIndex);
if (idxNewline > -1) {
retval = this._value.substring(this._currentIndex, idxNewline);
this._currentIndex += retval.length + 1;
this._lineIndex++;
this._columnIndex = 0;
}
else {
this._currentIndex += retval.length;
this._lineIndex++;
this._columnIndex = 0;
}
return retval;
}
readChars(count: number) {
let before: string = this._value.substring(this._currentIndex, this._currentIndex + count);
this._currentIndex += count;
return before;
}
readToken(): ZqTokenInfo | null {
let end = this._value.length;
let token = "";
let index = StringExtensions.indexOfAnyEx(this._value, [' ', '(', '{', ':'], this._currentIndex);
if (index.Index > -1) {
token = index.Needle;
end = index.Index;
}
let tok : ZqTokenInfo | null = null;
let val = this._value.substring(this._currentIndex, end);
if (val === '' && this._value.length > 0) {
tok = new ZqTokenInfo(this._value.substring(this._currentIndex, end + 1), end + 1);
} else {
tok = new ZqTokenInfo(val, end);
}
this._currentIndex += val.length + token.length;
tok.token = token;
return tok;
/*
const id = line.indexOf('function');
const id2 = line.indexOf('(', id);
let name: string = "";
if (id > -1) {
name = line.substring(id + 'function'.length, id2).trim();
}
return null;
*/
}
}