Skip to content

Commit

Permalink
Add @returns everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
markw65 committed Dec 11, 2023
1 parent f274c49 commit 1f4b15c
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions lib/compiler/passes/generate-bytecode.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,20 @@ function generateBytecode(ast, options) {
/** @type PEG.LocationRange[] */
const locations = [];

/** @param {string} value */
/**
* @param {string} value
* @returns {number}
*/
function addLiteralConst(value) {
const index = literals.indexOf(value);

return index === -1 ? literals.push(value) - 1 : index;
}

/** @param {PEG.ast.CharacterClass} node */
/**
* @param {PEG.ast.CharacterClass} node
* @returns {number}
*/
function addClassConst(node) {
const cls = {
value: node.parts,
Expand All @@ -312,7 +318,10 @@ function generateBytecode(ast, options) {
return index === -1 ? classes.push(cls) - 1 : index;
}

/** @param {PEG.ast.GrammarExpectation} expected */
/**
* @param {PEG.ast.GrammarExpectation} expected
* @returns {number}
*/
function addExpectedConst(expected) {
const pattern = JSON.stringify(expected);
const index = expectations.findIndex(e => JSON.stringify(e) === pattern);
Expand All @@ -324,6 +333,7 @@ function generateBytecode(ast, options) {
* @param {boolean} predicate
* @param {string[]} params
* @param {{code:string; codeLocation: PEG.LocationRange}} node
* @returns {number}
*/
function addFunctionConst(predicate, params, node) {
const func = {
Expand All @@ -338,7 +348,10 @@ function generateBytecode(ast, options) {
return index === -1 ? functions.push(func) - 1 : index;
}

/** @param {PEG.LocationRange} location */
/**
* @param {PEG.LocationRange} location
* @returns {number}
*/
function addLocation(location) {
// Don't bother de-duplicating. There can be a lot of locations,
// they will almost never collide, and unlike the "consts" above,
Expand All @@ -349,7 +362,10 @@ function generateBytecode(ast, options) {
/** @typedef {Record<string, number>} Env */
/** @typedef {{ sp: number; env:Env; action:PEG.ast.Action|null; pluck?: number[] }} Context */

/** @param {Env} env */
/**
* @param {Env} env
* @returns {Env}
*/
function cloneEnv(env) {
/** @type {Env} */
const clone = {};
Expand All @@ -364,6 +380,7 @@ function generateBytecode(ast, options) {
/**
* @param {number[]} first
* @param {number[][]} args
* @returns {number[]}
*/
function buildSequence(first, ...args) {
return first.concat(...args);
Expand All @@ -374,6 +391,7 @@ function generateBytecode(ast, options) {
* @param {number[]} condCode
* @param {number[]} thenCode
* @param {number[]} elseCode
* @returns {number[]}
*/
function buildCondition(match, condCode, thenCode, elseCode) {
if (match === ALWAYS_MATCH) { return thenCode; }
Expand All @@ -389,6 +407,7 @@ function generateBytecode(ast, options) {
/**
* @param {number[]} condCode
* @param {number[]} bodyCode
* @returns {number[]}
*/
function buildLoop(condCode, bodyCode) {
return condCode.concat([bodyCode.length], bodyCode);
Expand All @@ -399,6 +418,7 @@ function generateBytecode(ast, options) {
* @param {number} delta
* @param {Env} env
* @param {number} sp
* @returns {number[]}
*/
function buildCall(functionIndex, delta, env, sp) {
const params = Object.keys(env).map(name => sp - env[name]);
Expand All @@ -411,6 +431,7 @@ function generateBytecode(ast, options) {
* @param {PEG.ast.Expr<T>} expression
* @param {boolean} negative
* @param {Context} context
* @returns {number[]}
*/
function buildSimplePredicate(expression, negative, context) {
const match = expression.match || 0;
Expand Down Expand Up @@ -446,6 +467,7 @@ function generateBytecode(ast, options) {
* @param {PEG.ast.SemanticPredicate} node
* @param {boolean} negative
* @param {Context} context
* @returns {number[]}
*/
function buildSemanticPredicate(node, negative, context) {
const functionIndex = addFunctionConst(
Expand All @@ -471,8 +493,8 @@ function generateBytecode(ast, options) {
}

/**
*
* @param {number[]} expressionCode
* @returns {number[]}
*/
function buildAppendLoop(expressionCode) {
return buildLoop(
Expand Down Expand Up @@ -591,7 +613,7 @@ function generateBytecode(ast, options) {
* @param {number[]} expressionCode
* @param {Context} context
* @param {number} offset
* @returns
* @returns {number[]}
*/
function buildRangeBody(
delimiterNode,
Expand Down Expand Up @@ -640,7 +662,7 @@ function generateBytecode(ast, options) {

/**
* @param {PEG.compiler.visitor.NodeTypes} generators
* @returns
* @returns {PEG.compiler.visitor.AnyFunction}
*/
function wrapGenerators(generators) {
if (options && options.output === "source-and-map") {
Expand Down

0 comments on commit 1f4b15c

Please sign in to comment.