rob-revisited/ide/ui/js/expr-at.js

120 lines
5.3 KiB
JavaScript

// The PHP expression under the mouse (the debugger's hover inspector): `$user->name`, `$a['x']`, `$this->items[$i]->hp`, `self::$count`,
// not just the word. The chain is read like PHP's tokens: a variable (or Class::$static), then any number of ->prop, ?->prop, ::$static,
// ::CONST and [key] (balanced, quotes honoured). A call `(` ends the chain BEFORE the method: hovering never runs the program's code.
// expressionAt(line, col): the chain from its start to the end of the segment under the mouse (hover `name` of `$user->name` gives
// `$user->name`, hover `$user` gives `$user`; inside `$a[$i]`, the `$i` is its own expression). Pure: no DOM, tested by expr-at.test.mjs.
const IDENT = /[A-Za-z_\u0080-￿0-9]/;
const IDENT_START = /[A-Za-z_\u0080-￿]/;
const isIdentChar = (c) => c !== undefined && IDENT.test(c);
const isIdentStart = (c) => c !== undefined && IDENT_START.test(c);
/** The end (exclusive) of the identifier that starts at i (i itself is an identifier start), or i when there is none. */
function identEnd(s, i) {
if (!isIdentStart(s[i])) return i;
let j = i + 1;
while (isIdentChar(s[j])) j++;
return j;
}
/** The index after the `]` that closes the `[` at i, honouring quotes; -1 when it never closes on this line or holds a statement. */
function bracketEnd(s, i) {
let depth = 0;
for (let j = i; j < s.length; j++) {
const c = s[j];
if (c === '\'' || c === '"') {
for (j++; j < s.length && s[j] !== c; j++) if (s[j] === '\\') j++;
if (j >= s.length) return -1;
} else if (c === '[') depth++;
else if (c === ']') { depth--; if (depth === 0) return j + 1; }
else if (c === ';' || c === '{' || c === '}') return -1;
}
return -1;
}
/** Where a chain starts at i: `$name` or `Name::$name` (self, static, parent, a class). Returns the end of that first segment, or -1. */
function chainHead(s, i) {
if (s[i] === '$') {
const e = identEnd(s, i + 1);
return e > i + 1 ? e : -1;
}
if (isIdentStart(s[i]) || s[i] === '\\') {
let j = i;
while (s[j] === '\\' || isIdentChar(s[j])) j++;
if (s[j] === ':' && s[j + 1] === ':' && s[j + 2] === '$') {
const e = identEnd(s, j + 3);
return e > j + 3 ? e : -1;
}
}
return -1;
}
/**
* Every chain of `s` (nested ones inside [...] too): {start, ends: [end of segment 0, end of segment 1...]}.
* Chains inside strings are found as well ("Hello $name"): they are expressions too.
*/
export function chains(s, from = 0, to = s.length, out = []) {
let i = from;
while (i < to) {
const prev = s[i - 1];
const starts = (s[i] === '$' || isIdentStart(s[i]) || s[i] === '\\') && !(i > from && (isIdentChar(prev) || prev === '$' || prev === '\\'))
&& !(prev === '>' && s[i - 2] === '-') && !(prev === ':' && s[i - 2] === ':');
const headEnd = starts ? chainHead(s, i) : -1;
if (headEnd < 0 || headEnd > to) {
// skip the rest of an identifier so `foo$x` (not a chain start) does not restart inside a word
i++;
continue;
}
const chain = { start: i, ends: [headEnd] };
let j = headEnd;
for (;;) {
if (s[j] === '-' && s[j + 1] === '>' && isIdentStart(s[j + 2]) || s[j] === '?' && s[j + 1] === '-' && s[j + 2] === '>' && isIdentStart(s[j + 3])) {
const nameStart = s[j] === '?' ? j + 3 : j + 2;
const e = identEnd(s, nameStart);
if (s[e] === '(') break; // a method call: not evaluated, the chain stops before it
chain.ends.push(e); j = e;
} else if (s[j] === ':' && s[j + 1] === ':' && (s[j + 2] === '$' || isIdentStart(s[j + 2]))) {
const e = s[j + 2] === '$' ? identEnd(s, j + 3) : identEnd(s, j + 2);
if (e <= j + 3 && s[j + 2] === '$') break;
if (s[e] === '(') break;
chain.ends.push(e); j = e;
} else if (s[j] === '[') {
const e = bracketEnd(s, j);
if (e < 0 || e > to || e === j + 2) break; // never closed, or `[]` (an append)
chains(s, j + 1, e - 1, out); // `$a[$i]`: `$i` is an expression too
chain.ends.push(e); j = e;
} else break;
}
out.push(chain);
i = j;
}
return out;
}
/**
* The expression under character `col` (0-based) of the line, or null.
* @returns {{expression: string, start: number, end: number}|null}
*/
export function expressionAt(line, col) {
if (typeof line !== 'string' || col < 0 || col >= line.length) return null;
let best = null;
for (const chain of chains(line)) {
for (const end of chain.ends) {
if (col < chain.start || col >= end) continue;
if (!best || end - chain.start < best.end - best.start) best = { start: chain.start, end };
break; // the first segment that covers the mouse is the shortest prefix
}
}
if (!best) return null;
const expression = line.slice(best.start, best.end);
// a bare class-less word like `self` is not an expression: only `$x...` or `Name::$x...`
return expression.includes('$') ? { expression, start: best.start, end: best.end } : null;
}
/** Can this text be read WITHOUT running code (a variable chain)? The inspector reads those with property_get and evaluates the rest. */
export function isPlainChain(text) {
const t = String(text).trim();
const found = chains(t);
return found.length >= 1 && found[0].start === 0 && found[0].ends[found[0].ends.length - 1] === t.length;
}