mirror of
https://github.com/adelphes/android-dev-ext.git
synced 2025-12-23 01:48:18 +00:00
refactor to prepare for merging with type parsing
This commit is contained in:
72
langserver/java/TokenList.js
Normal file
72
langserver/java/TokenList.js
Normal file
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* @typedef {import('./tokenizer').Token} Token
|
||||
*/
|
||||
const ParseProblem = require('./parsetypes/parse-problem');
|
||||
|
||||
class TokenList {
|
||||
/**
|
||||
* @param {Token[]} tokens
|
||||
*/
|
||||
constructor(tokens) {
|
||||
this.tokens = tokens;
|
||||
this.idx = -1;
|
||||
/** @type {Token} */
|
||||
this.current = null;
|
||||
this.inc();
|
||||
/** @type {ParseProblem[]} */
|
||||
this.problems = [];
|
||||
}
|
||||
|
||||
inc() {
|
||||
for (; ;) {
|
||||
this.current = this.tokens[this.idx += 1];
|
||||
if (!this.current || this.current.kind !== 'wsc') {
|
||||
return this.current;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Check if the current token matches the specified value and consumes it
|
||||
* @param {string} value
|
||||
*/
|
||||
isValue(value) {
|
||||
if (this.current.value === value) {
|
||||
this.inc();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current token matches the specified value and consumes it or reports an error
|
||||
* @param {string} value
|
||||
*/
|
||||
expectValue(value) {
|
||||
if (this.isValue(value)) {
|
||||
return true;
|
||||
}
|
||||
const addproblem = require("./body-parser3").addproblem;
|
||||
addproblem(this, ParseProblem.Error(this.current, `${value} expected`));
|
||||
return false;
|
||||
}
|
||||
|
||||
get previous() {
|
||||
for (let idx = this.idx - 1; idx >= 0; idx--) {
|
||||
if (idx === 0 || this.tokens[idx].kind !== 'wsc') {
|
||||
return this.tokens[idx];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} start
|
||||
* @param {number} delete_count
|
||||
* @param {...Token} insert
|
||||
*/
|
||||
splice(start, delete_count, ...insert) {
|
||||
this.tokens.splice(start, delete_count, ...insert);
|
||||
this.current = this.tokens[this.idx];
|
||||
}
|
||||
}
|
||||
|
||||
exports.TokenList = TokenList;
|
||||
Reference in New Issue
Block a user