Skip to content

Commit

Permalink
Update lint rules to match @peggyjs/eslint-config@3.1.0. Still need t…
Browse files Browse the repository at this point in the history
…o update package.json and package-lock.json once that version is shipped.
  • Loading branch information
hildjj committed Feb 24, 2023
1 parent d80b194 commit db2d723
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 169 deletions.
45 changes: 21 additions & 24 deletions bin/peggy-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ function commaArg(val, prev) {
return (prev || []).concat(val.split(",").map(x => x.trim()));
}

function print(stream, ...args) {
stream.write(util.formatWithOptions({
colors: stream.isTTY,
depth: Infinity,
maxArrayLength: Infinity,
maxStringLength: Infinity,
}, ...args));
stream.write("\n");
}

// Files

function readStream(inputStream) {
Expand Down Expand Up @@ -203,9 +213,9 @@ class PeggyCLI extends Command {
new Option("-O, --optimize <style>")
.hideHelp()
.argParser(() => {
this.print(this.std.err, "Option --optimize is deprecated from 1.2.0 and has no effect anymore.");
this.print(this.std.err, "It will be deleted in 2.0.");
this.print(this.std.err, "Parser will be generated in the former \"speed\" mode.");
print(this.std.err, "Option --optimize is deprecated from 1.2.0 and has no effect anymore.");
print(this.std.err, "It will be deleted in 2.0.");
print(this.std.err, "Parser will be generated in the former \"speed\" mode.");
return "speed";
})
)
Expand Down Expand Up @@ -338,9 +348,9 @@ class PeggyCLI extends Command {
this.verbose('INPUT: "%s"', this.inputFile);
this.verbose('OUTPUT: "%s"', this.outputFile);
if (this.progOptions.verbose) {
this.argv.info = (pass, msg) => this.print(this.std.err, `INFO(${pass}): ${msg}`);
this.argv.info = (pass, msg) => print(this.std.err, `INFO(${pass}): ${msg}`);
}
this.argv.warning = (pass, msg) => this.print(this.std.err, `WARN(${pass}): ${msg}`);
this.argv.warning = (pass, msg) => print(this.std.err, `WARN(${pass}): ${msg}`);
});
}

Expand Down Expand Up @@ -383,34 +393,21 @@ class PeggyCLI extends Command {
super.error(message, opts);
}

print(stream, ...args) {
stream.write(util.formatWithOptions({
colors: stream.isTTY,
depth: Infinity,
maxArrayLength: Infinity,
maxStringLength: Infinity,
}, ...args));
stream.write("\n");
}

verbose(...args) {
if (!this.progOptions.verbose) {
return false;
}
this.print(this.std.err, ...args);
print(this.std.err, ...args);
return true;
}

addExtraOptionsJSON(json, source) {
let extraOptions;

try {
extraOptions = JSON.parse(json);
const extraOptions = JSON.parse(json);
return this.addExtraOptions(extraOptions, source);
} catch (e) {
throw new InvalidArgumentError(`Error parsing JSON: ${e.message}`);
}

return this.addExtraOptions(extraOptions, source);
}

