UNPKG

coc-autark

Version:

Autark build-script completion for coc.nvim

404 lines (375 loc) • 11.6 kB
'use strict'; // Minimal Autark script scanner. // // Autark tokens are whitespace-separated bare atoms, quoted strings and braces // (`#` starts a comment only when it is the first non-space character of a // line). Inline groups such as `${...}`, `@{...}`, `S{...}` or `^{...}` are // braces directly attached to a symbolic helper name. // // `analyze()` reports the rule/inline group stack and the token around the // cursor. `collectDefinitions()`, `collectIncludes()` and // `collectCheckOutputs()` walk the whole script and report declared variables, // included scripts and variables produced by check scripts. Everything works // on plain strings and is independent of coc.nvim. // Symbolic forms that open an inline group instead of a nested rule. const INLINE_NAMES = new Set(['$', '@', '@@', '^', '%', '&', 'S', 'SS', 'C', 'CC']); // Rules whose first bare word declares a variable. const NAME_RULES = new Set(['set', 'let', 'library', 'env', 'option', 'macro', 'foreach']); // Rules that compile sources and may declare an object list variable. const OBJECT_RULES = new Set(['cc', 'cxx', 'host-cc', 'host-cxx']); // Autark's PEG grammar tries a quoted string only at the start of a value: // `VALR = STRQ | STRQQ | RULE | STRP`, and bare atoms may contain quotes // (`STRP` excludes only braces, backslash, space, tab and EOL). A quote in the // middle of an atom is therefore an ordinary character and must not open a // string. const WORD_BREAK = new Set(['{', '}', ' ', '\t', '\r', '\n']); /** Removes `!` negation and `..` spread modifiers from a rule token. */ export function stripModifiers(value) { let rest = value; for (;;) { if (rest.startsWith('!')) { rest = rest.slice(1); } else if (rest.startsWith('..')) { rest = rest.slice(2); } else { return rest; } } } /** Token text without surrounding quotes. */ export function wordValue(token) { const value = token.value; if (value.length > 1) { const first = value[0]; if ((first === "'" || first === '"') && value[value.length - 1] === first) { return value.slice(1, -1); } } return value; } /** * Splits Autark text into word, string, comment, open and close tokens. * Open tokens remember the word directly before the brace so the caller can * tell `cc { ... }` from `${...}` or `S{...}`. */ export function tokenize(text) { const tokens = []; const length = text.length; let index = 0; let atLineStart = true; let previousWord = 0; while (index < length) { const ch = text[index]; if (ch === '\n' || ch === '\r') { atLineStart = true; index += 1; continue; } if (ch === ' ' || ch === '\t') { index += 1; continue; } if (atLineStart && ch === '#') { let end = index; while (end < length && text[end] !== '\n' && text[end] !== '\r') { end += 1; } tokens.push({ type: 'comment', value: text.slice(index, end), start: index, end }); index = end; continue; } atLineStart = false; if (ch === '{') { tokens.push({ type: 'open', raw: previousWord ? previousWord.value : '', rawStart: previousWord ? previousWord.start : -1, start: index, end: index + 1, }); previousWord = 0; index += 1; continue; } if (ch === '}') { tokens.push({ type: 'close', start: index, end: index + 1 }); previousWord = 0; index += 1; continue; } // A quote opens a string only at the start of a value. if (ch === "'" || ch === '"') { let end = index + 1; while (end < length && text[end] !== ch) { end += 1; } if (end < length) { end += 1; } tokens.push({ type: 'string', value: text.slice(index, end), start: index, end }); previousWord = 0; index = end; continue; } let end = index; while (end < length) { const ch2 = text[end]; if (WORD_BREAK.has(ch2)) { break; } if (ch2 === '\\' && end + 1 < length && '{}nrt\\'.includes(text[end + 1])) { end += 2; continue; } end += 1; } if (end === index) { // Defensive: never loop forever on an unexpected character. end += 1; } const token = { type: 'word', value: text.slice(index, end), start: index, end }; tokens.push(token); previousWord = token; index = end; } return tokens; } function pushWord(stack, token) { const frame = stack[stack.length - 1]; if (frame) { frame.words.push(token); frame.values.push(token); } } function closeFrame(stack) { const frame = stack.pop(); const parent = stack[stack.length - 1]; if (!frame || !parent) { return frame; } // Inline groups and nested rules are single values of the enclosing rule. parent.values.push(frame.kind === 'inline' ? { inline: true, name: frame.name } : { rule: true, name: frame.name }); return frame; } function openFrame(token, stack) { const parent = stack[stack.length - 1]; const raw = token.raw; if (parent && raw && parent.words.length && parent.words[parent.words.length - 1].start === token.rawStart) { // The rule name belongs to the nested frame, not to the parent body. parent.words.pop(); if (parent.values.length && parent.values[parent.values.length - 1].start === token.rawStart) { parent.values.pop(); } } const name = stripModifiers(raw); const attached = !!raw && token.rawStart >= 0 && token.rawStart + raw.length === token.start; const frame = { kind: attached && INLINE_NAMES.has(name) ? 'inline' : 'rule', name, open: token.start, words: [], values: [], children: [], }; if (parent) { parent.children.push(frame); } return frame; } /** * Reports the brace stack and the word around `offset`. * * Returned fields: `inString`, `inComment`, `tokenStart` (start of the word * being typed), `prefix` (text typed so far in that word), `stack` (frames * outermost first), `frame` (innermost frame), `firstWord` (the innermost * frame has no completed value yet) and `prevWord` (its last bare word). */ export function analyze(text, offset) { const cleanOffset = Math.max(0, Math.min(offset, text.length)); const tokens = tokenize(text); const stack = []; let current = 0; for (const token of tokens) { if (token.start >= cleanOffset) { break; } if (token.type === 'open') { stack.push(openFrame(token, stack)); continue; } if (token.type === 'close') { closeFrame(stack); continue; } if (token.type === 'word') { if (token.end >= cleanOffset) { current = token; break; } pushWord(stack, token); continue; } if (token.type === 'string') { if (token.end > cleanOffset) { current = token; break; } pushWord(stack, token); continue; } // A comment ends at the line break, so a cursor at its end is still in it. if (token.end >= cleanOffset) { current = token; break; } } const frame = stack.length ? stack[stack.length - 1] : null; const lastWord = frame && frame.words.length ? frame.words[frame.words.length - 1] : 0; return { stack, frame, inString: !!current && current.type === 'string', inComment: !!current && current.type === 'comment', tokenStart: current ? current.start : cleanOffset, prefix: current ? text.slice(current.start, cleanOffset) : '', firstWord: !frame || frame.values.length === 0, prevWord: lastWord ? wordValue(lastWord) : null, }; } /** Builds the full rule tree of a script. */ function buildFrames(text) { const root = { kind: 'root', name: '', open: 0, words: [], values: [], children: [] }; const stack = [root]; for (const token of tokenize(text)) { if (token.type === 'open') { stack.push(openFrame(token, stack)); } else if (token.type === 'close') { if (stack.length > 1) { closeFrame(stack); } } else if (token.type === 'word' || token.type === 'string') { pushWord(stack, token); } } return root; } function walkFrames(root, visitor) { for (const child of root.children) { visitor(child); walkFrames(child, visitor); } } function offsetToLine(text, offset) { let line = 0; for (let i = 0; i < offset && i < text.length; i += 1) { if (text[i] === '\n') { line += 1; } } return line; } function harvest(frame, definitions, text) { const line = offsetToLine(text, frame.open) + 1; const add = (name, detail) => { if (name && name !== '_' && !definitions.has(name)) { definitions.set(name, { name, detail, line }); } }; if (NAME_RULES.has(frame.name)) { let scoped = false; for (const child of frame.children) { if (child.name === 'parent' || child.name === 'root') { scoped = true; if (child.words.length) { add(wordValue(child.words[0]), `${frame.name} ${child.name}`); } } } if (!scoped && frame.words.length) { add(wordValue(frame.words[0]), frame.name); } return; } if (OBJECT_RULES.has(frame.name)) { for (const child of frame.children) { if (child.name === 'objects' && child.words.length) { add(wordValue(child.words[0]), `${frame.name} objects`); } } return; } if (frame.name === 'meta') { for (const child of frame.children) { if (child.name === 'let') { if (child.words.length) { add(wordValue(child.words[0]), 'meta let'); } continue; } if (child.name) { add(`META_${child.name.toUpperCase()}`, 'meta'); } } } } /** * Collects variable names declared by `set`, `let`, `library`, `env`, * `option`, `macro`, `foreach`, `meta` and `objects { ... }` fields. * * @returns {Map<string, {name: string, detail: string, line: number}>} */ export function collectDefinitions(text) { const definitions = new Map(); walkFrames(buildFrames(text), (frame) => harvest(frame, definitions, text)); return definitions; } /** Returns script paths referenced by `include` rules. */ export function collectIncludes(text) { const includes = []; walkFrames(buildFrames(text), (frame) => { if (frame.name !== 'include') { return; } for (const value of frame.values) { if (value.inline || value.rule) { continue; } const candidate = wordValue(value); if (candidate && !'$@^%&'.includes(candidate[0])) { includes.push(candidate); } } }); return includes; } /** * Collects variables produced by check scripts. * * @param {string} text script source * @param {Record<string, number>} outputArgs script name -> index of the * argument that receives the variable name. * @returns {Array<{name: string, script: string}>} */ export function collectCheckOutputs(text, outputArgs) { const outputs = []; walkFrames(buildFrames(text), (frame) => { if (frame.name !== 'check') { return; } for (const child of frame.children) { const index = outputArgs[child.name]; if (index === undefined) { continue; } const value = child.values[index]; if (!value || value.inline || value.rule) { continue; } const name = wordValue(value); if (/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) { outputs.push({ name, script: child.name }); } } }); return outputs; }