addExtraOptions(extraOptions, source) {
Expand Down Expand Up @@ -487,7 +484,7 @@ class PeggyCLI extends Command {
// it is unaware of the source map location
const json = sourceMap.map.toJSON();
json.sources = json.sources.map(
src => (src === null) ? null : path.relative(mapDir, src)
src => ((src === null) ? null : path.relative(mapDir, src))
);

if (inline) {
Expand Down Expand Up @@ -610,12 +607,12 @@ class PeggyCLI extends Command {
opts.startRule = this.progOptions.startRule;
}
const results = exec.parse(this.testText, opts);
this.print(this.std.out, "%O", results);
print(this.std.out, "%O", results);
}
}

async main() {
let inputStream;
let inputStream = undefined;

if (this.inputFile === "-") {
this.std.in.resume();
Expand Down
24 changes: 12 additions & 12 deletions lib/compiler/asts.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,35 @@ const asts = {
function consumesFalse() { return false; }

const consumes = visitor.build({
choice(node) {
return node.alternatives.every(consumes);
choice(nd) {
return nd.alternatives.every(consumes);
},

sequence(node) {
return node.elements.some(consumes);
sequence(nd) {
return nd.elements.some(consumes);
},

simple_and: consumesFalse,
simple_not: consumesFalse,
optional: consumesFalse,
zero_or_more: consumesFalse,
repeated(node) {
repeated(nd) {
// If minimum is `null` it is equals to maximum (parsed from `|exact|` syntax)
const min = node.min ? node.min : node.max;
const min = nd.min ? nd.min : nd.max;

// If the low boundary is variable then it can be zero.
// Expression, repeated zero times, does not consume any input
// but always matched - so it does not always consumes on success
if (min.type !== "constant" || min.value === 0) {
return false;
}
if (consumes(node.expression)) {
if (consumes(nd.expression)) {
return true;
}
// |node.delimiter| used only when |node.expression| match at least two times
// The first `if` filtered out all non-constant minimums, so at this point
// |min.value| is always a constant
if (min.value > 1 && node.delimiter && consumes(node.delimiter)) {
if (min.value > 1 && nd.delimiter && consumes(nd.delimiter)) {
return true;
}

Expand All @@ -68,16 +68,16 @@ const asts = {
semantic_and: consumesFalse,
semantic_not: consumesFalse,

rule_ref(node) {
const rule = asts.findRule(ast, node.name);
rule_ref(nd) {
const rule = asts.findRule(ast, nd.name);

// Because we run all checks in one stage, some rules could be missing.
// Checking for missing rules could be executed in parallel to this check
return rule ? consumes(rule) : undefined;
},

literal(node) {
return node.value !== "";
literal(nd) {
return nd.value !== "";
},

class: consumesTrue,
Expand Down
2 changes: 2 additions & 0 deletions lib/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ const compiler = {

switch (options.output) {
case "parser":
// This is core to this library.
// eslint-disable-next-line no-eval
return eval(ast.code.toString());

case "source":
Expand Down
42 changes: 23 additions & 19 deletions lib/compiler/passes/generate-bytecode.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ function generateBytecode(ast, options) {
return buildSequence(
[op.PUSH_CURR_POS],
[op.SILENT_FAILS_ON],
// Mutual recursion
// eslint-disable-next-line no-use-before-define
generate(expression, {
sp: context.sp + 1,
env: cloneEnv(context.env),
Expand Down Expand Up @@ -519,6 +521,8 @@ function generateBytecode(ast, options) {
if (delimiterNode) {
return buildSequence( // stack:[ ]
[op.PUSH_CURR_POS], // pos = peg$currPos; stack:[ pos ]
// Mutual recursion
// eslint-disable-next-line no-use-before-define
generate(delimiterNode, { // item = delim(); stack:[ pos, delim ]
// +1 for the saved offset
sp: context.sp + offset + 1,
Expand Down Expand Up @@ -619,7 +623,7 @@ function generateBytecode(ast, options) {
},

choice(node, context) {
function buildAlternativesCode(alternatives, context) {
function buildAlternativesCode(alternatives) {
const match = alternatives[0].match | 0;
const first = generate(alternatives[0], {
sp: context.sp,
Expand All @@ -645,15 +649,15 @@ function generateBytecode(ast, options) {
[op.IF_ERROR],
buildSequence(
[op.POP],
buildAlternativesCode(alternatives.slice(1), context)
buildAlternativesCode(alternatives.slice(1))
),
[]
)
: []
);
}

return buildAlternativesCode(node.alternatives, context);
return buildAlternativesCode(node.alternatives);
},

action(node, context) {
Expand Down Expand Up @@ -690,25 +694,25 @@ function generateBytecode(ast, options) {
},

sequence(node, context) {
function buildElementsCode(elements, context) {
function buildElementsCode(elements, ctx) {
if (elements.length > 0) {
const processedCount = node.elements.length - elements.length + 1;

return buildSequence(
generate(elements[0], {
sp: context.sp,
env: context.env,
pluck: context.pluck,
sp: ctx.sp,
env: ctx.env,
pluck: ctx.pluck,
action: null,
}),
buildCondition(
elements[0].match | 0,
[op.IF_NOT_ERROR],
buildElementsCode(elements.slice(1), {
sp: context.sp + 1,
env: context.env,
pluck: context.pluck,
action: context.action,
sp: ctx.sp + 1,
env: ctx.env,
pluck: ctx.pluck,
action: ctx.action,
}),
buildSequence(
processedCount > 1 ? [op.POP_N, processedCount] : [op.POP],
Expand All @@ -718,27 +722,27 @@ function generateBytecode(ast, options) {
)
);
} else {
if (context.pluck.length > 0) {
if (ctx.pluck.length > 0) {
return buildSequence(
[op.PLUCK, node.elements.length + 1, context.pluck.length],
context.pluck.map(eSP => context.sp - eSP)
[op.PLUCK, node.elements.length + 1, ctx.pluck.length],
ctx.pluck.map(eSP => ctx.sp - eSP)
);
}

if (context.action) {
if (ctx.action) {
const functionIndex = addFunctionConst(
false,
Object.keys(context.env),
context.action
Object.keys(ctx.env),
ctx.action
);

return buildSequence(
[op.LOAD_SAVED_POS, node.elements.length],
buildCall(
functionIndex,
node.elements.length + 1,
context.env,
context.sp
ctx.env,
ctx.sp
)
);
} else {
Expand Down
Loading

0 comments on commit db2d723

Please sign in to comment.